Changeset - cc954f60d118
[Not reviewed]
0 1 0
Jan Kaluza - 14 years ago 2011-06-21 08:18:40
hanzz.k@gmail.com
Check if backend can be executed and write error message if not
1 file changed with 21 insertions and 10 deletions:
0 comments (0 inline, 0 general)
src/networkpluginserver.cpp
Show inline comments
 
@@ -86,30 +86,41 @@ class NetworkFactory : public Factory {
 

	
 
#define WRAP(MESSAGE, TYPE) 	pbnetwork::WrapperMessage wrap; \
 
	wrap.set_type(TYPE); \
 
	wrap.set_payload(MESSAGE); \
 
	wrap.SerializeToString(&MESSAGE);
 
	
 
static int exec_(const char *path, const char *host, const char *port, const char *config) {
 
static pid_t exec_(const char *path, const char *host, const char *port, const char *config) {
 
	LOG4CXX_INFO(logger, "Starting new backend " << path);
 
// 	char *argv[] = {(char*)script_name, '\0'}; 
 
	int status = 0;
 
	pid_t pid = fork();
 
	if ( pid == 0 ) {
 
		// child process
 
		execlp(path, path, "--host", host, "--port", port, config, NULL);
 
		abort();
 
		exit(execlp(path, path, "--host", host, "--port", port, config, NULL));
 
	} else if ( pid < 0 ) {
 
		// fork failed
 
		status = -1;
 
	}
 
	return status;
 

	
 
	return pid;
 
}
 

	
 
static void SigCatcher(int n) {
 
	wait3(NULL,WNOHANG,NULL);
 
	pid_t result;
 
	int status;
 
	while ((result = waitpid(0, &status, WNOHANG)) > 0) {
 
		if (result != 0) {
 
			if (WIFEXITED(status)) {
 
				if (WEXITSTATUS(status) != 0) {
 
					LOG4CXX_ERROR(logger, "Backend can not be started, exit_code=" << WEXITSTATUS(status));
 
				}
 
			}
 
			else {
 
				LOG4CXX_ERROR(logger, "Backend can not be started");
 
			}
 
		}
 
	}
 
}
 

	
 
static void handleBuddyPayload(LocalBuddy *buddy, const pbnetwork::Buddy &payload) {
 
	buddy->setName(payload.buddyname());
 
	buddy->setAlias(payload.alias());
 
	std::vector<std::string> groups;
 
@@ -125,13 +136,14 @@ NetworkPluginServer::NetworkPluginServer(Component *component, Config *config, U
 
	m_component = component;
 
	m_component->m_factory = new NetworkFactory(this);
 
	m_userManager->onUserCreated.connect(boost::bind(&NetworkPluginServer::handleUserCreated, this, _1));
 
	m_userManager->onUserDestroyed.connect(boost::bind(&NetworkPluginServer::handleUserDestroyed, this, _1));
 

	
 
	m_pingTimer = component->getNetworkFactories()->getTimerFactory()->createTimer(10000);
 
	m_pingTimer->onTick.connect(boost::bind(&NetworkPluginServer::pingTimeout, this)); 
 
	m_pingTimer->onTick.connect(boost::bind(&NetworkPluginServer::pingTimeout, this));
 
	m_pingTimer->start();
 

	
 
	m_vcardResponder = new VCardResponder(component->getIQRouter(), userManager);
 
	m_vcardResponder->onVCardRequired.connect(boost::bind(&NetworkPluginServer::handleVCardRequired, this, _1, _2, _3));
 
	m_vcardResponder->onVCardUpdated.connect(boost::bind(&NetworkPluginServer::handleVCardUpdated, this, _1, _2));
 
	m_vcardResponder->start();
 

	
 
@@ -172,13 +184,12 @@ void NetworkPluginServer::handleNewClientConnection(boost::shared_ptr<Swift::Con
 

	
 
	m_clients.push_back(client);
 

	
 
	c->onDisconnected.connect(boost::bind(&NetworkPluginServer::handleSessionFinished, this, client));
 
	c->onDataRead.connect(boost::bind(&NetworkPluginServer::handleDataRead, this, client, _1));
 
	sendPing(client);
 
	m_pingTimer->start();
 
}
 

	
 
void NetworkPluginServer::handleSessionFinished(Client *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());
 
@@ -461,16 +472,16 @@ void NetworkPluginServer::send(boost::shared_ptr<Swift::Connection> &c, const st
 
	char header[4];
 
	*((int*)(header)) = htonl(data.size());
 
	c->write(Swift::createSafeByteArray(std::string(header, 4) + data));
 
}
 

	
 
void NetworkPluginServer::pingTimeout() {
 
	// check ping responses
 
	for (std::list<Client *>::const_iterator it = m_clients.begin(); it != m_clients.end(); it++) {
 
		if ((*it)->pongReceived) {
 
			sendPing((*it));
 
			m_pingTimer->start();
 
		}
 
		else {
 
			exec_(CONFIG_STRING(m_config, "service.backend").c_str(), CONFIG_STRING(m_config, "service.backend_host").c_str(), CONFIG_STRING(m_config, "service.backend_port").c_str(), m_config->getConfigFile().c_str());
 
		}
 
	}
 
}
0 comments (0 inline, 0 general)