Changeset - 9d23a3ba8ba4
[Not reviewed]
0 4 0
HanzZ - 14 years ago 2011-07-09 12:59:53
hanzz.k@gmail.com
Renamed NPS::Client into NPS::Backend. Better Spectrum Manager output formating
4 files changed with 71 insertions and 43 deletions:
0 comments (0 inline, 0 general)
include/transport/networkpluginserver.h
Show inline comments
 
@@ -42,7 +42,7 @@ class RosterResponder;
 

	
 
class NetworkPluginServer {
 
	public:
 
		struct Client {
 
		struct Backend {
 
			bool pongReceived;
 
			std::list<User *> users;
 
			Swift::SafeByteArray data;
 
@@ -57,12 +57,16 @@ class NetworkPluginServer {
 
			return m_clients.size();
 
		}
 

	
 
		const std::list<Backend *> &getBackends() {
 
			return m_clients;
 
		}
 

	
 
		void handleMessageReceived(NetworkConversation *conv, boost::shared_ptr<Swift::Message> &message);
 

	
 
	private:
 
		void handleNewClientConnection(boost::shared_ptr<Swift::Connection> c);
 
		void handleSessionFinished(Client *c);
 
		void handleDataRead(Client *c, const Swift::SafeByteArray&);
 
		void handleSessionFinished(Backend *c);
 
		void handleDataRead(Backend *c, const Swift::SafeByteArray&);
 

	
 
		void handleConnectedPayload(const std::string &payload);
 
		void handleDisconnectedPayload(const std::string &payload);
 
@@ -92,15 +96,15 @@ class NetworkPluginServer {
 
		void send(boost::shared_ptr<Swift::Connection> &, const std::string &data);
 

	
 
		void pingTimeout();
 
		void sendPing(Client *c);
 
		Client *getFreeClient();
 
		void sendPing(Backend *c);
 
		Backend *getFreeClient();
 

	
 
		UserManager *m_userManager;
 
		VCardResponder *m_vcardResponder;
 
		RosterResponder *m_rosterResponder;
 
		Config *m_config;
 
		boost::shared_ptr<Swift::BoostConnectionServer> m_server;
 
		std::list<Client *>  m_clients;
 
		std::list<Backend *>  m_clients;
 
		Swift::Timer::ref m_pingTimer;
 
		Component *m_component;
 
};
spectrum_manager/src/main.cpp
Show inline comments
 
@@ -29,7 +29,9 @@ static void handleConnected(Swift::Client *client) {
 
}
 
 
static void handleMessageReceived(Swift::Client *client, Swift::Message::ref message) {
 
	std::cout << "[      OK      ] " << client->getJID().getDomain() << ": " << message->getBody() <<  "\n";
 
	std::string body = message->getBody();
 
	boost::replace_all(body, "\n", "\n[      OK      ] " + client->getJID().getDomain() + ": ");
 
	std::cout << "[      OK      ] " << client->getJID().getDomain() << ": " << body <<  "\n";
 
	if (--finished == 0) {
 
		exit(0);
 
	}
src/admininterface.cpp
Show inline comments
 
@@ -73,20 +73,7 @@ void AdminInterface::handleMessageReceived(Swift::Message::ref message) {
 
		int backends = m_server->getBackendCount() - 1;
 
		message->setBody("Running (" + boost::lexical_cast<std::string>(users) + " users connected using " + boost::lexical_cast<std::string>(backends) + " backends)");
 
	}
 
	else if (message->getBody() == "online_users_count") {
 
		int users = m_userManager->getUserCount();
 
		message->setBody(boost::lexical_cast<std::string>(users));
 
	}
 
	else if (message->getBody() == "backends_count") {
 
		int backends = m_server->getBackendCount() - 1;
 
		message->setBody(boost::lexical_cast<std::string>(backends));
 
	}
 
	else if (message->getBody().find("has_online_user") == 0) {
 
		User *user = m_userManager->getUser(getArg(message->getBody()));
 
		std::cout << getArg(message->getBody()) << "\n";
 
		message->setBody(boost::lexical_cast<std::string>(user != NULL));
 
	}
 
	else if (message->getBody().find("online_users") == 0) {
 
	else if (message->getBody() == "online_users") {
 
		std::string lst;
 
		const std::map<std::string, User *> &users = m_userManager->getUsers();
 
		if (users.size() == 0)
 
@@ -98,6 +85,41 @@ void AdminInterface::handleMessageReceived(Swift::Message::ref message) {
 

	
 
		message->setBody(lst);
 
	}
 
	else if (message->getBody() == "online_users_count") {
 
		int users = m_userManager->getUserCount();
 
		message->setBody(boost::lexical_cast<std::string>(users));
 
	}
 
	else if (message->getBody() == "online_users_per_backend") {
 
		std::string lst;
 
		int id = 1;
 

	
 
		const std::list <NetworkPluginServer::Backend *> &backends = m_server->getBackends();
 
		for (std::list <NetworkPluginServer::Backend *>::const_iterator b = backends.begin(); b != backends.end(); b++) {
 
			NetworkPluginServer::Backend *backend = *b;
 
			lst += "Backend " + boost::lexical_cast<std::string>(id) + ":\n";
 
			if (backend->users.size() == 0) {
 
				lst += "   waiting for users\n";
 
			}
 
			else {
 
				for (std::list<User *>::const_iterator u = backend->users.begin(); u != backend->users.end(); u++) {
 
					User *user = *u;
 
					lst += "   " + user->getJID().toBare().toString() + "\n";
 
				}
 
			}
 
			id++;
 
		}
 

	
 
		message->setBody(lst);
 
	}
 
	else if (message->getBody().find("has_online_user") == 0) {
 
		User *user = m_userManager->getUser(getArg(message->getBody()));
 
		std::cout << getArg(message->getBody()) << "\n";
 
		message->setBody(boost::lexical_cast<std::string>(user != NULL));
 
	}
 
	else if (message->getBody() == "backends_count") {
 
		int backends = m_server->getBackendCount() - 1;
 
		message->setBody(boost::lexical_cast<std::string>(backends));
 
	}
 
	else if (message->getBody().find("help") == 0) {
 
		std::string help;
 
		help += "status - shows instance status\n";
src/networkpluginserver.cpp
Show inline comments
 
@@ -172,7 +172,7 @@ NetworkPluginServer::~NetworkPluginServer() {
 
}
 

	
 
void NetworkPluginServer::handleNewClientConnection(boost::shared_ptr<Swift::Connection> c) {
 
	Client *client = new Client;
 
	Backend *client = new Backend;
 
	client->pongReceived = true;
 
	client->connection = c;
 

	
 
@@ -190,7 +190,7 @@ void NetworkPluginServer::handleNewClientConnection(boost::shared_ptr<Swift::Con
 
	sendPing(client);
 
}
 

	
 
void NetworkPluginServer::handleSessionFinished(Client *c) {
 
void NetworkPluginServer::handleSessionFinished(Backend *c) {
 
	LOG4CXX_INFO(logger, "Backend " << c << " disconnected. Current backend count=" << (m_clients.size() - 1));
 
	for (std::list<User *>::const_iterator it = c->users.begin(); it != c->users.end(); it++) {
 
		LOG4CXX_ERROR(logger, "Backend " << c << " disconnected (probably crashed) with active user " << (*it)->getJID().toString());
 
@@ -202,7 +202,7 @@ void NetworkPluginServer::handleSessionFinished(Client *c) {
 
	delete c;
 

	
 
	// Execute new session only if there's no free one after this crash/disconnection
 
	for (std::list<Client *>::const_iterator it = m_clients.begin(); it != m_clients.end(); it++) {
 
	for (std::list<Backend *>::const_iterator it = m_clients.begin(); it != m_clients.end(); it++) {
 
		if ((*it)->users.size() < CONFIG_INT(m_config, "service.users_per_backend")) {
 
			return;
 
		}
 
@@ -424,7 +424,7 @@ void NetworkPluginServer::handleAttentionPayload(const std::string &data) {
 
	conv->handleMessage(msg);
 
}
 

	
 
void NetworkPluginServer::handleDataRead(Client *c, const Swift::SafeByteArray &data) {
 
void NetworkPluginServer::handleDataRead(Backend *c, const Swift::SafeByteArray &data) {
 
	c->data.insert(c->data.end(), data.begin(), data.end());
 
	while (c->data.size() != 0) {
 
		unsigned int expected_size;
 
@@ -504,7 +504,7 @@ void NetworkPluginServer::send(boost::shared_ptr<Swift::Connection> &c, const st
 

	
 
void NetworkPluginServer::pingTimeout() {
 
	// check ping responses
 
	for (std::list<Client *>::const_iterator it = m_clients.begin(); it != m_clients.end(); it++) {
 
	for (std::list<Backend *>::const_iterator it = m_clients.begin(); it != m_clients.end(); it++) {
 
		if ((*it)->pongReceived) {
 
			sendPing((*it));
 
		}
 
@@ -516,7 +516,7 @@ void NetworkPluginServer::pingTimeout() {
 
}
 

	
 
void NetworkPluginServer::handleUserCreated(User *user) {
 
	Client *c = getFreeClient();
 
	Backend *c = getFreeClient();
 
	if (!c) {
 
		LOG4CXX_ERROR(logger, "There is no backend to handle user " << user->getJID().toString());
 
		user->handleDisconnected("Internal Server Error, please reconnect.");
 
@@ -545,7 +545,7 @@ void NetworkPluginServer::handleUserReadyToConnect(User *user) {
 

	
 
	WRAP(message, pbnetwork::WrapperMessage_Type_TYPE_LOGIN);
 

	
 
	Client *c = (Client *) user->getData();
 
	Backend *c = (Backend *) user->getData();
 
	send(c->connection, message);
 
}
 

	
 
@@ -565,7 +565,7 @@ void NetworkPluginServer::handleUserPresenceChanged(User *user, Swift::Presence:
 

	
 
	WRAP(message, pbnetwork::WrapperMessage_Type_TYPE_STATUS_CHANGED);
 

	
 
	Client *c = (Client *) user->getData();
 
	Backend *c = (Backend *) user->getData();
 
	send(c->connection, message);
 
}
 

	
 
@@ -583,7 +583,7 @@ void NetworkPluginServer::handleRoomJoined(User *user, const std::string &r, con
 

	
 
	WRAP(message, pbnetwork::WrapperMessage_Type_TYPE_JOIN_ROOM);
 
 
 
	Client *c = (Client *) user->getData();
 
	Backend *c = (Backend *) user->getData();
 
	send(c->connection, message);
 

	
 
	NetworkConversation *conv = new NetworkConversation(user->getConversationManager(), r, true);
 
@@ -605,7 +605,7 @@ void NetworkPluginServer::handleRoomLeft(User *user, const std::string &r) {
 

	
 
	WRAP(message, pbnetwork::WrapperMessage_Type_TYPE_LEAVE_ROOM);
 
 
 
	Client *c = (Client *) user->getData();
 
	Backend *c = (Backend *) user->getData();
 
	send(c->connection, message);
 

	
 
	NetworkConversation *conv = (NetworkConversation *) user->getConversationManager()->getConversation(r);
 
@@ -630,7 +630,7 @@ void NetworkPluginServer::handleUserDestroyed(User *user) {
 

	
 
	WRAP(message, pbnetwork::WrapperMessage_Type_TYPE_LOGOUT);
 
 
 
	Client *c = (Client *) user->getData();
 
	Backend *c = (Backend *) user->getData();
 
	if (!c) {
 
		return;
 
	}
 
@@ -672,7 +672,7 @@ void NetworkPluginServer::handleMessageReceived(NetworkConversation *conv, boost
 

	
 
			WRAP(message, type);
 

	
 
			Client *c = (Client *) conv->getConversationManager()->getUser()->getData();
 
			Backend *c = (Backend *) conv->getConversationManager()->getUser()->getData();
 
			send(c->connection, message);
 
		}
 
	}
 
@@ -689,7 +689,7 @@ void NetworkPluginServer::handleMessageReceived(NetworkConversation *conv, boost
 

	
 
		WRAP(message, pbnetwork::WrapperMessage_Type_TYPE_ATTENTION);
 

	
 
		Client *c = (Client *) conv->getConversationManager()->getUser()->getData();
 
		Backend *c = (Backend *) conv->getConversationManager()->getUser()->getData();
 
		send(c->connection, message);
 
		return;
 
	}
 
@@ -706,7 +706,7 @@ void NetworkPluginServer::handleMessageReceived(NetworkConversation *conv, boost
 

	
 
		WRAP(message, pbnetwork::WrapperMessage_Type_TYPE_CONV_MESSAGE);
 

	
 
		Client *c = (Client *) conv->getConversationManager()->getUser()->getData();
 
		Backend *c = (Backend *) conv->getConversationManager()->getUser()->getData();
 
		send(c->connection, message);
 
	}
 
}
 
@@ -726,7 +726,7 @@ void NetworkPluginServer::handleBuddyRemoved(Buddy *b) {
 

	
 
	WRAP(message, pbnetwork::WrapperMessage_Type_TYPE_BUDDY_REMOVED);
 

	
 
	Client *c = (Client *) user->getData();
 
	Backend *c = (Backend *) user->getData();
 
	send(c->connection, message);
 
}
 

	
 
@@ -749,7 +749,7 @@ void NetworkPluginServer::handleBuddyUpdated(Buddy *b, const Swift::RosterItemPa
 

	
 
	WRAP(message, pbnetwork::WrapperMessage_Type_TYPE_BUDDY_CHANGED);
 

	
 
	Client *c = (Client *) user->getData();
 
	Backend *c = (Backend *) user->getData();
 
	send(c->connection, message);
 
}
 

	
 
@@ -769,7 +769,7 @@ void NetworkPluginServer::handleVCardUpdated(User *user, boost::shared_ptr<Swift
 

	
 
	WRAP(message, pbnetwork::WrapperMessage_Type_TYPE_VCARD);
 

	
 
	Client *c = (Client *) user->getData();
 
	Backend *c = (Backend *) user->getData();
 
	send(c->connection, message);
 
}
 

	
 
@@ -784,11 +784,11 @@ void NetworkPluginServer::handleVCardRequired(User *user, const std::string &nam
 

	
 
	WRAP(message, pbnetwork::WrapperMessage_Type_TYPE_VCARD);
 

	
 
	Client *c = (Client *) user->getData();
 
	Backend *c = (Backend *) user->getData();
 
	send(c->connection, message);
 
}
 

	
 
void NetworkPluginServer::sendPing(Client *c) {
 
void NetworkPluginServer::sendPing(Backend *c) {
 

	
 
	std::string message;
 
	pbnetwork::WrapperMessage wrap;
 
@@ -800,10 +800,10 @@ void NetworkPluginServer::sendPing(Client *c) {
 
	LOG4CXX_INFO(logger, "PING to " << c);
 
}
 

	
 
NetworkPluginServer::Client *NetworkPluginServer::getFreeClient() {
 
	NetworkPluginServer::Client *c = NULL;
 
NetworkPluginServer::Backend *NetworkPluginServer::getFreeClient() {
 
	NetworkPluginServer::Backend *c = NULL;
 
	bool spawnNew = false;
 
	for (std::list<Client *>::const_iterator it = m_clients.begin(); it != m_clients.end(); it++) {
 
	for (std::list<Backend *>::const_iterator it = m_clients.begin(); it != m_clients.end(); it++) {
 
		if ((*it)->users.size() < CONFIG_INT(m_config, "service.users_per_backend")) {
 
			if ((*it)->users.size() + 1 == CONFIG_INT(m_config, "service.users_per_backend")) {
 
				spawnNew = true;
0 comments (0 inline, 0 general)