Proper SIGTERM/SIGINT handlers

This commit is contained in:
HanzZ 2011-09-09 11:51:25 +02:00
parent c9e9772506
commit 4c885cc947
4 changed files with 23 additions and 3 deletions

View file

@ -80,6 +80,8 @@ class UserManager {
/// \param user User class to remove
void removeUser(User *user);
void removeAllUsers();
/// Called when new User class is created.
/// \param user newly created User class
boost::signal<void (User *user)> onUserCreated;

View file

@ -30,13 +30,21 @@ using namespace Transport;
static LoggerPtr logger = log4cxx::Logger::getLogger("Spectrum");
Swift::SimpleEventLoop *eventLoop_ = NULL;
Component *component_ = NULL;
UserManager *userManager_ = NULL;
static void spectrum_sigint_handler(int sig) {
static void stop_spectrum() {
userManager_->removeAllUsers();
component_->stop();
eventLoop_->stop();
}
static void spectrum_sigint_handler(int sig) {
eventLoop_->postEvent(&stop_spectrum);
}
static void spectrum_sigterm_handler(int sig) {
eventLoop_->stop();
eventLoop_->postEvent(&stop_spectrum);
}
#ifndef WIN32
@ -236,6 +244,7 @@ int main(int argc, char **argv)
UserRegistry userRegistry(&config, factories);
Component transport(&eventLoop, factories, &config, NULL, &userRegistry);
component_ = &transport;
// Logger logger(&transport);
StorageBackend *storageBackend = NULL;
@ -260,6 +269,7 @@ int main(int argc, char **argv)
#endif
UserManager userManager(&transport, &userRegistry, storageBackend);
userManager_ = &userManager;
UserRegistration *userRegistration = NULL;
if (storageBackend) {
userRegistration = new UserRegistration(&transport, &userManager, storageBackend);
@ -275,6 +285,7 @@ int main(int argc, char **argv)
eventLoop_ = &eventLoop;
eventLoop.run();
if (userRegistration) {
userRegistration->stop();
delete userRegistration;

View file

@ -177,7 +177,8 @@ void Component::start() {
void Component::stop() {
if (m_component) {
m_reconnectCount = 0;
m_component->disconnect();
// TODO: Call this once swiften will fix assert(!session_);
// m_component->disconnect();
m_reconnectTimer->stop();
}
else if (m_server) {

View file

@ -105,6 +105,12 @@ void UserManager::removeUser(User *user) {
// VALGRIND_DO_LEAK_CHECK;
}
void UserManager::removeAllUsers() {
while(m_users.begin() != m_users.end()) {
removeUser((*m_users.begin()).second);
}
}
int UserManager::getUserCount() {
return m_users.size();
}