From 67bf2f66dd46409d2f5c91f187d496aacadaeced Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Fri, 17 Jan 2014 19:24:15 +0100 Subject: StatsServer corrections --- src/StatsServer.cpp | 30 +++++++++++++++--------------- src/StatsServer.h | 15 ++++++++++++++- src/TestStatsServer.cpp | 2 ++ 3 files changed, 31 insertions(+), 16 deletions(-) (limited to 'src') diff --git a/src/StatsServer.cpp b/src/StatsServer.cpp index 4ae92d6..f870e14 100644 --- a/src/StatsServer.cpp +++ b/src/StatsServer.cpp @@ -105,14 +105,14 @@ std::string StatsServer::getConfigJSON() ss << "{ \"config\" : [\n"; std::map::iterator iter; - bool first = true; - for(iter = m_inputStats.begin(); iter != m_inputStats.end(); ++iter) + int i = 0; + for(iter = m_inputStats.begin(); iter != m_inputStats.end(); + ++iter, i++) { std::string id = iter->first; - if (! first) { + if (i > 0) { ss << ", "; - first = false; } ss << " \"" << id << "\" "; @@ -129,15 +129,15 @@ std::string StatsServer::getValuesJSON() ss << "{ \"values\" : {\n"; std::map::iterator iter; - bool first = true; - for(iter = m_inputStats.begin(); iter != m_inputStats.end(); ++iter) + int i = 0; + for(iter = m_inputStats.begin(); iter != m_inputStats.end(); + ++iter, i++) { const std::string& id = iter->first; InputStat& stats = iter->second; - if (! first) { + if (i > 0) { ss << " ,\n"; - first = false; } ss << " \"" << id << "\" : "; @@ -151,7 +151,7 @@ std::string StatsServer::getValuesJSON() } void StatsServer::serverThread() { - int sock, accepted_sock; + int accepted_sock; char buffer[256]; char welcome_msg[256]; struct sockaddr_in serv_addr, cli_addr; @@ -166,8 +166,8 @@ void StatsServer::serverThread() PACKAGE_NAME, PACKAGE_VERSION, GITVERSION); - sock = socket(AF_INET, SOCK_STREAM, 0); - if (sock < 0) { + m_sock = socket(AF_INET, SOCK_STREAM, 0); + if (m_sock < 0) { etiLog.level(error) << "Error opening Stats Server socket: " << strerror(errno); return; @@ -178,13 +178,13 @@ void StatsServer::serverThread() serv_addr.sin_addr.s_addr = INADDR_ANY; // TODO listen only on 127.0.0.1 serv_addr.sin_port = htons(m_listenport); - if (bind(sock, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) { + if (bind(m_sock, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) { etiLog.level(error) << "Error binding Stats Server socket: " << strerror(errno); goto end_serverthread; } - if (listen(sock, 5) < 0) { + if (listen(m_sock, 5) < 0) { etiLog.level(error) << "Error listening on Stats Server socket: " << strerror(errno); goto end_serverthread; @@ -195,7 +195,7 @@ void StatsServer::serverThread() socklen_t cli_addr_len = sizeof(cli_addr); /* Accept actual connection from the client */ - accepted_sock = accept(sock, (struct sockaddr *)&cli_addr, &cli_addr_len); + accepted_sock = accept(m_sock, (struct sockaddr *)&cli_addr, &cli_addr_len); if (accepted_sock < 0) { etiLog.level(warn) << "Stats Server cound not accept connection: " << strerror(errno); @@ -247,7 +247,7 @@ void StatsServer::serverThread() } end_serverthread: - close(sock); + close(m_sock); } diff --git a/src/StatsServer.h b/src/StatsServer.h index 76ca497..5bbf327 100644 --- a/src/StatsServer.h +++ b/src/StatsServer.h @@ -84,7 +84,18 @@ class StatsServer m_listenport(listen_port), m_running(true), m_thread(&StatsServer::serverThread, this) - {} + { + m_sock = 0; + } + + ~StatsServer() + { + m_running = false; + if (m_sock) { + close(m_sock); + } + m_thread.join(); + } void registerInput(std::string id); // The input notifies the StatsServer about a new buffer size @@ -106,6 +117,8 @@ class StatsServer bool m_running; boost::thread m_thread; + int m_sock; + /******* Statistics Data ********/ std::map m_inputStats; diff --git a/src/TestStatsServer.cpp b/src/TestStatsServer.cpp index 0d86195..d52becc 100644 --- a/src/TestStatsServer.cpp +++ b/src/TestStatsServer.cpp @@ -9,6 +9,8 @@ int main(int argc, char **argv) StatsServer serv(2720); serv.registerInput("foo"); + serv.registerInput("bar"); + while (true) { for (int i = 0; i < NUMOF(stats_example); i++) { usleep(400000); -- cgit v1.2.3