From cb1e126ab60e86cfc5e154ff790da6c07b001be7 Mon Sep 17 00:00:00 2001 From: Jan Kaluza Date: Wed, 29 Aug 2012 15:03:21 +0200 Subject: [PATCH] Fixed login when client answers with invalid disco#info. Web-interface tweaks --- spectrum_manager/src/main.cpp | 2 +- spectrum_manager/src/methods.cpp | 7 ++- spectrum_manager/src/server.cpp | 81 +++++++++++++++++++++++++++++--- spectrum_manager/src/server.h | 4 +- src/user.cpp | 5 +- 5 files changed, 88 insertions(+), 11 deletions(-) diff --git a/spectrum_manager/src/main.cpp b/spectrum_manager/src/main.cpp index ee1a7997..ac816e20 100644 --- a/spectrum_manager/src/main.cpp +++ b/spectrum_manager/src/main.cpp @@ -139,7 +139,7 @@ int main(int argc, char **argv) } else if (command[0] == "server") { Server server(&config); - server.start(8080, "test", "test"); + server.start(8080); while (1) { sleep(10); } } else { diff --git a/spectrum_manager/src/methods.cpp b/spectrum_manager/src/methods.cpp index aa133f6b..d3197739 100644 --- a/spectrum_manager/src/methods.cpp +++ b/spectrum_manager/src/methods.cpp @@ -129,6 +129,7 @@ int isRunning(const std::string &pidfile) { } void start_instances(ManagerConfig *config, const std::string &_jid) { + response = ""; path p(CONFIG_STRING(config, "service.config_directory")); try { @@ -195,6 +196,7 @@ void start_instances(ManagerConfig *config, const std::string &_jid) { } void stop_instances(ManagerConfig *config, const std::string &_jid) { + response = ""; path p(CONFIG_STRING(config, "service.config_directory")); try { @@ -361,6 +363,7 @@ static void handleDataRead(boost::shared_ptr m_conn, boost::s continue; } m_conn->onDataRead.disconnect(boost::bind(&handleDataRead, m_conn, _1)); + m_conn->disconnect(); response = payload.config(); std::cout << payload.config() << "\n"; // exit(0); @@ -396,6 +399,7 @@ static void handleConnected(boost::shared_ptr m_conn, const s } void ask_local_server(ManagerConfig *config, Swift::BoostNetworkFactories &networkFactories, const std::string &jid, const std::string &message) { + response = ""; path p(CONFIG_STRING(config, "service.config_directory")); try { @@ -444,8 +448,9 @@ void ask_local_server(ManagerConfig *config, Swift::BoostNetworkFactories &netwo } if (!found) { + response = "Config file for Spectrum instance with this JID was not found\n"; std::cerr << "Config file for Spectrum instance with this JID was not found\n"; - exit(20); +// exit(20); } } catch (const filesystem_error& ex) { diff --git a/spectrum_manager/src/server.cpp b/spectrum_manager/src/server.cpp index 28f49985..0bde682b 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) { diff --git a/spectrum_manager/src/server.h b/spectrum_manager/src/server.h index 1fc90b7d..94c42da5 100644 --- a/spectrum_manager/src/server.h +++ b/spectrum_manager/src/server.h @@ -46,7 +46,7 @@ class Server { /// Destructor virtual ~Server(); - bool start(int port, const std::string &user, const std::string &password); + bool start(int port); void *event_handler(enum mg_event event, struct mg_connection *conn); @@ -55,6 +55,8 @@ class Server { void serve_root(struct mg_connection *conn, const struct mg_request_info *request_info); void serve_start(struct mg_connection *conn, const struct mg_request_info *request_info); void serve_stop(struct mg_connection *conn, const struct mg_request_info *request_info); + void serve_onlineusers(struct mg_connection *conn, const struct mg_request_info *request_info); + void serve_cmd(struct mg_connection *conn, const struct mg_request_info *request_info); void print_html(struct mg_connection *conn, const struct mg_request_info *request_info, const std::string &html); private: diff --git a/src/user.cpp b/src/user.cpp index fff62110..3d3cb06d 100644 --- a/src/user.cpp +++ b/src/user.cpp @@ -58,7 +58,7 @@ User::User(const Swift::JID &jid, UserInfo &userInfo, Component *component, User m_resources = 0; m_reconnectCounter = 0; - m_reconnectTimer = m_component->getNetworkFactories()->getTimerFactory()->createTimer(10000); + m_reconnectTimer = m_component->getNetworkFactories()->getTimerFactory()->createTimer(5000); m_reconnectTimer->onTick.connect(boost::bind(&User::onConnectingTimeout, this)); m_rosterManager = new RosterManager(this, m_component); @@ -199,6 +199,9 @@ void User::handlePresence(Swift::Presence::ref presence) { m_readyForConnect = true; onReadyToConnect(); } + else { + m_reconnectTimer->start(); + } } else if (m_component->inServerMode()) { LOG4CXX_INFO(logger, m_jid.toString() << ": Ready to be connected to legacy network");
JIDStatusCommandRun command
" + instance + "" + instance + "" + get_response() + "Stop
"; + html += ""; + html += ""; + html += ""; + html += "
Start