From 9413e8661d06790c770357b1323ef4388abee8b0 Mon Sep 17 00:00:00 2001 From: Thulinma Date: Thu, 2 Aug 2012 07:48:28 +0200 Subject: [PATCH] Fix controller and buffer conversion to libmist commandline parser. --- src/buffer.cpp | 26 ++++++++++---------------- src/controller.cpp | 13 ++----------- 2 files changed, 12 insertions(+), 27 deletions(-) diff --git a/src/buffer.cpp b/src/buffer.cpp index f2eb3d66..a3eb2626 100644 --- a/src/buffer.cpp +++ b/src/buffer.cpp @@ -12,6 +12,7 @@ #include #include #include +#include #include "buffer_stream.h" /// Holds all code unique to the Buffer. @@ -172,20 +173,12 @@ namespace Buffer{ /// Starts a loop, waiting for connections to send data to. int Start(int argc, char ** argv) { - //first make sure no segpipe signals will kill us - struct sigaction new_action; - new_action.sa_handler = termination_handler; - sigemptyset (&new_action.sa_mask); - new_action.sa_flags = 0; - sigaction (SIGPIPE, &new_action, NULL); - sigaction (SIGKILL, &new_action, NULL); - - //then check and parse the commandline - if (argc < 2) { - std::cout << "usage: " << argv[0] << " streamName [awaiting_IP]" << std::endl; - return 1; - } - std::string name = argv[1]; + Util::Config conf = Util::Config(argv[0], PACKAGE_VERSION); + conf.addOption("stream_name", JSON::fromString("{\"arg_num\":1, \"arg\":\"string\", \"help\":\"Name of the stream this buffer will be providing.\"}")); + conf.addOption("awaiting_ip", JSON::fromString("{\"arg_num\":2, \"arg\":\"string\", \"default\":\"\", \"help\":\"IP address to expect incoming data from. This will completely disable reading from standard input if used.\"}")); + conf.parseArgs(argc, argv); + + std::string name = conf.getString("stream_name"); SS = Socket::makeStream(name); if (!SS.connected()) { @@ -199,10 +192,11 @@ namespace Buffer{ tthread::thread StatsThread = tthread::thread(handleStats, 0); tthread::thread * StdinThread = 0; - if (argc < 3){ + std::string await_ip = conf.getString("awaiting_ip"); + if (await_ip == ""){ StdinThread = new tthread::thread(handleStdin, 0); }else{ - thisStream->setWaitingIP(argv[2]); + thisStream->setWaitingIP(await_ip); StdinThread = new tthread::thread(handlePushin, 0); } diff --git a/src/controller.cpp b/src/controller.cpp index 81651b23..0016351e 100644 --- a/src/controller.cpp +++ b/src/controller.cpp @@ -327,16 +327,6 @@ int main(int argc, char ** argv){ conf.addOption("account", JSON::fromString("{\"long\":\"account\", \"short\":\"a\", \"default\":\"\", \"help\":\"A username:password string to create a new account with.\"}")); conf.parseArgs(argc, argv); - //setup signal handler - struct sigaction new_action; - new_action.sa_handler = signal_handler; - sigemptyset (&new_action.sa_mask); - new_action.sa_flags = 0; - sigaction(SIGINT, &new_action, NULL); - sigaction(SIGHUP, &new_action, NULL); - sigaction(SIGTERM, &new_action, NULL); - sigaction(SIGPIPE, &new_action, NULL); - std::string account = conf.getString("account"); if (account.size() > 0){ size_t colon = account.find(':'); @@ -361,7 +351,7 @@ int main(int argc, char ** argv){ std::string jsonp; ConnectedUser * uplink = 0; Log("CONF", "Controller started"); - while (API_Socket.connected()){ + while (API_Socket.connected() && conf.is_active){ usleep(100000); //sleep for 100 ms - prevents 100% CPU time if (time(0) - processchecker > 10){ @@ -535,6 +525,7 @@ int main(int argc, char ** argv){ } } } + API_Socket.close(); Log("CONF", "Controller shutting down"); Util::Procs::StopAll(); WriteFile("config.json", Storage.toString());