def delete(self, client_callback, *args, **kwargs): if isinstance(args[0], str): path = str(args[0]) req = Request() req.uri_path = path if "Token" in kwargs.keys(): req.token = kwargs.get("Token") del kwargs["Token"] if "MID" in kwargs.keys(): req.mid = kwargs.get("MID") del kwargs["MID"] if "Server" in kwargs.keys(): req.destination = kwargs.get("Server") del kwargs["Server"] else: req = args[0] for key in kwargs: try: o = Option() o.number = defines.inv_options[key] o.value = kwargs[key] req.add_option(o) except KeyError: pass req.code = defines.inv_codes['DELETE'] req.type = defines.inv_types["CON"] self.send_callback(req, self.delete_results, client_callback)
def discover(self, client_callback, *args, **kwargs): req = Request() if "Token" in kwargs.keys(): req.token = kwargs.get("Token") req.code = defines.inv_codes['GET'] req.uri_path = ".well-known/core" req.type = defines.inv_types["CON"] self.send_callback(req, self.discover_results, client_callback)
def post(self, path, payload, callback=None): # pragma: no cover request = Request() request.destination = self.server request.code = defines.Codes.POST.number request.token = generate_random_token(2) request.uri_path = path request.payload = payload if callback is not None: thread = threading.Thread(target=self._thread_body, args=(request, callback)) thread.start() else: self.protocol.send_message(request) response = self.queue.get(block=True) return response
def post(self, path, payload, callback=None, timeout=None): # pragma: no cover request = Request() request.destination = self.server request.code = defines.Codes.POST.number request.token = generate_random_token(2) request.uri_path = path request.payload = payload if callback is not None: thread = threading.Thread(target=self._thread_body, args=(request, callback)) thread.start() else: self.protocol.send_message(request) response = self.queue.get(block=True, timeout=timeout) return response
def delete(self, client_callback, *args, **kwargs): path = args[0] req = Request() if "Token" in kwargs.keys(): req.token = kwargs.get("Token") del kwargs["Token"] for key in kwargs: o = Option() o.number = defines.inv_options[key] o.value = kwargs[key] req.add_option(o) req.code = defines.inv_codes['DELETE'] req.uri_path = path req.type = defines.inv_types["CON"] self.send_callback(req, self.delete_results, client_callback)
def put(self, client_callback, *args, **kwargs): path, payload = args req = Request() if "Token" in kwargs.keys(): req.token = kwargs.get("Token") del kwargs["Token"] for key in kwargs: o = Option() o.number = defines.inv_options[key] o.value = kwargs[key] req.add_option(o) req.code = defines.inv_codes['PUT'] req.uri_path = path req.type = defines.inv_types["CON"] req.payload = payload self.send_callback(req, self.put_results, client_callback)
def test_td_coap_core_10(self): print("TD_COAP_CORE_10") path = "/test" req = Request() req.code = defines.Codes.GET.number req.uri_path = path req.type = defines.Types["CON"] req._mid = self.current_mid req.destination = self.server_address req.token = "ciao" expected = Response() expected.type = defines.Types["ACK"] expected._mid = self.current_mid expected.code = defines.Codes.CONTENT.number expected.token = None expected.payload = "Test Resource" expected.token = "ciao" self.current_mid += 1 self._test_with_client([(req, expected)])
def test_td_coap_core_10(self): print "TD_COAP_CORE_10" path = "/test" req = Request() req.code = defines.inv_codes['GET'] req.uri_path = path req.type = defines.inv_types["CON"] req._mid = self.current_mid req.destination = self.server_address req.token = "ciao" expected = Response() expected.type = defines.inv_types["ACK"] expected._mid = self.current_mid expected.code = defines.responses["CONTENT"] expected.token = None expected.payload = "Test Resource" expected.token = "ciao" self.current_mid += 1 self._test_plugtest(req, expected)
def forward_request(self, request): """ Forward an incoming request to the specified server. :param request: the request to be forwarded :return: None if success, send an error otherwise """ # print "FORWARD REQUEST\n" uri = request.proxy_uri response = Response() response.destination = request.source token = self.generate_token() if uri is None: return self.send_error(request, response, "BAD_REQUEST") host, port, path = self.parse_path(uri) request.uri_path = path self._currentMID += 1 method = defines.codes[request.code] if method == 'GET': function = self.client.protocol.get req = Request() req.destination = (str(host), int(port)) req.mid = self._currentMID % (1 << 16) req.token = str(token) req.uri_path = path req.type = defines.inv_types["CON"] req.code = defines.inv_codes['GET'] args = (req,) kwargs = {} callback = self.result_forward err_callback = self.error elif method == 'POST': function = self.client.protocol.post req = Request() req.destination = (str(host), int(port)) req.mid = self._currentMID % (1 << 16) req.token = str(token) req.uri_path = path req.payload = request.payload req.type = defines.inv_types["CON"] req.code = defines.inv_codes['POST'] args = (req,) kwargs = {} callback = self.result_forward err_callback = self.error elif method == 'PUT': function = self.client.protocol.put req = Request() req.destination = (str(host), int(port)) req.mid = self._currentMID % (1 << 16) req.token = str(token) req.uri_path = path req.payload = request.payload req.type = defines.inv_types["CON"] req.code = defines.inv_codes['PUT'] args = (req,) kwargs = {} callback = self.result_forward err_callback = self.error elif method == 'DELETE': function = self.client.protocol.delete req = Request() req.destination = (str(host), int(port)) req.mid = self._currentMID % (1 << 16) req.token = str(token) req.uri_path = path req.type = defines.inv_types["CON"] req.code = defines.inv_codes['DELETE'] args = (req,) kwargs = {} callback = self.result_forward err_callback = self.error else: return self.send_error(request, response, "BAD_REQUEST") for option in request.options: if option.safe: kwargs[option.name] = option.value self.send_ack([request]) operations = [(function, args, kwargs, (callback, err_callback))] key = hash(str(host) + str(port) + str(token)) self._forward[key] = request key = hash(str(host) + str(port) + str(self._currentMID % (1 << 16))) self._forward_mid[key] = request # print "************" # print str(host), str(port), str(self._currentMID % (1 << 16)) # print req # print "************" key = req.mid self.sent[key] = (req, time.time()) # self.sent[str(self._currentMID % (1 << 16))] = (req, time.time()) self.client.start(operations) # Render_GET # key_timer = hash(str(request.source[0]) + str(request.source[1]) + str(request.mid)) # self.timer[key_timer] = reactor.callLater(defines.SEPARATE_TIMEOUT, self.send_ack, [request]) return None