示例#1
0
    def __init__(self, factory, config):

        # remember transport configuration
        #
        self._config = config

        # explicit list of WAMP serializers
        #
        if 'serializers' in config:
            serializers = []
            sers = set(config['serializers'])

            if 'flatbuffers' in sers:
                # try FlatBuffers WAMP serializer
                try:
                    from autobahn.wamp.serializer import FlatBuffersSerializer
                    serializers.append(FlatBuffersSerializer())
                except ImportError:
                    self.log.warn(
                        "Warning: could not load WAMP-FlatBuffers serializer")
                else:
                    sers.discard('flatbuffers')

            if 'cbor' in sers:
                # try CBOR WAMP serializer
                try:
                    from autobahn.wamp.serializer import CBORSerializer
                    serializers.append(CBORSerializer())
                except ImportError:
                    self.log.warn(
                        "Warning: could not load WAMP-CBOR serializer")
                else:
                    sers.discard('cbor')

            if 'msgpack' in sers:
                # try MsgPack WAMP serializer
                try:
                    from autobahn.wamp.serializer import MsgPackSerializer
                    serializer = MsgPackSerializer()
                    serializer._serializer.ENABLE_V5 = False  # FIXME
                    serializers.append(serializer)
                except ImportError:
                    self.log.warn(
                        "Warning: could not load WAMP-MsgPack serializer")
                else:
                    sers.discard('msgpack')

            if 'ubjson' in sers:
                # try UBJSON WAMP serializer
                try:
                    from autobahn.wamp.serializer import UBJSONSerializer
                    serializers.append(UBJSONSerializer(batched=True))
                    serializers.append(UBJSONSerializer())
                except ImportError:
                    self.log.warn(
                        "Warning: could not load WAMP-UBJSON serializer")
                else:
                    sers.discard('ubjson')

            if 'json' in sers:
                # try JSON WAMP serializer
                try:
                    from autobahn.wamp.serializer import JsonSerializer
                    serializers.append(JsonSerializer())
                except ImportError:
                    self.log.warn(
                        "Warning: could not load WAMP-JSON serializer")
                else:
                    sers.discard('json')

            if not serializers:
                raise Exception("no valid WAMP serializers specified")

            if len(sers) > 0:
                raise Exception(
                    "invalid WAMP serializers specified (the following were unprocessed) {}"
                    .format(sers))

        else:
            serializers = None

        rawsocket.WampRawSocketServerFactory.__init__(self, factory,
                                                      serializers)

        if 'options' in config:
            set_rawsocket_options(self, config['options'])

        self.log.debug(
            "RawSocket transport factory created using {serializers} serializers, max. message size {maxsize}",
            serializers=serializers,
            maxsize=self._max_message_size)
示例#2
0
    def __init__(self, factory, cbdir, config, templates):
        """

        :param factory: WAMP session factory.
        :type factory: An instance of ..

        :param cbdir: The Crossbar.io node directory.
        :type cbdir: str

        :param config: Crossbar transport configuration.
        :type config: dict

        :param templates:
        :type templates:
        """
        self.debug_traffic = config.get('debug_traffic', False)

        options = config.get('options', {})

        # announce Crossbar.io server version
        #
        self.showServerVersion = options.get('show_server_version',
                                             self.showServerVersion)
        if self.showServerVersion:
            server = "Crossbar/{}".format(crossbar.__version__)
        else:
            # do not disclose crossbar version
            server = "Crossbar"

        # external (public) listening port (eg when running behind a reverse proxy)
        #
        externalPort = options.get('external_port', None)

        # explicit list of WAMP serializers
        #
        if 'serializers' in config:
            serializers = []
            sers = set(config['serializers'])

            if 'flatbuffers' in sers:
                # try FlatBuffers WAMP serializer
                try:
                    from autobahn.wamp.serializer import FlatBuffersSerializer
                    serializers.append(FlatBuffersSerializer(batched=True))
                    serializers.append(FlatBuffersSerializer())
                except ImportError('FlatBuffersSerializer'):
                    self.log.warn(
                        "Warning: could not load WAMP-FlatBuffers serializer")
                else:
                    sers.discard('flatbuffers')

            if 'cbor' in sers:
                # try CBOR WAMP serializer
                try:
                    from autobahn.wamp.serializer import CBORSerializer
                    serializers.append(CBORSerializer(batched=True))
                    serializers.append(CBORSerializer())
                except ImportError('CBORSerializer'):
                    self.log.warn(
                        "Warning: could not load WAMP-CBOR serializer")
                else:
                    sers.discard('cbor')

            if 'msgpack' in sers:
                # try MsgPack WAMP serializer
                try:
                    from autobahn.wamp.serializer import MsgPackSerializer
                    serializers.append(MsgPackSerializer(batched=True))
                    serializers.append(MsgPackSerializer())
                except ImportError('MsgPackSerializer'):
                    self.log.warn(
                        "Warning: could not load WAMP-MsgPack serializer")
                else:
                    sers.discard('msgpack')

            if 'ubjson' in sers:
                # try UBJSON WAMP serializer
                try:
                    from autobahn.wamp.serializer import UBJSONSerializer
                    serializers.append(UBJSONSerializer(batched=True))
                    serializers.append(UBJSONSerializer())
                except ImportError('UBJSONSerializer'):
                    self.log.warn(
                        "Warning: could not load WAMP-UBJSON serializer")
                else:
                    sers.discard('ubjson')

            if 'json' in sers:
                # try JSON WAMP serializer
                try:
                    from autobahn.wamp.serializer import JsonSerializer
                    serializers.append(JsonSerializer(batched=True))
                    serializers.append(JsonSerializer())
                except ImportError('JsonSerializer'):
                    self.log.warn(
                        "Warning: could not load WAMP-JSON serializer")
                else:
                    sers.discard('json')

            if not serializers:
                raise Exception("no valid WAMP serializers specified")

            if len(sers) > 0:
                raise Exception(
                    "invalid WAMP serializers specified (the following were unprocessed) {}"
                    .format(sers))

        else:
            serializers = None

        websocket.WampWebSocketServerFactory.__init__(
            self,
            factory,
            serializers=serializers,
            url=config.get('url', None),
            server=server,
            externalPort=externalPort)

        # Crossbar.io node directory
        self._cbdir = cbdir

        # transport configuration
        self._config = config

        # Jinja2 templates for 404 etc
        self._templates = templates

        # enable cookie tracking if a cookie store is configured
        if 'cookie' in config:
            # cookie store configuration item
            cookie_config = config['cookie']

            # cookie store
            cookie_store_config = cookie_config['store']
            cookie_store_type = cookie_store_config['type']

            # setup ephemeral, memory-backed cookie store
            if cookie_store_type == 'memory':
                self._cookiestore = CookieStoreMemoryBacked(cookie_config)
                self.log.info("Memory-backed cookie store active.")

            # setup persistent, file-backed cookie store
            elif cookie_store_type == 'file':
                cookie_store_file = os.path.abspath(
                    os.path.join(self._cbdir, cookie_store_config['filename']))
                self._cookiestore = CookieStoreFileBacked(
                    cookie_store_file, cookie_config)
                self.log.info(
                    "File-backed cookie store active {cookie_store_file}",
                    cookie_store_file=hlval(cookie_store_file))

            # setup persistent, database-backed cookie store
            elif cookie_store_type == 'database':
                cookie_dbpath = os.path.abspath(
                    os.path.join(self._cbdir, cookie_store_config['path']))
                self._cookiestore = CookieStoreDatabaseBacked(
                    cookie_dbpath, cookie_config)
                self.log.info(
                    "Database-backed cookie store active! [cookiestore={cookiestore}]",
                    cookiestore=hltype(CookieStoreDatabaseBacked))

            else:
                # should not arrive here as the config should have been checked before
                raise NotImplementedError(
                    '{}: implementation of cookiestore of type "{}" missing'.
                    format(self.__class__.__name__, cookie_store_type))
        else:
            # this disables cookie tracking (both with or without WAMP-cookie authentication)
            self._cookiestore = None

        # set WebSocket options
        set_websocket_options(self, options)