Ejemplo n.º 1
0
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
Ejemplo n.º 2
0
    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')