From de3e25eaded54a89be12a17c3e90916acf70a1dc Mon Sep 17 00:00:00 2001
From: KyleKlenk <kyle.c.klenk@gmail.com>
Date: Fri, 7 Oct 2022 19:03:24 +0000
Subject: [PATCH] Clients now exit when all batches are solved

---
 build/includes/summa_actor/client.hpp         |  6 ++++-
 build/source/actors/summa_actor/client.cpp    | 10 ++++++++
 .../actors/summa_actor/summa_server.cpp       | 23 +++++++++++++++++--
 3 files changed, 36 insertions(+), 3 deletions(-)

diff --git a/build/includes/summa_actor/client.hpp b/build/includes/summa_actor/client.hpp
index 7b7e949..fc413a5 100644
--- a/build/includes/summa_actor/client.hpp
+++ b/build/includes/summa_actor/client.hpp
@@ -50,8 +50,12 @@ class Client_Container {
          */
         void addClient(caf::actor client_actor, std::string hostname);
 
-        
         int getClientID(caf::actor);
 
+        Client removeClient_fromBack();
+
         std::string getHostname_ByClientID(int client_id);
+
+        bool isEmpty();
+
 };
\ No newline at end of file
diff --git a/build/source/actors/summa_actor/client.cpp b/build/source/actors/summa_actor/client.cpp
index ecf776b..ac926c7 100644
--- a/build/source/actors/summa_actor/client.cpp
+++ b/build/source/actors/summa_actor/client.cpp
@@ -48,3 +48,13 @@ int Client_Container::getClientID(caf::actor client_actor) {
 std::string Client_Container::getHostname_ByClientID(int client_id) {
     return this->client_list[client_id].getHostname();
 }
+
+bool Client_Container::isEmpty() {
+    return this->client_list.empty();
+}
+
+Client Client_Container::removeClient_fromBack() {
+    Client client = this->client_list.back();
+    this->client_list.pop_back();
+    return client;
+}
diff --git a/build/source/actors/summa_actor/summa_server.cpp b/build/source/actors/summa_actor/summa_server.cpp
index 98bf019..7130168 100644
--- a/build/source/actors/summa_actor/summa_server.cpp
+++ b/build/source/actors/summa_actor/summa_server.cpp
@@ -67,11 +67,30 @@ behavior summa_server(stateful_actor<summa_server_state>* self, Distributed_Sett
 
             std::optional<Batch> new_batch = self->state.batch_container->assignBatch(
                 self->state.client_container->getHostname_ByClientID(client_id), client_actor);
+            
             if (new_batch.has_value()) {
+                
                 self->send(client_actor, new_batch.value());
+            
             } else {
-                aout(self) << "no more batches left to assign\n";
-                aout(self) << "we are not done yet. Clients could Fail\n";
+                
+                if (self->state.batch_container->getBatchesRemaining() > 0) {
+                    
+                    aout(self) << "no more batches left to assign\n";
+                    aout(self) << "we are not done yet. Clients could Fail\n";
+
+                } else {
+                    aout(self) << "Telling Clients To Exit\n"; 
+                    while(!self->state.client_container->isEmpty()) {
+                        Client client = self->state.client_container->removeClient_fromBack();
+
+                        caf::actor client_actor =  client.getActor();
+                        self->send(client_actor, time_to_exit_v);
+                    }
+
+                    aout(self) << "SERVER EXITING!!\n";
+                    self->quit();
+                }
             }
         }
     };
-- 
GitLab