1
0
epfl_cs451/barrier.py

92 lines
2.5 KiB
Python
Raw Permalink Normal View History

2020-09-14 08:57:26 +02:00
#!/usr/bin/env python3
import argparse
import socket
2020-09-21 11:56:36 +02:00
import time
import struct
from collections import OrderedDict
2020-09-14 08:57:26 +02:00
2020-09-20 15:55:05 +02:00
class Barrier:
2020-09-21 11:56:36 +02:00
def __init__(self, host, port, waitFor, printer=None):
2020-09-20 15:55:05 +02:00
self.host = host
self.port = port
2020-09-21 11:56:36 +02:00
self.waitFor = waitFor
self.printer = printer
self.startTimes = dict()
2020-09-14 08:57:26 +02:00
2020-09-20 15:55:05 +02:00
def listen(self):
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.sock.bind((self.host, self.port))
2020-09-21 11:56:36 +02:00
self.sock.listen(128)
2020-09-20 15:55:05 +02:00
2020-09-21 11:56:36 +02:00
def wait(self):
2020-09-14 08:57:26 +02:00
connections = []
addresses = []
while True:
2020-09-20 15:55:05 +02:00
conn, addr = self.sock.accept()
2020-09-21 11:56:36 +02:00
idInBytes = []
while len(idInBytes) < 8:
data = conn.recv(8 - len(idInBytes))
if not data:
raise Exception("Could not recv the LogicalPID")
idInBytes += data
pid = struct.unpack('!Q', bytes(idInBytes))[0]
connections.append((pid, conn))
2020-09-14 08:57:26 +02:00
addresses.append(addr)
2020-09-21 11:56:36 +02:00
if self.printer:
self.printer("Connection from {}, corresponds to PID {}".format(addr, pid))
if len(connections) == self.waitFor:
2020-09-14 08:57:26 +02:00
break
2020-09-21 11:56:36 +02:00
for pid, conn in connections:
self.startTimes[pid] = int(time.time() * 1000)
2020-09-14 08:57:26 +02:00
conn.close()
2020-09-20 15:55:05 +02:00
return None
2020-09-21 11:56:36 +02:00
def startTimesFuture(self):
return self.startTimes
2020-09-20 15:55:05 +02:00
2020-09-14 08:57:26 +02:00
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"--host",
default="0.0.0.0",
dest="host",
help="IP address where the barrier listens to (default: any)",
)
parser.add_argument(
"--port",
2020-09-21 11:56:36 +02:00
default=10000,
2020-09-14 08:57:26 +02:00
type=int,
dest="port",
2020-09-21 11:56:36 +02:00
help="TCP port where the barrier listens to (default: 10000)",
2020-09-14 08:57:26 +02:00
)
parser.add_argument(
2020-09-21 11:56:36 +02:00
"-p",
2020-09-14 08:57:26 +02:00
"--processes",
required=True,
type=int,
dest="processes",
help="Number of processes the barrier waits for",
)
results = parser.parse_args()
2020-09-21 11:56:36 +02:00
barrier = Barrier(results.host, results.port, results.processes, print)
2020-09-20 15:55:05 +02:00
barrier.listen()
2020-09-14 08:57:26 +02:00
print("Barrier listens on {}:{} and waits for {} processes".format(results.host, results.port, results.processes))
2020-09-20 15:55:05 +02:00
2020-09-21 11:56:36 +02:00
barrier.wait()
2020-09-20 15:55:05 +02:00
2020-09-21 11:56:36 +02:00
for pid, startTs in OrderedDict(sorted(barrier.startTimesFuture().items())).items():
print("Process {} started broadcasting at time {} ms from Unix epoch ".format(pid, startTs))