Changeset - 92b1f98bcedb
[Not reviewed]
Merge
0 19 0
HanzZ - 13 years ago 2012-12-21 09:35:02
hanzz.k@gmail.com
Merge branch 'master' of github.com:hanzz/libtransport
5 files changed with 44 insertions and 11 deletions:
0 comments (0 inline, 0 general)
spectrum/src/main.cpp
Show inline comments
 
@@ -44,18 +44,12 @@ DEFINE_LOGGER(logger, "Spectrum");
 

	
 
Swift::SimpleEventLoop *eventLoop_ = NULL;
 
Component *component_ = NULL;
 
UserManager *userManager_ = NULL;
 
Config *config_ = NULL;
 

	
 
void stop() {
 
	userManager_->removeAllUsers(false);
 
	component_->stop();
 
	eventLoop_->stop();
 
}
 

	
 
static void stop_spectrum() {
 
	userManager_->removeAllUsers(false);
 
	component_->stop();
 
	eventLoop_->stop();
 
}
 

	
 
@@ -64,12 +58,22 @@ static void spectrum_sigint_handler(int sig) {
 
}
 

	
 
static void spectrum_sigterm_handler(int sig) {
 
	eventLoop_->postEvent(&stop_spectrum);
 
}
 

	
 
#ifdef WIN32
 
BOOL spectrum_control_handler( DWORD fdwCtrlType ) { 
 
	if (fdwCtrlType == CTRL_C_EVENT || fdwCtrlType == CTRL_CLOSE_EVENT) {
 
		eventLoop_->postEvent(&stop_spectrum);
 
		return TRUE;
 
	}
 
	return FALSE;
 
} 
 
#endif
 

	
 
static void removeOldIcons(std::string iconDir) {
 
	std::vector<std::string> dirs;
 
	dirs.push_back(iconDir);
 

	
 
	boost::thread thread(boost::bind(Util::removeEverythingOlderThan, dirs, time(NULL) - 3600*24*14));
 
}
 
@@ -123,13 +127,12 @@ static void daemonize(const char *cwd, const char *lock_file) {
 
	
 
	if (freopen( "/dev/null", "r", stdin) == NULL) {
 
		std::cout << "EE cannot open /dev/null. Exiting\n";
 
		exit(1);
 
	}
 
}
 

	
 
#endif
 

	
 
int mainloop() {
 

	
 
#ifndef WIN32
 
	mode_t old_cmask = umask(0007);
 
@@ -288,12 +291,18 @@ int main(int argc, char **argv)
 
	}
 

	
 
	if (signal(SIGTERM, spectrum_sigterm_handler) == SIG_ERR) {
 
		std::cout << "SIGTERM handler can't be set\n";
 
		return -1;
 
	}
 
#else
 
	if( !SetConsoleCtrlHandler( (PHANDLER_ROUTINE) spectrum_control_handler, TRUE ) ) 
 
	{ 
 
		std::cout << "control handler can't be set\n";
 
		return -1;
 
	} 
 
#endif
 
	boost::program_options::options_description desc(std::string("Spectrum version: ") + SPECTRUM_VERSION + "\nUsage: spectrum [OPTIONS] <config_file.cfg>\nAllowed options");
 
	desc.add_options()
 
		("help,h", "help")
 
		("no-daemonize,n", "Do not run spectrum as daemon")
 
		("no-debug,d", "Create coredumps on crash")
spectrum/src/win32/ServiceWrapper.cpp
Show inline comments
 
@@ -84,13 +84,13 @@ void WINAPI ServiceControlHandler(DWORD controlCode) {
 
		ServiceStatus.dwCurrentState = SERVICE_STOP_PENDING;		
 
		break;
 
	default:
 
		break;
 
	}
 
	SetServiceStatus(ServiceStatusHandle, &ServiceStatus);	
 
	stop();
 
	spectrum_control_handler(CTRL_CLOSE_EVENT);
 
}
 

	
 
void WINAPI ServiceMain(DWORD argc, LPSTR *argv) {
 
	ServiceStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
 
	ServiceStatus.dwServiceSpecificExitCode = NO_ERROR;
 

	
spectrum/src/win32/ServiceWrapper.h
Show inline comments
 
@@ -12,8 +12,8 @@ public:
 
	bool UnInstall();
 
	bool IsInstalled();
 
	void RunService();
 
};
 

	
 
int mainloop();
 
void stop();
 
BOOL spectrum_control_handler( DWORD fdwCtrlType );
 

	
src/sqlite3backend.cpp
Show inline comments
 
@@ -280,17 +280,18 @@ bool SQLite3Backend::getOnlineUsers(std::vector<std::string> &users) {
 

	
 
	return true;
 
}
 

	
 
long SQLite3Backend::addBuddy(long userId, const BuddyInfo &buddyInfo) {
 
// 	"INSERT INTO " + m_prefix + "buddies (user_id, uin, subscription, groups, nickname, flags) VALUES (?, ?, ?, ?, ?, ?)"
 
	std::string groups = StorageBackend::serializeGroups(buddyInfo.groups);
 
	BEGIN(m_addBuddy);
 
	BIND_INT(m_addBuddy, userId);
 
	BIND_STR(m_addBuddy, buddyInfo.legacyName);
 
	BIND_STR(m_addBuddy, buddyInfo.subscription);
 
	BIND_STR(m_addBuddy, StorageBackend::serializeGroups(buddyInfo.groups));
 
	BIND_STR(m_addBuddy, groups);
 
	BIND_STR(m_addBuddy, buddyInfo.alias);
 
	BIND_INT(m_addBuddy, buddyInfo.flags);
 

	
 
	if(sqlite3_step(m_addBuddy) != SQLITE_DONE) {
 
		LOG4CXX_ERROR(logger, "addBuddy query"<< (sqlite3_errmsg(m_db) == NULL ? "" : sqlite3_errmsg(m_db)));
 
		return -1;
 
@@ -309,14 +310,15 @@ long SQLite3Backend::addBuddy(long userId, const BuddyInfo &buddyInfo) {
 
	EXECUTE_STATEMENT(m_updateBuddySetting, "updateBuddySetting query");
 
	return id;
 
}
 

	
 
void SQLite3Backend::updateBuddy(long userId, const BuddyInfo &buddyInfo) {
 
// 	UPDATE " + m_prefix + "buddies SET groups=?, nickname=?, flags=?, subscription=? WHERE user_id=? AND uin=?
 
	std::string groups = StorageBackend::serializeGroups(buddyInfo.groups);
 
	BEGIN(m_updateBuddy);
 
	BIND_STR(m_updateBuddy, StorageBackend::serializeGroups(buddyInfo.groups));
 
	BIND_STR(m_updateBuddy, groups);
 
	BIND_STR(m_updateBuddy, buddyInfo.alias);
 
	BIND_INT(m_updateBuddy, buddyInfo.flags);
 
	BIND_STR(m_updateBuddy, buddyInfo.subscription);
 
	BIND_INT(m_updateBuddy, userId);
 
	BIND_STR(m_updateBuddy, buddyInfo.legacyName);
 

	
src/tests/util.cpp
Show inline comments
 
@@ -23,12 +23,13 @@
 

	
 
using namespace Transport;
 

	
 
class UtilTest : public CPPUNIT_NS :: TestFixture{
 
	CPPUNIT_TEST_SUITE(UtilTest);
 
	CPPUNIT_TEST(encryptDecryptPassword);
 
	CPPUNIT_TEST(serializeGroups);
 
	CPPUNIT_TEST_SUITE_END();
 

	
 
	public:
 
		void setUp (void) {
 
		}
 

	
 
@@ -38,9 +39,30 @@ class UtilTest : public CPPUNIT_NS :: TestFixture{
 

	
 
	void encryptDecryptPassword() {
 
		std::string encrypted = StorageBackend::encryptPassword("password", "key");
 
		CPPUNIT_ASSERT_EQUAL(std::string("password"), StorageBackend::decryptPassword(encrypted, "key"));
 
	}
 

	
 
	void serializeGroups() {
 
		std::vector<std::string> groups;
 
		std::string g = "";
 
		
 
		CPPUNIT_ASSERT_EQUAL(g, StorageBackend::serializeGroups(groups));
 
		CPPUNIT_ASSERT_EQUAL(0, (int) StorageBackend::deserializeGroups(g).size());
 

	
 
		groups.push_back("Buddies");
 
		g = "Buddies";
 
		CPPUNIT_ASSERT_EQUAL(g, StorageBackend::serializeGroups(groups));
 
		CPPUNIT_ASSERT_EQUAL(1, (int) StorageBackend::deserializeGroups(g).size());
 
		CPPUNIT_ASSERT_EQUAL(g, StorageBackend::deserializeGroups(g)[0]);
 

	
 
		groups.push_back("Buddies2");
 
		g = "Buddies\nBuddies2";
 
		CPPUNIT_ASSERT_EQUAL(g, StorageBackend::serializeGroups(groups));
 
		CPPUNIT_ASSERT_EQUAL(2, (int) StorageBackend::deserializeGroups(g).size());
 
		CPPUNIT_ASSERT_EQUAL(std::string("Buddies"), StorageBackend::deserializeGroups(g)[0]);
 
		CPPUNIT_ASSERT_EQUAL(std::string("Buddies2"), StorageBackend::deserializeGroups(g)[1]);
 
	}
 

	
 
};
 

	
 
CPPUNIT_TEST_SUITE_REGISTRATION (UtilTest);
0 comments (0 inline, 0 general)