def resp_action(d): """creates action with response's data""" a = Action() a.archetype = act.archetype a.transnum = act.transnum a.buff = d return a
def res_action(s): """creates action with request result code""" reply = ord( Frame.REPLY_PASS if s == self.SUCCESS else Frame.REPLY_FAIL ) a = Action() a.archetype = Frame.reply_archetype(act.archetype) a.transnum = act.transnum a.buff = bytes([reply, s]) return a
def incoming(self, mon, act): def result_buff(): rc = self.process_buff(act.buff) reply = ord( Frame.REPLY_PASS if rc == self.SUCCESS else Frame.REPLY_FAIL ) return bytes([reply, rc]) a = Action() a.archetype = Frame.reply_archetype(act.archetype) a.transnum = act.transnum a.buff = result_buff() mon.send(a)
def conn_delegate(self, conns_queue, profile_path, queue, configurer, debug): """deals with an active connection""" configurer(queue, debug) name = multiprocessing.current_process().name logger = logging.getLogger(name) conn = None def read_socket(s): d = conn.recv(s) if d == b'': raise RuntimeError("socket connection broken") return d read_header = lambda: read_socket(Frame.FRAME_HEADER_LENGTH) read_body = lambda hs: read_socket(hs) while True: try: conn = conns_queue.get() logger.debug('Taking a connection from queue with {}'.format( conns_queue.qsize())) logger.debug( 'File descriptor for this connection is {}'.format( conn.fileno())) factory = ControllerFactory(logger, profile_path) mon = Monitor(logger, conn, factory) logger.debug("Monitor is ready") while True: mon.receive( Action(read_body(Frame.decode_header(read_header())))) except RuntimeError as e: logger.info(e) except FrameError as e: logger.exception(e) except KeyboardInterrupt: # SIGINT is masked in the child processes. # that's why this workaround is required # to exit reliably logger.debug('Finishing worker {}'.format(name)) break except: logger.error(dump_exception()) finally: if conn is not None: logger.debug("Closing socket") conn.close() conns_queue.task_done()
def conn_delegate(self, conn, addr, factory): '''deals with an active connection''' def read_socket(s): d = conn.recv(s) if d == b'': raise RuntimeError("socket connection broken") read_header = lambda: read_socket(Frame.FRAME_HEADER_LENGTH) read_body = lambda hs: read_socket(hs) mon = Monitor(self.logger, conn, factory) try: self.logger.debug("Connected %r at %r", conn, addr) while True: mon.receive( Action(read_body(Frame.decode_header(read_header())))) except (RuntimeError, FrameError) as e: self.logger.exception(e) except: self.logger.exception("Problem handling request") finally: self.logger.debug("Closing socket") conn.close()
def makeup_action(transnum): act = Action() act.archetype = archetype act.buff = buff act.transnum = transnum return act