2018-10-21 07:37:29 +08:00
|
|
|
from .peerstore_interface import IPeerStore
|
2018-10-22 03:15:53 +08:00
|
|
|
from .peerdata import PeerData
|
2018-10-29 21:09:47 +08:00
|
|
|
from .peerinfo import PeerInfo
|
2018-10-21 07:37:29 +08:00
|
|
|
|
|
|
|
class PeerStore(IPeerStore):
|
|
|
|
|
|
|
|
def __init__(self, context):
|
2018-10-22 03:15:53 +08:00
|
|
|
IPeerStore.__init__(self, context)
|
|
|
|
self.peer_map = {}
|
2018-10-21 07:37:29 +08:00
|
|
|
|
2018-10-22 03:15:53 +08:00
|
|
|
def __create_or_get_peer(self, peer_id):
|
|
|
|
"""
|
|
|
|
Returns the peer data for peer_id or creates a new
|
|
|
|
peer data (and stores it in peer_map) if peer
|
|
|
|
data for peer_id does not yet exist
|
|
|
|
:param peer_id: peer ID
|
|
|
|
:return: peer data
|
|
|
|
"""
|
|
|
|
if peer_id in self.peer_map:
|
|
|
|
return self.peer_map[peer_id]
|
|
|
|
data = PeerData()
|
|
|
|
self.peer_map[peer_id] = data
|
|
|
|
return self.peer_map[peer_id]
|
2018-10-21 07:37:29 +08:00
|
|
|
|
2018-10-22 03:15:53 +08:00
|
|
|
def peer_info(self, peer_id):
|
|
|
|
if peer_id in self.peer_map:
|
|
|
|
peer = self.peer_map[peer_id]
|
2018-10-29 21:09:47 +08:00
|
|
|
return PeerInfo(peer_id, peer)
|
2018-10-22 03:15:53 +08:00
|
|
|
return None
|
2018-10-21 07:37:29 +08:00
|
|
|
|
2018-10-22 03:15:53 +08:00
|
|
|
def get_protocols(self, peer_id):
|
|
|
|
if peer_id in self.peer_map:
|
2018-10-29 20:13:39 +08:00
|
|
|
return self.peer_map[peer_id].get_protocols()
|
|
|
|
else:
|
|
|
|
raise Exception("peer ID not found")
|
2018-10-21 07:37:29 +08:00
|
|
|
|
2018-10-22 03:15:53 +08:00
|
|
|
def add_protocols(self, peer_id, protocols):
|
|
|
|
peer = self.__create_or_get_peer(peer_id)
|
|
|
|
peer.add_protocols(protocols)
|
2018-10-21 07:37:29 +08:00
|
|
|
|
2018-10-22 03:15:53 +08:00
|
|
|
def peers(self):
|
|
|
|
return self.peer_map.keys()
|
2018-10-21 07:37:29 +08:00
|
|
|
|
2018-10-22 03:15:53 +08:00
|
|
|
def get(self, peer_id, key):
|
|
|
|
if peer_id in self.peer_map:
|
2018-10-29 20:13:39 +08:00
|
|
|
val = self.peer_map[peer_id].get_metadata(key)
|
|
|
|
return val
|
|
|
|
else:
|
|
|
|
raise Exception("peer ID not found")
|
2018-10-21 07:37:29 +08:00
|
|
|
|
2018-10-22 03:15:53 +08:00
|
|
|
def put(self, peer_id, key, val):
|
|
|
|
# <<?>>
|
|
|
|
# This can output an error, not sure what the possible errors are
|
|
|
|
peer = self.__create_or_get_peer(peer_id)
|
|
|
|
peer.put_metadata(key, val)
|
2018-10-21 07:37:29 +08:00
|
|
|
|
2018-10-22 03:15:53 +08:00
|
|
|
def add_addr(self, peer_id, addr, ttl):
|
|
|
|
self.add_addrs(self, peer_id, [addr])
|
2018-10-21 07:37:29 +08:00
|
|
|
|
2018-10-22 03:15:53 +08:00
|
|
|
def add_addrs(self, peer_id, addrs, ttl):
|
|
|
|
# Ignore ttl for now
|
|
|
|
peer = self.__create_or_get_peer(peer_id)
|
|
|
|
peer.add_addrs(addrs)
|
2018-10-21 07:37:29 +08:00
|
|
|
|
2018-10-22 03:15:53 +08:00
|
|
|
def addrs(self, peer_id):
|
|
|
|
if peer_id in self.peer_map:
|
2018-10-29 20:13:39 +08:00
|
|
|
return self.peer_map[peer_id].get_addrs()
|
|
|
|
else:
|
|
|
|
raise Exception("peer ID not found")
|
2018-10-21 07:37:29 +08:00
|
|
|
|
2018-10-22 03:15:53 +08:00
|
|
|
def clear_addrs(self, peer_id):
|
|
|
|
# Only clear addresses if the peer is in peer map
|
|
|
|
if peer_id in self.peer_map:
|
|
|
|
self.peer_map[peer_id].clear_addrs()
|
2018-10-21 07:37:29 +08:00
|
|
|
|
2018-10-22 03:15:53 +08:00
|
|
|
def peers_with_addrs(self):
|
|
|
|
# Add all peers with addrs at least 1 to output
|
|
|
|
output = []
|
2018-10-21 07:37:29 +08:00
|
|
|
|
2018-10-22 03:15:53 +08:00
|
|
|
for key in self.peer_map:
|
|
|
|
if len(self.peer_map[key].get_addrs()) >= 1:
|
|
|
|
output.append(key)
|
|
|
|
return output
|