本文整理汇总了Python中dht.DHT.add_peer方法的典型用法代码示例。如果您正苦于以下问题:Python DHT.add_peer方法的具体用法?Python DHT.add_peer怎么用?Python DHT.add_peer使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类dht.DHT
的用法示例。
在下文中一共展示了DHT.add_peer方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: CryptoTransportLayer
# 需要导入模块: from dht import DHT [as 别名]
# 或者: from dht.DHT import add_peer [as 别名]
#.........这里部分代码省略.........
except (socket.error, ValueError):
seed_peers[idx] = "tcp://%s:12345" % seed
# Connect to persisted peers
db_peers = self.get_past_peers()
known_peers = list(set(seed_peers)) + list(set(db_peers))
self.connect_to_peers(known_peers)
# TODO: This needs rethinking. Normally we can search for ourselves
# but because we are not connected to them quick enough this
# will always fail. Need @gubatron to review
# Populate routing table by searching for self
# if len(known_peers) > 0:
# self.search_for_my_node()
if callback is not None:
callback('Joined')
def get_past_peers(self):
peers = []
result = self.db.selectEntries("peers", {"market_id": self.market_id})
for peer in result:
peers.append(peer['uri'])
return peers
def search_for_my_node(self):
print 'Searching for myself'
self.dht._iterativeFind(self.guid, self.dht.knownNodes, 'findNode')
def connect_to_peers(self, known_peers):
for known_peer in known_peers:
t = Thread(target=self.dht.add_peer, args=(self, known_peer,))
t.start()
def get_crypto_peer(self, guid=None, uri=None, pubkey=None, nickname=None,
callback=None):
if guid == self.guid:
self.log.error('Cannot get CryptoPeerConnection for your own node')
return
self.log.debug('Getting CryptoPeerConnection' +
'\nGUID:%s\nURI:%s\nPubkey:%s\nNickname:%s' %
(guid, uri, pubkey, nickname))
return connection.CryptoPeerConnection(self,
uri,
pubkey,
guid=guid,
nickname=nickname)
def addCryptoPeer(self, peer_to_add):
foundOutdatedPeer = False
for idx, peer in enumerate(self.dht.activePeers):
if (peer.address, peer.guid, peer.pub) == \
(peer_to_add.address, peer_to_add.guid, peer_to_add.pub):
self.log.info('Found existing peer, not adding.')
return
if peer.guid == peer_to_add.guid or \
peer.pub == peer_to_add.pub or \
peer.address == peer_to_add.address:
示例2: CryptoTransportLayer
# 需要导入模块: from dht import DHT [as 别名]
# 或者: from dht.DHT import add_peer [as 别名]
#.........这里部分代码省略.........
"sin": self.sin
}
self.db.updateEntries("settings", newsettings, {"market_id": self.market_id})
self.settings.update(newsettings)
def _generate_new_bitmessage_address(self):
# Use the guid generated previously as the key
self.bitmessage = self.bitmessage_api.createRandomAddress(
self.guid.encode('base64'),
False,
1.05,
1.1111
)
newsettings = {"bitmessage": self.bitmessage}
self.db.updateEntries("settings", newsettings, {"market_id": self.market_id})
self.settings.update(newsettings)
def join_network(self, seeds=None, callback=None):
if seeds is None:
seeds = []
self.log.info('Joining network')
# Connect up through seed servers
for idx, seed in enumerate(seeds):
seeds[idx] = network_util.get_peer_url(seed, "12345")
# Connect to persisted peers
db_peers = self.get_past_peers()
known_peers = list(set(seeds).union(db_peers))
for known_peer in known_peers:
self.dht.add_peer(self, known_peer)
# Populate routing table by searching for self
if known_peers:
# Check every one second if we are connected
# We could use a PeriodicCallback but I think this is simpler
# since this will be repeated in most cases less than 10 times
def join_callback():
# If we are not connected to any node, reschedule a check
if not self.dht.activePeers:
ioloop.IOLoop.instance().call_later(1, join_callback)
else:
self.search_for_my_node()
join_callback()
if callback is not None:
callback('Joined')
def get_past_peers(self):
result = self.db.selectEntries("peers", {"market_id": self.market_id})
return [peer['uri'] for peer in result]
def search_for_my_node(self):
self.log.info('Searching for myself')
self.dht._iterativeFind(self.guid, self.dht.knownNodes, 'findNode')
def get_crypto_peer(self, guid=None, uri=None, pubkey=None, nickname=None):
if guid == self.guid:
self.log.error('Cannot get CryptoPeerConnection for your own node')
return
self.log.debug(
'Getting CryptoPeerConnection'