diff --git a/src/controller/controller.cpp b/src/controller/controller.cpp index e27d196e..02446555 100644 --- a/src/controller/controller.cpp +++ b/src/controller/controller.cpp @@ -94,6 +94,7 @@ void createAccount (std::string account){ /// Will check outputs, inputs and converters every five seconds void statusMonitor(void * np){ IPC::semaphore configLock(SEM_CONF, O_CREAT | O_RDWR, ACCESSPERMS, 1); + Controller::loadActiveConnectors(); while (Controller::conf.is_active){ //this scope prevents the configMutex from being locked constantly { @@ -116,7 +117,12 @@ void statusMonitor(void * np){ Controller::configChanged = false; } } - Util::wait(5000);//wait at least 5 seconds + Util::sleep(5000);//wait at least 5 seconds + } + if (Controller::restarting){ + Controller::prepareActiveConnectorsForReload(); + }else{ + Controller::prepareActiveConnectorsForShutdown(); } configLock.unlink(); } diff --git a/src/controller/controller_connectors.cpp b/src/controller/controller_connectors.cpp index 7f1528ec..00417164 100644 --- a/src/controller/controller_connectors.cpp +++ b/src/controller/controller_connectors.cpp @@ -10,6 +10,8 @@ #include #include "controller_storage.h" #include "controller_connectors.h" +#include +#include #include #include @@ -20,6 +22,61 @@ namespace Controller { static std::map currentConnectors; ///::iterator it; + for (it = currentConnectors.begin(); it != currentConnectors.end(); ++it){ + A.setString("cmd", it->first, count); + A.setInt("pid", it->second, count); + ++count; + } + A.setRCount(count); + f.master = false;//Keep the shm page around, don't kill it + } + + /// Reads active connectors from the shared memory pages + void loadActiveConnectors(){ + IPC::sharedPage f("MstCnns", 4096, false, false); + const Util::RelAccX A(f.mapped, false); + if (A.isReady()){ + for (uint32_t i = 0; i < A.getRCount(); ++i){ + char * p = A.getPointer("cmd", i); + if (p != 0 && p[0] != 0){ + currentConnectors[p] = A.getInt("pid", i); + Util::Procs::remember(A.getInt("pid", i)); + } + } + } + } + + /// Deletes the shared memory page with connector information + /// in preparation of shutdown. + void prepareActiveConnectorsForShutdown(){ + IPC::sharedPage f("MstCnns", 4096, true, false); + } + + /// Forgets all active connectors, preventing them from being killed, + /// in preparation of reload. + void prepareActiveConnectorsForReload(){ + saveActiveConnectors(); + std::map::iterator it; + for (it = currentConnectors.begin(); it != currentConnectors.end(); ++it){ + Util::Procs::forget(it->second); + } + currentConnectors.clear(); + } + ///\brief Checks if the binary mentioned in the protocol argument is currently active, if so, restarts it. ///\param protocol The protocol to check. void UpdateProtocol(std::string protocol){ @@ -171,6 +228,7 @@ namespace Controller { } runningConns.erase(runningConns.begin()); } + if (action){saveActiveConnectors();} return action; } diff --git a/src/controller/controller_connectors.h b/src/controller/controller_connectors.h index 97e53267..e48ff308 100644 --- a/src/controller/controller_connectors.h +++ b/src/controller/controller_connectors.h @@ -8,4 +8,20 @@ namespace Controller { /// Checks current protocol configuration, updates state of enabled connectors if neccesary. bool CheckProtocols(JSON::Value & p, const JSON::Value & capabilities); + /// Updates the shared memory page with active connectors + void saveActiveConnectors(); + + /// Reads active connectors from the shared memory pages + void loadActiveConnectors(); + + + /// Deletes the shared memory page with connector information + /// in preparation of shutdown. + void prepareActiveConnectorsForShutdown(); + + /// Forgets all active connectors, preventing them from being killed, + /// in preparation of reload. + void prepareActiveConnectorsForReload(); + } +