-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpeer.py
More file actions
41 lines (29 loc) · 1 KB
/
peer.py
File metadata and controls
41 lines (29 loc) · 1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
from protocol.engine import B2BCProtocol
from protocol.message import B2BCMessage
from protocol.state import MentalState
class Peer:
def __init__(self, name):
self.name = name
self.state = MentalState()
self.engine = B2BCProtocol(self.state, entity_name=name)
def send(self, target, message: B2BCMessage):
raw = message.serialize()
print(f"\n[{self.name} SENDS]")
print(" " + message.pretty().replace("\n", "\n "))
print(f" (wire) {raw}")
return target.receive(raw)
def receive(self, raw: str):
print(f"\n[{self.name} RECEIVES]")
print(f" (wire) {raw}")
msg = B2BCMessage.parse(raw)
result = self.engine.handle(msg)
def show(m):
print(" ↳ " + m.pretty().replace("\n", "\n "))
if result is None:
return None
if isinstance(result, list):
for r in result:
show(r)
else:
show(result)
return result