Changeset - c4eda8c23b75
[Not reviewed]
0 3 0
Jan Kaluza - 10 years ago 2016-02-01 18:18:28
jkaluza@redhat.com
Web interface: Use [identity] name instead of hostname when set
3 files changed with 17 insertions and 7 deletions:
0 comments (0 inline, 0 general)
spectrum_manager/src/APIServer.cpp
Show inline comments
 
@@ -49,122 +49,132 @@ APIServer::APIServer(ManagerConfig *config, StorageBackend *storage) {
 
}
 

	
 
APIServer::~APIServer() {
 
}
 

	
 
void APIServer::send_json(struct mg_connection *conn, const Document &d) {
 
	StringBuffer buffer;
 
	Writer<StringBuffer> writer(buffer);
 
	d.Accept(writer);
 
	std::string json(buffer.GetString());
 

	
 
	std::cout << "Sending JSON:\n";
 
	std::cout << json << "\n";
 
	mg_printf(conn,
 
			"HTTP/1.1 200 OK\r\n"
 
			"Content-Type: text/json\r\n"
 
			"Content-Length: %d\r\n"
 
			"\r\n"
 
			"%s",
 
			(int) json.size(), json.c_str());
 
}
 

	
 
void APIServer::send_ack(struct mg_connection *conn, bool error, const std::string &message) {
 
	Document json;
 
	json.SetObject();
 
	json.AddMember("error", error, json.GetAllocator());
 
	json.AddMember("message", message.c_str(), json.GetAllocator());
 

	
 
	send_json(conn, json);
 
}
 

	
 
void APIServer::serve_instances(Server *server, Server::session *session, struct mg_connection *conn, struct http_message *hm) {
 
	// rapidjson stores const char* pointer to status, so we have to keep
 
	// the std::string stored out of BOOST_FOREACH scope, otherwise the
 
	// const char * returned by c_str() would be invalid during send_json.
 
	std::vector<std::string> statuses;
 
	std::vector<std::string> usernames;
 
	std::vector<std::string> list = show_list(m_config, false);
 

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

	
 
	Value instances(kArrayType);
 
	BOOST_FOREACH(std::string &id, list) {
 
		Value instance;
 
		instance.SetObject();
 
		instance.AddMember("id", id.c_str(), json.GetAllocator());
 
		instance.AddMember("name", id.c_str(), json.GetAllocator());
 

	
 
		std::string name = get_config(m_config, id, "identity.name");
 
		if (name.empty() || name == "Spectrum 2 Transport") {
 
			instance.AddMember("name", id.c_str(), json.GetAllocator());
 
		}
 
		else {
 
			statuses.push_back(name);
 
			instance.AddMember("name", statuses.back().c_str(), json.GetAllocator());
 
		}
 

	
 
		std::string status = server->send_command(id, "status");
 
		if (status.empty()) {
 
			status = "Cannot get the instance status.";
 
		}
 

	
 
		statuses.push_back(status);
 
		instance.AddMember("status", statuses.back().c_str(), json.GetAllocator());
 

	
 
		bool running = true;
 
		if (status.find("Running") == std::string::npos) {
 
			running = false;
 
		}
 
		instance.AddMember("running", running, json.GetAllocator());
 

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

	
 
		usernames.push_back(username);
 
		instance.AddMember("registered", !username.empty(), json.GetAllocator());
 
		instance.AddMember("username", usernames.back().c_str(), json.GetAllocator());
 
		instance.AddMember("frontend", is_slack(m_config, id) ? "slack" : "xmpp", json.GetAllocator());
 

	
 
		usernames.push_back(get_config(m_config, id, "service.frontend"));
 
		instance.AddMember("frontend", usernames.back().c_str(), json.GetAllocator());
 

	
 
		instances.PushBack(instance, json.GetAllocator());
 
	}
 

	
 
	json.AddMember("instances", instances, json.GetAllocator());
 
	send_json(conn, json);
 
}
 

	
 
void APIServer::serve_instances_list_rooms(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);
 

	
 
	if (username.empty()) {
 
		send_ack(conn, true, "You are not registered to this Spectrum 2 instance.");
 
		return;
 
	}
 

	
 
	std::string response = server->send_command(instance, "list_rooms " + username);
 

	
 
	std::vector<std::string> commands;
 
	boost::split(commands, response, boost::is_any_of("\n"));
 

	
 
	Document json;
 
	json.SetObject();
 
	json.AddMember("error", 0, json.GetAllocator());
 
	json.AddMember("name_label", "Nickname in 3rd-party room", json.GetAllocator());
 
	json.AddMember("legacy_room_label", "3rd-party room name", json.GetAllocator());
 
	json.AddMember("legacy_server_label", "3rd-party server", json.GetAllocator());
 
	json.AddMember("frontend_room_label", "Slack channel", json.GetAllocator());
 

	
 
	std::vector<std::vector<std::string> > tmp;
 
	Value rooms(kArrayType);
 
	BOOST_FOREACH(const std::string &command, commands) {
 
		if (command.size() > 5) {
 
			std::vector<std::string> args2;
 
			boost::split(args2, command, boost::is_any_of(" "));
 
			if (args2.size() == 6) {
 
				tmp.push_back(args2);
 
				Value room;
 
				room.SetObject();
 
				room.AddMember("name", tmp.back()[2].c_str(), json.GetAllocator());
spectrum_manager/src/methods.cpp
Show inline comments
 
@@ -456,133 +456,133 @@ static void handleDataRead(boost::shared_ptr<Swift::Connection> m_conn, boost::s
 
		_data.erase(_data.begin(), _data.begin() + 4 + expected_size);
 

	
 
		if (wrapper.type() == pbnetwork::WrapperMessage_Type_TYPE_QUERY) {
 
			pbnetwork::BackendConfig payload;
 
			if (payload.ParseFromString(wrapper.payload()) == false) {
 
				std::cout << "PARSING ERROR\n";
 
				// TODO: ERROR
 
				continue;
 
			}
 
			m_conn->onDataRead.disconnect(boost::bind(&handleDataRead, m_conn, _1));
 
			m_conn->disconnect();
 
			response = payload.config();
 
			if (response.empty()) {
 
				response = "Empty response";
 
			}
 
			std::cout << payload.config() << "\n";
 
// 			exit(0);
 
		}
 
	}
 
}
 

	
 
static void handleConnected(boost::shared_ptr<Swift::Connection> m_conn, const std::string &msg, bool error) {
 
		m_conn->onConnectFinished.disconnect(boost::bind(&handleConnected, m_conn, msg, _1));
 
	if (error) {
 
		std::cerr << "Can't connect the server\n";
 
		response = "Can't connect the server\n";
 
		m_conn->onDataRead.disconnect(boost::bind(&handleDataRead, m_conn, _1));
 

	
 
// 		exit(50);
 
	}
 
	else {
 
		pbnetwork::BackendConfig m;
 
		m.set_config(msg);
 

	
 
		std::string message;
 
		m.SerializeToString(&message);
 

	
 
		WRAP(message, pbnetwork::WrapperMessage_Type_TYPE_QUERY);
 

	
 
		uint32_t size = htonl(message.size());
 
		char *header = (char *) &size;
 

	
 
		
 
		// send header together with wrapper message
 
		m_conn->write(Swift::createSafeByteArray(std::string(header, 4) + message));
 
	}
 
}
 

	
 
bool is_slack(ManagerConfig *config, const std::string &jid) {
 
std::string get_config(ManagerConfig *config, const std::string &jid, const std::string &key) {
 
	path p(CONFIG_STRING(config, "service.config_directory"));
 

	
 
	try {
 
		if (!exists(p)) {
 
			std::cerr << "Config directory " << CONFIG_STRING(config, "service.config_directory") << " does not exist\n";
 
			exit(6);
 
		}
 

	
 
		if (!is_directory(p)) {
 
			std::cerr << "Config directory " << CONFIG_STRING(config, "service.config_directory") << " does not exist\n";
 
			exit(7);
 
		}
 

	
 
		directory_iterator end_itr;
 
		for (directory_iterator itr(p); itr != end_itr; ++itr) {
 
			if (is_regular(itr->path()) && extension(itr->path()) == ".cfg") {
 
				Config cfg;
 
				if (cfg.load(itr->path().string()) == false) {
 
					std::cerr << "Can't load config file " << itr->path().string() << ". Skipping...\n";
 
					continue;
 
				}
 

	
 
				if (CONFIG_STRING(&cfg, "service.jid") != jid) {
 
					continue;
 
				}
 

	
 
				return CONFIG_STRING(&cfg, "service.frontend") == "slack";
 
				return CONFIG_STRING(&cfg, key);
 
			}
 
		}
 

	
 
	}
 
	catch (const filesystem_error& ex) {
 
		return false;
 
		return "";
 
	}
 

	
 
	return false;
 
	return "";
 
}
 

	
 
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 {
 
		if (!exists(p)) {
 
			std::cerr << "Config directory " << CONFIG_STRING(config, "service.config_directory") << " does not exist\n";
 
			exit(6);
 
		}
 

	
 
		if (!is_directory(p)) {
 
			std::cerr << "Config directory " << CONFIG_STRING(config, "service.config_directory") << " does not exist\n";
 
			exit(7);
 
		}
 

	
 
		bool found = false;
 
		directory_iterator end_itr;
 
		for (directory_iterator itr(p); itr != end_itr; ++itr) {
 
			if (is_regular(itr->path()) && extension(itr->path()) == ".cfg") {
 
				Config cfg;
 
				if (cfg.load(itr->path().string()) == false) {
 
					std::cerr << "Can't load config file " << itr->path().string() << ". Skipping...\n";
 
					continue;
 
				}
 

	
 
				if (CONFIG_STRING(&cfg, "service.jid") != jid) {
 
					continue;
 
				}
 

	
 
				found = true;
 

	
 
				boost::shared_ptr<Swift::Connection> m_conn;
 
				m_conn = networkFactories.getConnectionFactory()->createConnection();
 
				m_conn->onDataRead.connect(boost::bind(&handleDataRead, m_conn, _1));
 
				m_conn->onConnectFinished.connect(boost::bind(&handleConnected, m_conn, message, _1));
 
				m_conn->connect(Swift::HostAddressPort(Swift::HostAddress(CONFIG_STRING(&cfg, "service.backend_host")), getPort(CONFIG_STRING(&cfg, "service.portfile"))));
 

	
 
// 				finished++;
 
// 				Swift::Client *client = new Swift::Client(CONFIG_VECTOR(&cfg, "service.admin_jid")[0], CONFIG_STRING(&cfg, "service.admin_password"), &networkFactories);
 
// 				client->setAlwaysTrustCertificates();
 
// 				client->onConnected.connect(boost::bind(&handleConnected, client, CONFIG_STRING(&cfg, "service.jid")));
 
// 				client->onDisconnected.connect(bind(&handleDisconnected, client, _1, CONFIG_STRING(&cfg, "service.jid")));
 
// 				client->onMessageReceived.connect(bind(&handleMessageReceived, client, _1, CONFIG_STRING(&cfg, "service.jid")));
 
// 				Swift::ClientOptions opt;
 
// 				opt.allowPLAINWithoutTLS = true;
 
// 				client->connect(opt);
spectrum_manager/src/methods.h
Show inline comments
 
@@ -6,53 +6,53 @@
 
 * This program is free software; you can redistribute it and/or modify
 
 * it under the terms of the GNU General Public License as published by
 
 * the Free Software Foundation; either version 2 of the License, or
 
 * (at your option) any later version.
 
 *
 
 * This program is distributed in the hope that it will be useful,
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
 * GNU General Public License for more details.
 
 *
 
 * You should have received a copy of the GNU General Public License
 
 * along with this program; if not, write to the Free Software
 
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111-1301  USA
 
 */
 

	
 
#pragma once
 

	
 
#include <boost/program_options.hpp>
 
#include <boost/foreach.hpp>
 
#include <boost/format.hpp>
 
#include <boost/algorithm/string.hpp>
 
#include <boost/assign.hpp>
 
#include <boost/bind.hpp>
 
#include <boost/signal.hpp>
 

	
 
#include "managerconfig.h"
 
#include "transport/Config.h"
 
#include "transport/protocol.pb.h"
 
#include "Swiften/Swiften.h"
 
#include "Swiften/EventLoop/SimpleEventLoop.h"
 

	
 

	
 

	
 
std::string searchForBinary(const std::string &binary);
 

	
 
// Executes new backend
 
unsigned long exec_(std::string path, std::string config, std::string jid = "");
 

	
 
int getPort(const std::string &portfile);
 

	
 
int isRunning(const std::string &pidfile);
 
int start_instances(ManagerConfig *config, const std::string &_jid = "");
 
int restart_instances(ManagerConfig *config, const std::string &_jid = "");
 
void stop_instances(ManagerConfig *config, const std::string &_jid = "");
 

	
 
int show_status(ManagerConfig *config);
 

	
 
void ask_local_server(ManagerConfig *config, Swift::BoostNetworkFactories &networkFactories, const std::string &jid, const std::string &message);
 
bool is_slack(ManagerConfig *config, const std::string &jid);
 
std::string get_config(ManagerConfig *config, const std::string &jid, const std::string &key);
 

	
 
std::vector<std::string> show_list(ManagerConfig *config, bool show = true);
 

	
 
std::string get_response();
0 comments (0 inline, 0 general)