Commit ed6e432b authored by Ivan Tyagov's avatar Ivan Tyagov

Multicast keep alive

parent 816b248e
import socket
import struct
import sys
samples_count = int(sys.argv[1])
multicast_group = '224.3.29.71'
server_address = ('', 10000)
# Create the socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# Bind to the server address
sock.bind(server_address)
# Tell the operating system to add the socket to the multicast group
# on all interfaces.
group = socket.inet_aton(multicast_group)
mreq = struct.pack('4sL', group, socket.INADDR_ANY)
sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
last_micro_second = 0
# Receive/respond loop
i = 0
l = []
while i < samples_count:
data, address = sock.recvfrom(1024)
current_micro_second = int(data)
diff = current_micro_second - last_micro_second
#if diff > 200:
# print >>sys.stderr, diff
last_micro_second = current_micro_second
if i > 0:
# omot first cycle as we care for diff between cycles
l.append(str(diff))
i += 1
# save to file
hostname = socket.gethostname()
f = open("report-%s.csv" %hostname, "w")
text = "\n".join(l)
f.write(text)
f.close()
import socket
import struct
import sys, time
multicast_group = ('224.3.29.71', 10000)
# Create the datagram socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
one_microsecond = 1 / 1000000.0
# Set the time-to-live for messages to 1 so they do not go past the
# local network segment.
ttl = struct.pack('b', 1)
sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, ttl)
last_micro_seconds = int(time.time() * 1000000)
# XXX: make CLI arguments
timeout = int(sys.argv[1])
timeout_tolerance = int(sys.argv[2])
warning_limit = timeout + timeout_tolerance
while True:
micro_seconds = int(time.time() * 1000000)
diff = micro_seconds - last_micro_seconds
last_micro_seconds = micro_seconds
message = str(micro_seconds)
if diff / 1000000 > warning_limit:
print >>sys.stderr, '%s' %diff
sent = sock.sendto(message, multicast_group)
time.sleep(timeout * one_microsecond) # wait interval
sock.close()
......@@ -259,7 +259,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.6"
"version": "3.8.10"
}
},
"nbformat": 4,
......
......@@ -248,7 +248,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.6"
"version": "3.8.10"
}
},
"nbformat": 4,
......
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