def __init__(self, request, client_address, server): # Read configuration parameter self.__isdebug = CONTEXT['debug'] self.__size = CONTEXT['request_size'] self.__logger = get_logger("TCPRequestHandler") # Compression Utility self.__compressedcontent = CONTEXT['compressed_content'] self.__compression = Utility() self.__client = client_address # populate the seller list self.__sellerObj = server.sellerObject # Initialize Ad Exchange self.__adExObject = server.adExObject # Initialize the DB connection self.__dbConnection = DBConnection() if DB_PARAMS['truncate']: self.__dbConnection.query("truncate table `VirtualFiber`.`IPAllocation`") self.__infra_tested = TEST_PARAMS['infra_tested'] # Call base class BaseRequestHandler.__init__(self, request, client_address, server)
def __init__(self, buyer_data=TEST_PARAMS['buyer_file_name'], path_to_data=TEST_PARAMS['path'], totalReqs=-1): ''' Class constructor ''' Thread.__init__(self) # Remote service bindings self.__serverhosts = SERVER_BINDING['address'] self.__serverport = int(SERVER_BINDING['port']) # Buffer settings self.__bufferdim = int(CONTEXT['client_socket_buffer']) # Compression helper self.__compression = Utility() self.__resourcepath = TEST_PARAMS['client_path'] self.__buyerFile = buyer_data self.__client_request_type = TEST_PARAMS['client_request_type'] self.__client_request_code = TEST_PARAMS['client_request_code'] self.__logger = get_logger("TCPClient") self.__conn_timeout = 1 self.__recv_timeout = 6000 self.totalReqs = totalReqs
def __init__(self, request, client_address, server): # Read configuration parameter self.__isdebug = CONTEXT['debug'] self.__size = CONTEXT['request_size'] self.__logger = get_logger("TCPRequestHandler") # Compression Utility self.__compressedcontent = CONTEXT['compressed_content'] self.__compression = Utility() self.__client = client_address # populate the seller list self.__sellerObj = server.sellerObject # Initialize Ad Exchange self.__adExObject = server.adExObject # Initialize the DB connection self.__dbConnection = server.dbConnection if DB_PARAMS['truncate']: self.__dbConnection.query("truncate table `GreyFiber`.`IPAllocation`") self.__infra_tested = TEST_PARAMS['infra_tested'] if self.__infra_tested == "MININET": self.__mininetConnection = server.mininetConnection self.__floodlightConnection = server.floodlightConnection # Call base class BaseRequestHandler.__init__(self, request, client_address, server)
def __init__(self): ''' Class constructor ''' Thread.__init__(self) # Remote service bindings self.__serverhost = SERVER_BINDING['address'] self.__serverport = int(SERVER_BINDING['port']) # Buffer settings self.__bufferdim = int(CONTEXT['client_socket_buffer']) # Compression helper self.__compression = Utility() self.__resourcepath = TEST_PARAMS['path'] self.__buyerFile = TEST_PARAMS['buyer_file_name'] self.__client_request_type = TEST_PARAMS['client_request_type'] self.__client_request_code = TEST_PARAMS['client_request_code'] self.__logger = get_logger("TCPClient")
class TCPClient(threading.Thread): ''' JSON/TCP client thread ''' def __init__(self): ''' Class constructor ''' Thread.__init__(self) # Remote service bindings self.__serverhosts = SERVER_BINDING['address'] self.__serverport = int(SERVER_BINDING['port']) # Buffer settings self.__bufferdim = int(CONTEXT['client_socket_buffer']) # Compression helper self.__compression = Utility() self.__resourcepath = TEST_PARAMS['client_path'] self.__buyerFile = TEST_PARAMS['buyer_file_name'] self.__client_request_type = TEST_PARAMS['client_request_type'] self.__client_request_code = TEST_PARAMS['client_request_code'] self.__logger = get_logger("TCPClient") self.__conn_timeout = 1 self.__recv_timeout = 60 def run(self): ''' Thread handler ''' try: sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Request creation resource = [] resource.append(self.__resourcepath) resource.append(self.__buyerFile) self.__logger.debug( "[TCPClient][run]Resource read: {}\n".format(resource)) # Test if compressed content is needed bidList = [] zippedfile = None testfile = None if (self.__compression): zippedfile = gzip.open(''.join(resource) + ".gz", "r+") bidList = zippedfile.readlines() self.__logger.info( "[TCPClient][run](Compressed) Content size {0}".format( sys.getsizeof(bidList))) else: testfile = open(''.join(resource), 'r') bidList = testfile.readlines() self.__logger.info( "[TCPClient][run](Uncompressed) Content size {0}".format( sys.getsizeof(bidList))) data = Request(self.__client_request_type, self.__client_request_code, bidList) # Client socket binding connected = False hosts = self.__serverhosts[:] while not connected and hosts: host = hosts.pop() try: self.__logger.debug( "[TCPClient][run]Trying to connect to host {0}".format( host)) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.settimeout(self.__conn_timeout) sock.connect((host, self.__serverport)) connected = True sock.settimeout(self.__recv_timeout) except socket.error as e: self.__logger.debug( "[TCPClient][run]Failed to connect to host {0}".format( host)) if not hosts: self.__logger.error("Error::NET::No hosts available") raise Exception # Sending JSON data over the socket sock.sendall(data.to_json()) self.__logger.info("[TCPClient][run]Request sent...") start = time.time() response = self.__receive_data(sock) end = time.time() self.__logger.debug( "[TCPClient][run]Reception::Time Elapsed: {0}".format(end - start)) self.__logger.debug( "[TCPClient][run](Compressed) Dimension:: {0}".format( sys.getsizeof(response))) self.__logger.debug( "[TCPClient][run]Raw Response: {0}".format(response)) # Treating compressed data result = self.__compression.decompress(response) data = Data(False, [], 0) data.from_json(result) if (int(data.nrbytes) == sys.getsizeof(data.vector)): self.__logger.info("[TCPClient][run]Integrity is OK") self.__logger.info("[TCPClient][run]Allocations") for allocation in data.vector: self.__logger.info(allocation) else: self.__logger.debug( "[TCPClient][run]Data from json: {0}".format(data)) except Exception, e: self.__logger.error("Error::NET::sending exception {0}".format(e)) finally:
class TCPRequestHandler(SocketServer.BaseRequestHandler): ''' Specific JSON/TCP request handler ''' def __init__(self, request, client_address, server): # Read configuration parameter self.__isdebug = CONTEXT['debug'] self.__size = CONTEXT['request_size'] self.__logger = get_logger("TCPRequestHandler") # Compression Utility self.__compressedcontent = CONTEXT['compressed_content'] self.__compression = Utility() self.__client = client_address # populate the seller list self.__sellerObj = server.sellerObject # Initialize Ad Exchange self.__adExObject = server.adExObject # Initialize the DB connection self.__dbConnection = server.dbConnection if DB_PARAMS['truncate']: self.__dbConnection.query("truncate table `GreyFiber`.`IPAllocation`") self.__infra_tested = TEST_PARAMS['infra_tested'] if self.__infra_tested == "MININET": self.__mininetConnection = server.mininetConnection self.__floodlightConnection = server.floodlightConnection # Call base class BaseRequestHandler.__init__(self, request, client_address, server) def getFlowTuples(self, item): flowTuples = [] for ipA, ipB, portA, portB in zip(item.ipA.split(','), item.ipB.split(','), \ item.portA.split(','), item.portB.split(',')): flowTuples.append((ipA, ipB, portA, portB)) return flowTuples def handle(self): ''' Service handler method ''' try: self.__logger.debug("[TCPRequestHandler][handle]Connection accepted... processing") # Reading request (assuming a small amount of bytes) data = self.request.recv(self.__size).strip() # Unmarshall the request request = Request('', 0, '') request.from_json(data) # Print data out, if debug layer3 = Layer3() overheadList = [] with Timer() as tTotalProcessing: self.__logger.debug("[TCPRequestHandler][handle]Received data:", str(request.content)) if (request.name == "BUYER" and request.code == 100): self.__logger.info("Request from Buyer received.") ClientRequest = collections.namedtuple('ClientRequest','linkA linkB numberOfStrands \ capacityPerStrand bidPerStrand \ clientName winnerFlag toPay \ ipA ipB portA portB \ ISP prefixA prefixB') crDict = defaultdict(list) # prepare requests data and store it in a namedtuple for r in request.content: if r.startswith("#"): continue vals = r.strip().split(";") va1 = vals[0].strip() va2 = vals[1].strip() cr = ClientRequest(linkA=va1, linkB=va2, numberOfStrands=int(vals[2]), \ capacityPerStrand=int(vals[3]), bidPerStrand=int(vals[4]), \ clientName=vals[5].strip(), winnerFlag=0, toPay=0, \ ipA='', ipB='', portA='', portB='', \ ISP='', prefixA='', prefixB='') key = va1+"#"+va2 crDict[key].append(cr) # Dispatch the request to the server self.__logger.debug("[TCPRequestHandler][handle]Dispatching request to the Ad Exchange...") with Timer() as tAd: allocationList = self.__adExObject.processClientRequests(crDict, self.__sellerObj) val = tAd.printTime("AdExchange", tAd, CONTEXT['meas_format'], CONTEXT['meas_to_file']) overheadList.append(val) # Allocate IP address for circuits updatedList = layer3.allocateIPAddresses(self.__sellerObj.getSellerGraph(), allocationList, self.__dbConnection) self.__logger.debug("IP address and interface allocations decisions.") for u in updatedList: self.__logger.debug(u) # Create e-2-e path for client if self.__infra_tested == 'MININET': self.__logger.info("Launching mininet experiments.") for item in updatedList: if item.winnerFlag == 1: # Create circuits flowTuples = self.getFlowTuples(item) # Push circuits # TODO: write installation logic for mininet self.__logger.info("Circuit pushed into networks by GreyFiber for winner: {0}".format(item.clientName)) elif self.__infra_tested == 'REAL': context = geni.util.loadContext() self.__logger.info("Launching real network experiments.") with Timer() as tCircuitCreation: for item in updatedList: if item.winnerFlag == 1: # Create circuits flowTuples = self.getFlowTuples(item) # Push circuits locationA = (item.linkA.split(",")[1]).strip() locationB = (item.linkB.split(",")[1]).strip() capacity = item.capacityPerStrand * 1000 with Timer() as tGeneration: rspecName = two_nodes_rspec_simple(context, TEST_PARAMS['geni_slice_name'], locationA, locationB, \ item.ipA, item.ipB, item.portA, item.portB, capacity, TEST_PARAMS['geni_rspec_location'], item.numberOfStrands) val = tGeneration.printTime("ConfigGeneration", tGeneration, CONTEXT['meas_format'], CONTEXT['meas_to_file']) overheadList.append(val) with Timer() as tCreation: # call("/Applications/omniTools-2.10/stitcher.app/Contents/MacOS/stitcher createsliver %s %s" % (TEST_PARAMS['geni_slice_name'], rspecName), shell=True) call("/Applications/omniTools-2.10/omni.app/Contents/MacOS/omni createsliver -a missouri-ig %s %s" % (TEST_PARAMS['geni_slice_name'], rspecName), shell=True) # print "circuit creation", rspecName val = tCreation.printTime("CircuitCreation", tCreation, CONTEXT['meas_format'], CONTEXT['meas_to_file']) overheadList.append(val) self.__logger.info("Circuit pushed into networks by GreyFiber for winner: {0}".format(item.clientName)) val = tCircuitCreation.printTime("TotalGenerationAndCreation", tCircuitCreation, CONTEXT['meas_format'], CONTEXT['meas_to_file']) overheadList.append(val) else: raise ValueError('Wrong configuration parameter in TEST_PARAMS') # Prepare the response data response = Data(True, [], 0) response.vector = updatedList response.nrbytes = int(sys.getsizeof(updatedList)) # Marshall JSON representation json_str = response.to_json() self.__logger.debug("[TCPRequestHandler][handle](Original) JSON Dimension::", sys.getsizeof(json_str)) c_response = self.__compression.compress(json_str) self.__logger.debug("[TCPRequestHandler][handle](Compressed) JSON Dimension::", sys.getsizeof(c_response)) self.request.sendall(c_response) self.__logger.debug("[TCPRequestHandler][handle]Bunch of compressed data sent back!") elif (request.name == "SDX" and request.code == 001): self.__logger.info("Request from SDX received.") else: self.__dbConnection.close() self.__sshConnection.close() raise ValueError('Bad request name and code. Either should be from SDX or from Buyer.') val = tTotalProcessing.printTime("ProcessClientRequest", tTotalProcessing, CONTEXT['meas_format'], CONTEXT['meas_to_file']) overheadList.append(val) strVal = "\n".join(overheadList) print strVal # call only when a coarse grained plot is needed # a = StringIO(strVal) # plotTimeline(a, CONTEXT['meas_to_location']+"overhead.eps") except Exception, e: self.__dbConnection.close() self.__sshConnection.close() self.__logger.error("Exception upon message reception: ", e)
class TCPRequestHandler(SocketServer.BaseRequestHandler): ''' Specific JSON/TCP request handler ''' def __init__(self, request, client_address, server): # Read configuration parameter self.__isdebug = CONTEXT['debug'] self.__size = CONTEXT['request_size'] self.__logger = get_logger("TCPRequestHandler") # Compression Utility self.__compressedcontent = CONTEXT['compressed_content'] self.__compression = Utility() self.__client = client_address # populate the seller list self.__sellerObj = server.sellerObject # Initialize Ad Exchange self.__adExObject = server.adExObject # Initialize the DB connection self.__dbConnection = DBConnection() if DB_PARAMS['truncate']: self.__dbConnection.query("truncate table `VirtualFiber`.`IPAllocation`") self.__infra_tested = TEST_PARAMS['infra_tested'] # Call base class BaseRequestHandler.__init__(self, request, client_address, server) def getFlowTuples(self, item): flowTuples = [] for ipA, ipB, portA, portB in zip(item.ipA.split(','), item.ipB.split(','), \ item.portA.split(','), item.portB.split(',')): flowTuples.append((ipA, ipB, portA, portB)) return flowTuples def handle(self): ''' Service handler method ''' try: self.__logger.debug("[TCPRequestHandler][handle]Connection accepted... processing") # Reading request (assuming a small amount of bytes) data = "" while True: new_data = self.request.recv(self.__size).strip() data += new_data if '}' in new_data: break # Unmarshall the request request = Request('', 0, '') request.from_json(data) overheadList = [] with Timer() as tTotalProcessing: self.__logger.debug("[TCPRequestHandler][handle]Received data: %s", str(request.content)) if (request.name == "BUYER" and request.code == 100): self.__logger.info("Request from Buyer received.") ClientRequest = namedtuple('ClientRequest','linkA linkB numberOfStrands \ capacityPerStrand bidPerStrand \ clientName winnerFlag toPay \ ipA ipB portA portB \ ISP prefixA prefixB') crDict = defaultdict(list) # prepare requests data and store it in a namedtuple for r in request.content: if r.startswith("#"): continue vals = r.strip().split(";") va1 = vals[0].strip() va2 = vals[1].strip() cr = ClientRequest(linkA=va1, linkB=va2, numberOfStrands=int(vals[2]), \ capacityPerStrand=int(vals[3]), bidPerStrand=int(vals[4]), \ clientName=vals[5].strip(), winnerFlag=0, toPay=0, \ ipA='', ipB='', portA='', portB='', \ ISP='', prefixA='', prefixB='') key = va1+"#"+va2 crDict[key].append(cr) # Dispatch the request to the Adex self.__logger.debug("[TCPRequestHandler][handle]Dispatching request to the Fiber Exchange...") with Timer() as tAd: allocationList, ip_port_pairs = self.__adExObject.processClientRequests(crDict, self.__sellerObj) val = tAd.printTime("FiberExchange", tAd, CONTEXT['meas_format'], CONTEXT['meas_to_file']) self.__logger.debug("[TCPRequestHandler][handle]Elapsed Time {}".format(val)) overheadList.append(val) self.__logger.debug("[TCPRequestHandler][handle]Received allocation List from Fiber Exchange...{}".format(allocationList)) self.__logger.debug("[TCPRequestHandler][handle]Received ip_port_pairs from Fiber Exchange...{}".format(ip_port_pairs)) # Allocate IP address for circuits # allocationList = layer3.allocateIPAddresses(self.__sellerObj.getSellerGraph(), allocationList, self.__dbConnection) self.__logger.debug("[TCPRequestHandler][handle]IP address and interface allocations decisions.") for u in allocationList: self.__logger.debug("[TCPRequestHandler][handle] {}".format(u)) # Create e-2-e path for client if self.__infra_tested == 'REAL': self.__logger.info("Launching real network experiments.") with Timer() as tCircuitCreation: for item in allocationList: if item.winnerFlag == 1: # Create circuits with Timer() as tGeneration: flowTuples = self.getFlowTuples(item) # Push circuits self.__logger.info("Launching real network experiments. connecting {} and {}".format(item.linkA, item.linkB)) if "," in item.linkA: locationA = (item.linkA.split(",")[1]).strip() else: locationA = item.linkA[1].strip() if "," in item.linkB: locationB = (item.linkB.split(",")[1]).strip() else: locationB = item.linkB[1].strip() capacity = item.capacityPerStrand # switch_ips = ["192.168.57.200", "192.168.57.201"] val = tGeneration.printTime("Configuration Generation", tGeneration, CONTEXT['meas_format'], CONTEXT['meas_to_file']) with Timer() as tCreation: light_path(ip_port_pairs) val = tCreation.printTime("CircuitCreation", tCreation, CONTEXT['meas_format'], CONTEXT['meas_to_file']) self.__logger.debug("[TCPRequestHandler][handle]Elapsed Time {}".format(val)) overheadList.append(val) self.__logger.info("Circuit pushed into networks by vFiber for winner: {0}".format(item.clientName)) val = tCircuitCreation.printTime("TotalGenerationAndCreation", tCircuitCreation, CONTEXT['meas_format'], CONTEXT['meas_to_file']) self.__logger.debug("[TCPRequestHandler][handle]Elapsed Time {}".format(val)) overheadList.append(val) elif self.__infra_tested == 'MOCK': self.__logger.info("Launching mock network experiments.") with Timer() as tCircuitCreation: for item in allocationList: if item.winnerFlag == 1: # Create circuits #flowTuples = self.getFlowTuples(item) for ip, port in ip_port_pairs: self.__logger.debug("Pushing circuit to {} interface {}".format(ip, port)) # Push circuits # locationA = (item.linkA.split(",")[1]).strip() # locationB = (item.linkB.split(",")[1]).strip() capacity = item.capacityPerStrand # with Timer() as tCreation: # val = tCreation.printTime("CircuitCreation", tCreation, CONTEXT['meas_format'], CONTEXT['meas_to_file']) # overheadList.append(val) self.__logger.info("Circuit pushed into networks by vFiber for winner: {0}".format(item.clientName)) val = tCircuitCreation.printTime("TotalGenerationAndCreation", tCircuitCreation, CONTEXT['meas_format'], CONTEXT['meas_to_file']) self.__logger.debug("[TCPRequestHandler][handle]Elapsed Time {}".format(val)) overheadList.append(val) else: raise ValueError('Wrong configuration parameter in TEST_PARAMS') # Prepare the response data response = Data(True, [], 0) response.vector = allocationList response.nrbytes = int(sys.getsizeof(allocationList)) # Marshall JSON representation json_str = response.to_json() self.__logger.debug("[TCPRequestHandler][handle](Original) JSON Dimension:: %d", sys.getsizeof(json_str)) c_response = self.__compression.compress(json_str) self.__logger.debug("[TCPRequestHandler][handle](Compressed) JSON Dimension:: %d", sys.getsizeof(c_response)) self.request.sendall(c_response) self.__logger.debug("[TCPRequestHandler][handle]Bunch of compressed data sent back!") elif (request.name == "BUYER" and request.code == 101): self.__logger.info("Request to terminate buyer's allocation.") ClientRequest = namedtuple('ClientRequest','linkA linkB numberOfStrands \ capacityPerStrand bidPerStrand \ clientName winnerFlag toPay \ ipA ipB portA portB \ ISP prefixA prefixB') crDict = defaultdict(list) # prepare requests data and store it in a namedtuple for r in request.content: if r.startswith("#"): continue vals = r.strip().split(";") va1 = vals[0].strip() va2 = vals[1].strip() cr = ClientRequest(linkA=va1, linkB=va2, numberOfStrands=int(vals[2]), \ capacityPerStrand=int(vals[3]), bidPerStrand=int(vals[4]), \ clientName=vals[5].strip(), winnerFlag=0, toPay=0, \ ipA='', ipB='', portA='', portB='', \ ISP='', prefixA='', prefixB='') key = va1+"#"+va2 crDict[key].append(cr) # get interfaces from seller graph self.__logger.debug("[TCPRequestHandler][handle]Dispatching request to the Fiber Exchange...") with Timer() as tAd: allocationList, ip_port_pairs = self.__adExObject.returnAllocationToInfrustructureGraph(crDict, self.__sellerObj) val = tAd.printTime("returnAllocation", tAd, CONTEXT['meas_format'], CONTEXT['meas_to_file']) self.__logger.debug("[TCPRequestHandler][handle]Elapsed Time {}".format(val)) overheadList.append(val) # take back the e-2-e path if self.__infra_tested == 'REAL': self.__logger.info("[TCPRequestHandler][handle]Launching real network experiments.") with Timer() as tCircuitConfigDestruction: for item in allocationList: # Create circuits with Timer() as tGeneration: flowTuples = self.getFlowTuples(item) # Push circuits self.__logger.info("[TCPRequestHandler][handle]Disconnecting {} and {}".format(item.linkA, item.linkB)) if "," in item.linkA: locationA = (item.linkA.split(",")[1]).strip() else: locationA = item.linkA[1].strip() if "," in item.linkB: locationB = (item.linkB.split(",")[1]).strip() else: locationB = item.linkB[1].strip() capacity = item.capacityPerStrand # switch_ips = ["192.168.57.200", "192.168.57.201"] val = tGeneration.printTime("Configuration Generation", tGeneration, CONTEXT['meas_format'], CONTEXT['meas_to_file']) with Timer() as tDestruction: extinguish_path(ip_port_pairs) val = tDestruction.printTime("CircuitDestruction", tDestruction, CONTEXT['meas_format'], CONTEXT['meas_to_file']) self.__logger.debug("[TCPRequestHandler][handle]Elapsed Time {}".format(val)) overheadList.append(val) self.__logger.info("Circuit pulled from networks by vFiber from client: {0}".format(item.clientName)) val = tCircuitConfigDestruction.printTime("TotalGenerationAndDestruction", tCircuitConfigDestruction, CONTEXT['meas_format'], CONTEXT['meas_to_file']) self.__logger.debug("[TCPRequestHandler][handle]Elapsed Time {}".format(val)) overheadList.append(val) elif self.__infra_tested == 'MOCK': self.__logger.info("Launching mock network experiments.") with Timer() as tCircuitConfigDestruction: for item in allocationList: if item.winnerFlag == 1: # Create circuits #flowTuples = self.getFlowTuples(item) for ip, port in ip_port_pairs: self.__logger.info("[TCPRequestHandler][handle]Disconnecting {} and {}".format(ip, port)) # Push circuits # locationA = (item.linkA.split(",")[1]).strip() # locationB = (item.linkB.split(",")[1]).strip() capacity = item.capacityPerStrand # with Timer() as tCreation: # val = tCreation.printTime("CircuitCreation", tCreation, CONTEXT['meas_format'], CONTEXT['meas_to_file']) # overheadList.append(val) self.__logger.info("Circuit pulled from networks by vFiber from client: {0}".format(item.clientName)) val = tCircuitConfigDestruction.printTime("TotalGenerationAndDestruction", tCircuitConfigDestruction, CONTEXT['meas_format'], CONTEXT['meas_to_file']) self.__logger.debug("[TCPRequestHandler][handle]Elapsed Time {}".format(val)) overheadList.append(val) else: raise ValueError('Wrong configuration parameter in TEST_PARAMS') elif (request.name == "MONITOR" and request.code == 102): self.__logger.info("[TCPRequestHandler][handle]Request from Monitor received.") (ip, port) = request.content self.__logger.debug("[TCPRequestHandler][handle]Broken Link: {}".format(ip, port)) try: (x, y, port_pair) = self.__sellerObj.find_edge_from_ip_port_pair((ip, port)) harzardous_path = self.__sellerObj.update_disconnected_strand(x, y, port_pair) new_path = self.__sellerObj.aquire_strand(x, y, 0) self.__logger.info("[TCPRequestHandler][handle]Cleaning up bad path.") extinguish_path(harzardous_path) self.__logger.info("[TCPRequestHandler][handle]Allocating new path.") light_path(new_path) except TypeError: pass elif (request.name == "SDX" and request.code == 001): self.__logger.info("Request from SDX received.") else: self.__dbConnection.close() raise ValueError('Bad request name and code. Either should be from SDX or from Buyer.') val = tTotalProcessing.printTime("ProcessClientRequest", tTotalProcessing, CONTEXT['meas_format'], CONTEXT['meas_to_file']) self.__logger.debug("[TCPRequestHandler][handle]Elapsed Time {}".format(val)) overheadList.append(val) strVal = "".join(overheadList) self.__logger.debug("[TCPRequestHandler][handle] Overhead list :\n {}".format(strVal)) # a = StringIO(strVal) # plotTimeline(a, CONTEXT['meas_to_location']+"overhead.eps") with open(CONTEXT['meas_to_location']+"overhead.txt", "a") as file: file.write(strVal) file.close() except Exception, e: self.__logger.error("Exception upon message reception: %s", str(e)) exc_type, exc_value, exc_traceback = exc_info() print_exception(exc_type, exc_value, exc_traceback) finally:
class TCPClient(threading.Thread): ''' JSON/TCP client thread ''' def __init__(self): ''' Class constructor ''' Thread.__init__(self) # Remote service bindings self.__serverhost = SERVER_BINDING['address'] self.__serverport = int(SERVER_BINDING['port']) # Buffer settings self.__bufferdim = int(CONTEXT['client_socket_buffer']) # Compression helper self.__compression = Utility() self.__resourcepath = TEST_PARAMS['path'] self.__buyerFile = TEST_PARAMS['buyer_file_name'] self.__client_request_type = TEST_PARAMS['client_request_type'] self.__client_request_code = TEST_PARAMS['client_request_code'] self.__logger = get_logger("TCPClient") def run(self): ''' Thread handler ''' try: # Request creation resource = [] resource.append(self.__resourcepath) resource.append(self.__buyerFile) self.__logger.debug("[TCPClient][run]Resource read: {}\n".format(resource)) # Test if compressed content is needed bidList = [] zippedfile = None testfile = None if (self.__compression): zippedfile = gzip.open(''.join(resource)+".gz", "r+") bidList = zippedfile.readlines() self.__logger.info("[TCPClient][run](Compressed) Content size {0}".format(sys.getsizeof(bidList))) else: testfile = open(''.join(resource), 'r') bidList = testfile.readlines() self.__logger.info("[TCPClient][run](Uncompressed) Content size {0}".format(sys.getsizeof(bidList))) data = Request(self.__client_request_type, self.__client_request_code, bidList) # Client socket binding sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect((self.__serverhost, self.__serverport)) # Sending JSON data over the socket sock.send(data.to_json()) self.__logger.info("[TCPClient][run]Request sent...") start = time.time() response = self.__receive_data(sock) end = time.time() self.__logger.debug("[TCPClient][run]Reception::Time Elapsed: {0}".format(end - start)) self.__logger.debug("[TCPClient][run](Compressed) Dimension:: {0}".format(sys.getsizeof(response))) # Treating compressed data result = self.__compression.decompress(response) data = Data(False, [], 0) data.from_json(result) if (int(data.nrbytes) == sys.getsizeof(data.vector)): self.__logger.info("[TCPClient][run]Integrity is OK") self.__logger.info("[TCPClient][run]Allocations") for allocation in data.vector: self.__logger.info(allocation) except Exception, e: self.__logger.error("Error::NET::sending exception {0}".format(e)) finally: