def add_handler(self, handler, eventtype): try: self.handlers[eventtype][handler] = self.pluginlist[handler]['object'] ldebug("event handler %s registered for event type: %d" % (handler, eventtype)) return True except: return False
def extension_status(self, exten): if (not exten): return None idlesleep = 0.1 timeout = int(self.status_timeout / idlesleep) if (self.extstates.has_key(exten) and ((time() - self.extstates[exten]['time']) < self.status_expire)): state = int(self.extstates[exten]['status']) else: ldebug('retrieving status of extension %s' % exten) message = "Action: ExtensionState\r\nExten: %s\r\n\r\n" % exten try: self.send(message) except: return None wait = 0 while (wait < timeout): if (self.extstates.has_key(exten)): state = int(self.extstates[exten]['status']) break else: sleep(idlesleep) state = None wait += 1 return state
def add_handler(self, handler, eventtype): try: self.handlers[eventtype][handler] = self.pluginlist[handler][ 'object'] ldebug("event handler %s registered for event type: %d" % (handler, eventtype)) return True except: return False
def run(self): while self.runthread: if (self.eventpipe): event = self.eventpipe.pop() else: event = False sleep(self.idlesleep) continue for handler in self.handlers[event['_type']]: self.handlers[event['_type']][handler].add_event(event) #if (event['_type'] == 2): # ldebug('2event: %s' % event) ldebug('closing event handler thread')
def start_modules(conf): ldebug('loading modules') modules_listeners = conf['modules']['listeners'].split(',') for key in range(0, len(modules_listeners)): modules_listeners[key] = modules_listeners[key].strip() modules_handlers = conf['modules']['handlers'].split(',') for key in range(0, len(modules_handlers)): modules_handlers[key] = modules_handlers[key].strip() modules_all = modules_listeners + modules_handlers plugins = {} for module_name in modules_all: if (modules.has_key(module_name)): continue ldebug('loading module %s' % module_name) try: module_file, module_path, module_info = find_module(module_name, conf['general']['modulepath'].split(',')) except: lerror('unable to load module: %s' % module_name) continue try: module_object = load_module(module_name, module_file, module_path, module_info) finally: if (module_file): module_file.close() if (module_name in modules_listeners): try: for module_class in module_object.classes_list(1): plugins[module_class.__name__] = {} plugins[module_class.__name__]['type'] = [1,] ldebug('loading listener %s' % module_class.__name__) plugins[module_class.__name__]['object'] = module_class() except: lerror('unable to load listeners of module: %s' % module_name) if (module_name in modules_handlers): try: for module_class in module_object.classes_list(2): if (plugins.has_key(module_class.__name__)): plugins[module_class.__name__]['type'].append(2) continue plugins[module_class.__name__] = {} plugins[module_class.__name__]['type'] = [2,] ldebug('loading handler %s' % module_class.__name__) plugins[module_class.__name__]['object'] = module_class() except: lerror('unable to load handlers of module: %s' % module_name) return plugins
def run(self): ldebug('starting asterisk manager thread') while self.runthread: self.mgsocket = self.login(self.host, self.port, self.user, self.password) if (self.mgsocket == False): lerror('failed to open asterisk manager connection to %s:%d' % (self.host, self.port)) if (self.runthread): sleep(1) continue ldebug('opening asterisk manager connection to %s:%d' % (self.host, self.port)) while self.runthread: try: data = self.mgsocket.recv(65634) timeout = False except: timeout = True if (timeout): continue events = data.split("\r\n\r\n") for event_line in events: if (not event_line): continue event = event_line.splitlines() event_array = self.to_array(event) self.addevent(event_array) if not data: break if (self.runthread): ldebug('lost connection to manager') sleep(1) if (self.mgsocket): self.mgsocket.close() ldebug('closing asterisk manager thread')
def run (self): ldebug('starting asterisk manager thread') while self.runthread: self.mgsocket = self.login(self.host, self.port, self.user, self.password) if (self.mgsocket == False): lerror('failed to open asterisk manager connection to %s:%d' % (self.host, self.port)) if (self.runthread): sleep(1) continue ldebug('opening asterisk manager connection to %s:%d' % (self.host, self.port)) while self.runthread: try: data = self.mgsocket.recv(65634) timeout = False except: timeout = True if (timeout): continue events = data.split("\r\n\r\n") for event_line in events: if (not event_line): continue event = event_line.splitlines() event_array = self.to_array(event) self.addevent(event_array) if not data: break if (self.runthread): ldebug('lost connection to manager') sleep(1) if (self.mgsocket): self.mgsocket.close() ldebug('closing asterisk manager thread')
def sig_handler(number, frame): global run_daemon ldebug('signal %d received ' % number) if (number == 15): ldebug('shutdown') run_daemon = False else: ldebug('signal %d ignored' % number)
def run(self): ret = self.em.add_event_handler('ChannelEventLogger', 1) ldebug('starting CEL thread') while self.runthread: if (self.eventpipe): event = self.eventpipe.pop() else: event = False sleep(self.idlesleep) continue ldebug('CEL: %s' % event) ldebug('closing CEL thread')
def queue_status(self, queue): if (not queue): return None idlesleep = 0.1 timeout = int(self.status_timeout / idlesleep) if (self.queuestats.has_key(queue) and ((time() - self.queuestats[queue]['time']) < self.status_expire)): status = self.queuestats[queue] else: if (self.queuereadflag == False): ldebug('retrieving status of queue %s' % queue) message = "Action: QueueStatus\r\nQueue: %s\r\n\r\n" % queue try: self.send(message) except: return None wait = 0 while (wait < timeout): if (self.queuestats.has_key(queue)): status = self.queuestats[queue] ldebug('got new status of queue %s' % queue) break else: ldebug('waiting for update of queue %s' % queue) sleep(idlesleep) status = None wait += 1 if (status == None): lwarn('updating of queue %s failed after timeout' % queue) return status
serversocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) try: serversocket.bind((self.address, self.port)) except ValueError, serror: lerror('listening socket address error: %s ' % serror) return False except socket.error, serror: lerror('listening socket error (%d): %s ' % (serror[0], serror[1])) return False except: lerror('listening socket error') return False serversocket.listen(5) ldebug('server listening on %s:%d' % (self.address, self.port)) return serversocket class NetServer(Thread): def __init__(self, port=None, address=None): Thread.__init__(self) self.daemon = True self.runthread = True self.port = port self.address = address self.pluginlist = None self.clients = {} self.event_subscriptions = {} self.em = EventManager()
def msg_channels_get(self, channel): channels = self.manager.get_channels() ldebug(channels) self.nack(NetClientThread.M_CHANNELSGET)
def run(self): ret = self.em.add_event_handler('AsteriskHandler', 1) while self.runthread: if (self.eventpipe): event = self.eventpipe.pop() else: event = False sleep(self.idlesleep) continue push_event = {} if (array_value(event, 'Message') == "Extension Status"): exinfo = {} exten = array_value(event, 'Exten') exinfo['status'] = array_value(event, 'Status', 255, int) exinfo['time'] = array_value(event, '_time', 0, int) if (exten): self.extstates[exten]=exinfo push_event['type'] = 1 push_event['status'] = exinfo['status'] push_event['time'] = exinfo['time'] push_event['ext'] = exten elif (array_value(event,"Message") == "Queue status will follow"): queue_stats_time = int(event['_time']) self.queuereadflag = queue_stats_time continue if (not event.has_key("Event")): continue if (event["Event"] == "ExtensionStatus"): exinfo = {} exten = array_value(event, 'Exten') exinfo['status'] = array_value(event, 'Status', 255, int) exinfo['time'] = array_value(event, '_time', 0, int) if (exten): self.extstates[exten]=exinfo push_event['type'] = 1 push_event['status'] = exinfo['status'] push_event['time'] = exinfo['time'] push_event['ext'] = exten elif (event["Event"] == "QueueStatusComplete"): queue_stats_time = int(event['_time']) self.queuereadflag = False elif (event["Event"] == "QueueParams"): queue = array_value(event, 'Queue') if (not queue): continue queue_entry = {} queue_stat = {} queue_stat['calls'] = array_value(event, 'Calls', None, int) queue_stat['completed'] = array_value(event, 'Completed', None, int) queue_stat['abandoned'] = array_value(event, 'Abandoned', None, int) queue_stat['maxlen'] = array_value(event, 'Max', None, int) queue_stat['holdtime'] = array_value(event, 'Holdtime', None, int) queue_stat['weight'] = array_value(event, 'Weight', None, int) queue_stat['servicelevel'] = array_value(event, 'ServiceLevel', None, int) queue_stat['serviceperf'] = array_value(event, 'ServicelevelPerf', None, float) queue_entry['time'] = int(event['_time']) queue_entry['status'] = queue_stat queue_entry['members'] = [] self.queuestats[queue] = queue_entry elif (event["Event"] == "QueueMember"): queue = array_value(event, 'Queue') if (not queue): continue queue_member = {} queue_member['status'] = array_value(event, 'Status', None, int) queue_member['penalty'] = array_value(event, 'Penalty', None, int) queue_member['name'] = array_value(event, 'Name', None, int) queue_member['membership'] = array_value(event, 'Membership', None, str) queue_member['location'] = array_value(event, 'Location', None, int) queue_member['lastcall'] = array_value(event, 'LastCall', None, int) queue_member['paused'] = array_value(event, 'Paused', None, int) queue_member['callstaken'] = array_value(event, 'CallsTaken', None, int) self.queuestats[queue]['members'].append(queue_member) elif (event["Event"] == "Join" or event["Event"] == "Leave"): queue = array_value(event, 'Queue', None, str) count = array_value(event, 'Count', None, int) if (queue == None): continue if (count == None): continue if (self.queuestats.has_key(queue)): self.queuestats[queue]['status']['calls'] = count elif (event["Event"] == "Newchannel"): channelinfo = {} chan_id = array_value(event, 'Uniqueid') channelinfo['channel'] = array_value(event, 'Channel') channelinfo['status'] = array_value(event, 'ChannelState', 255, int) channelinfo['time'] = time() channelinfo['starttime'] = channelinfo['time'] channelinfo['ext'] = array_value(event, 'Exten') channelinfo['cidnum'] = array_value(event, 'CallerIDNum') channelinfo['cidname'] = array_value(event, 'CallerIDName') if (chan_id): self.channels[chan_id]=channelinfo elif (event["Event"] == "ChannelUpdate"): chan_id = array_value(event, 'Uniqueid') if (chan_id): if (not self.channels.has_key(chan_id)): self.channels[chan_id]={} self.channels[chan_id]['channel'] = array_value(event, 'Channel') self.channels[chan_id]['time'] = time() self.channels[chan_id]['channeltype'] = array_value(event, 'Channeltype') elif (event["Event"] == "Newstate"): chan_id = array_value(event, 'Uniqueid') if (chan_id): if (not self.channels.has_key(chan_id)): self.channels[chan_id]={} self.channels[chan_id]['channel'] = array_value(event, 'Channel') self.channels[chan_id]['time'] = time() self.channels[chan_id]['cidnum'] = array_value(event, 'CallerIDNum') self.channels[chan_id]['cidname'] = array_value(event, 'CallerIDName') self.channels[chan_id]['status'] = array_value(event, 'ChannelState', 255, int) elif (event["Event"] == "NewCallerid"): chan_id = array_value(event, 'Uniqueid') if (chan_id): if (not self.channels.has_key(chan_id)): self.channels[chan_id]={} self.channels[chan_id]['channel'] = array_value(event, 'Channel') self.channels[chan_id]['time'] = time() self.channels[chan_id]['cidnum'] = array_value(event, 'CallerIDNum') self.channels[chan_id]['cidname'] = array_value(event, 'CallerIDName') elif (event["Event"] == "Bridge"): chan_id1 = array_value(event, 'Uniqueid1') chan_id2 = array_value(event, 'Uniqueid2') if (chan_id1 and chan_id2): if (not self.channels.has_key(chan_id1)): self.channels[chan_id1]={} self.channels[chan_id1]['channel'] = array_value(event, 'Channel1') self.channels[chan_id1]['time'] = time() self.channels[chan_id1]['cidnum'] = array_value(event, 'CallerID1') self.channels[chan_id1]['bridgestate'] = array_value(event, 'Bridgestate') self.channels[chan_id1]['bridgetype'] = array_value(event, 'Bridgetype') self.channels[chan_id1]['bridgechannel'] = array_value(event, 'Channel2') self.channels[chan_id1]['bridgechanid'] = chan_id2 if (not self.channels.has_key(chan_id2)): self.channels[chan_id2]={} self.channels[chan_id2]['channel'] = array_value(event, 'Channel2') self.channels[chan_id2]['time'] = time() self.channels[chan_id2]['cidnum'] = array_value(event, 'CallerID2') self.channels[chan_id2]['bridgestate'] = array_value(event, 'Bridgestate') self.channels[chan_id2]['bridgetype'] = array_value(event, 'Bridgetype') self.channels[chan_id2]['bridgechannel'] = array_value(event, 'Channel1') self.channels[chan_id2]['bridgechanid'] = chan_id1 elif (event["Event"] == "Hangup"): channelinfo = {} chan_id = array_value(event, 'Uniqueid') channelinfo['channel'] = array_value(event, 'Channel') channelinfo['status'] = array_value(event, 'ChannelState', 255, int) channelinfo['time'] = time() channelinfo['cidnum'] = array_value(event, 'CallerIDNum') channelinfo['cidname'] = array_value(event, 'CallerIDName') channelinfo['cause'] = array_value(event, 'Cause', 255, int) if (chan_id): self.channels[chan_id]=channelinfo try: del(self.channels[chan_id]) except: lwarn('channel id "%s" not in list' % chan_id); if (push_event): self.push_event(push_event) ldebug('closing asterisk handler thread')
def msg_extstat(self, extension, status, sequence): ldebug("SEND UPDATE MESSAGE status %d for %s with sequence %d" % (status, extension, sequence)) self.msg(NetClientThread.R_EXTGROUPUPDATE, chr(sequence)+chr(status)+extension)
def ret_ack(self, data): if (len(data) != 2): linfo('malformed ACK packet') return False packet_type, = unpack("H", data[0:2]) ldebug('packet %d acknowledged by client' % packet_type)
def run(self): self.netsocket.settimeout(60) exten_groups = {} queue_groups = {} self.exten_groups = exten_groups self.queue_groups = queue_groups address, port = self.clientaddress ldebug('opening connection to %s:%d' % (address, port)) while self.runthread: data = [] try: data = self.netsocket.recv(6) except: timeout = True self.runthread = False if (len(data) == 6): try: msg_ident, = unpack("H", data[0:2]) msg_type, = unpack("H", data[2:4]) msg_len, = unpack("H", data[4:6]) if (msg_ident == NetClientThread.PREAMBLE): if (msg_len > 0): data = self.netsocket.recv(msg_len) else: data = '' else: linfo('malformed packet - length:%d' % len(data)) except: linfo('packet error - length:%d' % len(data)) continue ldebug('received packet type:%d length:%d' % (msg_type, len(data))) if (msg_len != len(data)): self.nack(msg_type) continue if (msg_type == NetClientThread.M_CLOSE): self.runthread = False break elif (msg_type == NetClientThread.M_PING): self.ack(msg_type) elif (msg_type == NetClientThread.M_LOGIN_PLAINTEXT): if (msg_len > 1): self.msg_login_plaintext(data) else: self.nack(msg_type) elif (msg_type == NetClientThread.M_EXTGROUPSET): if (msg_len > 1): self.msg_extgroupset(exten_groups, data) else: self.nack(msg_type) elif (msg_type == NetClientThread.M_EXTGROUPSTAT): if (msg_len == 2): self.msg_extgroupstat(exten_groups, ord(data[0]), data[1]) else: self.nack(msg_type) elif (msg_type == NetClientThread.M_EXTGROUPSUBSCRIBE): if (msg_len == 2): self.msg_extgroupsubscribe(exten_groups, ord(data[0]), data[1]) else: self.nack(msg_type) elif (msg_type == NetClientThread.M_QUEUEGROUPSET): if (msg_len > 1): self.msg_queuegroupset(queue_groups, data) else: self.nack(msg_type) elif (msg_type == NetClientThread.M_QUEUEGROUPCALLS): if (msg_len == 2): self.msg_queuegroupcalls(queue_groups, ord(data[0]), data[1]) else: self.nack(msg_type) elif (msg_type == NetClientThread.M_CHANNELSGET): self.msg_channels_get(None) elif (msg_type == NetClientThread.M_CALLFORWARDGET): self.msg_callforward_get(None) else: self.nack(msg_type) if not data: break ldebug('closing client connection to %s:%d' % (address, port)) self.netsocket.close() for subscription in self.event_subscriptions: if (self.client_key in self.event_subscriptions[subscription]): self.event_subscriptions[subscription].remove(self.client_key) del self.clients[self.client_key]
def msg(self, msg_type, data=''): msg_len = len(data) msg_data = pack("HHH", NetClientThread.PREAMBLE, msg_type, msg_len) msg_data += data ldebug('sending packet %d length %d to client' % (msg_type, msg_len)) self.send(msg_data)
def run(self): ret = self.em.add_event_handler('AsteriskHandler', 1) while self.runthread: if (self.eventpipe): event = self.eventpipe.pop() else: event = False sleep(self.idlesleep) continue push_event = {} if (array_value(event, 'Message') == "Extension Status"): exinfo = {} exten = array_value(event, 'Exten') exinfo['status'] = array_value(event, 'Status', 255, int) exinfo['time'] = array_value(event, '_time', 0, int) if (exten): self.extstates[exten] = exinfo push_event['type'] = 1 push_event['status'] = exinfo['status'] push_event['time'] = exinfo['time'] push_event['ext'] = exten elif (array_value(event, "Message") == "Queue status will follow"): queue_stats_time = int(event['_time']) self.queuereadflag = queue_stats_time continue if (not event.has_key("Event")): continue if (event["Event"] == "ExtensionStatus"): exinfo = {} exten = array_value(event, 'Exten') exinfo['status'] = array_value(event, 'Status', 255, int) exinfo['time'] = array_value(event, '_time', 0, int) if (exten): self.extstates[exten] = exinfo push_event['type'] = 1 push_event['status'] = exinfo['status'] push_event['time'] = exinfo['time'] push_event['ext'] = exten elif (event["Event"] == "QueueStatusComplete"): queue_stats_time = int(event['_time']) self.queuereadflag = False elif (event["Event"] == "QueueParams"): queue = array_value(event, 'Queue') if (not queue): continue queue_entry = {} queue_stat = {} queue_stat['calls'] = array_value(event, 'Calls', None, int) queue_stat['completed'] = array_value(event, 'Completed', None, int) queue_stat['abandoned'] = array_value(event, 'Abandoned', None, int) queue_stat['maxlen'] = array_value(event, 'Max', None, int) queue_stat['holdtime'] = array_value(event, 'Holdtime', None, int) queue_stat['weight'] = array_value(event, 'Weight', None, int) queue_stat['servicelevel'] = array_value( event, 'ServiceLevel', None, int) queue_stat['serviceperf'] = array_value( event, 'ServicelevelPerf', None, float) queue_entry['time'] = int(event['_time']) queue_entry['status'] = queue_stat queue_entry['members'] = [] self.queuestats[queue] = queue_entry elif (event["Event"] == "QueueMember"): queue = array_value(event, 'Queue') if (not queue): continue queue_member = {} queue_member['status'] = array_value(event, 'Status', None, int) queue_member['penalty'] = array_value(event, 'Penalty', None, int) queue_member['name'] = array_value(event, 'Name', None, int) queue_member['membership'] = array_value( event, 'Membership', None, str) queue_member['location'] = array_value(event, 'Location', None, int) queue_member['lastcall'] = array_value(event, 'LastCall', None, int) queue_member['paused'] = array_value(event, 'Paused', None, int) queue_member['callstaken'] = array_value( event, 'CallsTaken', None, int) self.queuestats[queue]['members'].append(queue_member) elif (event["Event"] == "Join" or event["Event"] == "Leave"): queue = array_value(event, 'Queue', None, str) count = array_value(event, 'Count', None, int) if (queue == None): continue if (count == None): continue if (self.queuestats.has_key(queue)): self.queuestats[queue]['status']['calls'] = count elif (event["Event"] == "Newchannel"): channelinfo = {} chan_id = array_value(event, 'Uniqueid') channelinfo['channel'] = array_value(event, 'Channel') channelinfo['status'] = array_value(event, 'ChannelState', 255, int) channelinfo['time'] = time() channelinfo['starttime'] = channelinfo['time'] channelinfo['ext'] = array_value(event, 'Exten') channelinfo['cidnum'] = array_value(event, 'CallerIDNum') channelinfo['cidname'] = array_value(event, 'CallerIDName') if (chan_id): self.channels[chan_id] = channelinfo elif (event["Event"] == "ChannelUpdate"): chan_id = array_value(event, 'Uniqueid') if (chan_id): if (not self.channels.has_key(chan_id)): self.channels[chan_id] = {} self.channels[chan_id]['channel'] = array_value( event, 'Channel') self.channels[chan_id]['time'] = time() self.channels[chan_id]['channeltype'] = array_value( event, 'Channeltype') elif (event["Event"] == "Newstate"): chan_id = array_value(event, 'Uniqueid') if (chan_id): if (not self.channels.has_key(chan_id)): self.channels[chan_id] = {} self.channels[chan_id]['channel'] = array_value( event, 'Channel') self.channels[chan_id]['time'] = time() self.channels[chan_id]['cidnum'] = array_value( event, 'CallerIDNum') self.channels[chan_id]['cidname'] = array_value( event, 'CallerIDName') self.channels[chan_id]['status'] = array_value( event, 'ChannelState', 255, int) elif (event["Event"] == "NewCallerid"): chan_id = array_value(event, 'Uniqueid') if (chan_id): if (not self.channels.has_key(chan_id)): self.channels[chan_id] = {} self.channels[chan_id]['channel'] = array_value( event, 'Channel') self.channels[chan_id]['time'] = time() self.channels[chan_id]['cidnum'] = array_value( event, 'CallerIDNum') self.channels[chan_id]['cidname'] = array_value( event, 'CallerIDName') elif (event["Event"] == "Bridge"): chan_id1 = array_value(event, 'Uniqueid1') chan_id2 = array_value(event, 'Uniqueid2') if (chan_id1 and chan_id2): if (not self.channels.has_key(chan_id1)): self.channels[chan_id1] = {} self.channels[chan_id1]['channel'] = array_value( event, 'Channel1') self.channels[chan_id1]['time'] = time() self.channels[chan_id1]['cidnum'] = array_value( event, 'CallerID1') self.channels[chan_id1]['bridgestate'] = array_value( event, 'Bridgestate') self.channels[chan_id1]['bridgetype'] = array_value( event, 'Bridgetype') self.channels[chan_id1]['bridgechannel'] = array_value( event, 'Channel2') self.channels[chan_id1]['bridgechanid'] = chan_id2 if (not self.channels.has_key(chan_id2)): self.channels[chan_id2] = {} self.channels[chan_id2]['channel'] = array_value( event, 'Channel2') self.channels[chan_id2]['time'] = time() self.channels[chan_id2]['cidnum'] = array_value( event, 'CallerID2') self.channels[chan_id2]['bridgestate'] = array_value( event, 'Bridgestate') self.channels[chan_id2]['bridgetype'] = array_value( event, 'Bridgetype') self.channels[chan_id2]['bridgechannel'] = array_value( event, 'Channel1') self.channels[chan_id2]['bridgechanid'] = chan_id1 elif (event["Event"] == "Hangup"): channelinfo = {} chan_id = array_value(event, 'Uniqueid') channelinfo['channel'] = array_value(event, 'Channel') channelinfo['status'] = array_value(event, 'ChannelState', 255, int) channelinfo['time'] = time() channelinfo['cidnum'] = array_value(event, 'CallerIDNum') channelinfo['cidname'] = array_value(event, 'CallerIDName') channelinfo['cause'] = array_value(event, 'Cause', 255, int) if (chan_id): self.channels[chan_id] = channelinfo try: del (self.channels[chan_id]) except: lwarn('channel id "%s" not in list' % chan_id) if (push_event): self.push_event(push_event) ldebug('closing asterisk handler thread')
serversocket.setsockopt( socket.SOL_SOCKET, socket.SO_REUSEADDR, 1 ) try: serversocket.bind((self.address, self.port)) except ValueError, serror: lerror('listening socket address error: %s ' % serror) return False except socket.error, serror: lerror('listening socket error (%d): %s ' % (serror[0], serror[1])) return False except: lerror('listening socket error') return False serversocket.listen(5) ldebug('server listening on %s:%d' % (self.address, self.port)) return serversocket class NetServer(Thread): def __init__(self, port=None, address=None): Thread.__init__(self) self.daemon = True self.runthread = True self.port = port self.address = address self.pluginlist = None self.clients = {} self.event_subscriptions = {} self.em = EventManager()
def msg_extstat(self, extension, status, sequence): ldebug("SEND UPDATE MESSAGE status %d for %s with sequence %d" % (status, extension, sequence)) self.msg(NetClientThread.R_EXTGROUPUPDATE, chr(sequence) + chr(status) + extension)
def config(self, conf): ldebug('eventhandler: set configuration') self.conf = conf return True
def start_modules(conf): ldebug('loading modules') modules_listeners = conf['modules']['listeners'].split(',') for key in range(0, len(modules_listeners)): modules_listeners[key] = modules_listeners[key].strip() modules_handlers = conf['modules']['handlers'].split(',') for key in range(0, len(modules_handlers)): modules_handlers[key] = modules_handlers[key].strip() modules_all = modules_listeners + modules_handlers plugins = {} for module_name in modules_all: if (modules.has_key(module_name)): continue ldebug('loading module %s' % module_name) try: module_file, module_path, module_info = find_module( module_name, conf['general']['modulepath'].split(',')) except: lerror('unable to load module: %s' % module_name) continue try: module_object = load_module(module_name, module_file, module_path, module_info) finally: if (module_file): module_file.close() if (module_name in modules_listeners): try: for module_class in module_object.classes_list(1): plugins[module_class.__name__] = {} plugins[module_class.__name__]['type'] = [ 1, ] ldebug('loading listener %s' % module_class.__name__) plugins[module_class.__name__]['object'] = module_class() except: lerror('unable to load listeners of module: %s' % module_name) if (module_name in modules_handlers): try: for module_class in module_object.classes_list(2): if (plugins.has_key(module_class.__name__)): plugins[module_class.__name__]['type'].append(2) continue plugins[module_class.__name__] = {} plugins[module_class.__name__]['type'] = [ 2, ] ldebug('loading handler %s' % module_class.__name__) plugins[module_class.__name__]['object'] = module_class() except: lerror('unable to load handlers of module: %s' % module_name) return plugins
def sbserver(): global run_daemon run_daemon = True conf = {} if (not start_config_default(conf)): return 1 if (not start_config_params(conf)): return 1 if (not start_config_get(conf)): return 1 start_log(conf) linfo('starting %s process' % conf['general']['name']) if (not start_config_check(conf)): return 1 signal(SIGHUP, sig_handler) signal(SIGTERM, sig_handler) if (conf['general']['daemon'] == 'yes'): ldebug('%s process starting in daemon mode' % conf['general']['name']) ret = start_daemon() if (ret != 0): lcritic('failed to start %s daemon' % conf['general']['name']) os.exit(ret) ldebug('started %s process with pid: %d' % (conf['general']['name'], os.getpid())) eventhandler = EventHandler() plugins = start_modules(conf) plugins['EventHandler'] = {} plugins['EventHandler']['type'] = (0,) plugins['EventHandler']['object'] = eventhandler for plugin in plugins: if (0 in plugins[plugin]['type']): ldebug('start system plugin %s' % plugin) plugins[plugin]['object'].config(conf) plugins[plugin]['object'].plugins(plugins) plugins[plugin]['object'].start() elif (1 in plugins[plugin]['type']): ldebug('start listener plugin %s' % plugin) plugins[plugin]['object'].config(conf) plugins[plugin]['object'].plugins(plugins) plugins[plugin]['object'].start() elif (2 in plugins[plugin]['type']): ldebug('start handler plugin %s' % plugin) plugins[plugin]['object'].config(conf) plugins[plugin]['object'].plugins(plugins) plugins[plugin]['object'].start() while (run_daemon): sleep(0.5) for plugin in plugins: if (2 in plugins[plugin]['type']): ldebug('stop handler plugin %s' % plugin) plugins[plugin]['object'].stop() if (1 in plugins[plugin]['type']): ldebug('stop listener plugin %s' % plugin) plugins[plugin]['object'].stop() if (0 in plugins[plugin]['type']): ldebug('stop system plugin %s' % plugin) plugins[plugin]['object'].stop() sleep(1) linfo('exiting %s process' % conf['general']['name']) return 1
def sbserver(): global run_daemon run_daemon = True conf = {} if (not start_config_default(conf)): return 1 if (not start_config_params(conf)): return 1 if (not start_config_get(conf)): return 1 start_log(conf) linfo('starting %s process' % conf['general']['name']) if (not start_config_check(conf)): return 1 signal(SIGHUP, sig_handler) signal(SIGTERM, sig_handler) if (conf['general']['daemon'] == 'yes'): ldebug('%s process starting in daemon mode' % conf['general']['name']) ret = start_daemon() if (ret != 0): lcritic('failed to start %s daemon' % conf['general']['name']) os.exit(ret) ldebug('started %s process with pid: %d' % (conf['general']['name'], os.getpid())) eventhandler = EventHandler() plugins = start_modules(conf) plugins['EventHandler'] = {} plugins['EventHandler']['type'] = (0, ) plugins['EventHandler']['object'] = eventhandler for plugin in plugins: if (0 in plugins[plugin]['type']): ldebug('start system plugin %s' % plugin) plugins[plugin]['object'].config(conf) plugins[plugin]['object'].plugins(plugins) plugins[plugin]['object'].start() elif (1 in plugins[plugin]['type']): ldebug('start listener plugin %s' % plugin) plugins[plugin]['object'].config(conf) plugins[plugin]['object'].plugins(plugins) plugins[plugin]['object'].start() elif (2 in plugins[plugin]['type']): ldebug('start handler plugin %s' % plugin) plugins[plugin]['object'].config(conf) plugins[plugin]['object'].plugins(plugins) plugins[plugin]['object'].start() while (run_daemon): sleep(0.5) for plugin in plugins: if (2 in plugins[plugin]['type']): ldebug('stop handler plugin %s' % plugin) plugins[plugin]['object'].stop() if (1 in plugins[plugin]['type']): ldebug('stop listener plugin %s' % plugin) plugins[plugin]['object'].stop() if (0 in plugins[plugin]['type']): ldebug('stop system plugin %s' % plugin) plugins[plugin]['object'].stop() sleep(1) linfo('exiting %s process' % conf['general']['name']) return 1
def msg(self, msg_type, data = ''): msg_len = len(data) msg_data = pack("HHH", NetClientThread.PREAMBLE, msg_type, msg_len) msg_data += data ldebug('sending packet %d length %d to client' % (msg_type, msg_len)) self.send(msg_data)