Changeset - 3a76b3435abb
[Not reviewed]
0 2 0
Jan Kaluza - 14 years ago 2011-08-02 12:38:40
hanzz.k@gmail.com
Better spawning
2 files changed with 10 insertions and 48 deletions:
0 comments (0 inline, 0 general)
src/CMakeLists.txt
Show inline comments
 
@@ -3,22 +3,22 @@ FILE(GLOB SRC *.cpp *.h)
 
FILE(GLOB_RECURSE SWIFTEN_SRC ../include/Swiften/*.cpp)
 
FILE(GLOB HEADERS ../include/transport/*.h)
 
 
if (PROTOBUF_FOUND)
 
	PROTOBUF_GENERATE_CPP(PROTOBUF_SRC PROTOBUF_HDRS "pbnetwork.proto")
 
endif()
 
 
# SOURCE_GROUP(headers FILES ${HEADERS})
 
 
ADD_LIBRARY(transport SHARED ${HEADERS} ${SRC} ${SWIFTEN_SRC} ${PROTOBUF_SRC} ${PROTOBUF_HDRS})
 
ADD_DEFINITIONS(-fPIC)
 
 
TARGET_LINK_LIBRARIES(transport ${Boost_LIBRARIES} ${SQLITE3_LIBRARIES} ${SWIFTEN_LIBRARY} ${PROTOBUF_LIBRARIES} ${LOG4CXX_LIBRARIES})
 
TARGET_LINK_LIBRARIES(transport ${Boost_LIBRARIES} ${SQLITE3_LIBRARIES} ${SWIFTEN_LIBRARY} ${PROTOBUF_LIBRARIES} ${LOG4CXX_LIBRARIES} -lpopt)
 
 
SET_TARGET_PROPERTIES(transport PROPERTIES
 
      VERSION ${TRANSPORT_VERSION} SOVERSION ${TRANSPORT_VERSION}
 
)
 
 
INSTALL(TARGETS transport LIBRARY DESTINATION lib ARCHIVE DESTINATION lib COMPONENT libraries)
 
 
#CONFIGURE_FILE(transport.pc.in "${CMAKE_CURRENT_BINARY_DIR}/transport.pc")
 
#INSTALL(FILES "${CMAKE_CURRENT_BINARY_DIR}/transport.pc" DESTINATION lib/pkgconfig)
src/networkpluginserver.cpp
Show inline comments
 
@@ -32,24 +32,25 @@
 
#include "transport/rosterresponder.h"
 
#include "blockresponder.h"
 
#include "Swiften/Swiften.h"
 
#include "Swiften/Server/ServerStanzaChannel.h"
 
#include "Swiften/Elements/StreamError.h"
 
#include "Swiften/Network/BoostConnectionServer.h"
 
#include "Swiften/Elements/AttentionPayload.h"
 
#include "Swiften/Elements/XHTMLIMPayload.h"
 
#include "pbnetwork.pb.h"
 
#include "sys/wait.h"
 
#include "sys/signal.h"
 
#include "log4cxx/logger.h"
 
#include "popt.h"
 

	
 
using namespace log4cxx;
 

	
 
namespace Transport {
 

	
 
static LoggerPtr logger = Logger::getLogger("NetworkPluginServer");
 

	
 
class NetworkConversation : public Conversation {
 
	public:
 
		NetworkConversation(ConversationManager *conversationManager, const std::string &legacyName, bool muc = false) : Conversation(conversationManager, legacyName, muc) {
 
		}
 

	
 
@@ -82,82 +83,43 @@ class NetworkFactory : public Factory {
 
			if (buddyInfo.settings.find("icon_hash") != buddyInfo.settings.end())
 
				buddy->setIconHash(buddyInfo.settings.find("icon_hash")->second.s);
 
			return buddy;
 
		}
 
	private:
 
		NetworkPluginServer *m_nps;
 
};
 

	
 
#define WRAP(MESSAGE, TYPE) 	pbnetwork::WrapperMessage wrap; \
 
	wrap.set_type(TYPE); \
 
	wrap.set_payload(MESSAGE); \
 
	wrap.SerializeToString(&MESSAGE);
 
	
 
static std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems) {
 
    std::stringstream ss(s);
 
    std::string item;
 
    while(std::getline(ss, item, delim)) {
 
        elems.push_back(item);
 
    }
 
    return elems;
 
}
 

	
 

	
 
static std::vector<std::string> split(const std::string &s, char delim) {
 
    std::vector<std::string> elems;
 
    return split(s, delim, elems);
 
}
 
	
 
static pid_t exec_(std::string path, const char *host, const char *port, const char *config) {
 
	path += std::string(" --host ") + host + " --port " + port + " " + config;
 
	LOG4CXX_INFO(logger, "Starting new backend " << path);
 
	std::vector<std::string> cmd = split(path, ' ');
 
	char **argv = (char **) malloc(sizeof(char *) * (cmd.size() + 6));
 
	int i = 0;
 
	BOOST_FOREACH(std::string c, cmd) {
 
		argv[i] = (char *) malloc(c.size() + 1);
 
		strcpy(argv[i++], c.c_str());
 
// 		std::cout << i << "\n";
 
	}
 

	
 
	argv[i] = (char *) malloc(100);
 
	strcpy(argv[i++], "--host");
 

	
 
	argv[i] = (char *) malloc(100);
 
	strcpy(argv[i++], host);
 

	
 
	argv[i] = (char *) malloc(100);
 
	strcpy(argv[i++], "--port");
 

	
 
	argv[i] = (char *) malloc(100);
 
	strcpy(argv[i++], port);
 

	
 
	argv[i] = (char *) malloc(100);
 
	strcpy(argv[i++], config);
 

	
 
	argv[i] = 0;
 
	char *p = (char *) malloc(path.size() + 1);
 
	strcpy(p, path.c_str());
 
	int argc;
 
	char **argv;
 
	poptParseArgvString(p, &argc, (const char ***) &argv);
 

	
 
// 	char *argv[] = {(char*)script_name, '\0'}; 
 
	pid_t pid = fork();
 
	if ( pid == 0 ) {
 
		// child process
 
		exit(execv(cmd[0].c_str(), argv));
 
		exit(execv(argv[0], argv));
 
	} else if ( pid < 0 ) {
 
		// fork failed
 
	}
 

	
 
	i = 0;
 
	while (argv[i] != 0) {
 
		free(argv[i++]);
 
	}
 
	free(argv);
 
	free(p);
 

	
 
	return pid;
 
}
 

	
 
static void SigCatcher(int n) {
 
	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));
0 comments (0 inline, 0 general)