diff --git a/spectrum_manager/src/server.cpp b/spectrum_manager/src/server.cpp index 28f499853cfff11d634c8877d46b4aab1fec7035..0bde682bedf39b15c0cbdc6d3450b0566d09e500 100644 --- a/spectrum_manager/src/server.cpp +++ b/spectrum_manager/src/server.cpp @@ -144,6 +144,8 @@ static void generate_session_id(char *buf, const char *random, Server::Server(ManagerConfig *config) { srand((unsigned) time(0)); m_config = config; + m_user = CONFIG_STRING(m_config, "service.admin_username"); + m_password = CONFIG_STRING(m_config, "service.admin_password"); } Server::~Server() { @@ -156,9 +158,7 @@ static void *_event_handler(enum mg_event event, struct mg_connection *conn) { return static_cast(request_info->user_data)->event_handler(event, conn); } -bool Server::start(int port, const std::string &user, const std::string &password) { - m_user = user; - m_password = password; +bool Server::start(int port) { const char *options[] = { "listening_ports", boost::lexical_cast(port).c_str(), "num_threads", "1", @@ -306,13 +306,69 @@ void Server::serve_login(struct mg_connection *conn, const struct mg_request_inf print_html(conn, request_info, html); } +void Server::serve_onlineusers(struct mg_connection *conn, const struct mg_request_info *request_info) { + std::string html = get_header(); + char jid[255]; + get_qsvar(request_info, "jid", jid, sizeof(jid)); + + html += std::string("

") + jid + " online users

"; + + Swift::SimpleEventLoop eventLoop; + Swift::BoostNetworkFactories networkFactories(&eventLoop); + + ask_local_server(m_config, networkFactories, jid, "online_users"); + eventLoop.runUntilEvents(); + while(get_response().empty()) { + eventLoop.runUntilEvents(); + } + + std::string response = get_response(); + std::vector users; + boost::split(users, response, boost::is_any_of("\n")); + + BOOST_FOREACH(std::string &user, users) { + html += ""; + } + + html += "
JIDCommand
" + user + "
Back to main page"; + html += ""; + print_html(conn, request_info, html); +} + +void Server::serve_cmd(struct mg_connection *conn, const struct mg_request_info *request_info) { + std::string html = get_header(); + char jid[255]; + get_qsvar(request_info, "jid", jid, sizeof(jid)); + char cmd[4096]; + get_qsvar(request_info, "cmd", cmd, sizeof(cmd)); + + html += std::string("

") + jid + " command result

"; + + Swift::SimpleEventLoop eventLoop; + Swift::BoostNetworkFactories networkFactories(&eventLoop); + + ask_local_server(m_config, networkFactories, jid, cmd); + while(get_response().empty()) { + eventLoop.runUntilEvents(); + } + + std::string response = get_response(); + + html += "
" + response + "
"; + + html += "Back to main page"; + html += ""; + print_html(conn, request_info, html); +} + + void Server::serve_start(struct mg_connection *conn, const struct mg_request_info *request_info) { std::string html= get_header() ; char jid[255]; get_qsvar(request_info, "jid", jid, sizeof(jid)); start_instances(m_config, jid); - html += "" + get_response() + ""; + html += "" + get_response() + "
Back to main page"; html += ""; print_html(conn, request_info, html); } @@ -323,18 +379,18 @@ void Server::serve_stop(struct mg_connection *conn, const struct mg_request_info get_qsvar(request_info, "jid", jid, sizeof(jid)); stop_instances(m_config, jid); - html += "" + get_response() + ""; + html += "" + get_response() + "
Back to main page"; html += ""; print_html(conn, request_info, html); } void Server::serve_root(struct mg_connection *conn, const struct mg_request_info *request_info) { std::vector list = show_list(m_config, false); - std::string html= get_header() + "

List of instances

"; + std::string html= get_header() + "

List of instances

JIDStatusCommand
"; BOOST_FOREACH(std::string &instance, list) { html += ""; - html += ""; + html += ""; Swift::SimpleEventLoop eventLoop; Swift::BoostNetworkFactories networkFactories(&eventLoop); @@ -346,10 +402,17 @@ void Server::serve_root(struct mg_connection *conn, const struct mg_request_info html += ""; if (get_response().find("Running") == 0) { html += ""; + html += ""; } else { html += ""; + html += ""; } + html += ""; } @@ -370,6 +433,10 @@ void *Server::event_handler(enum mg_event event, struct mg_connection *conn) { serve_login(conn, request_info); } else if (strcmp(request_info->uri, "/") == 0) { serve_root(conn, request_info); + } else if (strcmp(request_info->uri, "/onlineusers") == 0) { + serve_onlineusers(conn, request_info); + } else if (strcmp(request_info->uri, "/cmd") == 0) { + serve_cmd(conn, request_info); } else if (strcmp(request_info->uri, "/start") == 0) { serve_start(conn, request_info); } else if (strcmp(request_info->uri, "/stop") == 0) {
JIDStatusCommandRun command
" + instance + "" + instance + "" + get_response() + "Stop
"; + html += ""; + html += ""; + html += ""; + html += "
Start