-
Notifications
You must be signed in to change notification settings - Fork 0
/
testapp_dsi.py
63 lines (41 loc) · 1.63 KB
/
testapp_dsi.py
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
56
57
58
59
60
from LibPeer.Application import Application
from LibPeer.Interfaces.DSI import DSI
from LibPeer.Interfaces.DSI.Connection import Connection
import sys
import threading
import time
app = Application("dsistreamer")
dsi = DSI(app)
def pipe(connection: Connection):
while connection.connected:
data = connection.read(1024)
# sys.stderr.write("%i bytes from %s\n" % (len(data), connection.peer))
sys.stdout.buffer.write(data)
sys.stdout.buffer.flush()
sys.stderr.write("%s disconnected\n" % connection.peer)
connections = []
def new_connection(connection: Connection):
sys.stderr.write("New connection from %s\n" % connection.peer)
threading.Thread(target=pipe, args=(connection,)).start()
connections.append(connection)
dsi.new_connection.subscribe(new_connection)
app.set_discoverable(True)
# Wait 10 seconds and attempt connection to all peers
time.sleep(10)
sys.stderr.write("Finding peers...\n")
for discovery in app.find_peers():
try:
sys.stderr.write("Attempting connection with %s\n" % str(discovery.peer))
conn = dsi.connect(discovery.peer)
connections.append(conn)
threading.Thread(target=pipe, args=(conn))
sys.stderr.write("Established connection with %s\n" % str(discovery.peer))
except Exception as e:
sys.stderr.write("Failed to establish connection with %s, %s\n" % (str(discovery.peer), str(e)))
sys.stderr.write("Now piping...\n")
while True:
data = sys.stdin.buffer.read(8192)
# Send to each subscribed peer
for connection in connections:
if(connection.connected):
connection.send(data)