Commit 17ea7806 authored by Ivan Tyagov's avatar Ivan Tyagov

Keep alive upd multicast

See merge request !31
parents 433d13c1 42025c70
import socket import socket
import struct import struct
import sys import sys
from threading import *
import time
samples_count = int(sys.argv[1]) host = "192.168.2.1"
port = 8000
one_microsecond = 1 / 1000000.
multicast_group = '224.3.29.71' multicast_group = '224.3.29.71'
server_address = ('', 10000) server_address = ('', 10000)
timeout = int(sys.argv[1])
samples_count = int(sys.argv[2])
# Create the socket # send to edge server unicast keep-alive messages
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) def send_msg():
while True:
msg = str(int(time.time() * 1000000))
s.send(msg.encode("utf-8"))
print "\t-> %s (unicast)" %msg
time.sleep(timeout * one_microsecond)
# Bind to the server address s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(server_address) s.connect((host,port))
send_thread = Thread(target = send_msg)
send_thread.daemon = True
send_thread.start()
#s.close()
# Subscribe to UDP datagrams published from edge server
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind(server_address)
# Tell the operating system to add the socket to the multicast group # Tell the operating system to add the socket to the multicast group
# on all interfaces. # on all interfaces.
group = socket.inet_aton(multicast_group) group = socket.inet_aton(multicast_group)
...@@ -19,18 +37,15 @@ mreq = struct.pack('4sL', group, socket.INADDR_ANY) ...@@ -19,18 +37,15 @@ mreq = struct.pack('4sL', group, socket.INADDR_ANY)
sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq) sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
last_micro_second = 0 last_micro_second = 0
# Receive/respond loop
i = 0 i = 0
l = [] l = []
while i < samples_count: while i < samples_count:
data, address = sock.recvfrom(1024) data, address = sock.recvfrom(1024)
current_micro_second = int(data) current_micro_second = int(data)
diff = current_micro_second - last_micro_second diff = current_micro_second - last_micro_second
#if diff > 200:
# print >>sys.stderr, diff
last_micro_second = current_micro_second last_micro_second = current_micro_second
if i > 0: if i > 0:
# omot first cycle as we care for diff between cycles # omit first cycle as we care for diff between cycles
l.append(str(diff)) l.append(str(diff))
i += 1 i += 1
......
import socket import socket
import struct import struct
import sys, time import sys, time
from threading import *
one_microsecond = 1 / 1000000.0
host = "192.168.2.1"
port = 8000
multicast_group = ('224.3.29.71', 10000) multicast_group = ('224.3.29.71', 10000)
timeout = int(sys.argv[1])
# Create the datagram socket # open socket unicast port for connection from couplers
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serversocket.bind((host, port))
serversocket.listen(5)
one_microsecond = 1 / 1000000.0 def receive_msg():
conn, addr = serversocket.accept()
while True:
try:
msg = conn.recv(1024).decode("utf8")
print "\t<- %s (unicast)" %msg
except OSError as error:
return error
receive_thread = Thread(target=receive_msg)
receive_thread.daemon = True
receive_thread.start()
print "Started unicast keep-alive server"
print "Start sending UDP keep-alive datagrams"
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# Set the time-to-live for messages to 1 so they do not go past the # Set the time-to-live for messages to 1 so they do not go past the
# local network segment. # local network segment.
ttl = struct.pack('b', 1) ttl = struct.pack('b', 1)
sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, ttl) sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, ttl)
last_micro_seconds = int(time.time() * 1000000) last_micro_seconds = int(time.time() * 1000000)
# XXX: make CLI arguments # send over UDP keep-alive messages
timeout = int(sys.argv[1])
timeout_tolerance = int(sys.argv[2])
warning_limit = timeout + timeout_tolerance
while True: while True:
micro_seconds = int(time.time() * 1000000) micro_seconds = int(time.time() * 1000000)
diff = micro_seconds - last_micro_seconds diff = micro_seconds - last_micro_seconds
last_micro_seconds = micro_seconds last_micro_seconds = micro_seconds
message = str(micro_seconds) message = str(micro_seconds)
if diff / 1000000 > warning_limit: print "-> %s (udp)" %message
print >>sys.stderr, '%s' %diff
sent = sock.sendto(message, multicast_group) sent = sock.sendto(message, multicast_group)
time.sleep(timeout * one_microsecond) # wait interval time.sleep(timeout * one_microsecond) # wait interval
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment