def send_request_message(self, request): # for deep copy of Request object obj = msgpack.unpackb(msgpack.packb(request.packed_object())) rcv_request = Request.create_from_packed(obj) response = self.dispatcher.dispatch_request(rcv_request) # for deep copy of Response object obj = msgpack.unpackb(msgpack.packb(response.packed_object())) return Response.create_from_packed(obj)
def __redisSubscriberRunnable(self): for mesg in self.__redisSubscriber.listen(): if mesg['type'] != 'message': continue try: bio = BytesIO() bio.write(mesg['data']) bio.seek(0) upk = msgpack.Unpacker(bio) tp = upk.unpack() sno = upk.unpack() srcid = upk.unpack() if tp == self.TYPE_REQUEST: request = Request.create_from_packed(upk.unpack()) def request_runnable(request, sno, srcid): response = self.dispatch_request(request) pk = msgpack.Packer() resb = bytearray() resb.extend(pk.pack(self.TYPE_RESPONSE)) resb.extend(pk.pack(sno)) resb.extend(pk.pack(request.object_id)) resb.extend(pk.pack(response.packed_object())) self.__pubsqueue.put( MessageDispatcher.PublishData(None, self.TYPE_RESPONSE, sno, srcid, resb)) self.thread_pool.submit(request_runnable, request, sno, srcid) elif tp == self.TYPE_RESPONSE: trans = self.__clients[srcid] response = Response.create_from_packed(upk.unpack()) trans.signalResponse(sno, response) elif tp == self.TYPE_EVENT: event = Event.create_from_packed(upk.unpack()) def event_runnable(event): self.dispatch_event(event) self.thread_pool.submit(event_runnable, event) except: logging.error(traceback.format_exc()) pass
def __redisSubscriberRunnable(self): for mesg in self.__redisSubscriber.listen(): if mesg['type'] != 'message': continue try: bio = BytesIO() bio.write(mesg['data']) bio.seek(0) upk = msgpack.Unpacker(bio) tp = upk.unpack() sno = upk.unpack() srcid = upk.unpack() if tp == self.TYPE_REQUEST: request = Request.create_from_packed(upk.unpack()) def request_runnable(request, sno, srcid): response = self.dispatch_request(request) pk = msgpack.Packer() resb = bytearray() resb.extend(pk.pack(self.TYPE_RESPONSE)) resb.extend(pk.pack(sno)) resb.extend(pk.pack(request.object_id)) resb.extend(pk.pack(response.packed_object())) self.__pubsqueue.put( MessageDispatcher.PublishData( None, self.TYPE_RESPONSE, sno, srcid, resb)) self.thread_pool.submit(request_runnable, request, sno, srcid) elif tp == self.TYPE_RESPONSE: trans = self.__clients[srcid] response = Response.create_from_packed(upk.unpack()) trans.signalResponse(sno, response) elif tp == self.TYPE_EVENT: event = Event.create_from_packed(upk.unpack()) def event_runnable(event): self.dispatch_event(event) self.thread_pool.submit(event_runnable, event) except: logging.error(traceback.format_exc()) pass
class ResponseTest(unittest.TestCase): Type = "BasicFlow" Version = "v01" Flow_id = "Id01" Owner = "Owner" Enabled = True Priority = "65535" Status = "none" Attributes = { "bandwidth": "10Mbps", "req_bandwidth": "11Mbps", "latency": "10msec", "req_latency": "11msec" } Publisher_id = 'Id1' Event_type = 'flow' value = { "type": Type, "version": Version, "flow_id": Flow_id, "owner": Owner, "enabled": Enabled, "priority": Priority, "status": Status, "attributes": Attributes } Packed = [Response.StatusCode.OK, value] flow_target = Flow(Type, Version, Flow_id, Owner, Enabled, Priority, Status, Attributes) flow_target_packed_object = flow_target.packed_object() def setUp(self): self.target = Response(Response.StatusCode.OK, self.flow_target) def tearDown(self): self.target = None def test_constructor_hasattr_True(self): self.assertEqual(self.target.status_code, Response.StatusCode.OK) self.assertEqual(self.target.body, self.value) def test_constructor_hasattr_Falseself(self): self.target2 = Response(Response.StatusCode.CREATED, self.value) self.assertEqual(self.target2.status_code, Response.StatusCode.CREATED) self.assertEqual(self.target2.body, self.value) def test_is_error_method_GET_status_code_OK(self): self.result = Response(Response.StatusCode.OK, self.flow_target) self.assertEqual(self.result.is_error("GET"), False) def test_is_error_method_GET_status_code_NotOK(self): self.result = Response(Response.StatusCode.CREATED, self.flow_target) self.assertEqual(self.result.is_error("GET"), True) def test_is_error_method_DELETE_status_code_OK(self): self.result = Response(Response.StatusCode.OK, self.flow_target) self.assertEqual(self.result.is_error("DELETE"), False) def test_is_error_method_DELETE_status_code_NotOK(self): self.result = Response(Response.StatusCode.CREATED, self.flow_target) self.assertEqual(self.result.is_error("DELETE"), True) def test_is_error_method_PUT_status_code_OK(self): self.result = Response(Response.StatusCode.OK, self.flow_target) self.assertEqual(self.result.is_error("PUT"), False) def test_is_error_method_PUT_status_code_CREATED(self): self.result = Response(Response.StatusCode.CREATED, self.flow_target) self.assertEqual(self.result.is_error("PUT"), False) def test_is_error_method_PUT_status_code_NotOKandCREATED(self): self.result = Response(Response.StatusCode.ACCEPTED, self.flow_target) self.assertEqual(self.result.is_error("PUT"), True) def test_is_error_method_other_status_code_OK(self): self.result = Response(Response.StatusCode.OK, self.flow_target) self.assertEqual(self.result.is_error("POST"), False) def test_is_error_method_other_status_code_CREATED(self): self.result = Response(Response.StatusCode.CREATED, self.flow_target) self.assertEqual(self.result.is_error("POST"), False) def test_is_error_method_other_status_code_NotOKandCREATED(self): self.result = Response(Response.StatusCode.ACCEPTED, self.flow_target) self.assertEqual(self.result.is_error("POST"), True) def test_create_from_packed(self): self.result = self.target.create_from_packed(self.Packed) self.assertEqual(self.result.status_code, Response.StatusCode.OK) self.assertEqual(self.result.body, self.value) def test_packed_object(self): self.result = self.target.packed_object() self.assertEqual(self.result, (Response.StatusCode.OK, self.value)) self.assertEqual(len(self.result), 2)
class ResponseTest(unittest.TestCase): Type = "BasicFlow" Version = "v01" Flow_id = "Id01" Owner = "Owner" Enabled = True Priority = "65535" Status = "none" Attributes = {"bandwidth": "10Mbps", "req_bandwidth": "11Mbps", "latency": "10msec", "req_latency": "11msec"} Publisher_id = 'Id1' Event_type = 'flow' value = {"type": Type, "version": Version, "flow_id": Flow_id, "owner": Owner, "enabled": Enabled, "priority": Priority, "status": Status, "attributes": Attributes} Packed = [Response.StatusCode.OK, value] flow_target = Flow(Type, Version, Flow_id, Owner, Enabled, Priority, Status, Attributes) flow_target_packed_object = flow_target.packed_object() def setUp(self): self.target = Response(Response.StatusCode.OK, self.flow_target) def tearDown(self): self.target = None def test_constructor_hasattr_True(self): self.assertEqual(self.target.status_code, Response.StatusCode.OK) self.assertEqual(self.target.body, self.value) def test_constructor_hasattr_Falseself(self): self.target2 = Response(Response.StatusCode.CREATED, self.value) self.assertEqual(self.target2.status_code, Response.StatusCode.CREATED) self.assertEqual(self.target2.body, self.value) def test_is_error_method_GET_status_code_OK(self): self.result = Response(Response.StatusCode.OK, self.flow_target) self.assertEqual(self.result.is_error("GET"), False) def test_is_error_method_GET_status_code_NotOK(self): self.result = Response(Response.StatusCode.CREATED, self.flow_target) self.assertEqual(self.result.is_error("GET"), True) def test_is_error_method_DELETE_status_code_OK(self): self.result = Response(Response.StatusCode.OK, self.flow_target) self.assertEqual(self.result.is_error("DELETE"), False) def test_is_error_method_DELETE_status_code_NotOK(self): self.result = Response(Response.StatusCode.CREATED, self.flow_target) self.assertEqual(self.result.is_error("DELETE"), True) def test_is_error_method_PUT_status_code_OK(self): self.result = Response(Response.StatusCode.OK, self.flow_target) self.assertEqual(self.result.is_error("PUT"), False) def test_is_error_method_PUT_status_code_CREATED(self): self.result = Response(Response.StatusCode.CREATED, self.flow_target) self.assertEqual(self.result.is_error("PUT"), False) def test_is_error_method_PUT_status_code_NotOKandCREATED(self): self.result = Response(Response.StatusCode.ACCEPTED, self.flow_target) self.assertEqual(self.result.is_error("PUT"), True) def test_is_error_method_other_status_code_OK(self): self.result = Response(Response.StatusCode.OK, self.flow_target) self.assertEqual(self.result.is_error("POST"), False) def test_is_error_method_other_status_code_CREATED(self): self.result = Response(Response.StatusCode.CREATED, self.flow_target) self.assertEqual(self.result.is_error("POST"), False) def test_is_error_method_other_status_code_NotOKandCREATED(self): self.result = Response(Response.StatusCode.ACCEPTED, self.flow_target) self.assertEqual(self.result.is_error("POST"), True) def test_create_from_packed(self): self.result = self.target.create_from_packed(self.Packed) self.assertEqual(self.result.status_code, Response.StatusCode.OK) self.assertEqual(self.result.body, self.value) def test_packed_object(self): self.result = self.target.packed_object() self.assertEqual(self.result, (Response.StatusCode.OK, self.value)) self.assertEqual(len(self.result), 2)