Skip to content

Commit

Permalink
Merge pull request #30 from devicehive/develop
Browse files Browse the repository at this point in the history
New release
  • Loading branch information
Nikolay-Kha authored Nov 22, 2017
2 parents 055127c + d8821ad commit f2a5cb9
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 31 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ install:
- pip install -r test_requirements.txt
before_install:
- export DEVICE_HIVE_TRANSPORT_URLS='http://playground.dev.devicehive.com/api/rest,ws://playground.dev.devicehive.com/api/websocket'
- openssl aes-256-cbc -K $encrypted_1ea9ebbf5537_key -iv $encrypted_1ea9ebbf5537_iv -in admin_refresh_token.txt.enc -out admin_refresh_token.txt -d
- openssl aes-256-cbc -K $encrypted_94dc46d8330a_key -iv $encrypted_94dc46d8330a_iv -in refresh_tokens.tar.enc -out refresh_tokens.tar -d
- tar xvf refresh_tokens.tar
- export DEVICE_HIVE_ADMIN_REFRESH_TOKEN=$(cat admin_refresh_token.txt)
- openssl aes-256-cbc -K $encrypted_94dc46d8330a_key -iv $encrypted_94dc46d8330a_iv -in user_refresh_token.txt.enc -out user_refresh_token.txt -d
- export DEVICE_HIVE_USER_REFRESH_TOKEN=$(cat user_refresh_token.txt)
script: pytest -xv tests --transport-urls=$DEVICE_HIVE_TRANSPORT_URLS --admin-refresh-token=$DEVICE_HIVE_ADMIN_REFRESH_TOKEN --user-refresh-token=$DEVICE_HIVE_USER_REFRESH_TOKEN
Binary file removed admin_refresh_token.txt.enc
Binary file not shown.
5 changes: 3 additions & 2 deletions devicehive/device_hive.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ def connect(self, transport_url, **options):
self._transport_name = self.transport_name(transport_url)
assert self._transport_name, 'Unexpected transport url scheme'
transport_keep_alive = options.pop('transport_keep_alive', True)
transport_alive_timeout = options.pop('transport_alive_timeout', 0.01)
transport_alive_sleep_time = options.pop('transport_alive_sleep_time',
1e-6)
connect_timeout = options.pop('connect_timeout', 30)
max_num_connect = options.pop('max_num_connect', 10)
connect_interval = options.pop('connect_interval', 1)
Expand All @@ -67,7 +68,7 @@ def connect(self, transport_url, **options):
self._ensure_transport_disconnect()
self._transport.connect(transport_url, **options)
while self._transport.is_alive():
time.sleep(transport_alive_timeout)
time.sleep(transport_alive_sleep_time)
exception_info = self._transport.exception_info
if exception_info and not isinstance(exception_info[1],
self._transport.error):
Expand Down
7 changes: 4 additions & 3 deletions devicehive/device_hive_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ class DeviceHiveApi(object):

def __init__(self, transport_url, **options):
self._transport_url = transport_url
self._transport_alive_timeout = options.pop('transport_alive_timeout',
0.01)
transport_alive_sleep_time = options.pop('transport_alive_sleep_time',
1e-6)
self._transport_alive_sleep_time = transport_alive_sleep_time
options['transport_keep_alive'] = False
options['api_init'] = False
self._options = options
Expand Down Expand Up @@ -71,7 +72,7 @@ def _call(self, call, *args, **kwargs):
device_hive = DeviceHive(ApiCallHandler, call, *args, **kwargs)
device_hive.connect(self._transport_url, **self._options)
while not device_hive.handler.ready:
time.sleep(self._transport_alive_timeout)
time.sleep(self._transport_alive_sleep_time)
if device_hive.transport.exception_info:
six.reraise(*device_hive.transport.exception_info)
return device_hive.handler.result
Expand Down
7 changes: 4 additions & 3 deletions devicehive/transports/http_transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,16 @@ def __init__(self, data_format_class, data_format_options, handler_class,
handler_options)
self._url = None
self._options = None
self._events_queue_timeout = None
self._events_queue_sleep = None
self._events_queue = []
self._subscription_ids = []
self._success_codes = [200, 201, 204]

def _connect(self, url, **options):
self._url = url
self._options = options
self._events_queue_timeout = options.pop('events_queue_timeout', 0.01)
self._events_queue_sleep_time = options.pop('events_queue_sleep_time',
1e-6)
if not self._url.endswith('/'):
self._url += '/'
self._connected = True
Expand All @@ -34,7 +35,7 @@ def _connect(self, url, **options):
def _receive(self):
while self._connected and not self._exception_info:
if not self._events_queue:
time.sleep(self._events_queue_timeout)
time.sleep(self._events_queue_sleep_time)
continue
for event in self._events_queue.pop(0):
self._handle_event(event)
Expand Down
39 changes: 19 additions & 20 deletions devicehive/transports/websocket_transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ def __init__(self, data_format_class, data_format_options, handler_class,
data_format_options,
handler_class, handler_options)
self._websocket = websocket.WebSocket()
self._event_queue_sleep = None
self._response_sleep = None
self._event_queue_sleep_time = None
self._response_sleep_time = None
self._pong_received = False
self._event_queue = []
self._responses = {}
Expand All @@ -37,12 +37,12 @@ def _websocket_call(self, websocket_method, *args, **kwargs):

def _connect(self, url, **options):
timeout = options.pop('timeout', None)
event_queue_sleep = options.pop('event_queue_sleep', 0.01)
response_sleep = options.pop('response_sleep', 0.01)
event_queue_sleep_time = options.pop('event_queue_sleep_time', 1e-6)
response_sleep_time = options.pop('response_sleep_time', 1e-6)
pong_timeout = options.pop('pong_timeout', None)
self._websocket.timeout = timeout
self._event_queue_sleep = event_queue_sleep
self._response_sleep = response_sleep
self._event_queue_sleep_time = event_queue_sleep_time
self._response_sleep_time = response_sleep_time
self._websocket_call(self._websocket.connect, url, **options)
self._connected = True
event_thread = threading.Thread(target=self._event)
Expand All @@ -62,11 +62,16 @@ def _event(self):
try:
opcode, data = self._websocket_call(self._websocket.recv_data,
True)
if opcode == websocket.ABNF.OPCODE_TEXT:
self._parse_event(self._decode(data.decode('utf-8')))
continue
if opcode == websocket.ABNF.OPCODE_BINARY:
self._parse_event(self._decode(data))
if opcode in (websocket.ABNF.OPCODE_TEXT,
websocket.ABNF.OPCODE_BINARY):
if opcode == websocket.ABNF.OPCODE_TEXT:
data = data.decode('utf-8')
event = self._decode(data)
request_id = event.get(self.REQUEST_ID_KEY)
if not request_id:
self._event_queue.append(event)
continue
self._responses[request_id] = event
continue
if opcode == websocket.ABNF.OPCODE_PONG:
self._pong_received = True
Expand All @@ -76,13 +81,6 @@ def _event(self):
except:
self._exception_info = sys.exc_info()

def _parse_event(self, event):
request_id = event.get(self.REQUEST_ID_KEY)
if not request_id:
self._event_queue.append(event)
return
self._responses[request_id] = event

def _ping(self, pong_timeout):
while self._connected:
try:
Expand All @@ -99,7 +97,7 @@ def _ping(self, pong_timeout):
def _receive(self):
while self._connected and not self._exception_info:
if not self._event_queue:
time.sleep(self._event_queue_sleep)
time.sleep(self._event_queue_sleep_time)
continue
for event in self._event_queue:
self._handle_event(event)
Expand All @@ -125,8 +123,9 @@ def _receive_response(self, request_id, timeout):
while time.time() - timeout < start_time:
response = self._responses.get(request_id)
if response:
del self._responses[request_id]
return response
time.sleep(self._response_sleep)
time.sleep(self._response_sleep_time)
raise self._error('Response timeout.')

def send_request(self, request_id, action, request, **params):
Expand Down
Binary file added refresh_tokens.tar.enc
Binary file not shown.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@


setup(name='devicehive',
version='2.1.0',
version='2.1.1',
author='DataArt (http://dataart.com)',
author_email='[email protected]',
url='https://devicehive.com',
Expand Down
Binary file removed user_refresh_token.txt.enc
Binary file not shown.

0 comments on commit f2a5cb9

Please sign in to comment.