Esempio n. 1
0
 async def setup(self, event, pipeline, context):
     # Setup current instance
     self.format = await self.format.read(event, pipeline, context)
     self.header = await self.header.read(event, pipeline, context)
     # Setup parent instance
     await super().setup(event, pipeline, context, await
                         self.buffer.read(event, pipeline, context))
     # Prepare formatter
     try:
         self.encoder = m42pl.encoder(self.format)(indent=2)
     except Exception as error:
         raise error
         self.encoder = m42pl.encoder('raw')()
Esempio n. 2
0
    async def setup(self, event, pipeline, context):

        async def websocket_handler(request: Any):
            """Handles an http client.

            :param request: Connection request
            """
            self.logger.info(f'new connection: remote="{request.remote}", url="{request.url}"')
            # Setup websocket instance
            self.logger.info(f'setup websocket: remote="{request.remote}", url="{request.url}"')
            ws = aiohttp.web.WebSocketResponse()
            await ws.prepare(request)
            # Extract client's path to emulate the subscription mechanism
            # and add clients to subscriber list
            path = request.path.strip('/')
            self.logger.info(f'remote subscription: path="{path}", remote="{request.remote}", url="{request.url}"')
            if not path in self.paths:
                self.paths[path] = [ws,]
            else:
                self.paths[path].append(ws)
            # Wait for the client to close.
            # In the meantime, the calling loop will continuously send
            # events as they arrive. 
            async for msg in ws:
                if msg.type == aiohttp.WSMsgType.TEXT:
                    if msg.data == 'close':
                        self.logger.info(f'closing websocket: remote="{request.remote}", url="{request.url}"')
                        await ws.close()
            # Remove client
            self.logger.info(f'removing websocket: remote="{request.remote}", url="{request.url}"')
            self.paths[path].remove(ws)
            return ws

        # Process fields
        self.fields = await self.fields.read(event, pipeline, context)
        # Setup encoder
        self.encoder = m42pl.encoder(self.fields.encoder)()
        # Setup aiohttp app
        self.app = aiohttp.web.Application()
        # Setup catch-all route
        # Clients can 'subscribe' to specific event using a path.
        self.app.add_routes([
            aiohttp.web.get('/{tail:.*}', websocket_handler),
        ])
        # Setup and start aiohttp runner
        runner = aiohttp.web.AppRunner(self.app)
        await runner.setup()
        site = aiohttp.web.TCPSite(
            runner,
            self.fields.host,
            self.fields.port,
            reuse_port=True
        )
        await site.start()
Esempio n. 3
0
    async def setup(self, sock_type, event, pipeline, context):
        """Setup the instance.

        This parent method has to be called by the child first.

        :param sock_type: ZMQ socket type.
        """
        self.args = await self.args.read(event, pipeline, context)
        self.encoder = m42pl.encoder(self.args.codec)()
        self.context = zmq.asyncio.Context.instance()
        # Create and connect socket
        self.socket = self.context.socket(sock_type)
        self.socket.connect(self.args.url)
Esempio n. 4
0
    async def setup(self, sock_type, event, pipeline, context):
        """Setup the instance.

        This parent method has to be called by the child first.

        :param sock_type:   ZMQ socket type.
        """
        # Publishers does not support parallel processing (yet ?)
        # TODO: Check for SOCK_REUSE_PORT with ZMQ
        if not self.first_chunk:
            return
        # ---
        self.args = await self.args.read(event, pipeline, context)
        self.encoder = m42pl.encoder(self.args.codec)()
        self.context = zmq.asyncio.Context.instance()
        # Create and bind socket
        self.socket = self.context.socket(sock_type)
        self.socket.bind(self.args.url)
        await sleep(0.25)
Esempio n. 5
0
 async def setup(self, event, pipeline, context):
     self.encoder = m42pl.encoder(await
                                  self.codec.read(event, pipeline,
                                                  context))()