From 39f3507324803a0c00f721507b250834a4a865eb Mon Sep 17 00:00:00 2001
From: Jeffrey Wigger <jeffrey.wigger@epfl.ch>
Date: Mon, 13 Jun 2022 13:31:05 +0200
Subject: [PATCH] neighbor fixes

---
 .../communication/TCPRandomWalkRouting.py     | 38 ++++++++++++-------
 1 file changed, 25 insertions(+), 13 deletions(-)

diff --git a/src/decentralizepy/communication/TCPRandomWalkRouting.py b/src/decentralizepy/communication/TCPRandomWalkRouting.py
index ce1c7e8..0557b95 100644
--- a/src/decentralizepy/communication/TCPRandomWalkRouting.py
+++ b/src/decentralizepy/communication/TCPRandomWalkRouting.py
@@ -414,13 +414,17 @@ class TCPRandomWalkRoutingInternal(TCPRandomWalkBase):
         if type(data) == tuple and data[0] == HELLO:
             logging.debug("Received {} from {}".format(HELLO, src))
             if src in self.current_neighbors:
-                if data[1] != "fw at neighbor": # TODO: this is wrong
-                    logging.critical(
-                        "{} wants to connect when already connected!".format(HELLO, src)
-                    )
-                    raise RuntimeError(
-                        "{} wants to connect when already connected!".format(HELLO, src)
-                    )
+                if data[1] == "fw at neighbor": # TODO: this is wrong
+                    # logging.critical(
+                    #     "{} wants to connect when already connected!".format(HELLO, src)
+                    # )
+                    # raise RuntimeError(
+                    #     "{} wants to connect when already connected!".format(HELLO, src)
+                    # )
+                    # can happen if neighbor is
+                    print("reconnect request from neighbor")
+                    logging.info("reconnect request from neighbor")
+                    return
                 else:
                     logging.info("fw arrived at a neighbour")
             else:
@@ -493,14 +497,14 @@ class TCPRandomWalkRoutingInternal(TCPRandomWalkBase):
                 )
                 if data["fuel"] > 0:
                     new_neighbor = self.rw_sampler()(data)
-                    if new_neighbor == None:
+                    if new_neighbor == None and src not in self.current_neighbors:
                         if src != self.uid:
                             logging.info(
                                 "RW message is delivered here due to no new neighbors being available: %s",
                                 str(data["visited"]),
                             )
                             # TODO: check if not already a neighbor
-                            #self.connect(new_data["routing_info"])
+                            self.connect(new_data["routing_info"])
                             return
                         else:
                             logging.info(
@@ -521,7 +525,7 @@ class TCPRandomWalkRoutingInternal(TCPRandomWalkBase):
                         return
                 else:
                     # the message has no more fuel so it is dropped
-                    if src != self.uid:
+                    if src != self.uid and src not in self.current_neighbors:
                         # TODO: keep track of all rw we send!
                         # TODO: what if it is our neighbour?
                         logging.info(
@@ -632,10 +636,18 @@ class TCPRandomWalkRoutingInternal(TCPRandomWalkBase):
                 )  # True -> we initiated
                 logging.info(f"Added {sender} to future neighbors")
                 del self.outgoing_request[sender]  # cannot advance until this is empty
-            elif round == self.current_round - 1:  # we are ahead
+            elif round < self.current_round:  # we are ahead
+                # TODO: might cause a blocking behaviour, if both start a connection to each other
+                if self.current_data != None:
+                    self.send(
+                        sender, self.current_data
+                    )  # messaging is tcp based --> always arrives after hello
+                self.current_neighbors.add(sender)
+                logging.info(f"Added {sender} to current neighbors")
+                del self.outgoing_request[sender]
                 # should never arrive, as other round should advance and then send to us with current round
-                logging.critical(f"Received a hello from {sender} with {round}")
-                raise RuntimeError(f"Received a hello from {sender} with {round}")
+                logging.info(f"Received a hello from {sender} with {round}")
+                # raise RuntimeError(f"Received a hello from {sender} with {round}")
             else:
                 logging.critical(f"Received a hello from {sender} with {round}")
                 raise RuntimeError(f"Received a hello from {sender} with {round}")
-- 
GitLab