def _internal_setup (self): self.signal = None signal.signal(signal.SIGTERM, self.sig_receiver) signal.signal(signal.SIGINT, self.sig_receiver) signal.signal(signal.SIGQUIT, self.sig_receiver) signal.signal(signal.SIGHUP, self.sig_receiver) signal.signal(signal.SIGCHLD, self.sig_receiver) signal.signal(signal.SIGUSR2, self.sig_receiver) self.reactor = get_reactor() self.reactor.call_later(self.housekeeping_period, self.housekeeping)
def schedule_ewma_decay (): get_reactor().call_later(EWMA_DECAY_INTERVAL, ewma_decay)
def __test (): from squib.core.async import get_reactor, LineOrientedProtocolMixin, TimeoutMixin class ChatProtocol (TCPReactable, LineOrientedProtocolMixin, TimeoutMixin): channels = dict() idletime = 10 def __init__ (self, address, sock, reactor=None): super(ChatProtocol, self).__init__(address, sock, reactor) ChatProtocol.channels[self] = 1 self.nick = None self.write_data('nickname: ') self.set_timeout(self.idletime) def on_closed (self): del ChatProtocol.channels[self] def on_timeout (self): self.write_line("Connection timed out. Goodbye.") self.handle_talk("[quit - timed out]") self.close_when_done() def on_message_received (self, line): self.reset_timeout() if self.nick is None: try: self.nick = line.split()[0] except IndexError: self.nick = None if not self.nick: self.write_line("Huh?") self.write_data('nickname: ') else: # Greet self.write_line("Hello, %s" % self.nick) self.handle_talk("[joined]") self.cmd_callers(None) else: if not line: pass elif line[0] != '/': self.handle_talk(line) else: self.handle_command(line) def handle_talk (self, line): for channel in ChatProtocol.channels.keys(): if channel is not self: channel.write_line("%s: %s" % (self.nick, line)) def handle_command (self, line): command = line.split() name = 'cmd_%s' % command[0][1:] if hasattr(self, name): method = getattr(self, name) if callable(method): method(command[1:]) return self.write_line('unknown command: %s' % command[0]) def cmd_quit (self, args): if args: self.handle_talk('[quit] (%s)' % ' '.join(args)) else: self.handle_talk('[quit]') self.write_line('goodbye.') self.close_when_done() cmd_q = cmd_quit def cmd_callers (self, args): num_channels = len(ChatProtocol.channels) if num_channels == 1: self.write_line("[You're the only caller]") else: self.write_line("[There are %d callers]" % (num_channels)) nicks = [ x.nick or '<unknown>' for x in ChatProtocol.channels.keys() ] self.write_data(' ' + '\r\n '.join(nicks) + '\r\n') class ChatServer (TCPListener): def on_accept (self, sock, addr): ChatProtocol(addr, sock) ChatServer(address=('', 8518)).activate() get_reactor().start()
def __init__ (self, config, metrics_recorder): super(SelfStatistics, self).__init__() self.config = config self.metrics_recorder = metrics_recorder self.reactor = get_reactor() self.setup()