From 7e1323dc7460b6f6d2d6e643747b003737bbf961 Mon Sep 17 00:00:00 2001 From: Eichhornchen Date: Thu, 7 Jan 2021 17:12:33 +0100 Subject: [PATCH] Multicast mode can be turned off (it does not really work, so... --- squinnondation/peers.py | 33 ++++++++++++++++++-------------- squinnondation/squinnondation.py | 3 +++ 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/squinnondation/peers.py b/squinnondation/peers.py index 6523c7a..40a390a 100644 --- a/squinnondation/peers.py +++ b/squinnondation/peers.py @@ -101,14 +101,15 @@ class User(Peer): # Bind the socket self.socket.bind(self.main_address) - # Create multicast socket - self.multicast_socket = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM, socket.IPPROTO_UDP) - self.multicast_socket.bind(('', 1212)) #listen on all interfaces? - # To join the group, we need to give setsockopt a binary packed representation of the multicast group's address and on what interfaces we will listen (here all) - mreq = struct.pack("16s15s", socket.inet_pton(socket.AF_INET6, "ff02::4242:4242"), bytes(socket.INADDR_ANY)) #The string "16s15s" corresponds to the packing options: here it packs the arguments into a 16-byte string and a 15-byte string. - self.multicast_socket.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_JOIN_GROUP, mreq) - self.squinnondation = instance + + if self.squinnondation.multicast: + # Create multicast socket + self.multicast_socket = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM, socket.IPPROTO_UDP) + self.multicast_socket.bind(('', 1212)) #listen on all interfaces? + # To join the group, we need to give setsockopt a binary packed representation of the multicast group's address and on what interfaces we will listen (here all) + mreq = struct.pack("16s15s", socket.inet_pton(socket.AF_INET6, "ff02::4242:4242"), bytes(socket.INADDR_ANY)) #The string "16s15s" corresponds to the packing options: here it packs the arguments into a 16-byte string and a 15-byte string. + self.multicast_socket.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_JOIN_GROUP, mreq) self.input_buffer = "" self.input_index = 0 @@ -140,7 +141,8 @@ class User(Peer): self.listener = Listener(self) self.neighbour_manager = PeerManager(self) self.inondator = Inondator(self) - self.multicastlistener = Multicastlistener(self) + if self.squinnondation.multicast: + self.multicastlistener = Multicastlistener(self) self.add_system_message(f"Listening on {self.main_address[0]}:{self.main_address[1]}", ignore_debug=True) self.add_system_message(f"I am {self.id}") @@ -255,12 +257,14 @@ class User(Peer): self.listener.setDaemon(True) self.neighbour_manager.setDaemon(True) self.inondator.setDaemon(True) - self.multicastlistener.setDaemon(True) self.listener.start() self.neighbour_manager.start() self.inondator.start() - self.multicastlistener.start() + + if self.squinnondation.multicast: + self.multicastlistener.setDaemon(True) + self.multicastlistener.start() def wait_for_key(self) -> None: """ @@ -1146,10 +1150,11 @@ class PeerManager(Thread): self.user.send_neighbours() self.last_neighbour = time.time() - # For the multicast discovery : send a hello every minute. - if time.time() - self.last_multicast > 10: #60: - self.user.send_hello_multicast() - self.last_multicast = time.time() + if self.user.squinnondation.multicast: + # For the multicast discovery : send a hello every minute. + if time.time() - self.last_multicast > 60: + self.user.send_hello_multicast() + self.last_multicast = time.time() # Avoid infinite loops time.sleep(1) diff --git a/squinnondation/squinnondation.py b/squinnondation/squinnondation.py index 9628e00..dab5162 100644 --- a/squinnondation/squinnondation.py +++ b/squinnondation/squinnondation.py @@ -17,6 +17,7 @@ class Squinnondation: no_emoji: bool no_markdown: bool debug: bool + multicast: bool screen: Any def parse_arguments(self) -> None: @@ -34,6 +35,7 @@ class Squinnondation: parser.add_argument('--no-markdown', '-nm', action='store_true', help="Don't replace emojis.") parser.add_argument('--debug', '-d', action='store_true', help="Debug mode.") + parser.add_argument('--multicast', '-mc', action='store_true', help="Use multicast?") self.args = parser.parse_args() if not (1024 <= self.args.bind_port <= 65535) or\ @@ -45,6 +47,7 @@ class Squinnondation: self.no_emoji = self.args.no_emoji self.no_markdown = self.args.no_markdown self.debug = self.args.debug + self.multicast = self.args.multicast @staticmethod def main() -> None: # pragma: no cover