diff --git a/main.cpp b/main.cpp index 31ce17b..1f3f7e6 100644 --- a/main.cpp +++ b/main.cpp @@ -23,11 +23,94 @@ // OTHER DEALINGS IN THE SOFTWARE. // +#include #include "metacall.hpp" using namespace metacall; -int main(int argc, char **argv) { + +// +// Constants +// + +static const int SERVER_PORT = 1338; +static const int SERVER_MAX_CLIENTS = 1; + + +// +// Local variables +// + +static bool executeServer = true; + + +// +// Local functions (bound) +// + +static void serverStop() { + printf("[S] Server stop request received\n"); + executeServer = false; +} + + +// +// Local functions +// + +static void server() { + Server server; + + printf("[S] Starting server on port %d\n", SERVER_PORT); + if (!server.start(SERVER_PORT, SERVER_MAX_CLIENTS)) { + perror("[S] Unable to start server"); + return; + } + + printf("[S] Binding server functions\n"); + server.binding()->bind("serverStop", serverStop); + + printf("[S] Beginning client updates\n"); + while (executeServer) { + server.advance(); + } + + printf("[S] Server shutting down"); + server.stop(); +} + +static void client() { Client client; + + printf("[C] Connecting to server on port %d...\n", SERVER_PORT); + if (!client.connect("localhost", SERVER_PORT)) { + perror("[C] Unable to connect to server\n"); + return; + } + + printf("[C] Sending server shutdown request\n"); + client.protocol()->invoke("serverStop"); + + while (client.connected()) { + client.advance(); + } +} + + +// +// Program entry +// + +int main(int argc, char **argv) { + if (fork() == 0) { + sleep(1); + client(); + } + else { + server(); + printf("Press enter to exit...\n"); + getchar(); + } + return 0; }