def _lookup_next_accountant(self): if len(self.connected_accountants ) >= 3: # TODO: read from settings.: max accountants if _Debug: lg.out( _DebugLevel, 'contract_chain_consumer._lookup_next_accountant SUCCESS, %d accountants connected' % len(self.connected_accountants)) self.automat('accountants-connected') return if self.accountant_lookups >= 10: # TODO: read from settings. if len(self.connected_accountants ) >= 1: # TODO: read from settings: min accountants if _Debug: lg.out( _DebugLevel, 'contract_chain_consumer._lookup_next_accountant FAILED after %d retries, but %d accountants connected' % (self.accountant_lookups, len(self.connected_accountants))) self.automat('accountants-connected') return if _Debug: lg.out( _DebugLevel, 'contract_chain_consumer._lookup_next_accountant FAILED after %d retries with no results' % self.accountant_lookups) self.automat('accountants-failed') return self.accountant_lookups += 1 p2p_service_seeker.connect_random_node( 'service_accountant', service_params='read', exclude_nodes=self.connected_accountants, ).addBoth(self._on_accountant_lookup_finished)
def _lookup_miner(self): if self.miner_lookups >= 5: # TODO: read value from settings if _Debug: lg.out(_DebugLevel, 'contract_chain_consumer._lookup_miner FAILED after %d retries' % self.miner_lookups) self.automat('miner-failed') return self.miner_lookups += 1 p2p_service_seeker.connect_random_node('service_miner').addBoth(self._on_miner_lookup_finished)
def _do_lookup_one_broker(self, broker_pos, skip_brokers): if _Debug: lg.args(_DebugLevel, broker_pos=broker_pos, skip_brokers=skip_brokers, connecting_brokers=self.connecting_brokers) exclude_brokers = set() for known_broker_id in groups.known_brokers(self.group_creator_id): if known_broker_id: exclude_brokers.add( global_id.glob2idurl(known_broker_id, as_field=False)) for connected_broker_idurl in self.connected_brokers.values(): exclude_brokers.add(id_url.to_bin(connected_broker_idurl)) for skip_idurl in skip_brokers: if skip_idurl: exclude_brokers.add(id_url.to_bin(skip_idurl)) if self.dead_broker_id: exclude_brokers.add( global_id.glob2idurl(self.dead_broker_id, as_field=False)) result = p2p_service_seeker.connect_random_node( lookup_method=lookup.random_message_broker, service_name='service_message_broker', service_params=lambda idurl: self. _do_prepare_service_request_params(idurl, broker_pos), exclude_nodes=list(exclude_brokers), ) result.addCallback(self._on_broker_hired, broker_pos) if _Debug: result.addErrback(lg.errback, debug=_Debug, debug_level=_DebugLevel, method='group_member._do_lookup_one_broker') result.addErrback(self._on_message_broker_lookup_failed, broker_pos) return result
def doHireNewBroker(self, event, *args, **kwargs): """ Action method. """ known_brokers = {} known_brokers.update(self.cooperated_brokers or {}) known_brokers[self.desired_position] = self.my_broker_idurl target_pos = self.desired_position - 1 exclude_brokers = list( id_url.to_bin_list(filter(None, self.dht_brokers.values()))) exclude_brokers.extend( list( id_url.to_bin_list( filter(None, self.requestor_known_brokers.values())))) preferred_brokers = [] preferred_brokers_raw = config.conf().getData( 'services/message-broker/preferred-brokers').strip() if preferred_brokers_raw: preferred_brokers_list = re.split('\n|,|;| ', preferred_brokers_raw) preferred_brokers.extend(preferred_brokers_list) preferred_brokers = id_url.to_bin_list(preferred_brokers) if preferred_brokers: preferred_brokers = [ x for x in preferred_brokers if x not in exclude_brokers ] if _Debug: lg.args(_DebugLevel, e=event, my=self.my_position, desired=self.desired_position, target=target_pos, exclude=exclude_brokers, preferred=preferred_brokers) if preferred_brokers: preferred_broker_idurl = id_url.field(preferred_brokers[0]) if preferred_broker_idurl and id_url.is_not_in( preferred_broker_idurl, exclude_brokers, as_field=False): result = p2p_service_seeker.connect_known_node( remote_idurl=preferred_broker_idurl, service_name='service_message_broker', service_params=lambda idurl: self ._do_prepare_service_request_params( idurl, target_pos, known_brokers, event), request_service_timeout=self.broker_negotiate_ack_timeout * (target_pos + 1), exclude_nodes=list(exclude_brokers), force_handshake=True, attempts=1, ) result.addCallback(self._on_new_broker_hired, target_pos, self.my_position, self.desired_position) if _Debug: result.addErrback( lg.errback, debug=_Debug, debug_level=_DebugLevel, method='broker_negotiator.doHirePrevBroker') result.addErrback(self._on_new_broker_lookup_failed, target_pos) return result = p2p_service_seeker.connect_random_node( lookup_method=lookup.random_message_broker, service_name='service_message_broker', service_params=lambda idurl: self._do_prepare_service_request_params( idurl, target_pos, known_brokers, event), request_service_timeout=self.broker_negotiate_ack_timeout * (target_pos + 1), exclude_nodes=list(exclude_brokers), attempts=1, force_handshake=True, ) result.addCallback(self._on_new_broker_hired, target_pos, self.my_position, self.desired_position) if _Debug: result.addErrback(lg.errback, debug=_Debug, debug_level=_DebugLevel, method='broker_negotiator.doHirePrevBroker') result.addErrback(self._on_new_broker_lookup_failed, target_pos)