def test_PresentYourselfResponse(self): senderData = {'type': Charstring().assignValueType(CharstringValue("HelloCritty")), 'nick': Charstring().assignValueType(CharstringValue("Sender"))} receiverData = {'type': Charstring().assignValueType(CharstringValue("HelloCritty")), 'nick': Charstring().assignValueType(CharstringValue("Receiver"))} presentYourselfResponse = PiewikPresentYourselfResponse() presentYourselfResponse.assignValueType({ 'messageName': Charstring().assignValueType(CharstringValue("PresentYourselfResponse")), 'sender': senderData, 'receiver': receiverData }) encoder = ProtobufEncoder() payload = PresentYourselfResponse() encoder.encodePayload(aPayloadContent=payload, aPayloadData=presentYourselfResponse) self.assertEqual(payload.messageName, "PresentYourselfResponse") self.assertEqual(payload.sender.type, "HelloCritty") self.assertEqual(payload.sender.nick, "Sender") self.assertEqual(payload.receiver.type, "HelloCritty") self.assertEqual(payload.receiver.nick, "Receiver")
def test_LoadGraphAndWorkResponse(self): senderData = {'type': Charstring().assignValueType(CharstringValue("HelloCritty")), 'nick': Charstring().assignValueType(CharstringValue("Sender"))} receiverData = {'type': Charstring().assignValueType(CharstringValue("HelloCritty")), 'nick': Charstring().assignValueType(CharstringValue("Receiver"))} graphsData = [ {'graphName': Charstring().assignValueType(CharstringValue("Graph1"))}, {'graphName': Charstring().assignValueType(CharstringValue("Graph2"))} ] worksData =[ {'graphName': Charstring().assignValueType(CharstringValue("Graph1")), 'workName': Charstring().assignValueType(CharstringValue("Work1"))}, {'graphName': Charstring().assignValueType(CharstringValue("Graph1")), 'workName': Charstring().assignValueType(CharstringValue("Work2"))} ] workPredecessorsData = [ {'workName': Charstring().assignValueType(CharstringValue("Work2")), 'predecessorWorkName': Charstring().assignValueType(CharstringValue("Work1"))} ] loadGraphAndWorkResponse = PiewikLoadGraphAndWorkResponse() loadGraphAndWorkResponse.assignValueType({ 'messageName': Charstring().assignValueType(CharstringValue("LoadGraphAndWorkResponse")), 'sender': senderData, 'receiver': receiverData, 'graphs': graphsData, 'works': worksData, 'workPredecessors': workPredecessorsData }) encoder = ProtobufEncoder() envelope = encoder.encode(aPayloadData=loadGraphAndWorkResponse) self.assertEqual(envelope.header.id, LOAD_GRAPH_AND_WORK_RESPONSE) payload = LoadGraphAndWorkResponse() payload.ParseFromString(envelope.payload.payload) self.assertEqual(payload.messageName, "LoadGraphAndWorkResponse") self.assertEqual(payload.sender.type, "HelloCritty") self.assertEqual(payload.sender.nick, "Sender") self.assertEqual(payload.receiver.type, "HelloCritty") self.assertEqual(payload.receiver.nick, "Receiver") self.assertEqual(payload.graphs[0].graphName, "Graph1") self.assertEqual(payload.graphs[1].graphName, "Graph2") self.assertEqual(payload.works[0].graphName, "Graph1") self.assertEqual(payload.works[0].workName, "Work1") self.assertEqual(payload.works[1].graphName, "Graph1") self.assertEqual(payload.works[1].workName, "Work2") self.assertEqual(payload.workPredecessors[0].workName, "Work2") self.assertEqual(payload.workPredecessors[0].predecessorWorkName, "Work1")
def test_LoadGraphAndWorkResponse(self): senderData = {'type': Charstring().assignValueType(CharstringValue("HelloCritty")), 'nick': Charstring().assignValueType(CharstringValue("Sender"))} receiverData = {'type': Charstring().assignValueType(CharstringValue("HelloCritty")), 'nick': Charstring().assignValueType(CharstringValue("Receiver"))} graphsData = [ {'graphName': Charstring().assignValueType(CharstringValue("Graph1"))}, {'graphName': Charstring().assignValueType(CharstringValue("Graph2"))} ] worksData =[ {'graphName': Charstring().assignValueType(CharstringValue("Graph1")), 'workName': Charstring().assignValueType(CharstringValue("Work1"))}, {'graphName': Charstring().assignValueType(CharstringValue("Graph1")), 'workName': Charstring().assignValueType(CharstringValue("Work2"))} ] workPredecessorsData = [ {'workName': Charstring().assignValueType(CharstringValue("Work2")), 'predecessorWorkName': Charstring().assignValueType(CharstringValue("Work1"))} ] loadGraphAndWorkResponse = PiewikLoadGraphAndWorkResponse() loadGraphAndWorkResponse.assignValueType({ 'messageName': Charstring().assignValueType(CharstringValue("LoadGraphAndWorkResponse")), 'sender': senderData, 'receiver': receiverData, 'graphs': graphsData, 'works': worksData, 'workPredecessors': workPredecessorsData }) encoder = ProtobufEncoder() payload = LoadGraphAndWorkResponse() encoder.encodePayload(aPayloadContent=payload, aPayloadData=loadGraphAndWorkResponse) self.assertEqual(payload.messageName, "LoadGraphAndWorkResponse") self.assertEqual(payload.sender.type, "HelloCritty") self.assertEqual(payload.sender.nick, "Sender") self.assertEqual(payload.receiver.type, "HelloCritty") self.assertEqual(payload.receiver.nick, "Receiver") self.assertEqual(payload.graphs[0].graphName, "Graph1") self.assertEqual(payload.graphs[1].graphName, "Graph2") self.assertEqual(payload.works[0].graphName, "Graph1") self.assertEqual(payload.works[0].workName, "Work1") self.assertEqual(payload.works[1].graphName, "Graph1") self.assertEqual(payload.works[1].workName, "Work2") self.assertEqual(payload.workPredecessors[0].workName, "Work2") self.assertEqual(payload.workPredecessors[0].predecessorWorkName, "Work1")
def test_LoadGraphAndWorkResponse(self): senderData = {'type': Charstring().assignValueType(CharstringValue("HelloCritty")), 'nick': Charstring().assignValueType(CharstringValue("Sender"))} receiverData = {'type': Charstring().assignValueType(CharstringValue("HelloCritty")), 'nick': Charstring().assignValueType(CharstringValue("Receiver"))} graphsData = [ {'graphName': Charstring().assignValueType(CharstringValue("Graph1"))}, {'graphName': Charstring().assignValueType(CharstringValue("Graph2"))} ] worksData = [ {'graphName': Charstring().assignValueType(CharstringValue("Graph1")), 'workName': Charstring().assignValueType(CharstringValue("Work1"))}, {'graphName': Charstring().assignValueType(CharstringValue("Graph1")), 'workName': Charstring().assignValueType(CharstringValue("Work2"))} ] workPredecessorsData = [ {'workName': Charstring().assignValueType(CharstringValue("Work2")), 'predecessorWorkName': Charstring().assignValueType(CharstringValue("Work1"))} ] loadGraphAndWorkResponse = PiewikLoadGraphAndWorkResponse() loadGraphAndWorkResponse.assignValueType({ 'messageName': Charstring().assignValueType(CharstringValue("LoadGraphAndWorkResponse")), 'sender': senderData, 'receiver': receiverData, 'graphs': graphsData, 'works': worksData, 'workPredecessors': workPredecessorsData }) encoder = ProtobufEncoder() envelope = encoder.encode(aPayloadData=loadGraphAndWorkResponse) decoder = ProtobufDecoder() message = decoder.decode(envelope.SerializeToString()) self.assertEqual(message.getField('messageName').valueType().value(), 'LoadGraphAndWorkResponse') self.assertEqual(message.getField('sender').getField('type').valueType().value(), 'HelloCritty') self.assertEqual(message.getField('sender').getField('nick').valueType().value(), 'Sender') self.assertEqual(message.getField('receiver').getField('type').valueType().value(), 'HelloCritty') self.assertEqual(message.getField('receiver').getField('nick').valueType().value(), 'Receiver') self.assertEqual(message.getField('graphs').getField(0).getField('graphName').valueType().value(), 'Graph1') self.assertEqual(message.getField('graphs').getField(1).getField('graphName').valueType().value(), 'Graph2') self.assertEqual(message.getField('works').getField(0).getField('graphName').valueType().value(), 'Graph1') self.assertEqual(message.getField('works').getField(0).getField('workName').valueType().value(), 'Work1') self.assertEqual(message.getField('works').getField(1).getField('graphName').valueType().value(), 'Graph1') self.assertEqual(message.getField('works').getField(1).getField('workName').valueType().value(), 'Work2') self.assertEqual(message.getField('workPredecessors').getField(0).getField('workName').valueType().value(), 'Work2') self.assertEqual(message.getField('workPredecessors').getField(0).getField('predecessorWorkName').valueType().value(), 'Work1')
def __init__(self, aEventQueue): MessagePort.__init__(self, aAddress=None, aMapParam=None, aUnmapParam=None, aIn=[], aOut=[], aInOut=[]) self.mEventQueue = aEventQueue self.mCtx = zmq.Context() self.mSendSocket = self.mCtx.socket(zmq.PUB) self.mSendSocket.setsockopt(zmq.LINGER, 0) # TODO: Remove the hardcoded value. self.mSendSocket.connect('tcp://127.0.0.1:2222') self.mReceiveSocket = self.mCtx.socket(zmq.SUB) # TODO: Remove the hardcoded value. self.mReceiveSocket.connect('tcp://127.0.0.1:4444') self.mReceiveSocket.setsockopt(zmq.SUBSCRIBE, '') self.mDecoder = ProtobufDecoder() self.mEncoder = ProtobufEncoder() self.mPortReceiver = PiewikPortReceiver(self) self.mPortReceiver.setDaemon(True) self.mPortReceiver.start()
class PiewikPort(MessagePort): def __init__(self, aEventQueue): MessagePort.__init__(self, aAddress=None, aMapParam=None, aUnmapParam=None, aIn=[], aOut=[], aInOut=[]) self.mEventQueue = aEventQueue self.mCtx = zmq.Context() self.mSendSocket = self.mCtx.socket(zmq.PUB) self.mSendSocket.setsockopt(zmq.LINGER, 0) # TODO: Remove the hardcoded value. self.mSendSocket.connect('tcp://127.0.0.1:2222') self.mReceiveSocket = self.mCtx.socket(zmq.SUB) # TODO: Remove the hardcoded value. self.mReceiveSocket.connect('tcp://127.0.0.1:4444') self.mReceiveSocket.setsockopt(zmq.SUBSCRIBE, '') self.mDecoder = ProtobufDecoder() self.mEncoder = ProtobufEncoder() self.mPortReceiver = PiewikPortReceiver(self) self.mPortReceiver.setDaemon(True) self.mPortReceiver.start() def send(self, aPiewikMessage): """Sends a message (a valid Piewik type). Arguments: aPiewikMessage: A message to be sent. """ protobufEnvelope = self.mEncoder.encode(aPiewikMessage) wireMessage = protobufEnvelope.SerializeToString() try: self.mSendSocket.send(wireMessage) except: # TODO: Raise a meaningful exception. raise def receive(self): """Receives bytes, translates them on the fly into a valid Piewik type, and puts them into the queue.""" bytesRead = self.mReceiveSocket.recv() piewikMessage = self.mDecoder.decode(bytesRead) if piewikMessage: self.mEventQueue.put(PortReceivedEvent(self, piewikMessage, None)) else: # TODO: Raise a meaningful exception. raise
def test_LoadGraphAndWorkReqeust(self): senderData = {'type': Charstring().assignValueType(CharstringValue("HelloCritty")), 'nick': Charstring().assignValueType(CharstringValue("Sender"))} loadGraphAndWorkRequest = PiewikLoadGraphAndWorkRequest() loadGraphAndWorkRequest.assignValueType({ 'messageName': Charstring().assignValueType(CharstringValue("LoadGraphAndWorkRequest")), 'sender': senderData }) encoder = ProtobufEncoder() payload = LoadGraphAndWorkRequest() encoder.encodePayload(aPayloadContent=payload, aPayloadData=loadGraphAndWorkRequest) self.assertEqual(payload.messageName, "LoadGraphAndWorkRequest") self.assertEqual(payload.sender.type, "HelloCritty") self.assertEqual(payload.sender.nick, "Sender")
def test_HeartbeatAnnouncement(self): critterData = {'type': Charstring().assignValueType(CharstringValue("TYPE")), 'nick': Charstring().assignValueType(CharstringValue("NICK"))} heartbeatAnnouncement = PiewikHeartbeatAnnouncement() heartbeatAnnouncement.assignValueType({ 'messageName': Charstring().assignValueType(CharstringValue("HeartbeatAnnouncement")), 'sender': critterData, 'timestamp': Float().assignValueType(FloatValue(1234.5678)) }) encoder = ProtobufEncoder() payload = HeartbeatAnnouncement() encoder.encodePayload(aPayloadContent=payload, aPayloadData=heartbeatAnnouncement) self.assertEqual(payload.messageName, "HeartbeatAnnouncement") self.assertEqual(payload.sender.type, "TYPE") self.assertEqual(payload.sender.nick, "NICK") self.assertEqual(payload.timestamp, 1234.5678)