def _create_transport_factory(loop, transport, session_factory): """ Create a WAMP-over-XXX transport factory. """ if transport.type == u'websocket': serializers = _create_transport_serializers(transport) factory = WampWebSocketClientFactory(session_factory, url=transport.url, serializers=serializers) elif transport.type == u'rawsocket': serializer = _create_transport_serializer(transport.serializers[0]) factory = WampRawSocketClientFactory(session_factory, serializer=serializer) else: assert(False), 'should not arrive here' # set the options one at a time so we can give user better feedback for k, v in transport.options.items(): try: factory.setProtocolOptions(**{k: v}) except (TypeError, KeyError): # this allows us to document options as snake_case # until everything internally is upgraded from # camelCase try: factory.setProtocolOptions( **{_camel_case_from_snake_case(k): v} ) except (TypeError, KeyError): raise ValueError( "Unknown {} transport option: {}={}".format(transport.type, k, v) ) return factory
def _create_transport_factory(loop, transport, session_factory): """ Create a WAMP-over-XXX transport factory. """ if transport.type == u'websocket': serializers = _create_transport_serializers(transport) factory = WampWebSocketClientFactory(session_factory, url=transport.url, serializers=serializers) elif transport.type == u'rawsocket': serializer = _create_transport_serializer(transport.serializers[0]) factory = WampRawSocketClientFactory(session_factory, serializer=serializer) else: assert (False), 'should not arrive here' # set the options one at a time so we can give user better feedback for k, v in transport.options.items(): try: factory.setProtocolOptions(**{k: v}) except (TypeError, KeyError): # this allows us to document options as snake_case # until everything internally is upgraded from # camelCase try: factory.setProtocolOptions( **{_camel_case_from_snake_case(k): v}) except (TypeError, KeyError): raise ValueError("Unknown {} transport option: {}={}".format( transport.type, k, v)) return factory
def run(self): try: asyncio.set_event_loop(self._loop) txaio.use_asyncio() txaio.config.loop = self._loop # Info is too verbose, use error by default # TODO: Make logging level for autobahn configurable txaio.start_logging(level='error') # create a WAMP-over-WebSocket transport client factory transport_factory = WampWebSocketClientFactory( lambda: self._akcomponent_factory(self._decoupler, self. _callback_executor, self. _allow_exception), url=self._url) # Basic settings with most features disabled transport_factory.setProtocolOptions(failByDrop=False, openHandshakeTimeout=5., closeHandshakeTimeout=1.) isSecure, host, port, _, _, _ = parse_ws_url(self._url) transport, protocol = self._loop.run_until_complete( self._loop.create_connection(transport_factory, host, port, ssl=isSecure)) try: self._loop.run_forever() except KeyboardInterrupt: # wait until we send Goodbye if user hit ctrl-c # (done outside this except so SIGTERM gets the same handling) pass # give Goodbye message a chance to go through, if we still # have an active session if protocol._session: self._loop.run_until_complete(protocol._session.leave()) self._loop.close() except Exception as e: errorStr = pformat(e) stderr.write(errorStr + "\n") # Wake the caller, this thread will terminate right after so the # error can be detected by checking if the thread is alive self._decoupler.set_joined() self._decoupler.unblock_caller()
mod, klass = '.'.join(c[:-1]), c[-1] app = importlib.import_module(mod) ## .. and set the session class on the factory ## session_factory.session = getattr(app, klass) if args.transport == "websocket": ## create a WAMP-over-WebSocket transport client factory ## from autobahn.asyncio.websocket import WampWebSocketClientFactory transport_factory = WampWebSocketClientFactory(session_factory, url=args.url, debug_wamp=args.debug) transport_factory.setProtocolOptions(failByDrop=False) elif args.transport in ['rawsocket-json', 'rawsocket-msgpack']: ## create a WAMP-over-RawSocket transport client factory ## if args.transport == 'rawsocket-msgpack': from autobahn.wamp.serializer import MsgPackSerializer serializer = MsgPackSerializer() elif args.transport == 'rawsocket-json': from autobahn.wamp.serializer import JsonSerializer serializer = JsonSerializer() else: raise Exception("should not arrive here") from autobahn.asyncio.rawsocket import WampRawSocketClientFactory
class ApplicationRunner(object): """ This class is a slightly modified version of autobahn.asyncio.wamp.ApplicationRunner with auto reconnection feature to with customizable strategies. """ def __init__(self, url, realm, extra=None, serializers=None, debug_app=False, ssl=None, loop=None, retry_strategy=BackoffStrategy(), open_handshake_timeout=30, auto_ping_interval=10, auto_ping_timeout=27): """ :param url: The WebSocket URL of the WAMP router to connect to (e.g. `ws://somehost.com:8090/somepath`) :type url: unicode :param realm: The WAMP realm to join the application session to. :type realm: unicode :param extra: Optional extra configuration to forward to the application component. :type extra: dict :param serializers: A list of WAMP serializers to use (or None for default serializers). Serializers must implement :class:`autobahn.wamp.interfaces.ISerializer`. :type serializers: list :param debug_app: Turn on app-level debugging. :type debug_app: bool :param ssl: An (optional) SSL context instance or a bool. See the documentation for the `loop.create_connection` asyncio method, to which this value is passed as the ``ssl=`` kwarg. :type ssl: :class:`ssl.SSLContext` or bool :param open_handshake_timeout: How long to wait for the opening handshake to complete (in seconds). :param auto_ping_interval: How often to send a keep-alive ping to the router (in seconds). A value of None turns off pings. :type auto_ping_interval: int :param auto_ping_timeout: Consider the connection dropped if the router does not respond to our ping for more than X seconds. :type auto_ping_timeout: int """ self._url = url self._realm = realm self._extra = extra or dict() self._debug_app = debug_app self._serializers = serializers self._loop = loop or asyncio.get_event_loop() self._retry_strategy = retry_strategy self._closing = False self._open_handshake_timeout = open_handshake_timeout self._auto_ping_interval = auto_ping_interval self._auto_ping_timeout = auto_ping_timeout self._isSecure, self._host, self._port, _, _, _ = parseWsUrl(url) if ssl is None: self._ssl = self._isSecure else: if ssl and not self._isSecure: raise RuntimeError( 'ssl argument value passed to %s conflicts with the "ws:" ' 'prefix of the url argument. Did you mean to use "wss:"?' % self.__class__.__name__) self._ssl = ssl def run(self, make): """ Run the application component. :param make: A factory that produces instances of :class:`autobahn.asyncio.wamp.ApplicationSession` when called with an instance of :class:`autobahn.wamp.types.ComponentConfig`. :type make: callable """ def _create_app_session(): cfg = ComponentConfig(self._realm, self._extra) try: session = make(cfg) except Exception as e: # the app component could not be created .. fatal asyncio.get_event_loop().stop() raise e else: session.debug_app = self._debug_app return session self._transport_factory = WampWebSocketClientFactory( _create_app_session, url=self._url, serializers=self._serializers) if self._auto_ping_interval is not None and self._auto_ping_timeout is not None: self._transport_factory.setProtocolOptions( openHandshakeTimeout=self._open_handshake_timeout, autoPingInterval=self._auto_ping_interval, autoPingTimeout=self._auto_ping_timeout) txaio.use_asyncio() txaio.config.loop = self._loop asyncio. async (self._connect(), loop=self._loop) try: self._loop.add_signal_handler(signal.SIGTERM, self.stop) except NotImplementedError: # Ignore if not implemented. Means this program is running in windows. pass try: self._loop.run_forever() except KeyboardInterrupt: # wait until we send Goodbye if user hit ctrl-c # (done outside this except so SIGTERM gets the same handling) pass self._closing = True if self._active_protocol and self._active_protocol._session: self._loop.run_until_complete( self._active_protocol._session.leave()) self._loop.close() @asyncio.coroutine def _connect(self): self._active_protocol = None self._retry_strategy.reset_retry_interval() while True: try: _, protocol = yield from self._loop.create_connection( self._transport_factory, self._host, self._port, ssl=self._ssl) protocol.is_closed.add_done_callback(self._reconnect) self._active_protocol = protocol return except OSError: print('Connection failed') if self._retry_strategy.retry(): retry_interval = self._retry_strategy.get_retry_interval() print('Retry in {} seconds'.format(retry_interval)) yield from asyncio.sleep(retry_interval) else: print('Exceeded retry count') self._loop.stop() raise ExceededRetryCount() self._retry_strategy.increase_retry_interval() def _reconnect(self, f): # Reconnect print('Connection lost') if not self._closing: print('Reconnecting') asyncio. async (self._connect(), loop=self._loop) def stop(self, *args): self._loop.stop()
c = args.component.split('.') mod, klass = '.'.join(c[:-1]), c[-1] app = importlib.import_module(mod) ## .. and set the session class on the factory ## session_factory.session = getattr(app, klass) if args.transport == "websocket": ## create a WAMP-over-WebSocket transport client factory ## from autobahn.asyncio.websocket import WampWebSocketClientFactory transport_factory = WampWebSocketClientFactory(session_factory, url = args.url, debug_wamp = args.debug) transport_factory.setProtocolOptions(failByDrop = False) elif args.transport in ['rawsocket-json', 'rawsocket-msgpack']: ## create a WAMP-over-RawSocket transport client factory ## if args.transport == 'rawsocket-msgpack': from autobahn.wamp.serializer import MsgPackSerializer serializer = MsgPackSerializer() elif args.transport == 'rawsocket-json': from autobahn.wamp.serializer import JsonSerializer serializer = JsonSerializer() else: raise Exception("should not arrive here") from autobahn.asyncio.rawsocket import WampRawSocketClientFactory
async def do_connect() -> None: def create_session() -> AsphaltSession: session = AsphaltSession(self.realm, self.auth_method, self.auth_id, self.auth_secret) session.on('disconnect', on_disconnect) session.on('join', on_join) session.on('leave', on_leave) return session def on_disconnect(session: AsphaltSession, was_clean: bool): if not was_clean: join_future.set_exception(ConnectionError('connection closed unexpectedly')) def on_join(session: AsphaltSession, details: SessionDetails): session.off('disconnect') join_future.set_result((session, details)) def on_leave(session: AsphaltSession, details): self._session = None self._session_details = None self._connect_task = None self._subscriptions.clear() self._registrations.clear() self.realm_left.dispatch(details) if not session._goodbye_sent: if not join_future.done(): join_future.set_exception(ConnectionError(details.message)) elif join_future.done(): logger.error('Connection lost; reconnecting') self.connect() proto = 'wss' if self.tls else 'ws' url = '{proto}://{self.host}:{self.port}{self.path}'.format(proto=proto, self=self) logger.info('Connecting to %s', url) serializers = [wrap_serializer(self.serializer)] attempts = 0 while self._session is None: transport = None attempts += 1 try: join_future = self._loop.create_future() transport_factory = WampWebSocketClientFactory( create_session, url=url, serializers=serializers, loop=self._loop) transport_factory.setProtocolOptions(**self.protocol_options) with timeout(self.connection_timeout): transport, protocol = await self._loop.create_connection( transport_factory, self.host, self.port, ssl=(cast(Optional[SSLContext], self.tls_context) or True if self.tls else False)) # Connection established; wait for the session to join the realm logger.info('Connected to %s; attempting to join realm %s', self.host, self.realm) self._session, self._session_details = await join_future # Register exception mappings with the session logger.info( 'Realm %r joined; registering %d procedure(s), %d subscription(s) and %d ' 'exception(s)', self._session_details.realm, len(self._registry.procedures), len(self._registry.subscriptions), len(self._registry.exceptions)) for error, exc_type in self._registry.exceptions.items(): self._session.define(exc_type, error) # Register procedures with the session for procedure in self._registry.procedures.values(): with timeout(10): await self._register(procedure) # Register subscribers with the session for subscriber in self._registry.subscriptions: with timeout(10): await self._subscribe(subscriber) except Exception as e: if self._session: await self.stop() elif transport: transport.close() if isinstance(e, CancelledError): logger.info('Connection attempt cancelled') raise if (self.max_reconnection_attempts is not None and attempts > self.max_reconnection_attempts): raise logger.warning('Connection failed (attempt %d): %s(%s); reconnecting in %s ' 'seconds', attempts, e.__class__.__name__, e, self.reconnect_delay) await sleep(self.reconnect_delay) # Notify listeners that we've joined the realm self.realm_joined.dispatch(self._session_details) logger.info('Joined realm %r', self.realm)
class ApplicationRunner(object): """ This class is a slightly modified version of autobahn.asyncio.wamp.ApplicationRunner with auto reconnection feature to with customizable strategies. """ def __init__(self, url, realm, extra=None, serializers=None, debug_app=False, ssl=None, loop=None, retry_strategy=BackoffStrategy(), auto_ping_interval=10, auto_ping_timeout=27): """ :param url: The WebSocket URL of the WAMP router to connect to (e.g. `ws://somehost.com:8090/somepath`) :type url: unicode :param realm: The WAMP realm to join the application session to. :type realm: unicode :param extra: Optional extra configuration to forward to the application component. :type extra: dict :param serializers: A list of WAMP serializers to use (or None for default serializers). Serializers must implement :class:`autobahn.wamp.interfaces.ISerializer`. :type serializers: list :param debug_app: Turn on app-level debugging. :type debug_app: bool :param ssl: An (optional) SSL context instance or a bool. See the documentation for the `loop.create_connection` asyncio method, to which this value is passed as the ``ssl=`` kwarg. :type ssl: :class:`ssl.SSLContext` or bool :param auto_ping_interval: How often to send a keep-alive ping to the router (in seconds). A value of None turns off pings. :type auto_ping_interval: int :param auto_ping_timeout: Consider the connection dropped if the router does not respond to our ping for more than X seconds. :type auto_ping_timeout: int """ self._url = url self._realm = realm self._extra = extra or dict() self._debug_app = debug_app self._serializers = serializers self._loop = loop or asyncio.get_event_loop() self._retry_strategy = retry_strategy self._closing = False self._auto_ping_interval = auto_ping_interval self._auto_ping_timeout = auto_ping_timeout self._isSecure, self._host, self._port, _, _, _ = parseWsUrl(url) if ssl is None: self._ssl = self._isSecure else: if ssl and not self._isSecure: raise RuntimeError( 'ssl argument value passed to %s conflicts with the "ws:" ' 'prefix of the url argument. Did you mean to use "wss:"?' % self.__class__.__name__) self._ssl = ssl def run(self, make): """ Run the application component. :param make: A factory that produces instances of :class:`autobahn.asyncio.wamp.ApplicationSession` when called with an instance of :class:`autobahn.wamp.types.ComponentConfig`. :type make: callable """ def _create_app_session(): cfg = ComponentConfig(self._realm, self._extra) try: session = make(cfg) except Exception as e: # the app component could not be created .. fatal asyncio.get_event_loop().stop() raise e else: session.debug_app = self._debug_app return session self._transport_factory = WampWebSocketClientFactory(_create_app_session, url=self._url, serializers=self._serializers) if self._auto_ping_interval is not None and self._auto_ping_timeout is not None: self._transport_factory.setProtocolOptions(autoPingInterval=self._auto_ping_interval, autoPingTimeout=self._auto_ping_timeout) txaio.use_asyncio() txaio.config.loop = self._loop asyncio.async(self._connect(), loop=self._loop) self._loop.add_signal_handler(signal.SIGTERM, self.stop) try: self._loop.run_forever() except KeyboardInterrupt: # wait until we send Goodbye if user hit ctrl-c # (done outside this except so SIGTERM gets the same handling) pass self._closing = True if self._active_protocol and self._active_protocol._session: self._loop.run_until_complete(self._active_protocol._session.leave()) self._loop.close() @asyncio.coroutine def _connect(self): self._active_protocol = None self._retry_strategy.reset_retry_interval() while True: try: _, protocol = yield from self._loop.create_connection(self._transport_factory, self._host, self._port, ssl=self._ssl) protocol.is_closed.add_done_callback(self._reconnect) self._active_protocol = protocol return except OSError: print('Connection failed') if self._retry_strategy.retry(): retry_interval = self._retry_strategy.get_retry_interval() print('Retry in {} seconds'.format(retry_interval)) yield from asyncio.sleep(retry_interval) else: print('Exceeded retry count') self._loop.stop() raise ExceededRetryCount() self._retry_strategy.increase_retry_interval() def _reconnect(self, f): # Reconnect print('Connection lost') if not self._closing: print('Reconnecting') asyncio.async(self._connect(), loop=self._loop) def stop(self, *args): self._loop.stop()