-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestapp_libpeer.py
More file actions
55 lines (37 loc) · 1.23 KB
/
testapp_libpeer.py
File metadata and controls
55 lines (37 loc) · 1.23 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from LibPeer.Networks.Ipv4 import Ipv4
from LibPeer.Muxer import Muxer
from LibPeer.Transports.EDP import EDP
from LibPeer.Discoverers.Samband import Samband
from LibPeer.Formats.BinaryAddress import BinaryAddress
import struct
from LibPeer.Logging import log
log.settings(True, 0)
def recv(info):
data = info[0]
chan = info[1]
addr = info[2]
print("Got message from peer %s over channel %s: %s" % (str(addr), str(chan), data.decode("utf-8")))
peers = set()
def new_peer(address):
print("Found peer %s" % str(address[0]))
peers.add(address[0])
app = b"helloworld"
net = Ipv4({"address": "192.168.178.34", "port": 3001})
net.go_up()
muxer = Muxer([net])
muxer.add_application(app)
disc = Samband([net])
disc.add_application(app)
disc.discovered.subscribe(new_peer)
trans = EDP(muxer, {})
trans.incoming.subscribe(recv)
while True:
# chan = int(input("Channel: "))
mesg = input("Message: ").encode("utf-8")
for peer in peers:
print("************" * 4)
trans.send(mesg, struct.pack("!QQ", 0, 0), peer).subscribe(lambda x: print("Sent to %s" % peer))
for address in disc.get_addresses():
address.application = app
disc.advertise(address)
print("Advertised")