Ejemplo n.º 1
0
    def run(self):
        if conf.station_api_port is not None:
            self.station_api_server = xmlrpcutil.SimpleThreadedXmlRpcServer(
                (conf.station_api_bind_address, int(conf.station_api_port)),
                allow_none=True,
                logRequests=logging.getLogger().level <= logging.DEBUG)
            self.station_api_server.register_instance(STATION_API)

            # Discovery is useless if station_api is disabled, so we don't ever start
            # a MulticastListener if station_api_port isn't set, even if
            # enable_station_discovery is set.
            if conf.enable_station_discovery:
                self.multicast_listener = multicast.MulticastListener(
                    self.multicast_response, **MULTICAST_KWARGS())
                _LOG.debug('Listening for multicast discovery at %s:%s',
                           self.multicast_listener.address,
                           self.multicast_listener.port)
                self.multicast_listener.start()

            # server_forever() doesn't return until we call stop()
            self.station_api_server.serve_forever()
            _LOG.debug(
                'Station API returned from serve_forever(), done serving.')
        else:
            _LOG.debug(
                'Started Station API, but station_api_port disabled, bailing.')
Ejemplo n.º 2
0
    def _create_or_update_rpc_server(self):
        """Create or update the XML-RPC server for remote access to plugs.

    We register on the server the public methods (ones that don't start with _)
    of those plugs which have enable_remote set to True.

    Those methods are then available via RPC calls to:
      'plugs.<plug_module>.<plug_type>.<plug_method>'
    """

        # Create a list of (method, method_name) pairs.
        plug_methods = []

        for name, plug in self._plugs_by_name.iteritems():
            if not plug.enable_remote:
                continue

            for attr_name in dir(plug):
                attr = getattr(plug, attr_name)
                if (isinstance(attr, types.MethodType)
                        and not attr_name.startswith('_')
                        and attr_name != 'tearDown'
                        and attr_name not in plug.disable_remote_attrs):
                    plug_methods.append((attr, '.'.join(
                        ('plugs', name, attr_name))))

        if not plug_methods:
            return

        if not self._xmlrpc_server:
            _LOG.debug('Starting PlugManager XML-RPC server.')
            self._xmlrpc_server = xmlrpcutil.SimpleThreadedXmlRpcServer(
                (conf.station_api_bind_address, 0))
            self._xmlrpc_server.register_introspection_functions()
            server_thread = threading.Thread(
                target=self._xmlrpc_server.serve_forever,
                name='PlugManager-XMLRPCServer')
            server_thread.daemon = True
            server_thread.start()

        for method, name in plug_methods:
            self._xmlrpc_server.register_function(method, name=name)