|
@@ -4,14 +4,18 @@ import socket
|
|
|
import select
|
|
|
import random
|
|
|
import subprocess
|
|
|
+import time
|
|
|
import re
|
|
|
|
|
|
import peerfinder_pb2 as pf
|
|
|
|
|
|
SERVER = ("::1", 9999)
|
|
|
+# In seconds
|
|
|
+HEARTBEAT_INTERVAL = random.randint(5, 15)
|
|
|
+RECONNECT_INTERVAL = random.randint(5, 15)
|
|
|
+NB_PINGS = 3
|
|
|
|
|
|
ping_regexp = re.compile(r"time=(.*) ms")
|
|
|
-NB_PINGS = 3
|
|
|
|
|
|
def ping(target, nb_pings=NB_PINGS):
|
|
|
"""Returns the list of latencies towards the given target"""
|
|
@@ -24,16 +28,14 @@ def ping(target, nb_pings=NB_PINGS):
|
|
|
data = [ping_regexp.search(line) for line in data]
|
|
|
return [float(m.group(1)) for m in data if m != None]
|
|
|
|
|
|
-def test_socket():
|
|
|
- s = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
|
|
|
- s.settimeout(random.randint(2, 8))
|
|
|
- s.connect(SERVER)
|
|
|
+def run_measurements(s):
|
|
|
while True:
|
|
|
try:
|
|
|
data = s.recv(1024)
|
|
|
print(data)
|
|
|
if len(data) == 0:
|
|
|
- break
|
|
|
+ s.close()
|
|
|
+ return
|
|
|
msg = pf.Message()
|
|
|
msg.ParseFromString(data)
|
|
|
if msg.type == pf.Message.Pong:
|
|
@@ -53,15 +55,20 @@ def test_socket():
|
|
|
msg = pf.Message()
|
|
|
msg.type = pf.Message.Ping
|
|
|
s.send(msg.SerializeToString())
|
|
|
-
|
|
|
-
|
|
|
-def test_protobuf():
|
|
|
- a = pf.IPAddress()
|
|
|
- a.address = "2001:db8::1"
|
|
|
- a.family = pf.IPAddress.IPV6
|
|
|
- print(a.SerializeToString())
|
|
|
+ except ConnectionResetError:
|
|
|
+ s.close()
|
|
|
+ return
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
- #test_protobuf()
|
|
|
- test_socket()
|
|
|
+ while True:
|
|
|
+ sock = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
|
|
|
+ sock.settimeout(HEARTBEAT_INTERVAL)
|
|
|
+ try:
|
|
|
+ sock.connect(SERVER)
|
|
|
+ print("Connected to {}".format(SERVER))
|
|
|
+ run_measurements(sock)
|
|
|
+ except ConnectionRefusedError:
|
|
|
+ pass
|
|
|
+ print("Disconnected, reconnecting in {} seconds".format(RECONNECT_INTERVAL))
|
|
|
+ time.sleep(RECONNECT_INTERVAL)
|