def verifyPurchase(sock2, qty): received = sock2.recv(16800) messagePurchase= Message(received) print messagePurchase.__str__() if (not messagePurchase.isMessageStatusOk()): raise FoundationException("error in creating purchase") else: # verify that the quantity purchased. qtyPurchasedStr = messagePurchase.getParameter('Quantity_Purchased') qtyPurchased = float(qtyPurchasedStr) if (qtyPurchased <> qty): raise FoundationException("error in creating purchase - Qty purchased" + str(qtyPurchased)+ " not equal to " + str(qty))
def verifyAvailability(sock2, qty): received = sock2.recv(16800) messageAvail= Message(received) print messageAvail.__str__() if (not messageAvail.isMessageStatusOk()): raise FoundationException("error in the availability message") else: # verify that the quantity purchased. qtyStr = messageAvail.getParameter('Quantity') qtyAvail = float(qtyStr) qtyAvail = round(qtyAvail,2) if (qtyAvail <> qty): raise FoundationException("error in the quantity available" + str(qtyAvail)+ "- Qty not equal to " + str(qty))
def isComplete(self, lenght): try: size = self.getParameter("Message_Size") messageSize = int(size) if (lenght == messageSize): return True else: return False except FoundationException as e: print 'The message is' + '\n' + self.__str__() raise FoundationException("Parameter not found") except ValueError as e: print 'The value in size is' + str(size) raise FoundationException("Invalid value inside Message Size")
def __init__(self, address, port): # Creates the socket for marketplace self._streamStaged = '' #HOST = '192.168.1.129' # The remote host HOST = address #PORT = 5555 # The same port as used by the server PORT = port try: self._s_mkt = socket.socket(socket.AF_INET, socket.SOCK_STREAM) err = self._s_mkt.connect_ex((HOST, PORT)) if (err > 0): raise FoundationException( "Error: the Market Place Server is not running") except socket.error, msg: raise FoundationException('Failed to create socket. Error code: ' \ + str(msg[0]) + ' , Error message : ' + msg[1])
def getParameter(self, param): ''' This method returns the message parameter. ''' if param in self._parameters: return self._parameters[param] else: raise FoundationException('Parameter not found')
def getDecisionVariable(self, Id): ''' This method gets a decision variable from the service. ''' if Id in self._decision_variables: return self._decision_variables[Id] else: raise FoundationException("The Decision variable is not part of the service")
def addDecisionVariable(self, decision_param): ''' This method adds a decision variable to the service. ''' if decision_param.getId() in self._decision_variables: raise FoundationException("Decision variable is already included") else: self._decision_variables[decision_param.getId()] = decision_param
def __init__(self, address, port): # Creates the socket self._streamStaged = '' HOST = address PORT = port self._s_provider = socket.socket(socket.AF_INET, socket.SOCK_STREAM) err = self._s_provider.connect_ex((HOST, PORT)) if (err > 0): raise FoundationException("Error: the provider is not running")
def __init__(self): # Creates the socket for Clock Server self._streamStaged = '' #HOST = '192.168.1.129' # The remote host HOST = agent_properties.addr_clock_server #PORT = 3333 # The same port as used by the server PORT = agent_properties.clock_listening_port self._s_clock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) err = self._s_clock.connect_ex((HOST, PORT)) if (err > 0): raise FoundationException("Error: the Clock Server is not running")
def setParameter(self, parameterKey, parameterValue): ''' This method add the parameter to the list of parameters. ''' # First we check if the parameter key is already on the list if parameterKey in self._parameters: # If it is already on the list, we create an exception raise FoundationException( 'Parameter is already included on the list') # If not, we add the parameter to the list else: self._parameters[parameterKey] = parameterValue
def handleGetServices(document): print 'starting get service handler' document = removeIlegalCharacters(document) try: dom = xml.dom.minidom.parseString(document) servicesXml = dom.getElementsByTagName("Service") services = {} print 'starting get service handler' for servicexml in servicesXml: service = Service() print 'starting get service handler' service.setFromXmlNode(servicexml) print 'starting get service handler' services[service.getId()] = service return services except Exception as e: raise FoundationException(str(e))
def test_bulk_capacity_service4(): message1= Message('') message1.setMethod(Message.CONNECT) strProv = "Provider8" serviceId = "4" # Priority_Class_Streaming message1.setParameter("Agent", strProv) try: # Connect to Clock server and send data print 'Connecting Host:', HOST, ' Port:', PORT sock.connect((HOST, PORT)) sock.sendall(message1.__str__()) received = "" received = sock.recv(4096) recMsg = Message(received) if (recMsg.isMessageStatusOk()): connect = Message("") connect.setMethod(Message.GET_SERVICES) connect.setParameter("Service",serviceId) sock.sendall(connect.__str__()) received = sock.recv(4096) serviceResponse = Message(received) if (serviceResponse.isMessageStatusOk() == False): print 'Service not received' else: print 'Successful Clock Server Connection' #print "Sent: {}".format(message1.__str__()) #print "Received: {}".format(received) # Connect to market Place server and send data print 'Connecting Host:', HOST2, ' Port:', PORT2 sock2.connect((HOST2, PORT2)) sock2.sendall(message1.__str__()) received = sock2.recv(4096) recMsg = Message(received) if (recMsg.isMessageStatusOk()): # sends the message with the port connect. This assumes that # the program socket_server.py was previously executed. port_message = Message("") port_message.setMethod(Message.SEND_PORT) port_message.setParameter("Port", agent_properties.l_port_provider) port_message.setParameter("Type", "provider") port_message.setParameter("CapacityType","bulk") print 'recMsg', port_message.__str__() sock2.sendall(port_message.__str__()) received = sock2.recv(4096) recMsg = Message(received) print 'recMsg', recMsg.__str__() if (recMsg.isMessageStatusOk()): pass time.sleep(1) print 'Successful Market Place Server Connection' # send the provider availability resource = "2" message = send_availability(strProv,resource, str(120)) print 'message to send:' + message.__str__() sock2.sendall(message.__str__()) received = sock2.recv(4096) print received recMsg= Message(received) if (not recMsg.isMessageStatusOk()): raise FoundationException("error in sending availability") print "It is connected to the agent server" # creates bids for the service 01 message, bidId1 = createBidService4(strProv, serviceId, str(0), str(0.324563087213)) sock2.sendall(message.__str__()) verifyBid(sock2) # Number of resource used: 28.386816*(1 + 1)= 13.66 message = purchaseService4(serviceId, bidId1, str(28.386816), str(0), str(0.324563087213)) sock2.sendall(message.__str__()) received = sock2.recv(16800) messagePur1 = Message(received) if (not messagePur1.isMessageStatusOk()): raise FoundationException("error in creating purchase" + messagePur1.__str__()) else: print 'Message arrived \n' + messagePur1.__str__() message = getAvailability(strProv,serviceId,'') sock2.sendall(message.__str__()) qtyAvail = 120 - (28.386816*2) qtyAvail = round(qtyAvail,2) verifyAvailability(sock2, qtyAvail) # Number of resource used: 66.749068 *(1 + 1)= 133.498136, as there are not enough quantities, the the server only purchase 31 message = purchaseService4(serviceId, bidId1, str(66.749068), str(0), str(0.324563087213)) sock2.sendall(message.__str__()) verifyPurchase(sock2,31.613184) message = getAvailability(strProv,serviceId,'') sock2.sendall(message.__str__()) qtyAvail = 0 verifyAvailability(sock2, qtyAvail) print 'It is going to sleep 10 seconds to see whether or not the server restart the availability' # this part test the restart of the capacity in the provider. time.sleep(10) message = getAvailability(strProv,serviceId,'') sock2.sendall(message.__str__()) qtyAvail = round(120, 2) verifyAvailability(sock2, qtyAvail) # we have performance problems with bid insertion. we test it. i = 1 q = 0.05 while i <= 40: message2, bidId2 = createBidService4(strProv, serviceId, str(q), str(0.324563087213)) sock2.sendall(message2.__str__()) verifyBid(sock2) message3 = purchaseService4(serviceId, bidId2, str(q), str(0), str(0.324563087213)) sock2.sendall(message3.__str__()) verifyPurchaseMessage(sock2,q) message3 = getAvailability(strProv,serviceId,'') sock2.sendall(message3.__str__()) verifyAvailabilityMessage(sock2, qtyAvail) if q < 0.5: q = q + 0.05 i = i + 1 finally: sock.shutdown(socket.SHUT_WR) sock.close() sock2.shutdown(socket.SHUT_WR) sock2.close()
def test_bulk_capacity(): try: # Connect to Clock server and send data print 'Connecting Host:', HOST, ' Port:', PORT sock.connect((HOST, PORT)) sock.sendall(message1.__str__()) received = "" received = sock.recv(4096) recMsg = Message(received) if (recMsg.isMessageStatusOk()): connect = Message("") connect.setMethod(Message.GET_SERVICES) connect.setParameter("Service",serviceId) sock.sendall(connect.__str__()) received = sock.recv(4096) serviceResponse = Message(received) if (serviceResponse.isMessageStatusOk() == False): print 'Service not received' else: print 'Successful Clock Server Connection' #print "Sent: {}".format(message1.__str__()) #print "Received: {}".format(received) # Connect to market Place server and send data print 'Connecting Host:', HOST2, ' Port:', PORT2 sock2.connect((HOST2, PORT2)) sock2.sendall(message1.__str__()) received = sock2.recv(4096) recMsg = Message(received) if (recMsg.isMessageStatusOk()): # sends the message with the port connect. This assumes that # the program socket_server.py was previously executed. port_message = Message("") port_message.setMethod(Message.SEND_PORT) port_message.setParameter("Port", agent_properties.l_port_provider) port_message.setParameter("Type", "provider") port_message.setParameter("CapacityType","bulk") print 'recMsg', port_message.__str__() sock2.sendall(port_message.__str__()) received = sock2.recv(4096) recMsg = Message(received) print 'recMsg', recMsg.__str__() if (recMsg.isMessageStatusOk()): pass time.sleep(1) print 'Successful Market Place Server Connection' resource = "1" # send the provider availability message = send_availability(strProv,resource, str(100)) print 'message to send:' + message.__str__() sock2.sendall(message.__str__()) received = sock2.recv(4096) print received recMsg= Message(received) if (not recMsg.isMessageStatusOk()): raise FoundationException("error in sending availability") print "It is connected to the agent server" # creates bids for the service 01 message, bidId1 = createBid(strProv, serviceId, str(0.145), str(20)) sock2.sendall(message.__str__()) verifyBid(sock2) # Number of resource used: 10*(0.4 + 1*(0.916666667)= 13.66 message = purchase(serviceId, bidId1, str(10), str(0.145), str(20)) sock2.sendall(message.__str__()) received = sock2.recv(16800) messagePur1 = Message(received) if (not messagePur1.isMessageStatusOk()): raise FoundationException("error in creating purchase") message = getAvailability(strProv,serviceId,'') sock2.sendall(message.__str__()) qtyAvail = round(100 - 13.166,2) verifyAvailability(sock2, qtyAvail) message, bidId2 = createBid(strProv, serviceId, str(0.16), str(18)) sock2.sendall(message.__str__()) verifyBid(sock2) # Number of resource used: 5*(0.4 + 1*(0.6667))=5.333 message = purchase(serviceId, bidId2, str(5), str(0.16), str(18)) sock2.sendall(message.__str__()) verifyPurchase(sock2,5) message = getAvailability(strProv,serviceId,'') sock2.sendall(message.__str__()) qtyAvail = round(qtyAvail - 5.333,2) verifyAvailability(sock2, qtyAvail) message, bidId3 = createBid(strProv, serviceId, str(0.15), str(21)) sock2.sendall(message.__str__()) verifyBid(sock2) # Number of resource used: 4*(0.4 + 1*(0.83333))=4.9333 message = purchase(serviceId, bidId3, str(4), str(0.15), str(21)) sock2.sendall(message.__str__()) verifyPurchase(sock2,4) message = getAvailability(strProv,serviceId,'') sock2.sendall(message.__str__()) qtyAvail = round(qtyAvail - 4.9333,2) verifyAvailability(sock2, qtyAvail) message, bidId4 = createBid(strProv, serviceId, str(0.15), str(19.5)) sock2.sendall(message.__str__()) verifyBid(sock2) # Number of resource used: 3*(0.4 + 1*(0.83333))=3.7 message = purchase(serviceId, bidId4, str(3), str(0.15), str(19.5)) sock2.sendall(message.__str__()) verifyPurchase(sock2,3) message = getAvailability(strProv,serviceId,'') sock2.sendall(message.__str__()) qtyAvail = round(qtyAvail - 3.7, 2) verifyAvailability(sock2, qtyAvail) message, bidId5 = createBid(strProv, serviceId, str(0.15), str(18)) sock2.sendall(message.__str__()) verifyBid(sock2) # Number of resource used: 2*(0.4 + 1*(0.83333))=2.46 message = purchase(serviceId, bidId5, str(2), str(0.15), str(18)) sock2.sendall(message.__str__()) verifyPurchase(sock2,2) message = getAvailability(strProv,serviceId,'') sock2.sendall(message.__str__()) qtyAvail = round(qtyAvail - 2.466, 2) verifyAvailability(sock2, qtyAvail) message, bidId6 = createBid(strProv, serviceId, str(0.155), str(17.5)) sock2.sendall(message.__str__()) verifyBid(sock2) # Number of resource used: 1*(0.4 + 1*(0.75))=1.15 message = purchase(serviceId, bidId6, str(1), str(0.155), str(17.5)) sock2.sendall(message.__str__()) verifyPurchase(sock2,1) message = getAvailability(strProv,serviceId,'') sock2.sendall(message.__str__()) qtyAvail = round(qtyAvail - 1.15, 2) verifyAvailability(sock2, qtyAvail) # # Number of resource used: 10*(0.4 + 1*(1.066))=11.5 message = purchase(serviceId, bidId6, str(10), str(0.16), str(17.5)) sock2.sendall(message.__str__()) verifyPurchase(sock2,10) message = getAvailability(strProv,serviceId,'') sock2.sendall(message.__str__()) qtyAvail = round(qtyAvail - 11.5, 2) verifyAvailability(sock2, qtyAvail) # # Number of resource used: 10*(0.4 + 1*(0.833))=24.66 message = purchase(serviceId, bidId4, str(20), str(0.125), str(20)) sock2.sendall(message.__str__()) verifyPurchase(sock2,20) message = getAvailability(strProv,serviceId,'') sock2.sendall(message.__str__()) qtyAvail = 33.08 verifyAvailability(sock2, qtyAvail) # Number of resource used: 10*(0.4 + 1*(0.833))=12.333 message = purchase(serviceId, bidId5, str(10), str(0.16), str(17)) sock2.sendall(message.__str__()) verifyPurchase(sock2,10) message = getAvailability(strProv,serviceId,'') sock2.sendall(message.__str__()) qtyAvail = round(qtyAvail - 12.33, 2) verifyAvailability(sock2, qtyAvail) # The quantity is not enough, so it could not purchase. message = purchase(serviceId, bidId4, str(30), str(0.16), str(17)) sock2.sendall(message.__str__()) verifyPurchase(sock2,0) #try to buy an inactive bid message, bidId6 = inactiveBid(bidId6, strProv, serviceId, str(0.155), str(17.5)) sock2.sendall(message.__str__()) received = sock2.recv(4096) messageBid6 = Message(received) if (messageBid6.isMessageStatusOk()): pass message = purchase(serviceId, bidId6, str(1), str(0.16), str(17)) sock2.sendall(message.__str__()) verifyPurchase(sock2,0) #message = getAvailability(strProv,serviceId,'') #sock2.sendall(message.__str__()) #qtyAvail = round(qtyAvail - 1.15, 2) #verifyAvailability(sock2, qtyAvail) # this part test the restart of the capacity in the provider. time.sleep(10) message = getAvailability(strProv,serviceId,'') sock2.sendall(message.__str__()) qtyAvail = round(100, 2) verifyAvailability(sock2, qtyAvail) finally: sock.shutdown(socket.SHUT_WR) sock.close() sock2.shutdown(socket.SHUT_WR) sock2.close()
def verifyBid(sock2): received = sock2.recv(16800) messageBid= Message(received) if (not messageBid.isMessageStatusOk()): raise FoundationException("error in creating bid")