Changeset - f9a49506a1d0
[Not reviewed]
0 4 0
Jan Kaluza - 13 years ago 2012-08-29 10:39:58
hanzz.k@gmail.com
Starting/stopping instances from web-interface
4 files changed with 35 insertions and 1 deletions:
0 comments (0 inline, 0 general)
spectrum_manager/src/main.cpp
Show inline comments
 
@@ -176,16 +176,16 @@ int main(int argc, char **argv)
 
		float elapsed = 0; 
 
		gettimeofday(&td_start, NULL);
 
	
 
		time_t started = time(NULL);
 
		while(get_response().empty()) {
 
			eventLoop.runUntilEvents();
 
		}
 
		if (!get_response().empty()) {
 
			gettimeofday(&td_end, NULL);
 
			elapsed = 1000000.0 * (td_end.tv_sec -td_start.tv_sec); \
 
			elapsed += (td_end.tv_usec - td_start.tv_usec); \
 
			elapsed = elapsed / 1000 / 1000; \
 
			std::cout << "Response received after " << (elapsed) << " seconds\n";
 
// 			std::cout << "Response received after " << (elapsed) << " seconds\n";
 
		}
 
	}
 
}
spectrum_manager/src/methods.cpp
Show inline comments
 
@@ -167,28 +167,30 @@ void start_instances(ManagerConfig *config, const std::string &_jid) {
 
					Config vhostCfg;
 
					if (vhostCfg.load(itr->path().string(), vhost) == false) {
 
						std::cerr << "Can't load config file " << itr->path().string() << ". Skipping...\n";
 
						continue;
 
					}
 

	
 
					if (!_jid.empty() && _jid != vhost) {
 
						continue;
 
					}
 

	
 
					int pid = isRunning(CONFIG_STRING(&vhostCfg, "service.pidfile"));
 
					if (pid == 0) {
 
						response = "Starting " + itr->path().string() + ": OK\n";
 
						std::cout << "Starting " << itr->path() << ": OK\n";
 
						exec_(spectrum2_binary, itr->path().string(), vhost);
 
					}
 
					else {
 
						response = "Starting " + itr->path().string() + ": Already started (PID=" + boost::lexical_cast<std::string>(pid) + ")\n";
 
						std::cout << "Starting " << itr->path() << ": Already started (PID=" << pid << ")\n";
 
					}
 
				}
 
			}
 
		}
 
	}
 
	catch (const filesystem_error& ex) {
 
		std::cerr << "boost filesystem error\n";
 
		exit(5);
 
	}
 
}
 

	
 
@@ -223,42 +225,46 @@ void stop_instances(ManagerConfig *config, const std::string &_jid) {
 
					Config vhostCfg;
 
					if (vhostCfg.load(itr->path().string(), vhost) == false) {
 
						std::cerr << "Can't load config file " << itr->path().string() << ". Skipping...\n";
 
						continue;
 
					}
 

	
 
					if (!_jid.empty() && _jid != vhost) {
 
						continue;
 
					}
 

	
 
					int pid = isRunning(CONFIG_STRING(&vhostCfg, "service.pidfile"));
 
					if (pid) {
 
						response ="Stopping " + itr->path().string() + ": ";
 
						std::cout << "Stopping " << itr->path() << ": ";
 
						kill(pid, SIGTERM);
 

	
 
						sleep(1);
 
						int count = 20;
 
						while (kill(pid, 0) == 0 && count != 0) {
 
							std::cout << ".";
 
							sleep(1);
 
							count--;
 
						}
 
						if (count == 0) {
 
							response += "ERROR (timeout)\n";
 
							std::cout << " ERROR (timeout)\n";
 
						}
 
						else {
 
							response += "OK\n";
 
							std::cout << " OK\n";
 
						}
 
					}
 
					else {
 
						response = "Stopping " + itr->path().string() + ": Not running\n";
 
						std::cout << "Stopping " << itr->path() << ": Not running\n";
 
					}
 
				}
 
			}
 
		}
 
	}
 
	catch (const filesystem_error& ex) {
 
		std::cerr << "boost filesystem error\n";
 
		exit(5);
 
	}
 
}
 

	
spectrum_manager/src/server.cpp
Show inline comments
 
@@ -297,24 +297,46 @@ void Server::serve_login(struct mg_connection *conn, const struct mg_request_inf
 
      <form action=\"/authorize\">\
 
        Username: <input type=\"text\" name=\"user\"></input><br/>\
 
        Password: <input type=\"text\" name=\"password\"></input><br/>\
 
        <input type=\"submit\" value=\"Login\"></input>\
 
      </form>\
 
    </center>\
 
  </body>\
 
</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 += "<b>" + get_response() + "</b>";
 
	html += "</body></html>";
 
	print_html(conn, request_info, html);
 
}
 

	
 
void Server::serve_stop(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));
 

	
 
	stop_instances(m_config, jid);
 
	html += "<b>" + get_response() + "</b>";
 
	html += "</body></html>";
 
	print_html(conn, request_info, html);
 
}
 

	
 
void Server::serve_root(struct mg_connection *conn, const struct mg_request_info *request_info) {
 
	std::vector<std::string> list = show_list(m_config, false);
 
	std::string html= get_header() + "<h2>List of instances</h2><table><tr><th>JID<th>Status</th><th>Command</th></tr>";
 

	
 
	BOOST_FOREACH(std::string &instance, list) {
 
		html += "<tr>";
 
		html += "<td>" + instance + "</td>";
 
		Swift::SimpleEventLoop eventLoop;
 
		Swift::BoostNetworkFactories networkFactories(&eventLoop);
 

	
 
		ask_local_server(m_config, networkFactories, instance, "status");
 
		eventLoop.runUntilEvents();
 
@@ -339,24 +361,28 @@ void *Server::event_handler(enum mg_event event, struct mg_connection *conn) {
 
	const struct mg_request_info *request_info = mg_get_request_info(conn);
 
	void *processed = (void *) 0x1;
 

	
 
	if (event == MG_NEW_REQUEST) {
 
		if (!is_authorized(conn, request_info)) {
 
			redirect_to(conn, request_info, "/login");
 
		} else if (strcmp(request_info->uri, "/authorize") == 0) {
 
			authorize(conn, request_info);
 
		} else if (strcmp(request_info->uri, "/login") == 0) {
 
			serve_login(conn, request_info);
 
		} else if (strcmp(request_info->uri, "/") == 0) {
 
			serve_root(conn, request_info);
 
		} else if (strcmp(request_info->uri, "/start") == 0) {
 
			serve_start(conn, request_info);
 
		} else if (strcmp(request_info->uri, "/stop") == 0) {
 
			serve_stop(conn, request_info);
 
		} else {
 
			// No suitable handler found, mark as not processed. Mongoose will
 
			// try to serve the request.
 
			processed = NULL;
 
		}
 
	} else {
 
		processed = NULL;
 
	}
 

	
 
	return processed;
 
}
 

	
spectrum_manager/src/server.h
Show inline comments
 
@@ -44,24 +44,26 @@ class Server {
 
		Server(ManagerConfig *config);
 

	
 
		/// Destructor
 
		virtual ~Server();
 

	
 
		bool start(int port, const std::string &user, const std::string &password);
 

	
 
		void *event_handler(enum mg_event event, struct mg_connection *conn);
 

	
 
	private:
 
		void serve_login(struct mg_connection *conn, const struct mg_request_info *request_info);
 
		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 print_html(struct mg_connection *conn, const struct mg_request_info *request_info, const std::string &html);
 

	
 
	private:
 
		bool check_password(const char *user, const char *password);
 
		session *new_session(const char *user);
 
		session *get_session(const struct mg_connection *conn);
 

	
 
		void authorize(struct mg_connection *conn, const struct mg_request_info *request_info);
 

	
 
		bool is_authorized(const struct mg_connection *conn, const struct mg_request_info *request_info);
 

	
 
		void redirect_to(struct mg_connection *conn, const struct mg_request_info *request_info, const char *where);
0 comments (0 inline, 0 general)