def data_received(self, data): """Called when some data is received. """ self.timer.cancel() logger.debug('-> Data received: {!r}'.format(data)) response = self.handle_data(data) if response is not None: logger.debug('<- Sending response: {!r}'.format(response)) self.transport.write(response)
def connection_made(self, transport): """Called when a connection is made. """ self.transport = transport peername = transport.get_extra_info('peername') self.ip = peername[0] self.client = "{:s}:{:d}".format(*peername) logger.debug('Connection from {}'.format(peername)) clients.append(self) self.env = envs[self.ip]
def on_enq(self, data): """Calls on <ENQ> message receiving. """ logger.debug('on_enq: %r', data) if not self.in_transfer_state: self.in_transfer_state = True return ACK else: logger.error('ENQ is not expected') return NAK
def on_eot(self, data): """Calls on <EOT> message receiving.""" logger.debug('on_eot: %r', data) if self.in_transfer_state: # put the records together to a message if self.messages: message = b"".join(self.messages) self.queue.put_nowait(message) self.discard_env() else: raise InvalidState('Server is not ready to accept EOT message.')
def on_message(self, data): """Calls on ASTM message receiving.""" logger.debug('on_message: %r', data) if not self.in_transfer_state: self.discard_chunked_messages() return NAK else: try: self.handle_message(data) return ACK except Exception as exc: logger.error( 'Error occurred on message handling. {!r}'.format(exc)) return NAK
def dispatch_astm_message(message): """Dispatch astm message """ logger.debug('Dispatching ASTM Message') if output: path = os.path.abspath(output) loop.create_task(asyncio.to_thread(write_message, message, path)) if url: session = lims.Session(url) session_args = { 'delay': args.delay, 'retries': args.retries, 'consumer': args.consumer, } loop.create_task( asyncio.to_thread(post_to_senaite, message, session, **session_args))
def __init__(self, loop, queue, **kwargs): logger.debug("ASTMProtocol:constructor") # Invoke on_timeout callback *after* the given time. timeout = kwargs.get("timeout", TIMEOUT) self.timer = loop.call_later(timeout, self.on_timeout) self.queue = queue
def connection_lost(self, ex): """Called when the connection is lost or closed. """ logger.debug('Connection lost: {!s}'.format(self.client)) # remove the connected client clients.remove(self)
def on_timeout(self): """Calls when timeout event occurs. Used to limit waiting time for response data.""" logger.debug("on_timeout") self.discard_env() self.transport.close()
def on_nak(self, data): """Calls on <NAK> message receiving.""" logger.debug('on_nak: %r', data) raise NotAccepted('Server should not be NAKed.')