def connect_ensime_server(self): """Start initial connection with the server. Return True if the connection info is received else returns False""" self.env.logger.debug('connect_ensime_server: in') def disable_completely(e): if e: self.env.logger.error('connection error: %s', e, exc_info=True) self.shutdown_server() self.env.logger.info("Server was shutdown.") self._display_ws_warning() if self.running and self.number_try_connection: if not self.ensime_server: port = self.ensime.http_port() uri = "websocket" self.ensime_server = gconfig['ensime_server'].format(port, uri) with catch(websocket.WebSocketException, disable_completely): # Use the default timeout (no timeout). options = {"subprotocols": ["jerky"]} options['enable_multithread'] = True self.env.logger.info("About to connect to %s with options %s", self.ensime_server, options) self.ws = websocket.create_connection(self.ensime_server, **options) self.number_try_connection -= 1 got_response = ConnectionInfoRequest().run_in(self.env) # confirm response return bool(got_response is not None) else: # If it hits this, number_try_connection is 0 disable_completely(None) return False
def connect_ensime_server(self): """Start initial connection with the server. Return True if the connection info is received else returns False""" self.env.logger.debug('connect_ensime_server: in') def disable_completely(e): if e: self.env.logger.error('connection error: %s', e, exc_info=True) self.shutdown_server() self.env.logger.info("Server was shutdown.") self._display_ws_warning() if self.running and self.number_try_connection: if not self.ensime_server: port = self.ensime.http_port() uri = "websocket" self.ensime_server = gconfig['ensime_server'].format(port, uri) with catch(websocket.WebSocketException, disable_completely): # Use the default timeout (no timeout). options = {"subprotocols": ["jerky"]} options['enable_multithread'] = True self.env.logger.info("About to connect to %s with options %s", self.ensime_server, options) self.ws = websocket.create_connection(self.ensime_server, **options) self.number_try_connection -= 1 got_response = ConnectionInfoRequest().run_in( self.env) # confirm response return bool(got_response is not None) else: # If it hits this, number_try_connection is 0 disable_completely(None) return False
def send(self, msg): """Send something to the ensime server.""" def reconnect(e): self.env.logger.error('send error, reconnecting...') self.connect_ensime_server() if self.ws: self.ws.send(msg + "\n") self.env.logger.debug('send: in') if self.ws is not None: with catch(websocket.WebSocketException, reconnect): self.env.logger.debug('send: sending JSON on WebSocket') self.ws.send(msg + "\n")
def queue_poll(self, sleep_t=0.5): """Put new messages in the map as they arrive. Since putting a value in a map is an atomic operation, existence of a certain key and retrieval can be done from a separate thread by the client. Value of sleep is low to improve responsiveness. """ while self.running: if self.ws is not None: def log_and_close(msg): if self.connected: self.env.logger.error('Websocket exception', exc_info=True) self.env.logger.warning( "Forcing shutdown. Check server log to see what happened." ) # Stop everything. self.shutdown_server() self._display_ws_warning() with catch(websocket.WebSocketException, log_and_close): result = self.ws.recv() if result: try: _json = json.loads(result) except json.JSONDecodeError as e: self.env.logger.error(e.msg) else: # Watch if it has a callId call_id = _json.get("callId") def handle_now(): if _json["payload"]: self.handle_incoming_response( call_id, _json["payload"]) def handle_later(): self.responses[call_id] = _json if call_id is None: handle_now() else: call_opt = self.call_options.get(call_id) if call_opt and call_opt['async']: handle_now() else: handle_later() else: time.sleep(sleep_t)
def queue_poll(self, sleep_t=0.5): """Put new messages in the map as they arrive. Since putting a value in a map is an atomic operation, existence of a certain key and retrieval can be done from a separate thread by the client. Value of sleep is low to improve responsiveness. """ while self.running: if self.ws is not None: def log_and_close(msg): if self.connected: self.env.logger.error('Websocket exception', exc_info=True) self.env.logger.warning("Forcing shutdown. Check server log to see what happened.") # Stop everything. self.shutdown_server() self._display_ws_warning() with catch(websocket.WebSocketException, log_and_close): result = self.ws.recv() if result: try: _json = json.loads(result) except json.JSONDecodeError as e: self.env.logger.error(e.msg) else: # Watch if it has a callId call_id = _json.get("callId") def handle_now(): if _json["payload"]: self.handle_incoming_response(call_id, _json["payload"]) def handle_later(): self.responses[call_id] = _json if call_id is None: handle_now() else: call_opt = self.call_options.get(call_id) if call_opt and call_opt['async']: handle_now() else: handle_later() else: time.sleep(sleep_t)
def handle_incoming_response(self, call_id, payload): """Get a registered handler for a given response and execute it.""" self.env.logger.debug('handle_incoming_response: in [typehint: %s, call ID: %s]', payload['typehint'], call_id) # We already log the full JSON response typehint = payload["typehint"] handler = self.handlers.get(typehint) def feature_not_supported(m): msg = feedback["handler_not_implemented"] self.env.logger.error(msg.format(typehint, self.server_version)) self.env.status_message(msg.format(typehint, self.server_version)) if handler: with catch(NotImplementedError, feature_not_supported): handler(call_id, payload) else: self.env.logger.warning('Response has not been handled: %s', Pretty(payload))
def handle_incoming_response(self, call_id, payload): """Get a registered handler for a given response and execute it.""" self.env.logger.debug( 'handle_incoming_response: in [typehint: %s, call ID: %s]', payload['typehint'], call_id) # We already log the full JSON response typehint = payload["typehint"] handler = self.handlers.get(typehint) def feature_not_supported(m): msg = feedback["handler_not_implemented"] self.env.logger.error(msg.format(typehint, self.server_version)) self.env.status_message(msg.format(typehint, self.server_version)) if handler: with catch(NotImplementedError, feature_not_supported): handler(call_id, payload) else: self.env.logger.warning('Response has not been handled: %s', Pretty(payload))
def on_stop(): log.close() null.close() for path in [pid_path, http_path, port_path]: with catch(Exception): os.remove(path)
def test_tags(self): with self.assertRaises(util._Throw): util.catch('foo', self._function, 'foobar') util.catch('foobar', util.catch, 'foo', self._function, 'bar')
def test_basic(self): self.assertEqual(util.catch('foobar', self._function, 'foo'), 1) self.assertEqual(util.catch('foobar', self._function, 'bar'), 2) self.assertIsNone(util.catch('foobar', self._function, 'foobar'))