Merge branch 'master' of github.com:hanzz/libtransport

This commit is contained in:
HanzZ 2012-12-21 09:35:02 +01:00
commit 92b1f98bce
5 changed files with 44 additions and 11 deletions

View file

@ -47,12 +47,6 @@ 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();
@ -67,6 +61,16 @@ 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);
@ -126,7 +130,6 @@ static void daemonize(const char *cwd, const char *lock_file) {
exit(1);
}
}
#endif
int mainloop() {
@ -291,6 +294,12 @@ int main(int argc, char **argv)
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()

View file

@ -87,7 +87,7 @@ void WINAPI ServiceControlHandler(DWORD controlCode) {
break;
}
SetServiceStatus(ServiceStatusHandle, &ServiceStatus);
stop();
spectrum_control_handler(CTRL_CLOSE_EVENT);
}
void WINAPI ServiceMain(DWORD argc, LPSTR *argv) {

View file

@ -15,5 +15,5 @@ public:
};
int mainloop();
void stop();
BOOL spectrum_control_handler( DWORD fdwCtrlType );

View file

@ -283,11 +283,12 @@ bool SQLite3Backend::getOnlineUsers(std::vector<std::string> &users) {
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);
@ -312,8 +313,9 @@ long SQLite3Backend::addBuddy(long userId, const BuddyInfo &buddyInfo) {
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);

View file

@ -26,6 +26,7 @@ using namespace Transport;
class UtilTest : public CPPUNIT_NS :: TestFixture{
CPPUNIT_TEST_SUITE(UtilTest);
CPPUNIT_TEST(encryptDecryptPassword);
CPPUNIT_TEST(serializeGroups);
CPPUNIT_TEST_SUITE_END();
public:
@ -41,6 +42,27 @@ class UtilTest : public CPPUNIT_NS :: TestFixture{
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);