def initialize(self, apiurl, auth_backend, deployer, tokens, ws_source_template, ws_target_template, io_loop=None): """Initialize the WebSocket server. Create a new WebSocket client and connect it to the Juju API. Set up the authentication system. Handle the queued messages. """ if io_loop is None: io_loop = IOLoop.current() self._io_loop = io_loop self._summary = request_summary(self.request) + ' ' logging.info(self._summary + 'client connected') self.connected = True self.juju_connected = False self._juju_message_queue = queue = deque() # Set up the authentication infrastructure. self.tokens = tokens write_message = wrap_write_message(self) self.user = User() self.auth = AuthMiddleware(self.user, auth_backend, tokens, write_message) # Set up the bundle deployment and change set infrastructure. self.deployment = DeployMiddleware(self.user, deployer, write_message) self.changeset = ChangeSetMiddleware(self.user, write_message) apiurl = get_juju_api_url(self.request.path, ws_source_template, ws_target_template, apiurl) # Juju requires the Origin header to be included in the WebSocket # client handshake request. Propagate the client origin if present; # use the Juju API server as origin otherwise. headers = get_headers(self.request, apiurl) # Connect the WebSocket client to the Juju API server. self._juju_connected_future = websocket_connect(io_loop, apiurl, self.on_juju_message, headers=headers) try: self.juju_connection = yield self._juju_connected_future except Exception as err: logging.error(self._summary + 'unable to connect to the Juju API') logging.exception(err) self.connected = False return # At this point the Juju API is successfully connected. self.juju_connected = True logging.info(self._summary + 'Juju API connected: {}'.format(apiurl)) # Send all the messages that have been enqueued before the connection # to the Juju API server was established. while self.connected and self.juju_connected and len(queue): message = queue.popleft() encoded = message.encode('utf-8') logging.debug(self._summary + 'queue -> juju: {}'.format(encoded)) self.juju_connection.write_message(message)
def initialize(self, apiurl, auth_backend, deployer, tokens, io_loop=None): """Initialize the WebSocket server. Create a new WebSocket client and connect it to the Juju API. Set up the authentication system. Handle the queued messages. """ if io_loop is None: io_loop = IOLoop.current() self._io_loop = io_loop self._summary = request_summary(self.request) + ' ' logging.info(self._summary + 'client connected') self.connected = True self.juju_connected = False self._juju_message_queue = queue = deque() # Set up the authentication infrastructure. self.tokens = tokens write_message = wrap_write_message(self) self.user = User() self.auth = AuthMiddleware( self.user, auth_backend, tokens, write_message) # Set up the bundle deployment and change set infrastructure. self.deployment = DeployMiddleware(self.user, deployer, write_message) self.changeset = ChangeSetMiddleware(self.user, write_message) # XXX The handler is no longer path agnostic, and this can be fixed by # capturing the relevant path fragment on the regexp, and then # overriding the handler's open method to store the path in the # instance. path = self.request.path[len('/ws'):] if path: # If they provided a path in their request then we need to use # that api endpoint. apiurl = '{}{}'.format(apiurl, path) # Juju requires the Origin header to be included in the WebSocket # client handshake request. Propagate the client origin if present; # use the Juju API server as origin otherwise. headers = get_headers(self.request, apiurl) # Connect the WebSocket client to the Juju API server. self._juju_connected_future = websocket_connect( io_loop, apiurl, self.on_juju_message, headers=headers) try: self.juju_connection = yield self._juju_connected_future except Exception as err: logging.error(self._summary + 'unable to connect to the Juju API') logging.exception(err) self.connected = False return # At this point the Juju API is successfully connected. self.juju_connected = True logging.info(self._summary + 'Juju API connected') # Send all the messages that have been enqueued before the connection # to the Juju API server was established. while self.connected and self.juju_connected and len(queue): message = queue.popleft() encoded = message.encode('utf-8') logging.debug(self._summary + 'queue -> juju: {}'.format(encoded)) self.juju_connection.write_message(message)
def initialize( self, apiurl, auth_backend, deployer, tokens, ws_source_template, ws_target_template, io_loop=None): """Initialize the WebSocket server. Create a new WebSocket client and connect it to the Juju API. Set up the authentication system. Handle the queued messages. """ if io_loop is None: io_loop = IOLoop.current() self._io_loop = io_loop self._summary = request_summary(self.request) + ' ' logging.info(self._summary + 'client connected') self.connected = True self.juju_connected = False self._juju_message_queue = queue = deque() # Set up the authentication infrastructure. self.tokens = tokens write_message = wrap_write_message(self) self.user = User() self.auth = AuthMiddleware( self.user, auth_backend, tokens, write_message) # Set up the bundle deployment and change set infrastructure. self.deployment = DeployMiddleware(self.user, deployer, write_message) self.changeset = ChangeSetMiddleware(self.user, write_message) apiurl = get_juju_api_url( self.request.path, ws_source_template, ws_target_template, apiurl) # Juju requires the Origin header to be included in the WebSocket # client handshake request. Propagate the client origin if present; # use the Juju API server as origin otherwise. headers = get_headers(self.request, apiurl) # Connect the WebSocket client to the Juju API server. self._juju_connected_future = websocket_connect( io_loop, apiurl, self.on_juju_message, headers=headers) try: self.juju_connection = yield self._juju_connected_future except Exception as err: logging.error(self._summary + 'unable to connect to the Juju API') logging.exception(err) self.connected = False return # At this point the Juju API is successfully connected. self.juju_connected = True logging.info(self._summary + 'Juju API connected: {}'.format(apiurl)) # Send all the messages that have been enqueued before the connection # to the Juju API server was established. while self.connected and self.juju_connected and len(queue): message = queue.popleft() encoded = message.encode('utf-8') logging.debug(self._summary + 'queue -> juju: {}'.format(encoded)) self.juju_connection.write_message(message)
def make_client(self): """Return a WebSocket client ready to be connected to the server.""" url = self.get_wss_url('/ws') # The client callback is tested elsewhere. callback = lambda message: None return clients.websocket_connect(self.io_loop, url, callback)
def make_client(self): """Return a WebSocket client ready to be connected to the server.""" return clients.websocket_connect( self.io_loop, self.get_wss_url('/ws'), lambda message: None)
def connect(self, headers=None): """Return a future whose result is a connected client.""" return clients.websocket_connect(self.io_loop, self.get_wss_url('/'), self.received.append, headers=headers)
def connect(self, headers=None): """Return a future whose result is a connected client.""" return clients.websocket_connect( self.io_loop, self.get_wss_url('/'), self.received.append, headers=headers)