def onEvent(self, event): #It's not a real copy. The objects can change while they #are worked on here sm = SubscriptionManager() subs = sm.getSubscriptionsForTopicX(event.topic) if len(subs) == 0: return for sub in subs: self.sendEventNotification(sub, event)
class MyBot(JabberBot): def __init__(self, username, password, nickname="BoardBot1", res=None, debug=False): JabberBot.__init__(self,username,password,nickname,res,debug) self.thread_re = re.compile(r"http://(.+)/(.+)/res/([a-zA-Z0-9\.].+)") self.watcher = SubscriptionManager(self.sf,BotConfig.SubscriptionsFile,cycle_length=BotConfig.ClientUpdatePeriod,keeper_cycle=BotConfig.UpdateThreadsPeriod,scheduler_cycle=BotConfig.ThreadDownloadPeriod) self.watcher.start() def sf(self,client,mess): if not self.IsOnline(client): return False self.send(client,mess) return True @botcmd def info(self,mess,args): """Show all threads you are subscribed on""" (result,text) = self.watcher.GetSubscriptions(str(mess.getFrom())) return text @botcmd def add(self,mess,args): """Subscribes you on a thread usage: add thread-url [name] You can specify the name for this thread using name parameter""" if len(args)>=1: thread = args[0] try: data = self.thread_re.findall(thread)[0] name = "" if len(args)>=2: name = " ".join(args[1:]) chan = data[0] board = data[1] thr = data[2] (result,text) = self.watcher.AddSubscription(chan, board, thr, str(mess.getFrom()),name) return text except: return "" @botcmd def remove(self,mess,args): """Unsubscribe from a thread """ if len(args)>=1: thread = args[0] try: data = self.thread_re.findall(thread)[0] #print data chan = data[0] board = data[1] thr = data[2] (result,text) = self.watcher.RemoveSubscription(chan, board, thr, str(mess.getFrom())) return text except: return ""
def unsubscribe(): topic = request.headers.get('topic') subscriberIdentifier = request.headers.get('identifier') bootid = int(request.headers.get('bootid')) callbackAddress = request.remote_addr callbackPort = int(request.headers.get('callbackPort')) sm = SubscriptionManager() sm.removeSubscription(topic, subscriberIdentifier, bootid) logging.info('Unsubscribed successfully') logging.debug(sm.subs) return ('', 204)
class NotificationEngine(): # topics where messages are posted def __init__(self): super().__init__() self.storage = SubscriptionManager() def run(self, *args): message = args[0] operation = message['op'] topic = message['topic'] message = message['msg'] if operation == b'Publish': subscribers_notified = AmotEngine.attached(self).run( topic, message, self.storage) self.storage.keep_subscribers(topic, subscribers_notified) elif operation == b'Subscribe': ip_port = message.split(b' ') self.subscribe(topic, ip_port[0], int(ip_port[1])) elif operation == b'Unsubscribe': # TODO pass else: print('Notification engine :: Operation ' + str(args[0]['Operation']) + 'is not implemented by AMoT Engine') def publish(self, topic, message): ret = False if topic in self.topics.keys(): self.topics[topic].append(message) if self.topics[topic].__contains__(message): ret = True else: self.topics[topic] = deque([message], maxlen=20) if self.topics[topic].__contains__(message): ret = True return ret def subscribe(self, topic, ip, port): subscriber_address = (ip, port) self.storage.add_subscriber(topic, subscriber_address)
def subscribe(): topic = request.headers.get('topic') subscriberIdentifier = request.headers.get('identifier') bootid = int(request.headers.get('bootid')) callbackAddress = request.remote_addr callbackPort = int(request.headers.get('callbackPort')) logging.debug('topic: %s' % (topic)) logging.debug('subscriberIdentifier: %s' % (subscriberIdentifier)) logging.debug('bootid: %s' % (bootid)) logging.debug('callbackAddress: %s' % (callbackAddress)) logging.debug('callbackPort: %s' % (callbackPort)) sub = Subscription(topic=topic, subscriberIdentifier=subscriberIdentifier, bootid=bootid, callbackAddress=callbackAddress, port=callbackPort) sm = SubscriptionManager() sm.addSubscription(sub) logging.info('Subscribed successfully') logging.debug(sm.subs) return ('', 204)
def __init__(self): super().__init__() self.storage = SubscriptionManager()
sentence = 'topic:' + event.topic + ';' for key, value in event.values.items(): logging.debug('Key: %s, Value: %s' % (key, value)) sentence = sentence + key + ':' + value + ';' logging.debug('My Response %s' % (sentence)) sendSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sendSocket.connect((sub.callbackAddress, sub.port)) sendSocket.send(sentence.encode('utf-8')) sendSocket.close() if __name__ == '__main__': logging.basicConfig(level=logging.DEBUG) sm = SubscriptionManager() from Subscription import Subscription s1 = Subscription(topic='t1', subscriberIdentifier='s1', bootid=1) s2 = Subscription(topic='t2', subscriberIdentifier='s2', bootid=1) s3 = Subscription(topic='t1', subscriberIdentifier='s3', bootid=1) s4 = Subscription(topic='t4', subscriberIdentifier='s4', bootid=1) #sm.addSubscription(s1) #sm.addSubscription(s2) #sm.addSubscription(s3) #sm.addSubscription(s4) #print(sm.subs) ss = SubscriptionService() from Event import Event
def __init__(self, username, password, nickname="BoardBot1", res=None, debug=False): JabberBot.__init__(self,username,password,nickname,res,debug) self.thread_re = re.compile(r"http://(.+)/(.+)/res/([a-zA-Z0-9\.].+)") self.watcher = SubscriptionManager(self.sf,BotConfig.SubscriptionsFile,cycle_length=BotConfig.ClientUpdatePeriod,keeper_cycle=BotConfig.UpdateThreadsPeriod,scheduler_cycle=BotConfig.ThreadDownloadPeriod) self.watcher.start()