Exemplo n.º 1
0
    def send(self, message, cid=None):
        """ Called internally in preparation for sending messages to the client

        This method pre-processes the message then passes it to the overridden
        outgoing method.

        Keyword arguments:
        message -- a dict of message values to be marshalled and sent
        cid     -- (optional) an associated id

        """
        serialized = self.serialize(message, cid)
        if serialized is not None:
            if self.png == "png":
                # TODO: png compression on outgoing messages
                # encode message
                pass

            fragment_list = None
            if self.fragment_size != None and len(serialized) > self.fragment_size:
                mid = message.get("id", None)

                # TODO: think about splitting into fragments that have specified size including header-fields!
                # --> estimate header size --> split content into fragments that have the requested overall size, rather than requested content size
                fragment_list = Fragmentation(self).fragment(message, self.fragment_size, mid )

            # fragment list not empty -> send fragments
            if fragment_list != None:
                for fragment in fragment_list:
                    self.outgoing(json.dumps(fragment))
                    # okay to use delay here (sender's send()-function) because rosbridge is sending next request only to service provider when last one had finished)
                    #  --> if this was not the case this delay needed to be implemented in service-provider's (meaning message receiver's) send_message()-function in rosbridge_tcp.py)
                    time.sleep(self.delay_between_messages)
            # else send message as it is
            else:
                self.outgoing(serialized)
                time.sleep(self.delay_between_messages)