def connect_to_controller(self, engine_service, furl_or_file): """ Make a connection to a controller specified by a furl. This method takes an `IEngineBase` instance and a foolcap URL and uses the `tub` attribute to make a connection to the controller. The foolscap URL contains all the information needed to connect to the controller, including the ip and port as well as any encryption and authentication information needed for the connection. After getting a reference to the controller, this method calls the `register_engine` method of the controller to actually register the engine. :Parameters: engine_service : IEngineBase An instance of an `IEngineBase` implementer furl_or_file : str A furl or a filename containing a furl """ if not self.tub.running: self.tub.startService() self.engine_service = engine_service self.engine_reference = IFCEngine(self.engine_service) try: self.furl = find_furl(furl_or_file) except ValueError: return defer.fail(failure.Failure()) else: d = self.tub.getReference(self.furl) d.addCallbacks(self._register, self._log_failure) return d
def _try_to_connect(self, furl_or_file, delay, max_tries, attempt): """Try to connect to the controller with retry logic.""" if attempt < max_tries: log.msg("Attempting to connect to controller [%r]: %s" % \ (attempt, furl_or_file)) try: self.furl = find_furl(furl_or_file) # Uncomment this to see the FURL being tried. # log.msg("FURL: %s" % self.furl) rr = yield self.tub.getReference(self.furl) except: if attempt==max_tries-1: # This will propagate the exception all the way to the top # where it can be handled. raise else: yield sleep_deferred(delay) rr = yield self._try_to_connect( furl_or_file, 1.5*delay, max_tries, attempt+1 ) # rr becomes an int when there is a connection!!! returnValue(rr) else: returnValue(rr) else: raise EngineConnectorError( 'Could not connect to controller, max_tries (%r) exceeded. ' 'This usually means that i) the controller was not started, ' 'or ii) a firewall was blocking the engine from connecting ' 'to the controller.' % max_tries )
def get_client(self, furl_or_file): """ Get a remote reference and wrap it in a client by furl. This method first gets a remote reference and then calls its `get_client_name` method to find the apprpriate client class that should be used to wrap the remote reference. :Parameters: furl_or_file : str A furl or a filename containing a furl :Returns: A deferred to the actual client class """ furl = find_furl(furl_or_file) d = self.get_reference(furl) def wrap_remote_reference(rr): d = rr.callRemote('get_client_name') d.addCallback(lambda name: import_item(name)) def adapt(client_interface): client = client_interface(rr) client.tub = self.tub return client d.addCallback(adapt) return d d.addCallback(wrap_remote_reference) return d
def get_reference(self, furl_or_file): """ Get a remote reference using a furl or a file containing a furl. Remote references are cached locally so once a remote reference has been retrieved for a given furl, the cached version is returned. :Parameters: furl_or_file : str A furl or a filename containing a furl :Returns: A deferred to a remote reference """ furl = find_furl(furl_or_file) if furl in self._remote_refs: d = defer.succeed(self._remote_refs[furl]) else: d = self.tub.getReference(furl) d.addCallback(self.save_ref, furl) return d