def loadJSON(self, jsonstring): """ loads a JSON string in the envelope """ r = json.loads(jsonstring) if "to" in r: for a in r["to"]: aid = AID.aid() aid.setName(a["name"]) for addr in a["addresses"]: aid.addAddress(addr) self.addTo(aid) if "from" in r: aid = AID.aid() aid.setName(r["from"]["name"]) for addr in r["from"]["addresses"]: aid.addAddress(addr) self.setFrom(aid) if "acl-representation" in r: self.setAclRepresentation(r["acl-representation"]) if "payload-length" in r: self.setPayloadLength(r["payload-length"]) if "payload-encoding" in r: self.setPayloadEncoding(r["payload-encoding"]) if "date" in r: self.setDate(r["date"]) if "intended-receiver" in r: for ag in r["intended-receiver"]: aid = AID.aid() aid.setName(ag["name"]) for addr in ag["addresses"]: aid.addAddress(addr) self.addIntendedReceiver(aid)
def loadJSON(self, jsonstring): """ loads a JSON string in the message """ p = json.loads(jsonstring) if "performative" in p: self.setPerformative(p["performative"]) if "sender" in p: s = AID.aid() s.loadJSON(p["sender"]) self.setSender(s) if "receivers" in p: for i in p["receivers"]: s = AID.aid() s.loadJSON(i) self.addReceiver(s) if "content" in p: self.setContent(p["content"]) if "reply-with" in p: self.setReplyWith(p["reply-with"]) if "reply-by" in p: self.setReplyBy(p["reply-by"]) if "in-reply-to" in p: self.setInReplyTo(p["in-reply-to"]) if "reply-to" in p: for i in p["reply-to"]: s = AID.aid() s.loadJSON(i) self.addReplyTo(s) if "language" in p: self.setLanguage(p["language"]) if "encoding" in p: self.setEncoding(p["encoding"]) if "ontology" in p: self.setOntology(p['ontology']) if "protocol" in p: self.setProtocol(p['protocol']) if "conversation-id" in p: self.setConversationId(p["conversation-id"])
def __init__(self, content=None, co=None): """ AAD constructor Optionally accepts a string containing a SL definition of the AAD or a ContentObject version of the AAD """ self.name = None # AID.aid() self.ownership = None self.state = "active" if co: try: self.name = AID.aid(co=co["fipa:aid"]) except: self.name = None try: self.ownership = co["fipa:ownership"] except: self.ownership = None try: self.state = co["fipa:state"] except: self.state = None elif content is not None: self.loadSL0(content)
def __init__(self, content = None, co = None): """ AAD constructor Optionally accepts a string containing a SL definition of the AAD or a ContentObject version of the AAD """ self.name = None #AID.aid() self.ownership = None self.state = "active" if co: try: self.name = AID.aid(co=co["fipa:aid"]) except: self.name = None try: self.ownership = co["fipa:ownership"] except: self.ownership = None try: self.state = co["fipa:state"] except: self.state = None elif content != None: self.loadSL0(content)
def _process(self): self.result = [] self.msg.addReceiver(AID.aid(self.aname, addresses=["xmpp://"+self.aname])) self.msg.setContent("MEMBERS") self.myAgent.send(self.msg) rep = None rep = self._receive(True, 20) if rep: print "The members list arrived" self.result = rep.getContent().split(",")
def _process(self): msg = None msg = self._receive(block=True) if msg: #self.myAgent.DEBUG("AMS received presence message "+ str(msg),"info", "ams") typ = msg.getType() frm = msg.getFrom() status = msg.getStatus() show = msg.getShow() reply_address = frm if typ == "subscribe": frm = AID.aid(name=str(frm), addresses=["xmpp://" + str(frm)]) aad = AmsAgentDescription() aad.name = frm if status: aad.state = status if show: aad.ownership = show else: aad.ownership = frm.getName() if frm.getName() not in self.myAgent.agentdb.keys(): self.myAgent.agentdb[frm.getName()] = aad elif self.myAgent.agentdb[frm.getName()].getOwnership( ) == aad.getOwnership(): self.myAgent.agentdb[frm.getName()] = aad else: presence = xmpp.Presence(reply_address, typ="unsubscribed", xmlns=xmpp.NS_CLIENT) presence.setFrom(self.myAgent.JID) self.myAgent.send(presence) return self.myAgent.DEBUG( "AMS succesfully registered agent " + frm.getName(), "ok", "ams") presence = xmpp.Presence(reply_address, typ="subscribed") presence.setFrom(self.myAgent.JID) self.myAgent.DEBUG("AMS sends " + str(presence), "info") self.myAgent.send(presence) elif typ == "unsubscribe": if str(frm) in self.myAgent.agentdb.keys(): del self.myAgent.agentdb[str(frm)] self.myAgent.DEBUG( "Agent " + str(frm) + " deregistered from AMS", "ok", "ams") else: self.myAgent.DEBUG( "Agent " + str(frm) + " deregistered from AMS", "error", "ams") return
def startElement(self, localName, attributes): self.accumulator = "" if self.FIPA_MESSAGE_TAG == localName.lower(): self.msg.setPerformative(attributes.getValue(self.ACT_TAG)) try: self.msg.setConversationId( attributes.getValue(self.CONVERSATION_ID_TAG)) except: pass if self.SENDER_TAG == localName.lower(): self.aid = AID.aid() self.aidTag = self.SENDER_TAG if self.RECEIVER_TAG == localName.lower(): self.aid = AID.aid() self.aidTag = self.RECEIVER_TAG if self.REPLY_TO_TAG == localName.lower(): self.aid = AID.aid() self.aidTag = self.REPLY_TO_TAG if self.RESOLVERS_TAG == localName.lower(): self.aid = AID.aid() self.aidTag = self.RESOLVERS_TAG if self.REPLY_BY_TAG == localName.lower(): self.msg.setReplyBy( BasicFipaDateTime(attributes.getValue(self.TIME_TAG))) if self.NAME_TAG == localName.lower(): self.aid.setName(attributes.getValue(self.ID_TAG)) if self.URL_TAG == localName.lower(): self.aid.addAddress(attributes.getValue(self.HREF_TAG))
def loadSL0(self, content): """ inits the AAD with a SL string representation """ if content is not None: if "name" in content: self.name = AID.aid() self.name.loadSL0(content.name) if "ownership" in content: self.ownership = content.ownership[0] if "state" in content: self.state = content.state[0]
def loadSL0(self,content): """ inits the AAD with a SL string representation """ if content != None: if "name" in content: self.name = AID.aid() self.name.loadSL0(content.name) if "ownership" in content: self.ownership = content.ownership[0] if "state" in content: self.state = content.state[0]
def startElement(self, localName, attributes): self.accumulator = "" if self.FIPA_MESSAGE_TAG == localName.lower(): self.msg.setPerformative(attributes.getValue(self.ACT_TAG)) try: self.msg.setConversationId(attributes.getValue(self.CONVERSATION_ID_TAG)) except: pass if self.SENDER_TAG == localName.lower(): self.aid = AID.aid() self.aidTag = self.SENDER_TAG if self.RECEIVER_TAG == localName.lower(): self.aid = AID.aid() self.aidTag = self.RECEIVER_TAG if self.REPLY_TO_TAG == localName.lower(): self.aid = AID.aid() self.aidTag = self.REPLY_TO_TAG if self.RESOLVERS_TAG == localName.lower(): self.aid = AID.aid() self.aidTag = self.RESOLVERS_TAG if self.REPLY_BY_TAG == localName.lower(): self.msg.setReplyBy(BasicFipaDateTime(attributes.getValue(self.TIME_TAG))) if self.NAME_TAG == localName.lower(): self.aid.setName(attributes.getValue(self.ID_TAG)) if self.URL_TAG == localName.lower(): self.aid.addAddress(attributes.getValue(self.HREF_TAG))
def __init__(self, agent, jid, presence=''): self.myAgent = agent self._jid = jid self._presence = presence # Generate AID self._aid = AID.aid(name=jid, addresses=["xmpp://" + str(jid)]) # Get subscription from roster roster = agent._roster if jid in roster: if "subscription" in roster[jid]: self._subscription = roster[jid]["subscription"] else: self._subscription = "none" else: self._subscription = "none"
def _process(self): msg = None msg = self._receive(block=True) if msg: #self.myAgent.DEBUG("AMS received presence message "+ str(msg),"info", "ams") typ = msg.getType() frm = msg.getFrom() status = msg.getStatus() show = msg.getShow() reply_address = frm if typ == "subscribe": frm = AID.aid(name=str(frm), addresses=["xmpp://" + str(frm)]) aad = AmsAgentDescription() aad.name = frm if status: aad.state = status if show: aad.ownership = show else: aad.ownership = frm.getName() if frm.getName() not in self.myAgent.agentdb.keys(): self.myAgent.agentdb[frm.getName()] = aad elif self.myAgent.agentdb[frm.getName()].getOwnership() == aad.getOwnership(): self.myAgent.agentdb[frm.getName()] = aad else: presence = xmpp.Presence(reply_address, typ="unsubscribed", xmlns=xmpp.NS_CLIENT) presence.setFrom(self.myAgent.JID) self.myAgent.send(presence) return self.myAgent.DEBUG("AMS succesfully registered agent " + frm.getName(), "ok", "ams") presence = xmpp.Presence(reply_address, typ="subscribed") presence.setFrom(self.myAgent.JID) self.myAgent.DEBUG("AMS sends " + str(presence), "info") self.myAgent.send(presence) elif typ == "unsubscribe": if str(frm) in self.myAgent.agentdb.keys(): del self.myAgent.agentdb[str(frm)] self.myAgent.DEBUG("Agent " + str(frm) + " deregistered from AMS", "ok", "ams") else: self.myAgent.DEBUG("Agent " + str(frm) + " deregistered from AMS", "error", "ams") return
def processAID(self, _aid): """ parses an AID. returns a pyparsing.ParseResult class """ aid = AID.aid() if 'name' in _aid: aid.setName(_aid['name']) if 'addresses' in _aid: addr = _aid['addresses'] for i in addr: aid.addAddress(i) if 'resolvers' in _aid: res = _aid['resolvers'] for i in res: aid.addResolvers(self.processAID(i)) return aid