diff options
Diffstat (limited to 'src/ManagementServer.cpp')
-rw-r--r-- | src/ManagementServer.cpp | 87 |
1 files changed, 76 insertions, 11 deletions
diff --git a/src/ManagementServer.cpp b/src/ManagementServer.cpp index ae3a0cb..78f1c9d 100644 --- a/src/ManagementServer.cpp +++ b/src/ManagementServer.cpp @@ -47,7 +47,7 @@ void ManagementServer::registerInput(InputStat* is) if (m_inputStats.count(id) == 1) { etiLog.level(error) << - "Double registration in Stats Server with id '" << + "Double registration in MGMT Server with id '" << id << "'"; return; } @@ -196,7 +196,7 @@ void ManagementServer::serverThread() int welcome_msg_len = snprintf(welcome_msg, 256, "{ \"service\": \"" - "%s %s Stats Server\" }\n", + "%s %s MGMT Server\" }\n", PACKAGE_NAME, #if defined(GITVERSION) GITVERSION @@ -208,7 +208,8 @@ void ManagementServer::serverThread() m_sock = socket(AF_INET, SOCK_STREAM, 0); if (m_sock < 0) { - etiLog.level(error) << "Error opening Stats Server socket: " << + etiLog.level(error) << + "Error opening MGMT Server socket: " << strerror(errno); m_fault = true; return; @@ -220,13 +221,15 @@ void ManagementServer::serverThread() serv_addr.sin_port = htons(m_listenport); if (bind(m_sock, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) { - etiLog.level(error) << "Error binding Stats Server socket: " << + etiLog.level(error) << + "Error binding MGMT Server socket: " << strerror(errno); goto end_serverthread; } if (listen(m_sock, 5) < 0) { - etiLog.level(error) << "Error listening on Stats Server socket: " << + etiLog.level(error) << + "Error listening on MGMT Server socket: " << strerror(errno); goto end_serverthread; } @@ -242,14 +245,15 @@ void ManagementServer::serverThread() &cli_addr_len); if (accepted_sock < 0) { - etiLog.level(warn) << "Stats Server cound not accept connection: " << + etiLog.level(warn) << "MGMT Server cound not accept connection: " << strerror(errno); continue; } /* Send welcome message with version */ n = write(accepted_sock, welcome_msg, welcome_msg_len); if (n < 0) { - etiLog.level(warn) << "Error writing to Stats Server socket " << + etiLog.level(warn) << + "MGMT: Error writing to Stats Server socket " << strerror(errno); close(accepted_sock); continue; @@ -259,7 +263,8 @@ void ManagementServer::serverThread() memset(buffer, 0, 256); int n = read(accepted_sock, buffer, 255); if (n < 0) { - etiLog.level(warn) << "Error reading from Stats Server socket " << + etiLog.level(warn) << + "MGMT: Error reading from Stats Server socket " << strerror(errno); close(accepted_sock); continue; @@ -277,11 +282,41 @@ void ManagementServer::serverThread() std::string json = getStateJSON(); n = write(accepted_sock, json.c_str(), json.size()); } + else if (strcmp(buffer, "setptree\n") == 0) { + const ssize_t max_json_len = 32768; + char json[max_json_len] = {'\0'}; + + ssize_t json_len = read(accepted_sock, json, max_json_len); + + if (json_len < max_json_len) { + boost::unique_lock<boost::mutex> lock(m_configmutex); + + std::stringstream ss; + ss << json; + + m_pt.clear(); + boost::property_tree::json_parser::read_json(ss, m_pt); + } + else if (json_len == 0) { + etiLog.level(warn) << + "MGMT: No JSON data received"; + } + else if (json_len < 0) { + etiLog.level(warn) << + "MGMT: JSON data receive error: " << + strerror(errno); + } + else { + etiLog.level(warn) << + "MGMT: Received JSON too large"; + } + + } else if (strcmp(buffer, "getptree\n") == 0) { boost::unique_lock<boost::mutex> lock(m_configmutex); m_pending = true; - while (m_pending) { + while (m_pending && !m_retrieve_pending) { m_condition.wait(lock); } std::stringstream ss; @@ -297,7 +332,8 @@ void ManagementServer::serverThread() } if (n < 0) { - etiLog.level(warn) << "Error writing to Stats Server socket " << + etiLog.level(warn) << + "Error writing to MGMT Server socket " << strerror(errno); } close(accepted_sock); @@ -309,11 +345,40 @@ end_serverthread: } catch (std::exception& e) { - etiLog.level(error) << "Statistics server caught exception: " << e.what(); + etiLog.level(error) << + "MGMT server caught exception: " << + e.what(); m_fault = true; } } +bool ManagementServer::retrieve_new_ptree(boost::property_tree::ptree& pt) +{ + boost::unique_lock<boost::mutex> lock(m_configmutex); + + if (m_retrieve_pending) + { + pt = m_pt; + + m_retrieve_pending = false; + m_condition.notify_one(); + return true; + } + + return false; +} + +void ManagementServer::update_ptree(const boost::property_tree::ptree& pt) +{ + if (m_running) { + boost::unique_lock<boost::mutex> lock(m_configmutex); + m_pt = pt; + m_pending = false; + + m_condition.notify_one(); + } +} + /************************************************/ void InputStat::registerAtServer() |