def _on_connected(self, ok): from twisted.internet.defer import DeferredList from logs import lg from dht import dht_service from dht import known_nodes from main.config import conf lg.info('DHT node connected ID0=[%s] : %r' % (dht_service.node().layers[0], ok)) dht_service.node().add_rpc_callback('store', self._on_dht_rpc_store) dht_service.node().add_rpc_callback('request', self._on_dht_rpc_request) known_seeds = known_nodes.nodes() dl = [] attached_layers = conf().getData('services/entangled-dht/attached-layers', default='') if attached_layers: lg.info('more DHT layers to be attached: %r' % attached_layers) for layer_id in attached_layers.split(','): if layer_id.strip(): dl.append(dht_service.open_layer( layer_id=int(layer_id.strip()), seed_nodes=known_seeds, connect_now=True, attach=True, )) if dl: d = DeferredList(dl) d.addCallback(self._on_layers_attached) d.addErrback(self._on_connect_failed) else: if self.starting_deferred and not self.starting_deferred.called: self.starting_deferred.callback(True) return ok
def _on_connected(self, nodes): from dht import dht_service import base64 from logs import lg lg.out(self.debug_level, 'service_entangled_dht._on_connected nodes: %r' % nodes) lg.out(self.debug_level, ' DHT node is active, ID=[%s]' % base64.b64encode(dht_service.node().id)) dht_service.node().add_rpc_callback('store', self._on_dht_rpc_store) dht_service.node().add_rpc_callback('request', self._on_dht_rpc_request) return nodes
def stop(self): from dht import dht_service from main.config import conf dht_service.node().remove_rpc_callback('request') dht_service.node().remove_rpc_callback('store') conf().removeCallback('services/entangled-dht/udp-port') dht_service.disconnect() dht_service.shutdown() return True
def stop(self): from dht import dht_records from dht import dht_service from main.config import conf for layer_id in dht_records.LAYERS_REGISTRY.keys(): dht_service.close_layer(layer_id) dht_service.node().remove_rpc_callback('request') dht_service.node().remove_rpc_callback('store') conf().removeConfigNotifier('services/entangled-dht/udp-port') dht_service.disconnect() dht_service.shutdown() return True
def _on_connected(self, ok): from twisted.internet.defer import DeferredList from logs import lg from dht import dht_service from dht import known_nodes from main.config import conf from services import driver lg.info('DHT node connected ID0=[%s] : %r' % (dht_service.node().layers[0], ok)) dht_service.node().add_rpc_callback('store', self._on_dht_rpc_store) dht_service.node().add_rpc_callback('request', self._on_dht_rpc_request) known_seeds = known_nodes.nodes() dl = [] attached_layers = conf().getData('services/entangled-dht/attached-layers', default='') if attached_layers: attached_layers = list(filter(None, map(lambda v: int(str(v).strip()), attached_layers.split(',')))) else: attached_layers = [] lg.info('reading attached DHT layers from configuration: %r' % attached_layers) all_services_attached_layers = driver.get_attached_dht_layers().values() combined_services_attached_layers = set() count_combined = len(list(map(combined_services_attached_layers.update, all_services_attached_layers))) services_attached_layers = list(combined_services_attached_layers) lg.info('combined attached DHT layers from %d services: %r' % (count_combined, services_attached_layers, )) attached_layers = list(set(attached_layers + services_attached_layers)) lg.info('DHT layers to be attached at startup: %r' % attached_layers) for layer_id in attached_layers: dl.append(dht_service.open_layer( layer_id=layer_id, seed_nodes=known_seeds, connect_now=True, attach=True, )) if dl: d = DeferredList(dl) d.addCallback(self._on_layers_attached) d.addErrback(self._on_connect_failed) else: if self.starting_deferred and not self.starting_deferred.called: self.starting_deferred.callback(True) return ok
def safe_stun(udp_port=None, dht_port=None, result_defer=None): from twisted.internet.defer import Deferred result = result_defer or Deferred() if driver.is_started('service_entangled_dht'): if dht_service.node()._joinDeferred and not dht_service.node()._joinDeferred.called: dht_service.node()._joinDeferred.addCallback(lambda ok: safe_stun(udp_port=udp_port, dht_port=dht_port, result_defer=result)) dht_service.node()._joinDeferred.addErrback(result.errback) return result if not driver.is_on('service_entangled_dht'): result.errback(Exception('service_entangled_dht() is not started')) return result try: settings.init() dht_port = dht_port or settings.getDHTPort() udp_port = udp_port or settings.getUDPPort() if dht_port: dht_service.init(dht_port) d = dht_service.connect() if udp_port: udp.listen(udp_port) def _cb(cod, typ, ip, details): # A('shutdown') result.callback({ 'result': cod, # 'stun-success' or 'stun-failed' 'type': typ, 'ip': ip, 'details': details, }) def _go(live_nodes): A('init', udp_port) A('start', _cb) d.addCallback(_go) d.addErrback(lambda err: result.callback(dict(ip='127.0.0.1', errors=[str(err), ]))) except Exception as exc: lg.exc() result.callback(dict(ip='127.0.0.1', errors=[str(exc), ])) return result return result
def udp_dht_stun(udp_port=None, dht_port=None, result_defer=None): if not driver.is_on('service_my_ip_port'): if _Debug: lg.out( _DebugLevel, 'stun_client.udp_dht_stun SKIP because service_my_ip_port() is not started' ) if result_defer: result_defer.errback( Exception('service_my_ip_port() is not started')) return False from dht import dht_service if dht_service.node().connectingTask( ) and not dht_service.node().connectingTask().called: if _Debug: lg.out( _DebugLevel, 'stun_client.udp_dht_stun SKIP and run later because dht_service is still joining the network' ) dht_service.node().connectingTask( ).addCallback(lambda ok: udp_dht_stun( udp_port=udp_port, dht_port=dht_port, result_defer=result_defer)) if result_defer: dht_service.node().connectingTask().addErrback( result_defer.errback) return True dht_port = dht_port or settings.getDHTPort() udp_port = udp_port or settings.getUDPPort() if dht_port: dht_service.init(dht_port) d = dht_service.connect() if udp_port: udp.listen(udp_port) def _cb(cod, typ, ip, details): # A('shutdown') ret = { 'result': cod, # 'stun-success' or 'stun-failed' 'type': typ, 'ip': ip, 'details': details, } if _Debug: lg.out(_DebugLevel, 'stun_client.udp_dht_stun result : %r' % ret) result_defer.callback(ret) return None def _go(live_nodes): if _Debug: lg.out(_DebugLevel, 'stun_client.udp_dht_stun GO with nodes: %r' % live_nodes) A('init', udp_port) A('start', _cb) return live_nodes d.addCallback(_go) if result_defer: d.addErrback(result_defer.errback) # d.addErrback(lambda err: result_defer.callback(dict(ip='127.0.0.1', errors=[str(err), ]))) return True