def testGetOwnedTuples(self): node = StaticTupleSpacePeer() inputData = [('ownresource1','ivr',node.id), ('ownresource2','ivr',node.id), ('otherResource1','ivr','otherid123456')] expectedResult = [] # Attempt to publish the data tuple for item in inputData: node.put(item) returnedTuples = node.getOwnedTuples() # check that the expected result was returned # construct the expected result for i in range(2): parsedStr = (inputData[i][0], inputData[i][1]) serializedStr = cPickle.dumps(parsedStr) expectedResult.append([node.id, serializedStr]) expectedResult.reverse() # Only tuples owned by this node (has this nodes id) should be returned self.failUnlessEqual(returnedTuples, expectedResult, "Tuples returned from getOwnedTuples not the same as the expected result." \ " Only tuples owned by this node (has this nodes id) should be returned")
def __init__(self, udpPort=4000): StaticTupleSpacePeer.__init__(self, udpPort=udpPort) self._localSMSHandlers = [] self._localIVRHandlers = [] self.resourceConfig = {'ivr': {}, 'sms': {}} self.fastAGIServer = None self._joinedNetwork = False self._callQueue = [] self.claimedResources = 0 # Counter to keep track of how many consumable resources we are using self._log = setupLogger(debug=True, name='mobilIVR')
def testFindTuple(self): node = StaticTupleSpacePeer() inputData = ('resource','ivr',node.id) # Attempt to publish the data tuple node.put(inputData) # Attempt to find the data returnedTuple = node.findTuple(('resource', 'ivr')) # check that the expected result was returned expectedResult = ('resource', 'ivr', node.id) self.failUnlessEqual(returnedTuple, expectedResult, "Tuple returned from findTuple not the same as the expected result")
def testPut(self): node = StaticTupleSpacePeer() inputData = ('resource','ivr',node.id) parsedInput = (inputData[0], inputData[1]) serializedInput = cPickle.dumps(parsedInput) # Attempt to publish the data tuple node.put(inputData) # Check that the data is in the data store h = hashlib.sha1() h.update(serializedInput) mainKey = h.digest() dataStoreTuple = node.dataStore.__getitem__(mainKey) ownerID = node.dataStore.originalPublisherID(mainKey) self.failUnlessEqual(serializedInput, dataStoreTuple, "Input data not equal to the data found in the dataStore") self.failUnlessEqual(ownerID, node.id, "Input owner ID not equal to the owner ID found in the dataStore")
def setUp(self): # create a fake protocol to imitate communication with other nodes self._protocol = FakeRPCProtocol() # Note: The reactor is never started for this test. All deferred calls run sequentially, # since there is no asynchronous network communication # create the node to be tested in isolation self.node = StaticTupleSpacePeer(networkProtocol=self._protocol) self.updPort = 81173 # create a network with IP addresses, port numbers and the corresponding node ID self.network = [['nodeID1',('127.0.0.1', 12345)],['nodeID2',('146.24.1.1', 34567)], ['nodeID3',('147.22.1.16', 234566)]] self.dataStore = [['nodeID1', ('resource1','ivr1', 'nodeID1')], ['nodeID2', ('resource2','ivr2', 'nodeID2')],\ ['nodeID3', ('resource3','ivr3', 'nodeID3')]] self._protocol.createNetwork(self.network) self._protocol.createDataStore(self.dataStore)
sys.exit(1) try: int(sys.argv[1]) except ValueError: print '\nUDP_PORT must be an integer value.\n' print 'Usage:\n%s UDP_PORT [KNOWN_NODE_IP KNOWN_NODE_PORT]' % sys.argv[0] print 'or:\n%s UDP_PORT [FILE_WITH_KNOWN_NODES]' % sys.argv[0] print '\nIf a file is specified, it should contain one IP address and UDP port\nper line, seperated by a space.' sys.exit(1) if len(sys.argv) == 4: knownNodes = [(sys.argv[2], int(sys.argv[3]))] elif len(sys.argv) == 3: knownNodes = [] f = open(sys.argv[2], 'r') lines = f.readlines() f.close() for line in lines: ipAddress, udpPort = line.split() knownNodes.append((ipAddress, int(udpPort))) else: knownNodes = None node = StaticTupleSpacePeer( udpPort=int(sys.argv[1]) ) node.joinNetwork(knownNodes) node._joinDeferred.addCallback(startServices) node._joinDeferred.addErrback(checkStatus) #twisted.internet.reactor.callLater(0.5, node.get,'tuple') twisted.internet.reactor.run()
def joinNetwork(self, knownNodeAddresses=None): self._log.info('Joining network') StaticTupleSpacePeer.joinNetwork(self, knownNodeAddresses) self._joinDeferred.addCallback(self.startServices) self._joinDeferred.addCallback(self._execCallQueue) self._joinDeferred.addErrback(self._joinFailed)
print 'or:\n%s UDP_PORT [FILE_WITH_KNOWN_NODES]' % sys.argv[0] print '\nIf a file is specified, it should containg one IP address and UDP port\nper line, seperated by a space.' sys.exit(1) try: int(sys.argv[1]) except ValueError: print '\nUDP_PORT must be an integer value.\n' print 'Usage:\n%s UDP_PORT [KNOWN_NODE_IP KNOWN_NODE_PORT]' % sys.argv[0] print 'or:\n%s UDP_PORT [FILE_WITH_KNOWN_NODES]' % sys.argv[0] print '\nIf a file is specified, it should contain one IP address and UDP port\nper line, seperated by a space.' sys.exit(1) if len(sys.argv) == 4: knownNodes = [(sys.argv[2], int(sys.argv[3]))] elif len(sys.argv) == 3: knownNodes = [] f = open(sys.argv[2], 'r') lines = f.readlines() f.close() for line in lines: ipAddress, udpPort = line.split() knownNodes.append((ipAddress, int(udpPort))) else: knownNodes = None node = StaticTupleSpacePeer( udpPort=int(sys.argv[1]) ) node.put(('handler', 'jokeapp'), node.id) node.put(('ivr', 'english', 'time'), node.id) node.put('otherTuple', 'jjksl33434') node.joinNetwork(knownNodes) twisted.internet.reactor.run()
class NetworkCreationTest(unittest.TestCase): """ This test suite tests that a StaticTupleSpacePeer can contact its peers (know addresses) and obtain the tuples(data) stored at those peers """ def setUp(self): # create a fake protocol to imitate communication with other nodes self._protocol = FakeRPCProtocol() # Note: The reactor is never started for this test. All deferred calls run sequentially, # since there is no asynchronous network communication # create the node to be tested in isolation self.node = StaticTupleSpacePeer(networkProtocol=self._protocol) self.updPort = 81173 # create a network with IP addresses, port numbers and the corresponding node ID self.network = [['nodeID1',('127.0.0.1', 12345)],['nodeID2',('146.24.1.1', 34567)], ['nodeID3',('147.22.1.16', 234566)]] self.dataStore = [['nodeID1', ('resource1','ivr1', 'nodeID1')], ['nodeID2', ('resource2','ivr2', 'nodeID2')],\ ['nodeID3', ('resource3','ivr3', 'nodeID3')]] self._protocol.createNetwork(self.network) self._protocol.createDataStore(self.dataStore) def testJoinNetwork(self): # get the known addresses from self.network knownAddresses = [] for item in self.network: knownAddresses.append(item[1]) # Attempt to join the network of known addresses self.node.joinNetwork(knownAddresses) # Check that the contacts have been added to the nodes contactsList # construct the contacts used for testing expectedContactsList = [] for item in self.network: cont = Contact(item[0], item[1][0], item[1][1], self._protocol) expectedContactsList.append(cont) # print 'expected list ' # for conti in expectedContactsList: # print str(conti) actualContactsList = self.node.contactsList # print 'actual contacts list ' + str(actualContactsList) # for conti in actualContactsList: # print str(conti) # Check that the expected contacts list are the same as the actual contacts list populated during the join sequence self.failUnlessEqual(expectedContactsList, actualContactsList, \ "Contacts List populated by peer during join sequence doesn't contain the expected contacts") # Check that the dataStore has been populated for item in self.dataStore: expectedTuple = item[1] #print 'searching for: ' + str(item[1]) returnedTuple = self.node.get(expectedTuple) #print 'returned Tuple ' + str(returnedTuple) # Check that the returnedTuple is the same as the expected tuple self.failUnlessEqual(expectedTuple[1], returnedTuple[1], \ "The data store was not populated correctly, expected tuple to be stored")