Changeset - 91fae24a0978
[Not reviewed]
0 5 0
Jan Kaluza - 10 years ago 2016-01-20 17:48:17
jkaluza@redhat.com
Web interface: working register/unregister
5 files changed with 70 insertions and 3 deletions:
0 comments (0 inline, 0 general)
spectrum_manager/src/APIServer.cpp
Show inline comments
 
@@ -179,6 +179,49 @@ void APIServer::serve_instances_unregister(Server *server, Server::session *sess
 
	}
 
}
 

	
 
void APIServer::serve_instances_register(Server *server, Server::session *session, struct mg_connection *conn, struct http_message *hm) {
 
	std::string uri(hm->uri.p, hm->uri.len);
 
	std::string instance = uri.substr(uri.rfind("/") + 1);
 

	
 
	UserInfo info;
 
	m_storage->getUser(session->user, info);
 

	
 
	std::string username = "";
 
	int type = (int) TYPE_STRING;
 
	m_storage->getUserSetting(info.id, instance, type, username);
 

	
 
	std::string jid = get_http_var(hm, "jid");
 
	std::string uin = get_http_var(hm, "uin");
 
	std::string password = get_http_var(hm, "password");
 

	
 
	if (jid.empty() || uin.empty() || password.empty()) {
 
		send_ack(conn, true, "Insufficient data.");
 
	}
 
	else {
 
		std::string response = server->send_command(instance, "register " + jid + " " + uin + " " + password);
 
		if (!response.empty()) {
 
			std::string value = jid;
 
			int type = (int) TYPE_STRING;
 
			m_storage->updateUserSetting(info.id, instance, value);
 
		}
 
		else {
 
			send_ack(conn, true, response);
 
			return;
 
		}
 

	
 
		Document json;
 
		json.SetObject();
 
		json.AddMember("error", false, json.GetAllocator());
 

	
 
		response = server->send_command(instance, "get_oauth2_url " + jid);
 
		if (!response.empty()) {
 
			json.AddMember("oauth2_url", response.c_str(), json.GetAllocator());
 
		}
 

	
 
		send_json(conn, json);
 
	}
 
}
 

	
 
void APIServer::serve_instances_register_form(Server *server, Server::session *session, struct mg_connection *conn, struct http_message *hm) {
 
	std::string uri(hm->uri.p, hm->uri.len);
 
	std::string instance = uri.substr(uri.rfind("/") + 1);
 
@@ -211,11 +254,14 @@ void APIServer::handleRequest(Server *server, Server::session *sess, struct mg_c
 
		serve_instances_stop(server, sess, conn, hm);
 
	}
 
	else if (has_prefix(&hm->uri, "/api/v1/instances/unregister/")) {
 
		serve_instances_stop(server, sess, conn, hm);
 
		serve_instances_unregister(server, sess, conn, hm);
 
	}
 
	else if (has_prefix(&hm->uri, "/api/v1/instances/register_form/")) {
 
		serve_instances_register_form(server, sess, conn, hm);
 
	}
 
	else if (has_prefix(&hm->uri, "/api/v1/instances/register/")) {
 
		serve_instances_register(server, sess, conn, hm);
 
	}
 
	else if (mg_vcmp(&hm->uri, "/api/v1/instances") == 0) {
 
		serve_instances(server, sess, conn, hm);
 
	}
spectrum_manager/src/APIServer.h
Show inline comments
 
@@ -56,6 +56,7 @@ class APIServer {
 
		void serve_instances_start(Server *server, Server::session *sess, struct mg_connection *conn, struct http_message *hm);
 
		void serve_instances_stop(Server *server, Server::session *sess, struct mg_connection *conn, struct http_message *hm);
 
		void serve_instances_unregister(Server *server, Server::session *sess, struct mg_connection *conn, struct http_message *hm);
 
		void serve_instances_register(Server *server, Server::session *sess, struct mg_connection *conn, struct http_message *hm);
 
		void serve_instances_register_form(Server *server, Server::session *sess, struct mg_connection *conn, struct http_message *hm);
 
		void send_json(struct mg_connection *conn, const Document &d);
 
		void send_ack(struct mg_connection *conn, bool error, const std::string &message);
spectrum_manager/src/html/instances/register.shtml
Show inline comments
 
@@ -18,7 +18,7 @@
 
</label> 
 
<label> 
 
	<span>&nbsp;</span> 
 
	<input type="submit" class="button" value="Register" />
 
	<input type="submit" class="button_command" value="Register" />
 
</label> 
 
<input type="hidden" name="instance" id="instance" value=""></input> 
 
</form><br/>
spectrum_manager/src/html/js/app.js
Show inline comments
 
@@ -67,6 +67,21 @@ function getQueryParams(qs) {
 
function fill_instances_register_form() {
 
	var query = getQueryParams(document.location.search);
 
	$("#instance").attr("value", query.id);
 

	
 
	$(".button_command").click(function(e) {
 
		e.preventDefault();
 
		$(this).parent().empty().progressbar( {value: false} ).css('height', '1em');
 

	
 
		var postdata ={
 
			"jid": $("#jid").val(),
 
			"uin": $("#uin").val(),
 
			"password": $("#password").val()
 
		};
 

	
 
		$.post($.cookie("base_location") + "api/v1/instances/register/" + $("#instance").val(), postdata, function(data) {
 
			window.location.replace("index.shtml");
 
		});
 
	})
 
	
 
	$.get($.cookie("base_location") + "api/v1/instances/register_form/" + query.id, function(data) {
 
		$("#jid_desc").html(data.username_label + ":");
spectrum_manager/src/server.cpp
Show inline comments
 
@@ -292,7 +292,12 @@ std::string Server::send_command(const std::string &jid, const std::string &cmd)
 
		eventLoop.runOnce();
 
	}
 

	
 
	return get_response();
 
	std::string response = get_response();
 
	if (response == "Empty response") {
 
		return "";
 
	}
 

	
 
	return response;
 
}
 

	
 
void Server::serve_onlineusers(struct mg_connection *conn, struct http_message *hm) {
0 comments (0 inline, 0 general)