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)
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)
class NavigatorApp(BaseNavigatorApp): """specific class to launch a simple non-graphic navigator""" def __init__(self, params=None, *args, **kargs): """available kargs: port""" BaseNavigatorApp.__init__(self, params, *args, **kargs) self.config_data = ConfigData self.listener = None self.OnInit() def OnInit(self): """ Main initialization handler. """ BaseNavigatorApp.OnInit(self) self.InitResources() self.world = BaseWorld(self.viewport) # Other tasks self.InitTwisted() self.InitNetwork() def InitIpAddress(self): """ Get local address from Stun """ def _succeed(address): """Discovery succeeded""" self.local_ip, port = address print "discovery found address %s:%d" % (self.local_ip, port) self.InitServices() def _fail(failure): """Discovery failed => try next discovery method""" print "discovery failed:", failure.getErrorMessage() print 'using getHostByName:', self.local_ip self.InitServices() deferred = DiscoverAddress(self.params.port, self.reactor, self.params) deferred.addCallback(_succeed) deferred.addErrback(_fail) return deferred def InitResources(self): """Create world""" self.viewport = Viewport() self.world = BaseWorld(self.viewport) def InitNetwork(self): """ Launch network event loop. """ self.network_loop = NetworkLoop(self.reactor, self) 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) def startListening(self): """wait for connection from manager""" if not self.listener: self.listener = self.reactor.listenTCP(self.local_port, SolipsisUiFactory(self)) def stopListening(self): """close connection from manager and stop accepting""" if self.listener: defered = self.listener.stopListening() self.listener = None return defered else: return None # # Helpers # def future_call(self, delay, function): """call function after delay (milli sec)""" threading.Timer(delay/1000, function).start() def display_message(self, title, msg): """Display message to user, using for instance a dialog""" print title, ":", msg def display_error(self, title, msg): """Report error to user""" print >> sys.stderr, title, ":", 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. """ pass