Move some serialization logic into ServerPool, where it belongs.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Fri, 12 Apr 2013 20:31:05 +0000 (22:31 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Fri, 12 Apr 2013 20:31:05 +0000 (22:31 +0200)
main.cpp
serverpool.cpp
serverpool.h

index c5640e0..eed486e 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -73,10 +73,9 @@ int make_tempfile(const CubemapStateProto &state)
 CubemapStateProto collect_state(const timeval &serialize_start,
                                 const vector<Acceptor *> acceptors,
                                 const vector<Input *> inputs,
-                                ServerPool *servers,
-                                int num_servers)
+                                ServerPool *servers)
 {
-       CubemapStateProto state;
+       CubemapStateProto state = servers->serialize();  // Fills streams() and clients().
        state.set_serialize_start_sec(serialize_start.tv_sec);
        state.set_serialize_start_usec(serialize_start.tv_usec);
        
@@ -88,18 +87,6 @@ CubemapStateProto collect_state(const timeval &serialize_start,
                state.add_inputs()->MergeFrom(inputs[i]->serialize());
        }
 
-       for (int i = 0; i < num_servers; ++i) { 
-               CubemapStateProto local_state = servers->get_server(i)->serialize();
-
-               // The stream state should be identical between the servers, so we only store it once.
-               if (i == 0) {
-                       state.mutable_streams()->MergeFrom(local_state.streams());
-               }
-               for (int j = 0; j < local_state.clients_size(); ++j) {
-                       state.add_clients()->MergeFrom(local_state.clients(j));
-               }
-       }
-
        return state;
 }
 
@@ -375,7 +362,7 @@ int main(int argc, char **argv)
 
        fprintf(stderr, "Serializing state and re-execing...\n");
        int state_fd = make_tempfile(collect_state(
-               serialize_start, acceptors, inputs, servers, config.num_servers));
+               serialize_start, acceptors, inputs, servers));
        delete servers;
         
        char buf[16];
index cf6933c..9f1a728 100644 (file)
@@ -1,4 +1,5 @@
 #include "serverpool.h"
+#include "state.pb.h"
 
 using namespace std;
 
@@ -13,6 +14,25 @@ ServerPool::~ServerPool()
 {
        delete[] servers;
 }
+       
+CubemapStateProto ServerPool::serialize()
+{
+       CubemapStateProto state;
+
+       for (int i = 0; i < num_servers; ++i) {
+                CubemapStateProto local_state = servers[i].serialize();
+
+               // The stream state should be identical between the servers, so we only store it once.
+               if (i == 0) {
+                       state.mutable_streams()->MergeFrom(local_state.streams());
+               }
+               for (int j = 0; j < local_state.clients_size(); ++j) {
+                       state.add_clients()->MergeFrom(local_state.clients(j));
+               }
+        }
+
+       return state;
+}
 
 void ServerPool::add_client(int sock)
 {
index 5dee5f7..559f76a 100644 (file)
@@ -13,9 +13,8 @@ public:
        ServerPool(int num_servers);
        ~ServerPool();
 
-       // Accessor. Only to be used in rare situations, really.
-       // The ServerPool retains ownership.
-       Server *get_server(int num) { return &servers[num]; }
+       // Fills streams() and clients().
+       CubemapStateProto serialize();
 
        // Picks a server (round-robin) and allocates the given client to it.
        void add_client(int sock);