From 99feeae89e59998b57dc7c579108093b00e8805f Mon Sep 17 00:00:00 2001
From: "mohamad.moussa" <mohamad.moussa@hesge.ch>
Date: Mon, 10 Jun 2024 14:47:05 +0200
Subject: [PATCH] Update sock.py

---
 Local-Without-Prediction/sock.py | 43 +++++++++++++++-----------------
 1 file changed, 20 insertions(+), 23 deletions(-)

diff --git a/Local-Without-Prediction/sock.py b/Local-Without-Prediction/sock.py
index 5795c20..12266d7 100644
--- a/Local-Without-Prediction/sock.py
+++ b/Local-Without-Prediction/sock.py
@@ -6,12 +6,16 @@ import select
 import os
 import logging
 
-NB_ITERATIONS = 20
-
 # Setup logging
 logging.basicConfig(level=logging.INFO, format='%(asctime)s %(levelname)s %(message)s', handlers=[logging.StreamHandler(sys.stdout)])
 logger = logging.getLogger(__name__)
 
+
+def send_Message(sock,src,dest,msg):
+    sock.connect((dest, base_port))
+    data = json.dumps((msg, src))
+    sock.send(data.encode())
+
 def mainProg(neighbors, rank, base_port, ip_address):
     c = 0
     iteration = 0
@@ -24,22 +28,18 @@ def mainProg(neighbors, rank, base_port, ip_address):
     time.sleep(10)
     logger.info(f"Server {rank} listening on {ip_address} and port {base_port}")
 
-    while iteration < NB_ITERATIONS:
-        if iteration < 3:
-            for neighbor in neighbors:
-                try:
-                    neighbor_ip = f"node{neighbor}"  # Use Docker service name
-                    client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
-                    client_socket.connect((neighbor_ip, base_port))
-                    message = "REQUEST_WEIGHT"
-                    data = json.dumps((message, rank))
-                    client_socket.send(data.encode())
-                    logger.info(f"Rank {rank} sent {message} to {neighbor} at iteration {iteration}")
-                    time.sleep(3)
-                    client_socket.close()
-                except ConnectionRefusedError as e:
-                    logger.error(f"Exception {e}\nConnection refused when {rank} tried to connect to {neighbor}'s service {neighbor_ip}.")
-                    continue
+    while True:
+        for neighbor in neighbors:
+            try:
+                neighbor_ip = f"node{neighbor}" 
+                client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+                send_Message(client_socket,rank,neighbor_ip,"REQUEST_WEIGHT")
+                logger.info(f"Rank {rank} sent REQUEST_WEIGHT to {neighbor} at iteration {iteration}")
+                time.sleep(3)
+                client_socket.close()
+            except ConnectionRefusedError as e:
+                logger.error(f"Exception {e}\nConnection refused when {rank} tried to connect to {neighbor}'s service {neighbor_ip}.")
+                continue
 
         logger.info(f"I am at -- iteration {iteration}")
 
@@ -53,12 +53,9 @@ def mainProg(neighbors, rank, base_port, ip_address):
             
             if message_rcvd == "REQUEST_WEIGHT":
                 try:
-                    sender_ip = f"node{sender}"  # Use Docker service name
+                    sender_ip = f"node{sender}" 
                     cl_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
-                    cl_socket.connect((sender_ip, base_port))
-                    message = "WEIGHTS"
-                    data = json.dumps((message, rank))
-                    cl_socket.send(data.encode())
+                    send_Message(cl_socket,rank,sender_ip,"WEIGHTS")
                     time.sleep(3)
                     cl_socket.close()
                 except ConnectionRefusedError as e:
-- 
GitLab