From 4c1447bb5e75862ed4e5906ca2a6b190ae5c9e0c Mon Sep 17 00:00:00 2001 From: Athanasios Xygkis Date: Mon, 14 Sep 2020 08:57:26 +0200 Subject: [PATCH] Add TCP barrier --- barrier.py | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100755 barrier.py diff --git a/barrier.py b/barrier.py new file mode 100755 index 0000000..e399ef6 --- /dev/null +++ b/barrier.py @@ -0,0 +1,56 @@ +#!/usr/bin/env python3 + +import argparse +import socket + +def barrier(host, port, wait_for): + with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: + s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) + s.bind((host, port)) + s.listen() + + connections = [] + addresses = [] + + while True: + conn, addr = s.accept() + connections.append(conn) + addresses.append(addr) + print("Connection from {}".format(addr)) + + if len(connections) == wait_for: + break + + for conn in connections: + conn.close() + +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", + default=11000, + type=int, + dest="port", + help="TCP port where the barrier listens to (default: 11000)", + ) + + parser.add_argument( + "--processes", + required=True, + type=int, + dest="processes", + help="Number of processes the barrier waits for", + ) + + results = parser.parse_args() + + print("Barrier listens on {}:{} and waits for {} processes".format(results.host, results.port, results.processes)) + barrier(results.host, results.port, results.processes) \ No newline at end of file