def test_can_deserialize_homogeneous_string():
    abc = ["alpha", "beta", "gamma"]
    frames = [Frame(x.encode('utf-8')) for x in abc]

    deserialized = deserialize_frames(frames)

    for r in range(len(abc)):
        assert abc[r] == deserialized[r], f"Element {r} is not the same."
Beispiel #2
0
    def recv_vip_dict(self, flags=0, copy=True, track=False):
        """Receive a complete VIP message and return in a dict."""
        state = self._recv_state
        frames = self.recv_vip(flags=flags, copy=copy, track=track)
        via = frames.pop(0) if state == -1 else None
        # from volttron.utils.frame_serialization import decode_frames
        # decoded = decode_frames(frames)

        myframes = deserialize_frames(frames)
        dct = dict(zip(('peer', 'user', 'id', 'subsystem', 'args'), myframes))
        if via is not None:
            dct['via'] = via
        return dct
def test_mixed_array():
    original = [
        "alpha",
        dict(alpha=5, gamma="5.0", theta=5.0), "gamma",
        ["from", "to", 'VIP1', ['third', 'level', 'here', 50]]
    ]
    frames = serialize_frames(original)
    for x in frames:
        assert isinstance(x, Frame)

    after_deserialize = deserialize_frames(frames)

    for r in range(len(original)):
        assert original[r] == after_deserialize[
            r], f"Element {r} is not the same."
Beispiel #4
0
    def _handle_error(self, channel, method, props, body):
        """
         Handle unroutable messages. Send error message back to sender.
         Ignore if subsystem is pubsub.
        :param channel: channel object
        :param method: method frame - contains routing key
        :param props: message properties containing VIP info such as
                      [SENDER, RECIPIENT, PROTO, USER_ID, MSG_ID, SUBSYS,]
        :param body: message body
        :return:
        """
        # Ignore if message type is 'pubsub'
        if props.type == 'pubsub':
            return

        sender = props.app_id
        subsystem = props.type
        props.app_id = self.routing_key
        props.type = 'error'
        props.user_id = self._rmq_userid
        errnum = errno.EHOSTUNREACH
        errmsg = os.strerror(errnum).encode('ascii')
        # Handle if the recipient header is not specified (improper vip) but should
        # not bring down the platform.
        recipient = props.headers.get('recipient', '') if props.headers else ''
        message = [errnum, errmsg, recipient, subsystem]

        _log.error(
            "Host Unreachable Error Message is: {0}, {1}, {2}, {3}".format(
                message, method.routing_key, sender, props))

        real_message = jsonapi.dumps(deserialize_frames(message),
                                     ensure_ascii=False)

        # The below try/except protects the platform from someone who is not communicating
        # via vip protocol.  If sender is not a string then the channel publish will throw
        # an AssertionError and it will kill the platform.
        try:
            self.channel.basic_publish(self.exchange, sender, real_message,
                                       props)
        except AssertionError:
            pass
Beispiel #5
0
    def vip_loop(self):
        """
        Infinite VIP loop to receive and send messages over ZMQ message bus.
        :return:
        """
        connection = self.core.connection
        # Set up ZMQ router socket connection
        self.zmq_router.start()
        # Register proxy agent handle
        self.zmq_router.pubsub.add_rabbitmq_agent(self)
        self._vip_loop_running = True

        while True:
            try:
                frames = self.zmq_router.socket.recv_multipart(copy=False)
                frames = deserialize_frames(frames)
                sender, recipient, proto, auth_token, msg_id, subsystem = frames[:
                                                                                 6]
                sender = sender
                recipient = recipient
                subsystem = subsystem

                if subsystem == 'hello':
                    self.vip.peerlist.add_peer(sender, 'zmq')
                    self._zmq_peers.add(sender)
                elif subsystem == 'agentstop':
                    self.vip.peerlist.drop_peer(sender, 'zmq')
                    self._zmq_peers.remove(sender)
                if not recipient or recipient in self._zmq_peers:
                    # Handle router specific messages or route to ZMQ peer
                    self.zmq_router.route(frames)
                else:
                    # Route to RabbitMQ agent
                    self._route_to_agent(frames)
            except ZMQError as exc:
                # _log.error("Error while receiving message: {}".format(exc))
                if exc.errno == ENOTSOCK:
                    break
        self._vip_loop_running = False