Lint peer classes

This commit is contained in:
Stuckinaboot 2018-10-21 15:15:53 -04:00
parent 88b7eda256
commit 7d124bb047
7 changed files with 173 additions and 181 deletions

View File

@ -2,50 +2,51 @@ from abc import ABC, abstractmethod
class IAddrBook(ABC): class IAddrBook(ABC):
def __init__(self, context): def __init__(self, context):
self.context = context self.context = context
@abstractmethod @abstractmethod
def add_addr(self, peerID, addr, ttl): def add_addr(self, peer_id, addr, ttl):
"""
Calls add_addrs(peerID, [addr], ttl)
:param peerID: the peer to add address for
:param addr: multiaddress of the peer
:param ttl: time-to-live for the address (after this time, address is no longer valid)
"""
pass
@abstractmethod
def add_addrs(self, peerID, addrs []ma.Multiaddr, ttl time.Duration):
"""
Adds addresses for a given peer all with the same time-to-live. If one of the
addresses already exists for the peer and has a longer TTL, no operation should take place.
If one of the addresses exists with a shorter TTL, extend the TTL to equal param ttl.
:param peerID: the peer to add address for
:param addr: multiaddresses of the peer
:param ttl: time-to-live for the address (after this time, address is no longer valid
"""
pass
@abstractmethod
def addrs(self, peerID):
"""
:param peerID: peer to get addresses of
:return: all known (and valid) addresses for the given peer
"""
pass
@abstractmethod
def clear_addrs(self, peerID):
"""
Removes all previously stored addresses
:param peerID: peer to remove addresses of
"""
pass
@abstractmethod
def peers_with_addrs(self):
"""
:return: all of the peer IDs stored with addresses
""" """
pass Calls add_addrs(peer_id, [addr], ttl)
:param peer_id: the peer to add address for
:param addr: multiaddress of the peer
:param ttl: time-to-live for the address (after this time, address is no longer valid)
"""
pass
@abstractmethod
def add_addrs(self, peer_id, addrs, ttl):
"""
Adds addresses for a given peer all with the same time-to-live. If one of the
addresses already exists for the peer and has a longer TTL, no operation should take place.
If one of the addresses exists with a shorter TTL, extend the TTL to equal param ttl.
:param peer_id: the peer to add address for
:param addr: multiaddresses of the peer
:param ttl: time-to-live for the address (after this time, address is no longer valid
"""
pass
@abstractmethod
def addrs(self, peer_id):
"""
:param peer_id: peer to get addresses of
:return: all known (and valid) addresses for the given peer
"""
pass
@abstractmethod
def clear_addrs(self, peer_id):
"""
Removes all previously stored addresses
:param peer_id: peer to remove addresses of
"""
pass
@abstractmethod
def peers_with_addrs(self):
"""
:return: all of the peer IDs stored with addresses
"""
pass

View File

View File

@ -2,31 +2,30 @@ from .peerdata_interface import IPeerData
class PeerData(IPeerData): class PeerData(IPeerData):
def __init__(self, context): def __init__(self):
self.metadata = {} self.metadata = {}
self.protocols = [] self.protocols = []
self.addrs = [] self.addrs = []
def get_protocols(self): def get_protocols(self):
return self.protocols return self.protocols
def add_protocols(self, protocols): def add_protocols(self, protocols):
self.protocols.extend(protocols) self.protocols.extend(protocols)
def add_addrs(self, addrs): def add_addrs(self, addrs):
self.addrs.extend(addrs) self.addrs.extend(addrs)
def get_addrs(self): def get_addrs(self):
return self.addrs return self.addrs
def clear_addrs(self): def clear_addrs(self):
self.addrs = [] self.addrs = []
def put_metadata(self, key, val): def put_metadata(self, key, val):
self.metadata[key] = val self.metadata[key] = val
def get_metadata(self, key): def get_metadata(self, key):
if key in self.metadata: if key in self.metadata:
return self.metadata[key], None return self.metadata[key], None
else: return None, "key not found"
return None, "key not found"

View File

@ -2,50 +2,46 @@ from abc import ABC, abstractmethod
class IPeerData(ABC): class IPeerData(ABC):
def __init__(self, context):
self.context = context
""" """
:return: all protocols associated with given peer :return: all protocols associated with given peer
""" """
def get_protocols(self): def get_protocols(self):
pass pass
""" """
:param protocols: protocols to add :param protocols: protocols to add
""" """
def add_protocols(self, protocols): def add_protocols(self, protocols):
pass pass
""" """
:param addrs: multiaddresses to add :param addrs: multiaddresses to add
""" """
def add_addrs(self, addrs): def add_addrs(self, addrs):
pass pass
""" """
:return: all multiaddresses :return: all multiaddresses
""" """
def get_addrs(self): def get_addrs(self):
pass pass
""" """
Clear all addresses Clear all addresses
""" """
def clear_addrs(self): def clear_addrs(self):
pass pass
""" """
:param key: key in KV pair :param key: key in KV pair
:param val: val to associate with key :param val: val to associate with key
""" """
def put_metadata(self, key, val): def put_metadata(self, key, val):
pass pass
""" """
:param key: key in KV pair :param key: key in KV pair
:return: val for key, error (only defined if key not found) :return: val for key, error (only defined if key not found)
""" """
def get_metadata(self, key): def get_metadata(self, key):
pass pass

View File

@ -2,24 +2,25 @@ from abc import ABC, abstractmethod
class IPeerMetadata(ABC): class IPeerMetadata(ABC):
def __init__(self, context): def __init__(self, context):
self.context = context self.context = context
@abstractmethod @abstractmethod
def get(self, peerID, key): def get(self, peer_id, key):
""" """
:param peerID: peer ID to lookup key for :param peer_id: peer ID to lookup key for
:param key: key to look up :param key: key to look up
:return: value at key for given peer, error :return: value at key for given peer, error
""" """
pass pass
@abstractmethod @abstractmethod
def put(self, peerID, key, val): def put(self, peer_id, key, val):
""" """
:param peerID: peer ID to lookup key for :param peer_id: peer ID to lookup key for
:param key: key to associate with peer :param key: key to associate with peer
:param val: value to associated with key :param val: value to associated with key
:return: error :return: error
""" """
pass pass

View File

@ -1,88 +1,82 @@
from .peerstore_interface import IPeerStore from .peerstore_interface import IPeerStore
import asyncio from .peerdata import PeerData
class PeerStore(IPeerStore): class PeerStore(IPeerStore):
def __init__(self, context): def __init__(self, context):
self.context = context IPeerStore.__init__(self, context)
self.peerMap = {} self.peer_map = {}
def peer_info(self, peerID): def __create_or_get_peer(self, peer_id):
if peerID in self.peerMap: """
peer = self.peerMap[peerID] Returns the peer data for peer_id or creates a new
return { peer data (and stores it in peer_map) if peer
"peerID": peerID, data for peer_id does not yet exist
"addrs": peer.get_addrs() :param peer_id: peer ID
} :return: peer data
else: """
return None 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]
def get_protocols(self, peerID): def peer_info(self, peer_id):
if peerID in self.peerMap: if peer_id in self.peer_map:
return self.peerMap[peerId].get_protocols(), None peer = self.peer_map[peer_id]
else: return {
return None, peerID + " not found" "peer_id": peer_id,
"addrs": peer.get_addrs()
}
return None
def add_protocols(self, peerID, protocols): def get_protocols(self, peer_id):
peer = __create_or_get_peer(peerID) if peer_id in self.peer_map:
peer.add_protocols(protocols) return self.peer_map[peer_id].get_protocols(), None
return None, peer_id + " not found"
def peers(self): def add_protocols(self, peer_id, protocols):
return self.peerMap.keys() peer = self.__create_or_get_peer(peer_id)
peer.add_protocols(protocols)
def get(self, peerID, key): def peers(self):
if peerID in self.peerMap: return self.peer_map.keys()
val, error = self.peerMap[peerID].get_metadata(key)
return val, error
else:
return None, peerID + " not found"
def put(self, peerID, key, val): def get(self, peer_id, key):
# <<?>> if peer_id in self.peer_map:
# This can output an error, not sure what the possible errors are val, error = self.peer_map[peer_id].get_metadata(key)
peer = __create_or_get_peer(peerID) return val, error
self.peerMap[peerID].put_metadata(key, val) return None, peer_id + " not found"
def add_addr(self, peerID, addr, ttl): def put(self, peer_id, key, val):
self.add_addrs(self, peerID, [addr]) # <<?>>
# 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)
def add_addrs(self, peerID, addrs, ttl): def add_addr(self, peer_id, addr, ttl):
# Ignore ttl for now self.add_addrs(self, peer_id, [addr])
peer = __create_or_get_peer(peerID)
peer.add_addrs(addrs)
def addrs(self, peerID): def add_addrs(self, peer_id, addrs, ttl):
if peerID in self.peerMap: # Ignore ttl for now
return self.peerMap[peerId].get_addrs(), None peer = self.__create_or_get_peer(peer_id)
else: peer.add_addrs(addrs)
return None, peerID + " not found"
pass
def clear_addrs(self, peerID): def addrs(self, peer_id):
# Only clear addresses if the peer is in peer map if peer_id in self.peer_map:
if peerID in self.peerMap: return self.peer_map[peer_id].get_addrs(), None
self.peerMap[peerID].clear_addrs() return None, peer_id + " not found"
def peers_with_addrs(self): def clear_addrs(self, peer_id):
# Add all peers with addrs at least 1 to output # Only clear addresses if the peer is in peer map
output = [] if peer_id in self.peer_map:
self.peer_map[peer_id].clear_addrs()
for key in self.peerMap: def peers_with_addrs(self):
if len(self.peerMap[key].get_addrs()) >= 1: # Add all peers with addrs at least 1 to output
output.append(key) output = []
return output
def __create_or_get_peer(self, peerID): for key in self.peer_map:
""" if len(self.peer_map[key].get_addrs()) >= 1:
Returns the peer data for peerID or creates a new output.append(key)
peer data (and stores it in peerMap) if peer return output
data for peerID does not yet exist
:param peerID: peer ID
:return: peer data
"""
if peerId in self.peerMap:
return self.peerMap[peerId]
else:
data = PeerData(self.context)
self.peerMap[peerId] = data
return self.peerMap[peerId]

View File

@ -1,49 +1,50 @@
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
from .addrbook_interace import IAddrBook from .addrbook_interface import IAddrBook
from .peermetadata_interface import IPeerMetadata from .peermetadata_interface import IPeerMetadata
class IPeerStore(ABC, IAddrBook, IPeerMetadata): class IPeerStore(ABC, IAddrBook, IPeerMetadata):
def __init__(self, context): def __init__(self, context):
self.context = context IPeerMetadata.__init__(self, context)
IAddrBook.__init__(self, context)
@abstractmethod @abstractmethod
def peer_info(self, peerID): def peer_info(self, peer_id):
""" """
:param peerID: peer ID to get info for :param peer_id: peer ID to get info for
:return: peer info object :return: peer info object
""" """
pass pass
@abstractmethod @abstractmethod
def get_protocols(self, peerID): def get_protocols(self, peer_id):
""" """
:param peerID: peer ID to get protocols for :param peer_id: peer ID to get protocols for
:return: protocols (as strings), error :return: protocols (as strings), error
""" """
pass pass
@abstractmethod @abstractmethod
def add_protocols(self, peerID, protocols): def add_protocols(self, peer_id, protocols):
""" """
:param peerID: peer ID to add protocols for :param peer_id: peer ID to add protocols for
:param protocols: protocols to add :param protocols: protocols to add
:return: error :return: error
""" """
pass pass
@abstractmethod @abstractmethod
def set_protocols(self, peerID, protocols): def set_protocols(self, peer_id, protocols):
""" """
:param peerID: peer ID to set protocols for :param peer_id: peer ID to set protocols for
:param protocols: protocols to set :param protocols: protocols to set
:return: error :return: error
""" """
pass pass
@abstractmethod @abstractmethod
def peers(self): def peers(self):
""" """
:return: all of the peer IDs stored in peer store :return: all of the peer IDs stored in peer store
""" """
pass pass