Multicast mode can be turned off (it does not really work, so...

This commit is contained in:
Eichhornchen 2021-01-07 17:12:33 +01:00
parent f8fa48f274
commit 7e1323dc74
2 changed files with 22 additions and 14 deletions

View File

@ -101,6 +101,9 @@ class User(Peer):
# Bind the socket
self.socket.bind(self.main_address)
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?
@ -108,8 +111,6 @@ class User(Peer):
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
self.input_buffer = ""
self.input_index = 0
self.last_line = -1
@ -140,6 +141,7 @@ class User(Peer):
self.listener = Listener(self)
self.neighbour_manager = PeerManager(self)
self.inondator = Inondator(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)
@ -255,11 +257,13 @@ 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()
if self.squinnondation.multicast:
self.multicastlistener.setDaemon(True)
self.multicastlistener.start()
def wait_for_key(self) -> None:
@ -1146,8 +1150,9 @@ class PeerManager(Thread):
self.user.send_neighbours()
self.last_neighbour = time.time()
if self.user.squinnondation.multicast:
# For the multicast discovery : send a hello every minute.
if time.time() - self.last_multicast > 10: #60:
if time.time() - self.last_multicast > 60:
self.user.send_hello_multicast()
self.last_multicast = time.time()

View File

@ -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