2019-07-27 16:27:01 +08:00
|
|
|
from typing import (
|
|
|
|
Any,
|
|
|
|
Dict,
|
|
|
|
List,
|
|
|
|
Sequence,
|
|
|
|
)
|
|
|
|
|
|
|
|
from multiaddr import Multiaddr
|
|
|
|
|
2018-10-21 05:11:44 +08:00
|
|
|
from .peerdata_interface import IPeerData
|
|
|
|
|
2019-01-10 02:38:56 +08:00
|
|
|
|
2018-10-21 05:11:44 +08:00
|
|
|
class PeerData(IPeerData):
|
|
|
|
|
2019-07-27 16:27:01 +08:00
|
|
|
metadata: Dict[Any, Any]
|
|
|
|
protocols: List[str]
|
|
|
|
addrs: List[Multiaddr]
|
|
|
|
|
|
|
|
def __init__(self) -> None:
|
2018-10-22 03:15:53 +08:00
|
|
|
self.metadata = {}
|
2018-10-21 05:11:44 +08:00
|
|
|
self.protocols = []
|
|
|
|
self.addrs = []
|
|
|
|
|
2019-07-27 16:27:01 +08:00
|
|
|
def get_protocols(self) -> List[str]:
|
2018-10-22 03:15:53 +08:00
|
|
|
return self.protocols
|
2018-10-21 05:11:44 +08:00
|
|
|
|
2019-07-27 16:27:01 +08:00
|
|
|
def add_protocols(self, protocols: Sequence[str]) -> None:
|
|
|
|
self.protocols.extend(list(protocols))
|
2018-10-21 05:11:44 +08:00
|
|
|
|
2019-07-27 16:27:01 +08:00
|
|
|
def set_protocols(self, protocols: Sequence[str]) -> None:
|
|
|
|
self.protocols = list(protocols)
|
2018-11-03 04:16:46 +08:00
|
|
|
|
2019-07-27 16:27:01 +08:00
|
|
|
def add_addrs(self, addrs: Sequence[Multiaddr]) -> None:
|
|
|
|
self.addrs.extend(list(addrs))
|
2018-10-21 05:11:44 +08:00
|
|
|
|
2019-07-27 16:27:01 +08:00
|
|
|
def get_addrs(self) -> List[Multiaddr]:
|
2018-10-22 03:15:53 +08:00
|
|
|
return self.addrs
|
2018-10-21 05:11:44 +08:00
|
|
|
|
2019-07-27 16:27:01 +08:00
|
|
|
def clear_addrs(self) -> None:
|
2018-10-22 03:15:53 +08:00
|
|
|
self.addrs = []
|
2018-10-21 05:11:44 +08:00
|
|
|
|
2019-07-27 16:27:01 +08:00
|
|
|
def put_metadata(self, key: Any, val: Any) -> None:
|
2018-10-22 03:15:53 +08:00
|
|
|
self.metadata[key] = val
|
2018-10-21 05:11:44 +08:00
|
|
|
|
2019-07-27 16:27:01 +08:00
|
|
|
def get_metadata(self, key: Any) -> Any:
|
2018-10-22 03:15:53 +08:00
|
|
|
if key in self.metadata:
|
2018-10-30 07:03:19 +08:00
|
|
|
return self.metadata[key]
|
2018-11-27 07:24:29 +08:00
|
|
|
raise PeerDataError("key not found")
|
2018-10-30 07:03:19 +08:00
|
|
|
|
2019-01-10 02:38:56 +08:00
|
|
|
|
2018-10-30 07:03:19 +08:00
|
|
|
class PeerDataError(KeyError):
|
|
|
|
"""Raised when a key is not found in peer metadata"""
|