def load(name): '''Load the protocol module.''' global mod try: mod = load_source('protocol', name) except ImportError, e: shutdown(os.EX_SOFTWARE, 'protocol unable to load: %s' % e)
def handle_close(self): '''Handle connection closings.''' asyncore.dispatcher.close(self) logger.info('connection to %s lost' % self.server['id']) self.server['connected'] = False if self.server['recontime']: logger.info('reconnecting to %s in %d second%s' % (self.server['id'], self.server['recontime'], 's' if self.server['recontime'] != 1 else '')) timer.add('uplink.reconnect', True, reinit, self.server['recontime'], self.server) event.dispatch('OnReconnect', self.server) else: # Exit. shutdown(os.EX_SOFTWARE, 'no reconnection to %s, therefore we dont need to hang around' % self.server['id'])
def init(): '''Connect to the uplink.''' serv = { 'id' : None, 'address' : None, 'password' : None, 'vhost' : None, 'actual' : None, 'services_name' : None, 'services_desc' : None, 'port' : 0, 'recontime' : 0, 'connected' : False } try: serv['id'] = var.conf.get('uplink', 'id')[0] serv['address'] = var.conf.get('uplink', 'address')[0] serv['password'] = var.conf.get('uplink', 'password')[0] serv['vhost'] = var.conf.get('uplink', 'vhost')[0] serv['actual'] = var.conf.get('uplink', 'actual')[0] serv['port'] = int(var.conf.get('uplink', 'port')[0]) if var.conf.get('uplink', 'recontime')[0]: serv['recontime'] = int(var.conf.get('uplink', 'recontime')[0]) serv['services_name'] = var.conf.get('server', 'name')[0] serv['services_desc'] = var.conf.get('server', 'desc')[0] except (conf.BlockNotFound, conf.VariableNotFound): shutdown(os.EX_CONFIG, 'uplink directives missing') event.dispatch('OnUplinkRecognization', serv) logger.info('connecting to %s (%s:%d)' % (serv['id'], serv['address'], serv['port'])) var.conn = UplinkConnection(serv) event.dispatch('OnPreConnect', serv) # This step is low-level to permit IPv6. af, type, proto, canon, sa = socket.getaddrinfo(serv['address'], serv['port'], 0, 1)[0] var.conn.create_socket(af, type) # If there's a vhost, bind to it. if serv['vhost']: var.conn.bind((serv['vhost'], 0)) # Now connect to the uplink. var.conn.connect(sa)
def handle_error(self): '''Record a normal traceback and exit.''' logger.critical('asyncore failure (BUG)') try: traceback_file = var.conf.get('options', 'traceback_file')[0] except conf.VariableNotFound: raise try: tracefile = open(traceback_file, 'w') traceback.print_exc(file=tracefile) tracefile.close() # Print one to the screen if we're not forked. if not var.fork: traceback.print_exc() except: raise shutdown(os.EX_SOFTWARE, 'asyncore failure')
global mod mod.parse_data(conn, data) def load(name): '''Load the protocol module.''' global mod try: mod = load_source('protocol', name) except ImportError, e: shutdown(os.EX_SOFTWARE, 'protocol unable to load: %s' % e) if not hasattr(mod, 'protocol_init'): shutdown(os.EX_SOFTWARE, 'protocol does not have entry point') elif not hasattr(mod, 'protocol_fini'): shutdown(os.EX_SOFTWARE, 'protocol does not have exit point') elif not hasattr(mod, 'parse_data'): shutdown(os.EX_SOFTWARE, 'protocol does not have parser') mod.protocol_init() logger.info('protocol %s loaded' % mod.__name__) def unload(): '''Unload the protocol module.''' global mod mod.protocol_fini() logger.info('protocol %s unloaded' % mod.__name__)
def on_sigterm(signum, frame): '''Handle signal SIGTERM. This will exit gracefully.''' shutdown(sighun, 'Caught SIGTERM')
def on_sigint(signum, frame): '''Handle signal SIGINT. This will exit gracefully.''' shutdown(signum, 'Caught SIGINT (terminal interrupt)')
else: print 'nuclix: pid', os.getpid() print 'nuclix: running in foregroud mode from', os.getcwd() # Initialize the logger. logger.init() # Load all modules listed in the configuration. module.load_all_from_conf() # Load the protocol. protocol.load(var.conf.get('uplink', 'protocol')[0]) # Initialize connection to the uplink. conn.init() # Start the loop. while True: delay = timer.next_run() if delay <= time.time(): timer.run() asyncore.poll(1) # This should NEVER happen. shutdown(os.EX_SOFTWARE, 'main loop exited (BUG)') if __name__ == '__main__': main(sys.argv[1:])