Package spade :: Module SIMBA
[hide private]
[frames] | no frames]

Source Code for Module spade.SIMBA

 1  import xmpp 
 2  import threading 
 3  import Agent 
 4  import Envelope 
 5  import FIPAMessage 
 6  import AID 
 7  import Behaviour 
 8  import ACLParser 
 9  import socket 
10  import SocketServer 
11   
12  SIMBAPORT = 20001 
13   
14 -class SIMBA(Agent.PlatformAgent):
15 - class OutboxBehaviour(Behaviour.Behaviour):
16 ''' 17 Behaviour that routes outgoing SIMBA messages 18 ''' 19
20 - def __init__(self):
22
23 - def sendToSimba(self, msg, to):
24 '''Sends a message to a SIMBA receiver''' 25 26 # Socket work 27 ip = to.strip("simba://") 28 s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) 29 try: 30 s.connect((ip, SIMBAPORT)) 31 except: 32 print "Could not connect to SIMBA socket on " + str(ip) 33 s.send(str(msg)) 34 s.close()
35
36 - def _process(self):
37 msg = self._receive(True) 38 if (msg != None): 39 to_list = msg.getReceivers() 40 d = {} 41 for to in to_list: 42 self.sendToSimba(msg, to) 43 else: 44 print "SIMBA::dying... it shouldn't happen"
45
46 - class InboxBehaviour(Behaviour.Behaviour):
47 ''' 48 Behaviour that routes incoming SIMBA messages 49 ''' 50
51 - class SimbaRequestHandler(SocketServer.DatagramRequestHandler):
52 ''' 53 Request handler for SIMBA messages 54 '''
55 - def handle(self):
56 print "SIMBA SS: New incoming message"
57
58 - def onStart(self):
59 self.SS = SocketServer.ThreadingUDPServer(("", SIMBAPORT), SimbaRequestHandler) 60 print "SIMBA SS listening on port ", SIMBAPORT 61 self.SS.serve_forever()
62
63 - def __init__(self, node, password, server, port):
64 Agent.PlatformAgent.__init__(self, node, password, server, port, debug=[])
65
66 - def _setup(self):
67 self.addBehaviour(self.InboxBehaviour(), None) 68 self.setDefaultBehaviour(self.OutboxBehaviour())
69