示例#1
0
    def __call__(self, req):
        serviceUrl = yield self._node_handle._master_proxy.lookupService(self._name)

        protocol, rest = serviceUrl.split('://', 1)
        host, port_str = rest.rsplit(':', 1)
        port = int(port_str)

        assert protocol == 'rosrpc'

        conn = yield endpoints.TCP4ClientEndpoint(reactor, host, port).connect(
            util.AutoServerFactory(lambda addr: tcpros.Protocol()))
        try:
            conn.sendString(tcpros.serialize_dict(dict(
                callerid=self._node_handle._name,
                service=self._name,
                md5sum=self._type._md5sum,
                type=self._type._type,
            )))

            tcpros.deserialize_dict((yield conn.receiveString()))

            # request could be sent before header is received to reduce latency...
            x = StringIO.StringIO()
            self._type._request_class.serialize(req, x)
            data = x.getvalue()
            conn.sendString(data)

            result = ord((yield conn.receiveByte()))
            data = yield conn.receiveString()
            if result:  # success
                defer.returnValue(self._type._response_class().deserialize(data))
            else:
                raise ServiceError(data)
        finally:
            conn.transport.loseConnection()
示例#2
0
 def __call__(self, req):
     serviceUrl = yield self._node_handle._master_proxy.lookupService(self._name)
     
     protocol, rest = serviceUrl.split('://', 1)
     host, port_str = rest.rsplit(':', 1)
     port = int(port_str)
     
     assert protocol == 'rosrpc'
     
     conn = yield endpoints.TCP4ClientEndpoint(reactor, host, port).connect(util.AutoServerFactory(lambda addr: tcpros.Protocol()))
     try:
         conn.sendString(tcpros.serialize_dict(dict(
             callerid=self._node_handle._name,
             service=self._name,
             md5sum=self._type._md5sum,
             type=self._type._type,
         )))
         
         header = tcpros.deserialize_dict((yield conn.receiveString()))
         # XXX do something with header
         
         # request could be sent before header is received to reduce latency...
         x = StringIO.StringIO()
         self._type._request_class.serialize(req, x)
         data = x.getvalue()
         conn.sendString(data)
         
         result = ord((yield conn.receiveByte()))
         data = yield conn.receiveString()
         if result: # success
             defer.returnValue(self._type._response_class().deserialize(data))
         else:
             raise ServiceError(data)
     finally:
         conn.transport.loseConnection()
示例#3
0
    def _publisher_thread(self, url):
        while True:
            try:
                proxy = rosxmlrpc.Proxy(xmlrpc.Proxy(url),
                                        self._node_handle._name)
                value = yield proxy.requestTopic(self._name, [['TCPROS']])

                protocol, host, port = value
                conn = yield endpoints.TCP4ClientEndpoint(
                    reactor, host, port).connect(
                        util.AutoServerFactory(lambda addr: tcpros.Protocol()))
                try:
                    conn.sendString(
                        tcpros.serialize_dict(
                            dict(
                                message_definition=self._type._full_text,
                                callerid=self._node_handle._name,
                                topic=self._name,
                                md5sum=self._type._md5sum,
                                type=self._type._type,
                            )))
                    header = tcpros.deserialize_dict((yield
                                                      conn.receiveString()))
                    self._connections[conn] = header.get('callerid', None)
                    try:
                        while True:
                            data = yield conn.receiveString()
                            msg = self._type().deserialize(data)
                            try:
                                self._callback(msg)
                            except:
                                traceback.print_exc()

                            self._last_message = msg
                            self._last_message_time = self._node_handle.get_time(
                            )

                            old, self._message_dfs = self._message_dfs, []
                            for df in old:
                                df.callback(msg)
                    finally:
                        del self._connections[conn]
                finally:
                    conn.transport.loseConnection()
            except (error.ConnectionDone, error.ConnectionLost,
                    error.ConnectionRefusedError):
                pass
            except Exception:
                traceback.print_exc()

            yield util.wall_sleep(
                1
            )  # pause so that we don't repeatedly reconnect immediately on failure
示例#4
0
 def _handle_tcpros_conn(conn):
     try:
         header = tcpros.deserialize_dict((yield conn.receiveString()))
         def default(header, conn):
             conn.sendString(tcpros.serialize_dict(dict(error='unhandled connection')))
             conn.transport.loseConnection()
         if 'service' in header:
             self._tcpros_handlers.get(('service', header['service']), default)(header, conn)
         elif 'topic' in header:
             self._tcpros_handlers.get(('topic', header['topic']), default)(header, conn)
         else:
             conn.sendString(tcpros.serialize_dict(dict(error='no topic or service name detected')))
             conn.transport.loseConnection()
     except:
         conn.transport.loseConnection()
         raise
示例#5
0
 def _handle_tcpros_conn(conn):
     try:
         header = tcpros.deserialize_dict((yield conn.receiveString()))
         def default(header, conn):
             conn.sendString(tcpros.serialize_dict(dict(error='unhandled connection')))
             conn.transport.loseConnection()
         if 'service' in header:
             self._tcpros_handlers.get(('service', header['service']), default)(header, conn)
         elif 'topic' in header:
             self._tcpros_handlers.get(('topic', header['topic']), default)(header, conn)
         else:
             conn.sendString(tcpros.serialize_dict(dict(error='no topic or service name detected')))
             conn.transport.loseConnection()
     except:
         conn.transport.loseConnection()
         raise
示例#6
0
文件: subscriber.py 项目: txros/txros
    def _publisher_thread(self, url):
        while True:
            try:
                proxy = rosxmlrpc.Proxy(xmlrpc.Proxy(url), self._node_handle._name)
                value = yield proxy.requestTopic(self._name, [['TCPROS']])

                protocol, host, port = value
                conn = yield endpoints.TCP4ClientEndpoint(reactor, host, port).connect(
                    util.AutoServerFactory(lambda addr: tcpros.Protocol()))
                try:
                    conn.sendString(tcpros.serialize_dict(dict(
                        message_definition=self._type._full_text,
                        callerid=self._node_handle._name,
                        topic=self._name,
                        md5sum=self._type._md5sum,
                        type=self._type._type,
                    )))
                    header = tcpros.deserialize_dict((yield conn.receiveString()))
                    self._connections[conn] = header.get('callerid', None)
                    try:
                        while True:
                            data = yield conn.receiveString()
                            msg = self._type().deserialize(data)
                            try:
                                self._callback(msg)
                            except:
                                traceback.print_exc()

                            self._last_message = msg
                            self._last_message_time = self._node_handle.get_time()

                            old, self._message_dfs = self._message_dfs, []
                            for df in old:
                                df.callback(msg)
                    finally:
                        del self._connections[conn]
                finally:
                    conn.transport.loseConnection()
            except (error.ConnectionDone, error.ConnectionLost, error.ConnectionRefusedError):
                pass
            except Exception:
                traceback.print_exc()

            yield util.wall_sleep(1)  # pause so that we don't repeatedly reconnect immediately on failure