Update.
authorFrançois Fleuret <francois@fleuret.org>
Mon, 28 Oct 2024 19:28:12 +0000 (20:28 +0100)
committerFrançois Fleuret <francois@fleuret.org>
Mon, 28 Oct 2024 19:28:12 +0000 (20:28 +0100)
distributed.py

index adaa36f..b5e98e9 100755 (executable)
@@ -69,26 +69,23 @@ class CultureServer:
 
             threading.Thread(
                 target=self.client_loop,
-                kwargs={
-                    "link": link,
-                    "nb": self.nb_accepts,
-                },
+                kwargs={"link": link, "client_nb": self.nb_accepts},
                 daemon=True,
             ).start()
 
             self.nb_accepts += 1
 
-    def client_loop(self, link, nb):
-        link.send(f"HELLO #{nb}")
+    def client_loop(self, link, client_nb):
+        link.send(f"HELLO #{client_nb}")
         try:
             while True:
                 r = link.receive()
-                print(f'from #{nb} receive "{r}"')
+                print(f'from #{client_nb} receive "{r}"')
                 link.send(f"ACK {r}")
                 if r == "BYE":
                     break
         except EOFError:
-            print(f"closing #{nb} on EOF")
+            print(f"closing #{client_nb} on EOF")
 
 
 ######################################################################
@@ -99,31 +96,33 @@ class CultureClient:
         server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
         server_socket.connect((server_hostname, port))
         self.link = SocketConnection(server_socket)
+        self.buffer = []
 
         threading.Thread(target=self.receive, daemon=True).start()
-
-        self.send()
-        # threading.Thread(target=self.send, daemon=True).start()
+        self.loop()
 
     def receive(self):
         try:
             while True:
-                x = self.link.receive()
-                print(f'CultureClient receive "{x}"')
+                self.buffer.append(self.link.receive())
         except EOFError:
-            print(f"closing connection on EOF")
+            print(f"** closing connection on EOF **")
 
-    def send(self):
+    def loop(self):
         try:
-            self.link.send(f"HELLO")
-            x = 0
             while True:
-                time.sleep(5)
-                print(f'CultureClient send "{x}"')
-                self.link.send(x)
-                x += 1
+                self.link.send(f"PING {time.localtime().tm_sec}")
+
+                try:
+                    while True:
+                        print(self.buffer.pop(0))
+                except IndexError:
+                    pass
+
+                time.sleep(1)
+
         except BrokenPipeError:
-            print(f"closing connection on broken pipe")
+            print(f"** closing connection on broken pipe **")
 
 
 ######################################################################