Package ioids :: Module g4dsconnector
[hide private]
[frames] | no frames]

Source Code for Module ioids.g4dsconnector

  1  """ 
  2  Handles communication with G4DS communication plattform. 
  3   
  4  Inter-Organisational Intrusion Detection System (IOIDS) 
  5   
  6  @author: Michael Pilgermann 
  7  @contact: mailto:mpilgerm@glam.ac.uk 
  8  @license: GPL (General Public License) 
  9  """ 
 10   
 11  # "singleton" 
 12  _g4dsConnector = None 
13 -def getG4dsConnector():
14 """ 15 Singleton implementation. 16 """ 17 global _g4dsConnector 18 if not _g4dsConnector: 19 _g4dsConnector = G4dsConnector() 20 return _g4dsConnector
21
22 -class G4dsConnector:
23
24 - def __init__(self):
25 """ 26 Initialises local variables. 27 28 Perform the connection with L{connect}. Please shut down properly the connection with 29 L{disconnect}. 30 """ 31 from config import LOCATION_PRIVATE_KEY 32 self._locationKey = LOCATION_PRIVATE_KEY
33 34
35 - def connect(self):
36 """ 37 Establishes the connection to the G4DS backend. 38 """ 39 from g4ds import g4dsservice 40 from config import G4DS_SERVICE_ID 41 from errorhandling import IoidsException 42 43 self._g4ds = g4dsservice.G4dsService() 44 try: 45 self._g4ds.connect(G4DS_SERVICE_ID, None, self._locationKey, callback = self._incomingMessageCallback) 46 except Exception, msg: 47 raise IoidsException('G4DS connection failed: %s' %(msg))
48
49 - def disconnect(self):
50 """ 51 Shuts down connection to G4DS. 52 """ 53 try: 54 self._g4ds.disconnect() 55 except Exception, msg: 56 from errorhandling import IoidsException 57 raise IoidsException('G4DS connection shutdown failed: %s' %(msg))
58
59 - def _incomingMessageCallback(self, data, metadata):
60 """ 61 Callback function for G4DS to receive messages from it. 62 """ 63 from g4ds import g4dsservice 64 ## import cPickle as pickle 65 ## from StringIO import StringIO 66 ## st = StringIO(data) 67 ## data = pickle.load(st) 68 #### print "Received (%s): %s" %(metadata[g4dsservice.METADATA_SENDERID], data) 69 70 print "Received sth from %s" %(metadata[g4dsservice.METADATA_SENDERID]) 71 getDispatcher().dispatch(data, metadata)
72
73 - def sendEventUpdate(self, data, receiver = None):
74 self.sendMessage(data, 'ioids.write.newevent', receiver)
75
76 - def sendKnowledgeRequest(self, conditions, receiver):
77 """ 78 @param conditions: List of conditions; each item a list itself (attribute name | operator indicator | value) 79 @type conditions: C{List} of C{List} 80 """ 81 condXml = None # here we go 82 self.sendMessage(condXml, 'ioids.read.events', receiver)
83
84 - def sendMessage(self, data, action, receiver = None):
85 """ 86 Testing - send it off to everybody in the service :) 87 """ 88 if not receiver: 89 from config import G4DS_SERVICE_ID 90 receiver = G4DS_SERVICE_ID 91 92 self._g4ds.sendMessage(receiver, None, data, action) 93 from ioidslogging import getDefaultLogger, G4DS_CONNECTOR_OUTGOING_MSG, G4DS_CONNECTOR_OUTGOING_MSG_DETAILS 94 getDefaultLogger().newMessage(G4DS_CONNECTOR_OUTGOING_MSG, 'G4DS Outgoing message: Passed new message to %s' %(receiver)) 95 getDefaultLogger().newMessage(G4DS_CONNECTOR_OUTGOING_MSG_DETAILS, '-- Outgoing message details: action is %s' %(action)) 96 getDefaultLogger().newMessage(G4DS_CONNECTOR_OUTGOING_MSG_DETAILS, '-- Outgoing message details: data size is %s' %(len(data)))
97 98 99 # "singleton" 100 _dispatcher = None
101 -def getDispatcher():
102 """ 103 Singleton implementation. 104 105 @return: The instance for the Dispatcher 106 @rtype: L{Dispatcher} 107 """ 108 global _dispatcher 109 if not _dispatcher: 110 _dispatcher = Dispatcher() 111 return _dispatcher
112
113 -class Dispatcher:
114
115 - def __init__(self):
116 """ 117 Yet empty constructor. 118 """ 119 pass
120
121 - def dispatch(self, data, metadata):
122 """ 123 First instance for incoming messages. 124 """ 125 ## print "My meta data: %s" %metadata 126 from ioidslogging import getDefaultLogger, G4DS_CONNECTOR_INCOMING_MSG, G4DS_CONNECTOR_INCOMING_MSG_DETAILS 127 getDefaultLogger().newMessage(G4DS_CONNECTOR_INCOMING_MSG, 'G4DS Incoming message: Received data from %s | %s' %(metadata['senderid'], metadata['communityid'])) 128 getDefaultLogger().newMessage(G4DS_CONNECTOR_INCOMING_MSG_DETAILS, '-- G4DS Incoming message details: action is %s ' %(metadata['actionstring'])) 129 getDefaultLogger().newMessage(G4DS_CONNECTOR_INCOMING_MSG_DETAILS, '-- G4DS Incoming message details: data size is %s ' %(len(data))) 130 if metadata['actionstring'] == 'ioids.write.newevent': 131 self._incomingMessageNewIoidsEvent(data) 132 elif metadata['actionstring'] == 'ioids.read.events': 133 sender = metadata['senderid'] 134 community = metadata['communityid'] 135 self._replyToKnowledgeRequest(sender, community, data)
136 137
138 - def _incomingMessageNewIoidsEvent(self, data):
139 ioidsevent = None 140 relations = [] 141 142 from messagewrapper import getIoidsMessageWrapper 143 ioidsevent, relations = getIoidsMessageWrapper().unwrapFullIoidsEventMessage(data) 144 from dataengine import getDataEngine 145 getDataEngine().newIoidsEventFromRemote(ioidsevent , relations)
146 147
148 - def _replyToKnowledgeRequest(self, sender, community, data):
149 """ 150 @param data: XML encoded conditions 151 @type data: C{String} xml encoded 152 """ 153 pass
154