Esempio n. 1
0
 def InitNetwork(self):
     """
     Launch network event loop.
     """
     self.factory = SolipsisUiFactory(self)
     self.listener = self.reactor.listenTCP(self.local_port, self.factory)
     self.network_loop = NetworkLoop(self.reactor, self, self.testing)
     print "listening on port", self.local_port
     BaseNavigatorApp.InitNetwork(self)
Esempio n. 2
0
class NavigatorApp(BaseNavigatorApp):
    """specific class to launch a simple non-graphic navigator"""

    def __init__(self, params, *args, **kargs):
        """available kargs: port"""
        BaseNavigatorApp.__init__(self, params, *args, **kargs)
        self.config_data = ConfigData(self.params)
        self.waiting = True
        self.waiting_deferred = None
        self.initialised = False
        self.factory = None
        self.listener = None
        self.OnInit()

    def OnInit(self):
        """
        Main initialization handler.
        """
        BaseNavigatorApp.OnInit(self)
        self.InitTwisted()
        self.InitNetwork()

    def InitResources(self):
        """Create world"""
        self.viewport = Viewport()
        self.world = BaseWorld(self.viewport)

    def InitNetwork(self):
        """
        Launch network event loop.
        """
        self.factory = SolipsisUiFactory(self)
        self.listener = self.reactor.listenTCP(self.local_port,
                                               self.factory)
        self.network_loop = NetworkLoop(self.reactor, self, self.testing)
        print "listening on port", self.local_port
        BaseNavigatorApp.InitNetwork(self)

    def InitServices(self):
        """
        Initialize all services.
        """
        assert self.config_data, "config must be initialised first"
        self.services = ServiceCollector(self.params, self.local_ip,
                                         self.reactor, self)
        if self.config_data.used_services:
            for service in self.config_data.used_services:
                service_path = self.services._ServiceDirectory(service)
                service_id, plugin = self.services.LoadService(service_path,
                                                               service)
                self.services.InitService(service_id, plugin)
                self.services.SetNode(self.config_data.GetNode())
                self.services.EnableServices()
                self.config_data.SetServices(self.services.GetServices())
        else:
            BaseNavigatorApp.InitServices(self)
        # flag end of initialisation
        self.initialised = True
        for deferred in self.factory.initialised:
            deferred.callback(True)

    def startListening(self):
        self.network_loop.start()

    def stopListening(self):
        pass
    
    def get_position(self, name=None):
        if self._CheckNodeProxy():
            if not name:
                name = self.world.node_id
            if name is not None:
                return self.world.viewport.GetObjectPosition(name)
            else:
                return "No node"
        else:
            return "Not connected"

    def get_peers_by_service(self, service=None):
        if self._CheckNodeProxy():
            if service is None:
                return self.world.items.keys()
            else:
                return [peer.id_
                        for peer in self.world.GetAllPeers()
                        if not peer.GetService(service) is None]
        else:
            return "Not connected"
        
    def get_action(self, peer_id=None):
        if not self._CheckNodeProxy():
            return "Not connected"
        return self.services.GetActions(peer_id)

    def do_wait(self, deferred):
        self.waiting_deferred = deferred

    #
    # Helpers
    #
    def future_call(self, delay, function):
        """call function after delay (milli sec)"""
        threading.Timer(delay/1000, function).start()

    def display_message(self, msg, title=None):
        """Display message to user, using for instance a dialog"""
	return title and "%s: %s"% (title, msg) or msg

    def display_error(self, msg, title=None, error=None, trace=None):
        """Report error to user"""
        msg == title and "%s: %s"% (title, msg) or msg
        print >> sys.stderr, msg

    def display_warning(self, msg, title=None):
        """Report error to user"""
        print msg == title and "%s: %s"% (title, msg) or msg

    def display_status(self, msg):
        """report a status"""
        print msg

    def _DestroyProgress(self):
        """
        Destroy progress dialog if necessary.
        """
        pass

    def _SetWaiting(self, waiting):
        """
        Set "waiting" state of the interface.
        """
        self.waiting = waiting
        if not waiting and not self.waiting_deferred is None:
            self.waiting_deferred.callback("ok")
            self.waiting_deferred = None

    def _OnQuit(self):
        self.stopListening()
        BaseNavigatorApp._OnQuit(self)

    #===-----------------------------------------------------------------===#
    # Actions from the network thread(s)
    #
    def NodeConnectionFailed(self, error):
        """
        Failed connecting to the node.
        """
        if self.connection_trials > 0:
            self.connection_trials -= 1
            self.future_call(3000, self._TryConnect)
        else:
            BaseNavigatorApp.NodeConnectionFailed(self, error)