def _test_with_client_delayed(self, message_list): # pragma: no cover client = HelperClient(self.server_address) start_etag = None for message, expected in message_list: if message is not None: received_message = client.send_request(message) time.sleep(5) if received_message.etag is not None: if start_etag is None: start_etag = received_message.etag if expected is not None: if expected.etag is not None: if expected.etag == 1: self.assertEqual(received_message, start_etag) else: self.assertNotEqual(received_message, start_etag) if expected.type is not None: self.assertEqual(received_message.type, expected.type) if expected.mid is not None: self.assertEqual(received_message.mid, expected.mid) self.assertEqual(received_message.code, expected.code) if expected.source is not None: self.assertEqual(received_message.source, self.server_address) client.stop()
def _test_with_client_observe(self, message_list): # pragma: no cover client = HelperClient(self.server_address) for message, expected in message_list: if message is not None: client.send_request(message, self.client_callback) if expected is not None: received_message = self.queue.get() if expected.type is not None: self.assertEqual(received_message.type, expected.type) if expected.mid is not None: self.assertEqual(received_message.mid, expected.mid) self.assertEqual(received_message.code, expected.code) if expected.source is not None: self.assertEqual(received_message.source, self.server_address) if expected.token is not None: self.assertEqual(received_message.token, expected.token) if expected.payload is not None: self.assertEqual(received_message.payload, expected.payload) if expected.options: self.assertEqual(len(received_message.options), len(expected.options)) for o in expected.options: assert isinstance(o, Option) option_value = getattr(expected, o.name.lower().replace("-", "_")) option_value_rec = getattr(received_message, o.name.lower().replace("-", "_")) self.assertEqual(option_value, option_value_rec) client.stop()
def _test_with_client_delayed(self, message_list): # pragma: no cover client = HelperClient(self.server_address) for message, expected in message_list: if message is not None: received_message = client.send_request(message) time.sleep(5) if expected is not None: if expected.etag is not None: self.assertEqual(received_message.etag, expected.etag) if expected.type is not None: self.assertEqual(received_message.type, expected.type) if expected.mid is not None: self.assertEqual(received_message.mid, expected.mid) self.assertEqual(received_message.code, expected.code) if expected.source is not None: self.assertEqual(received_message.source, self.server_address) if expected.token is not None: self.assertEqual(received_message.token, expected.token) if expected.payload is not None: self.assertEqual(received_message.payload, expected.payload) if expected.max_age is not None: if expected.max_age != 60: self.assertNotEqual(received_message.max_age, 60) else: self.assertEqual(received_message.max_age, expected.max_age) if expected.options: self.assertEqual(len(received_message.options), len(expected.options)) for o in expected.options: assert isinstance(o, Option) if o.name != defines.OptionRegistry.MAX_AGE.name and o.name != defines.OptionRegistry.ETAG.name: option_value = getattr(expected, o.name.lower().replace("-", "_")) option_value_rec = getattr(received_message, o.name.lower().replace("-", "_")) self.assertEqual(option_value, option_value_rec) client.stop()
def _test_with_client(self, message_list): # pragma: no cover client = HelperClient(self.server_address) for message, expected in message_list: if message is not None: received_message = client.send_request(message) if expected is not None: # print(received_message.payload) # print(expected.payload) if expected.type is not None: self.assertEqual(received_message.type, expected.type) if expected.mid is not None: self.assertEqual(received_message.mid, expected.mid) self.assertEqual(received_message.code, expected.code) if expected.source is not None: self.assertEqual(received_message.source, self.server_address) if expected.token is not None: self.assertEqual(received_message.token, expected.token) if expected.payload is not None: self.assertEqual(received_message.payload, expected.payload) if expected.options is not None: self.assertEqual(received_message.options, expected.options) for o in expected.options: option_value = getattr( expected, o.name.lower().replace("-", "_")) option_value_rec = getattr( received_message, o.name.lower().replace("-", "_")) self.assertEqual(option_value, option_value_rec) client.stop()
def _forward_request(transaction, destination, path): """ Forward requests. :type transaction: Transaction :param transaction: the transaction that owns the request :param destination: the destination of the request (IP, port) :param path: the path of the request. :rtype : Transaction :return: the edited transaction """ client = HelperClient(destination) request = Request() request.options = copy.deepcopy(transaction.request.options) del request.block2 del request.block1 del request.uri_path del request.proxy_uri del request.proxy_schema # TODO handle observing del request.observe # request.observe = transaction.request.observe request.uri_path = path request.destination = destination request.payload = transaction.request.payload request.code = transaction.request.code response = client.send_request(request) client.stop() transaction.response.payload = response.payload transaction.response.code = response.code transaction.response.options = response.options return transaction
def discover_remote(self, destination, name): assert (isinstance(destination, str)) split = destination.split(":", 1) host = split[0] port = int(split[1]) server = (host, port) client = HelperClient(server) response = client.discover() client.stop() self.discover_remote_results(response, name)
def discover_remote(self, destination, name): assert isinstance(destination, str) split = destination.split(":", 1) host = split[0] port = int(split[1]) server = (host, port) client = HelperClient(server) response = client.discover() client.stop() self.discover_remote_results(response, name)
def _handle_request(self, transaction, new_resource): """ Forward requests. Used by reverse proxies to also create new virtual resources on the proxy in case of created resources :type new_resource: bool :type transaction: Transaction :param transaction: the transaction that owns the request :rtype : Transaction :param new_resource: if the request will generate a new resource :return: the edited transaction """ client = HelperClient(transaction.resource.remote_server) request = Request() request.options = copy.deepcopy(transaction.request.options) del request.block2 del request.block1 del request.uri_path del request.proxy_uri del request.proxy_schema # TODO handle observing del request.observe # request.observe = transaction.request.observe request.uri_path = "/".join( transaction.request.uri_path.split("/")[1:]) request.destination = transaction.resource.remote_server request.payload = transaction.request.payload request.code = transaction.request.code logger.info("forward_request - " + str(request)) response = client.send_request(request) client.stop() logger.info("forward_response - " + str(response)) transaction.response.payload = response.payload transaction.response.code = response.code transaction.response.options = response.options if response.code == defines.Codes.CREATED.number: lp = transaction.response.location_path del transaction.response.location_path transaction.response.location_path = transaction.request.uri_path.split( "/")[0] + "/" + lp # TODO handle observing if new_resource: resource = RemoteResource('server', transaction.resource.remote_server, lp, coap_server=self, visible=True, observable=False, allow_children=True) self._server.add_resource(transaction.response.location_path, resource) if response.code == defines.Codes.DELETED.number: del self._server.root["/" + transaction.request.uri_path] return transaction
def _test_with_client_observe(self, message_list, callback): # pragma: no cover client = HelperClient(self.server_address) token = None last_mid = 0 for message, expected in message_list: if message is not None: token = message.token client.send_request(message, callback) received_message = self.queue.get() if expected is not None: last_mid = expected.mid if expected.type is not None: self.assertEqual(received_message.type, expected.type) if expected.mid is not None: self.assertEqual(received_message.mid, expected.mid) self.assertEqual(received_message.code, expected.code) if expected.source is not None: self.assertEqual(received_message.source, self.server_address) if expected.token is not None: self.assertEqual(received_message.token, expected.token) if expected.payload is not None: self.assertEqual(received_message.payload, expected.payload) if expected.options is not None: self.assertEqual(received_message.options, expected.options) message = Message() message.type = defines.Types["RST"] message.token = token message._mid = last_mid message.destination = self.server_address client.send_empty(message) client.stop()
def _handle_request(self, transaction, new_resource): """ Forward requests. Used by reverse proxies to also create new virtual resources on the proxy in case of created resources :type new_resource: bool :type transaction: Transaction :param transaction: the transaction that owns the request :rtype : Transaction :param new_resource: if the request will generate a new resource :return: the edited transaction """ client = HelperClient(transaction.resource.remote_server) request = Request() request.options = copy.deepcopy(transaction.request.options) del request.block2 del request.block1 del request.uri_path del request.proxy_uri del request.proxy_schema # TODO handle observing del request.observe # request.observe = transaction.request.observe request.uri_path = "/".join(transaction.request.uri_path.split("/")[1:]) request.destination = transaction.resource.remote_server request.payload = transaction.request.payload request.code = transaction.request.code response = client.send_request(request) client.stop() transaction.response.payload = response.payload transaction.response.code = response.code transaction.response.options = response.options if response.code == defines.Codes.CREATED.number: lp = transaction.response.location_path del transaction.response.location_path transaction.response.location_path = transaction.request.uri_path.split("/")[0] + "/" + lp # TODO handle observing if new_resource: resource = RemoteResource('server', transaction.resource.remote_server, lp, coap_server=self, visible=True, observable=False, allow_children=True) self._server.add_resource(transaction.response.location_path, resource) if response.code == defines.Codes.DELETED.number: del self._server.root["/" + transaction.request.uri_path] return transaction
def _test_with_client_observe(self, message_list, callback): client = HelperClient(self.server_address) token = None last_mid = 0 for message, expected in message_list: if message is not None: token = message.token client.send_request(message, callback) received_message = self.queue.get() if expected is not None: last_mid = expected.mid if expected.type is not None: self.assertEqual(received_message.type, expected.type) if expected.mid is not None: self.assertEqual(received_message.mid, expected.mid) self.assertEqual(received_message.code, expected.code) if expected.source is not None: self.assertEqual(received_message.source, self.server_address) if expected.token is not None: self.assertEqual(received_message.token, expected.token) if expected.payload is not None: self.assertEqual(received_message.payload, expected.payload) if expected.options is not None: self.assertEqual(received_message.options, expected.options) message = Message() message.type = defines.Types["RST"] message.token = token message._mid = last_mid message.destination = self.server_address client.send_empty(message) client.stop()
def _handle_request(self, transaction, new_resource): client = HelperClient(transaction.resource.remote_server) request = Request() request.options = copy.deepcopy(transaction.request.options) del request.block2 del request.block1 del request.uri_path del request.proxy_uri del request.proxy_schema # TODO handle observing del request.observe # request.observe = transaction.request.observe request.uri_path = "/".join( transaction.request.uri_path.split("/")[1:]) request.destination = transaction.resource.remote_server request.payload = transaction.request.payload request.code = transaction.request.code response = client.send_request(request) client.stop() transaction.response.payload = response.payload transaction.response.code = response.code transaction.response.options = response.options if response.code == defines.Codes.CREATED.number: lp = transaction.response.location_path del transaction.response.location_path transaction.response.location_path = transaction.request.uri_path.split( "/")[0] + "/" + lp # TODO handle observing if new_resource: resource = RemoteResource('server', transaction.resource.remote_server, lp, coap_server=self, visible=True, observable=False, allow_children=True) self._server.add_resource(transaction.response.location_path, resource) if response.code == defines.Codes.DELETED.number: del self._server.root["/" + transaction.request.uri_path] return transaction
def _test_with_client(self, message_list): client = HelperClient(self.server_address) for message, expected in message_list: if message is not None: received_message = client.send_request(message) if expected is not None: if expected.type is not None: self.assertEqual(received_message.type, expected.type) if expected.mid is not None: self.assertEqual(received_message.mid, expected.mid) self.assertEqual(received_message.code, expected.code) if expected.source is not None: self.assertEqual(received_message.source, self.server_address) if expected.token is not None: self.assertEqual(received_message.token, expected.token) if expected.payload is not None: self.assertEqual(received_message.payload, expected.payload) if expected.options is not None: self.assertEqual(received_message.options, expected.options) client.stop()
def _test_with_client_observe(self, message_list): # pragma: no cover client = HelperClient(self.server_address) for message, expected in message_list: if message is not None: client.send_request(message, self.client_callback) if expected is not None: received_message = self.queue.get() if expected.type is not None: self.assertEqual(received_message.type, expected.type) if expected.mid is not None: self.assertEqual(received_message.mid, expected.mid) self.assertEqual(received_message.code, expected.code) if expected.source is not None: self.assertEqual(received_message.source, self.server_address) if expected.token is not None: self.assertEqual(received_message.token, expected.token) if expected.payload is not None: self.assertEqual(received_message.payload, expected.payload) if expected.options: self.assertEqual(len(received_message.options), len(expected.options)) for o in expected.options: assert isinstance(o, Option) option_value = getattr( expected, o.name.lower().replace("-", "_")) option_value_rec = getattr( received_message, o.name.lower().replace("-", "_")) self.assertEqual(option_value, option_value_rec) client.stop()
def _forward_request(transaction, destination, path): client = HelperClient(destination) request = Request() request.options = copy.deepcopy(transaction.request.options) del request.block2 del request.block1 del request.uri_path del request.proxy_uri del request.proxy_schema # TODO handle observing del request.observe # request.observe = transaction.request.observe request.uri_path = path request.destination = destination request.payload = transaction.request.payload request.code = transaction.request.code response = client.send_request(request) client.stop() transaction.response.payload = response.payload transaction.response.code = response.code transaction.response.options = response.options return transaction
def _forward_request(transaction, destination, path): """ Forward requests. :type transaction: Transaction :param transaction: the transaction that owns the request :param destination: the destination of the request (IP, port) :param path: the path of the request. :rtype : Transaction :return: the edited transaction """ client = HelperClient(destination) request = Request() request.options = copy.deepcopy(transaction.request.options) del request.block2 del request.block1 del request.uri_path del request.proxy_uri del request.proxy_schema # TODO handle observing del request.observe # request.observe = transaction.request.observe request.uri_path = path request.destination = destination request.payload = transaction.request.payload request.code = transaction.request.code response = client.send_request(request) client.stop() if response is not None: transaction.response.payload = response.payload transaction.response.code = response.code transaction.response.options = response.options else: transaction.response.code = defines.Codes.SERVICE_UNAVAILABLE.number return transaction
def _handle_request(self, transaction, new_resource): client = HelperClient(transaction.resource.remote_server) request = Request() request.options = copy.deepcopy(transaction.request.options) del request.block2 del request.block1 del request.uri_path del request.proxy_uri del request.proxy_schema # TODO handle observing del request.observe # request.observe = transaction.request.observe request.uri_path = "/".join(transaction.request.uri_path.split("/")[1:]) request.destination = transaction.resource.remote_server request.payload = transaction.request.payload request.code = transaction.request.code response = client.send_request(request) client.stop() transaction.response.payload = response.payload transaction.response.code = response.code transaction.response.options = response.options if response.code == defines.Codes.CREATED.number: lp = transaction.response.location_path del transaction.response.location_path transaction.response.location_path = transaction.request.uri_path.split("/")[0] + "/" + lp # TODO handle observing if new_resource: resource = RemoteResource('server', transaction.resource.remote_server, lp, coap_server=self, visible=True, observable=False, allow_children=True) self._server.add_resource(transaction.response.location_path, resource) if response.code == defines.Codes.DELETED.number: del self._server.root["/" + transaction.request.uri_path] return transaction
def callback(): try: index = listbox.curselection()[0] # get the line's text server_text = str(listbox.get(index)) if (server_text == ""): print "ERROR : Empty selection" return except: print "ERROR !" return AttributeError proxy_texter = str(label_proxy.cget("text")) text_test = path_text.get("1.0", END) path_texter = str(text_test) print "Selected : " + str(server_text) print "chosen path : " + str(path_texter) server_text = "198.199.92.83" proxy_texter = "104.236.174.175" # Setting up the application setup_time_begin = time.time() server_address = (str(proxy_texter), 5684) current_mid = random.randint(1, 1000) client = HelperClient(server_address) setup_time_end = time.time() print "Starting the first request" print "\nRequesting for Resource 1" first_request_begin = time.time() # Starting first request path = "/basic1" req = Request() req._usecache = use_cache req.code = defines.Codes.GET.number req.uri_path = path req.type = defines.Types["CON"] req._mid = current_mid req.destination = server_address req.proxy_uri = "coap://" + str(server_text) + ":5683/basic1" received_message = client.send_request(req) first_request_end = time.time() time.sleep(5) print "Starting the second request" print "\nRequesting for Resource 2" second_request_begin = time.time() # Starting second request current_mid = current_mid + 1 req2 = Request() req2._usecache = use_cache req2.code = defines.Codes.GET.number path = "/basic2" req2.uri_path = path req2.type = defines.Types["CON"] req2._mid = current_mid req2.destination = server_address req2.proxy_uri = "coap://" + server_text + ":5683/basic2" received_message = client.send_request(req2) second_request_end = time.time() time.sleep(5) print "Starting the third request" print "\nRequesting for Resource 3" third_request_begin = time.time() # Starting third request current_mid = current_mid + 1 req3 = Request() req3._usecache = use_cache req3.code = defines.Codes.GET.number path = "/basic3" req3.uri_path = path req3.type = defines.Types["CON"] req3._mid = current_mid req3.destination = server_address req3.proxy_uri = "coap://" + server_text + ":5683/basic3" received_message = client.send_request(req3) third_request_end = time.time() time.sleep(5) print "Starting the fourth request" print "\nRequesting for Resource 1 again" fourth_request_begin = time.time() # Starting first request path = "/basic1" current_mid = current_mid + 1 req4 = Request() req4._usecache = use_cache req4.code = defines.Codes.GET.number req4.uri_path = path req4.type = defines.Types["CON"] req4._mid = current_mid req4.destination = server_address req4.proxy_uri = "coap://" + server_text + ":5683/basic1" received_message = client.send_request(req4) fourth_request_end = time.time() time.sleep(5) print "Starting the fifth request" print "\nRequesting for Resource 2 again" fifth_request_begin = time.time() # Starting second request current_mid = current_mid + 1 req5 = Request() req5._usecache = use_cache req5.code = defines.Codes.GET.number path = "/basic2" req5.uri_path = path req5.type = defines.Types["CON"] req5._mid = current_mid req5.destination = server_address req5.proxy_uri = "coap://" + server_text + ":5683/basic2" received_message = client.send_request(req5) fifth_request_end = time.time() time.sleep(5) print "Starting the sixth request" print "\nRequesting for Resource 3 again" # Starting third request sixth_request_begin = time.time() current_mid = current_mid + 1 req6 = Request() req6._usecache = use_cache req6.code = defines.Codes.GET.number path = "/basic3" req6.uri_path = path req6.type = defines.Types["CON"] req6._mid = current_mid req6.destination = server_address req6.proxy_uri = "coap://" + server_text + ":5683/basic3" received_message = client.send_request(req6) sixth_request_end = time.time() time.sleep(5) print "Starting the seventh request" print "\nRequesting for Resource 4" seventh_request_begin = time.time() # Starting first request path = "/basic4" current_mid = current_mid + 1 req7 = Request() req7._usecache = use_cache req7.code = defines.Codes.GET.number req7.uri_path = path req7.type = defines.Types["CON"] req7._mid = current_mid req7.destination = server_address req7.proxy_uri = "coap://" + server_text + ":5683/basic4" received_message = client.send_request(req7) seventh_request_end = time.time() time.sleep(5) print "Starting the eighth request" print "\nRequesting for Resource 5" eighth_request_begin = time.time() # Starting second request current_mid = current_mid + 1 req8 = Request() req8._usecache = use_cache req8.code = defines.Codes.GET.number path = "/basic5" req8.uri_path = path req8.type = defines.Types["CON"] req8._mid = current_mid req8.destination = server_address req8.proxy_uri = "coap://" + server_text + ":5683/basic5" received_message = client.send_request(req8) eighth_request_end = time.time() time.sleep(5) print "Starting the ninth request" print "\nRequesting for Resource 6" ninth_request_begin = time.time() # Starting third request current_mid = current_mid + 1 req9 = Request() req9._usecache = use_cache req9.code = defines.Codes.GET.number path = "/basic6" req9.uri_path = path req9.type = defines.Types["CON"] req9._mid = current_mid req9.destination = server_address req9.proxy_uri = "coap://" + server_text + ":5683/basic6" received_message = client.send_request(req9) ninth_request_end = time.time() time.sleep(5) print "Starting the tenth request" print "\nRequesting for Resource 4 again" tenth_request_begin = time.time() # Starting third request current_mid = current_mid + 1 req10 = Request() req10._usecache = use_cache req10.code = defines.Codes.GET.number path = "/basic4" req10.uri_path = path req10.type = defines.Types["CON"] req10._mid = current_mid req10.destination = server_address req10.proxy_uri = "coap://" + server_text + ":5683/basic4" received_message = client.send_request(req10) tenth_request_end = time.time() time.sleep(5) print "Starting the eleven request" print "\nRequesting for Resource 7" eleventh_request_begin = time.time() # Starting third request current_mid = current_mid + 1 req11 = Request() req11._usecache = use_cache req11.code = defines.Codes.GET.number path = "/basic7" req11.uri_path = path req11.type = defines.Types["CON"] req11._mid = current_mid req11.destination = server_address req11.proxy_uri = "coap://" + server_text + ":5683/basic7" received_message = client.send_request(req11) eleventh_request_end = time.time() print "Tearing down application" # Tearing down application print "DONE !" print "-------------------" print "Printing Analytics" print "--------------------" print "Build Time : " + str(setup_time_end - setup_time_begin) print "First request : " + str(first_request_end - first_request_begin) print "Second request : " + str(second_request_end - second_request_begin) print "Third request : " + str(third_request_end - third_request_begin) print "Fourth request : " + str(fourth_request_end - fourth_request_begin) print "Fifth request : " + str(fifth_request_end - fifth_request_begin) print "Sixth request : " + str(sixth_request_end - sixth_request_begin) print "Seventh request : " + str(seventh_request_end - seventh_request_begin) print "Eighth request : " + str(eighth_request_end - eighth_request_begin) print "Ninth request : " + str(ninth_request_end - ninth_request_begin) print "Tenth request : " + str(tenth_request_end - tenth_request_begin) print "Eleventh request : " + str(eleventh_request_end - eleventh_request_begin) print "--------------------------" print "TOTAL TIME TAKEN : " + str( (first_request_end - first_request_begin) + (second_request_end - second_request_begin) + (third_request_end - third_request_begin) + (fourth_request_end - fourth_request_begin) + (fifth_request_end - fifth_request_begin) + (sixth_request_end - sixth_request_begin) + (seventh_request_end - seventh_request_begin) + (eighth_request_end - eighth_request_begin) + (ninth_request_end - ninth_request_begin) + (tenth_request_end - tenth_request_begin) + (eleventh_request_end - eleventh_request_begin)) print "--------------------------"
def callback(): try: index = listbox.curselection()[0] # get the line's text server_text = str(listbox.get(index)) if (server_text == ""): print "ERROR : Empty selection" return except: print "ERROR !" return AttributeError proxy_texter = str(label_proxy.cget("text")) text_test = path_text.get("1.0", END) path_texter = str(text_test) print "Selected : " + str(server_text) print "chosen path : " + str(path_texter) server_text = "198.199.92.83" proxy_texter = "104.236.174.175" # Setting up the application setup_time_begin = time.time() server_address = (str(proxy_texter), 5684) current_mid = random.randint(1, 1000) client = HelperClient(server_address) setup_time_end = time.time() print "Starting the first request" print "\nRequesting for Resource 1" first_request_begin = time.time() # Starting first request path = "/basic1" req = Request() req._usecache = use_cache req.code = defines.Codes.GET.number req.uri_path = path req.type = defines.Types["CON"] req._mid = current_mid req.destination = server_address req.proxy_uri = "coap://" + str(server_text) + ":5683/basic1" received_message = client.send_request(req) first_request_end = time.time() time.sleep(21) print "Starting the second request" print "\nRequesting for Resource 1 again" second_request_begin = time.time() # Starting second request current_mid = current_mid + 1 req2 = Request() req2._usecache = use_cache req2.code = defines.Codes.GET.number path = "/basic1" req2.uri_path = path req2.type = defines.Types["CON"] req2._mid = current_mid req2.destination = server_address req2.proxy_uri = "coap://" + server_text + ":5683/basic1" received_message = client.send_request(req2) second_request_end = time.time() time.sleep(5) print "Tearing down application" # Tearing down application print "DONE !" print "-------------------" print "Printing Analytics" print "--------------------" print "Build Time : " + str(setup_time_end - setup_time_begin) print "First request : " + str(first_request_end - first_request_begin) print "Second request : " + str(second_request_end - second_request_begin) print "--------------------------" print "TOTAL TIME TAKEN : " + str( (second_request_end - second_request_begin) + (first_request_end - first_request_begin)) print "--------------------------"