Factor serializing into its own function. Again, less stuff in main().
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Wed, 10 Apr 2013 22:21:09 +0000 (00:21 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Wed, 10 Apr 2013 22:21:09 +0000 (00:21 +0200)
main.cpp

index de3900b9ac6acc676143e9ae019099b77c9175e7..1979bb3ca8ca50b10e1a8b203cb68c0c57870c8e 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -68,6 +68,38 @@ int make_tempfile(const CubemapStateProto &state)
        return state_fd;
 }
 
        return state_fd;
 }
 
+CubemapStateProto collect_state(const timeval &serialize_start,
+                                int server_sock,
+                                int port,
+                                const vector<Input *> inputs,
+                                ServerPool *servers,
+                                int num_servers)
+{
+       CubemapStateProto state;
+       state.set_serialize_start_sec(serialize_start.tv_sec);
+       state.set_serialize_start_usec(serialize_start.tv_usec);
+       state.set_server_sock(server_sock);
+       state.set_port(port);
+
+       for (size_t i = 0; i < inputs.size(); ++i) {
+               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;
+}
+
 // Read the state back from the file descriptor made by make_tempfile,
 // and close it.
 CubemapStateProto read_tempfile(int state_fd)
 // Read the state back from the file descriptor made by make_tempfile,
 // and close it.
 CubemapStateProto read_tempfile(int state_fd)
@@ -361,36 +393,18 @@ int main(int argc, char **argv)
                stats_thread->stop();
        }
        acceptor_thread.stop();
                stats_thread->stop();
        }
        acceptor_thread.stop();
-
-       CubemapStateProto state;
-       state.set_serialize_start_sec(serialize_start.tv_sec);
-       state.set_serialize_start_usec(serialize_start.tv_usec);
-       state.set_server_sock(server_sock);
-       state.set_port(port);
-
        for (size_t i = 0; i < inputs.size(); ++i) {
                inputs[i]->stop();
        for (size_t i = 0; i < inputs.size(); ++i) {
                inputs[i]->stop();
-               state.add_inputs()->MergeFrom(inputs[i]->serialize());
        }
        }
-
        for (int i = 0; i < num_servers; ++i) { 
                servers->get_server(i)->stop();
        for (int i = 0; i < num_servers; ++i) { 
                servers->get_server(i)->stop();
-
-               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));
-               }
        }
        }
-       delete servers;
 
        fprintf(stderr, "Serializing state and re-execing...\n");
 
        fprintf(stderr, "Serializing state and re-execing...\n");
-       int state_fd = make_tempfile(state);
-
+       int state_fd = make_tempfile(collect_state(
+               serialize_start, server_sock, port, inputs, servers, num_servers));
+       delete servers;
+        
        char buf[16];
        sprintf(buf, "%d", state_fd);
 
        char buf[16];
        sprintf(buf, "%d", state_fd);