Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Supporting multiple server ports #51

Open
wants to merge 1 commit into
base: stable
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions devicehive/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from .api_response import ApiResponseError
from .device import DeviceError
from .network import NetworkError
from .transport_urls import TransportUrls
from .device_type import DeviceTypeError
from .subscription import SubscriptionError
from .user import UserError
15 changes: 4 additions & 11 deletions devicehive/device_hive.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,6 @@ def _ensure_transport_disconnect(self):
if self._transport.connected:
self._transport.disconnect()

@staticmethod
def transport_name(transport_url):
if transport_url[0:4] == 'http':
return 'http'
if transport_url[0:2] == 'ws':
return 'websocket'

@property
def transport(self):
return self._transport
Expand All @@ -61,8 +54,8 @@ def transport(self):
def handler(self):
return self._transport.handler.handler

def connect(self, transport_url, **options):
self._transport_name = self.transport_name(transport_url)
def connect(self, transport_urls, **options):
self._transport_name = transport_urls.getTransportName('auth_url')
assert self._transport_name, 'Unexpected transport url scheme'
transport_keep_alive = options.pop('transport_keep_alive', True)
transport_alive_sleep_time = options.pop('transport_alive_sleep_time',
Expand All @@ -80,13 +73,13 @@ def connect(self, transport_url, **options):
self._init_transport()
if not transport_keep_alive:
self._ensure_transport_disconnect()
self._transport.connect(transport_url, **options)
self._transport.connect(transport_urls, **options)
return
connect_time = time.time()
num_connect = 0
while True:
self._ensure_transport_disconnect()
self._transport.connect(transport_url, **options)
self._transport.connect(transport_urls, **options)
while self._transport.is_alive():
time.sleep(transport_alive_sleep_time)
exception_info = self._transport.exception_info
Expand Down
44 changes: 44 additions & 0 deletions devicehive/transport_urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@



class TransportUrls(object):
"""Holds url for devicehive server"""

def __init__(self):
self.urls = dict()
self.transport_name = None

def update(self, k, url):
transport_name = None
if url[0:4] == 'http':
transport_name = 'http'
if not url.endswith('/'):
url += '/'
if url[0:2] == 'ws':
transport_name = 'websocket'
if not self.transport_name:
self.transport_name = transport_name
assert(self.transport_name == transport_name)

self.urls[k] = (transport_name,url)

def getUrl(self, relative_url):
k = self._decodeRelUrl(relative_url)
(t,url) = self.urls[k]
return url

def getTransportName(self, k):
if not k in self.urls:
self.urls[k] = (None,u'')
(transport_name,u) = self.urls[k]
return transport_name

def _decodeRelUrl(self, relative_url):
# devicehive now split into several servers
parts = relative_url.split(u'/')
k = 'frontend_url'
if "token" in parts[0]:
k = 'auth_url'
if "plugin" in parts[0]:
k = 'plugin_url'
return k
11 changes: 5 additions & 6 deletions devicehive/transports/http_transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,18 @@ def __init__(self, data_format_class, data_format_options, handler_class,
data_format_class,
data_format_options, handler_class,
handler_options)
self._url = None
self._urls = None
self._options = 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
def _connect(self, urls, **options):
self._urls = urls
self._options = options
self._events_queue_sleep_time = options.pop('events_queue_sleep_time',
1e-6)
if not self._url.endswith('/'):
self._url += '/'
self._connected = True
self._handle_connect()

Expand Down Expand Up @@ -78,7 +76,8 @@ def _request_call(self, method, url, **params):

def _request(self, request_id, action, request, **params):
method = params.pop('method', 'GET')
url = self._url + params.pop('url')
relative_url = params.pop('url')
url = self._urls.getUrl(relative_url) + relative_url
request_delete_keys = params.pop('request_delete_keys', [])
request_key = params.pop('request_key', None)
response_key = params.pop('response_key', None)
Expand Down
3 changes: 2 additions & 1 deletion devicehive/transports/websocket_transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,15 @@ def _websocket_call(self, websocket_method, *args, **kwargs):
error = websocket_error
raise self._error(error)

def _connect(self, url, **options):
def _connect(self, urls, **options):
timeout = options.pop('timeout', None)
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_time = event_queue_sleep_time
self._response_sleep_time = response_sleep_time
url = urls.getUrl('auth_url')
self._websocket_call(self._websocket.connect, url, **options)
self._connected = True
event_thread = threading.Thread(target=self._event)
Expand Down