Beispiel #1
0
def _get_loggers(request):
    """
    ROS service handler to get the levels of all active loggers.
    """
    ret = GetLoggersResponse()
    for n in logging.Logger.manager.loggerDict.keys():
        level = logging.getLogger(n).getEffectiveLevel()
        level = _logging_level_names[level]
        ret.loggers.append(Logger(n, level))
    return ret
Beispiel #2
0
    def __new__(cls, ns, name, addr, master_uri, remappings):
        # constraints: anything blocking here should print something if it's
        # taking a long time in order to avoid confusion

        self = object.__new__(cls)

        if ns:
            assert ns[0] == '/'
        assert not ns.endswith('/')
        self._ns = ns  # valid values: '', '/a', '/a/b'

        assert '/' not in name
        self._name = self._ns + '/' + name

        self._shutdown_callbacks = set()
        reactor.addSystemEventTrigger('before', 'shutdown', self.shutdown)

        self._addr = addr
        self._master_uri = master_uri
        self._remappings = remappings

        self._master_proxy = rosxmlrpc.Proxy(xmlrpc.Proxy(self._master_uri),
                                             self._name)
        self._is_running = True

        self._xmlrpc_handlers = {}
        self._xmlrpc_server = reactor.listenTCP(
            0, server.Site(_XMLRPCSlave(self)))
        self._shutdown_callbacks.add(self._xmlrpc_server.loseConnection)
        self._xmlrpc_server_uri = 'http://%s:%i/' % (
            self._addr, self._xmlrpc_server.getHost().port)

        self._tcpros_handlers = {}

        @util.cancellableInlineCallbacks
        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

        def _make_tcpros_protocol(addr):
            conn = tcpros.Protocol()
            _handle_tcpros_conn(conn)
            return conn

        self._tcpros_server = reactor.listenTCP(
            0, util.AutoServerFactory(_make_tcpros_protocol))
        self._shutdown_callbacks.add(self._tcpros_server.loseConnection)
        self._tcpros_server_uri = 'rosrpc://%s:%i' % (
            self._addr, self._tcpros_server.getHost().port)
        self._tcpros_server_addr = self._addr, self._tcpros_server.getHost(
        ).port

        while True:
            try:
                other_node_uri = yield self._master_proxy.lookupNode(
                    self._name)
            except rosxmlrpc.Error:
                break  # assume that error means unknown node
            except Exception:
                traceback.print_exc()
                yield util.wall_sleep(1)  # pause so we don't retry immediately
            else:
                other_node_proxy = rosxmlrpc.Proxy(
                    xmlrpc.Proxy(other_node_uri), self._name)
                try:
                    yield util.wrap_timeout(
                        other_node_proxy.shutdown(
                            'new node registered with same name'), 3)
                except error.ConnectionRefusedError:
                    pass
                except Exception:
                    traceback.print_exc()
                break

        try:
            self._use_sim_time = yield self.get_param('/use_sim_time')
        except rosxmlrpc.Error:  # assume that error means not found
            self._use_sim_time = False
        if self._use_sim_time:

            def got_clock(msg):
                self._sim_time = msg.clock

            self._clock_sub = self.subscribe('/clock', Clock, got_clock)
            # make sure self._sim_time gets set before we continue
            yield util.wrap_time_notice(self._clock_sub.get_next_message(), 1,
                                        'getting simulated time from /clock')

        for k, v in self._remappings.iteritems():
            if k.startswith('_') and not k.startswith('__'):
                yield self.set_param(self.resolve_name('~' + k[1:]),
                                     yaml.load(v))

        self.advertise_service('~get_loggers', GetLoggers,
                               lambda req: GetLoggersResponse())
        self.advertise_service('~set_logger_level', SetLoggerLevel,
                               lambda req: SetLoggerLevelResponse())

        defer.returnValue(self)
Beispiel #3
0
 def _service_handler(self, req):
     print("Service is returning a value")
     return GetLoggersResponse()