diff --git a/src/controller/controller_api.cpp b/src/controller/controller_api.cpp index 12ed9ac6..46b2ac50 100644 --- a/src/controller/controller_api.cpp +++ b/src/controller/controller_api.cpp @@ -571,7 +571,7 @@ int Controller::handleAPIConnection(Socket::Connection & conn){ } if (Request.isMember("invalidate_sessions")){ - if (Request["totals"].isArray()){ + if (Request["invalidate_sessions"].isArray()){ for (unsigned int i = 0; i < Request["invalidate_sessions"].size(); ++i){ Controller::sessions_invalidate(Request["invalidate_sessions"][i].asStringRef()); } @@ -580,6 +580,16 @@ int Controller::handleAPIConnection(Socket::Connection & conn){ } } + if (Request.isMember("stop_sessions")){ + if (Request["stop_sessions"].isArray() || Request["stop_sessions"].isObject()){ + jsonForEach(Request["stop_sessions"], it){ + Controller::sessions_shutdown(it); + } + }else{ + Controller::sessions_shutdown(Request["stop_sessions"].asStringRef()); + } + } + if (Request.isMember("push_start")){ std::string stream; diff --git a/src/controller/controller_statistics.cpp b/src/controller/controller_statistics.cpp index 0a5cfedf..cdb815e5 100644 --- a/src/controller/controller_statistics.cpp +++ b/src/controller/controller_statistics.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include "controller_statistics.h" #include "controller_limits.h" #include "controller_push.h" @@ -160,6 +161,51 @@ void Controller::sessions_invalidate(const std::string & streamname){ INFO_MSG("Invalidated %u connections in %u sessions for stream %s", invalidated, sessCount, streamname.c_str()); } + +///Shuts down all current sessions for the given streamname +void Controller::sessions_shutdown(JSON::Iter & i){ + if (i->isArray() || i->isObject()){ + jsonForEach(*i, it){ + sessions_shutdown(it); + } + return; + } + if (i->isString()){ + sessions_shutdown(i.key(), i->asStringRef()); + return; + } + //not handled, ignore +} + +///Shuts down all current sessions for the given streamname +void Controller::sessions_shutdown(const std::string & streamname, const std::string & protocol){ + if (!statPointer){ + FAIL_MSG("In controller shutdown procedure - cannot shutdown sessions."); + return; + } + unsigned int murdered = 0; + unsigned int sessCount = 0; + tthread::lock_guard guard(statsMutex); + for (std::map::iterator it = sessions.begin(); it != sessions.end(); it++){ + if ((!streamname.size() || it->first.streamName == streamname) && (!protocol.size() || it->first.connector == protocol) && it->second.curConns.size()){ + sessCount++; + for (std::map::iterator jt = it->second.curConns.begin(); jt != it->second.curConns.end(); ++jt){ + char * data = statPointer->getIndex(jt->first); + if (data){ + IPC::statExchange tmpEx(data); + uint32_t pid = tmpEx.getPID(); + if (pid > 1){ + Util::Procs::Stop(pid); + INFO_MSG("Killing PID %lu", pid); + murdered++; + } + } + } + } + } + INFO_MSG("Shut down %u connections in %u sessions for stream %s/%s", murdered, sessCount, streamname.c_str(), protocol.c_str()); +} + /// This function runs as a thread and roughly once per second retrieves /// statistics from all connected clients, as well as wipes /// old statistics that have disconnected over 10 minutes ago. diff --git a/src/controller/controller_statistics.h b/src/controller/controller_statistics.h index a9dddda5..11b4df17 100644 --- a/src/controller/controller_statistics.h +++ b/src/controller/controller_statistics.h @@ -117,6 +117,8 @@ namespace Controller { void fillTotals(JSON::Value & req, JSON::Value & rep); void SharedMemStats(void * config); void sessions_invalidate(const std::string & streamname); + void sessions_shutdown(JSON::Iter & i); + void sessions_shutdown(const std::string & streamname, const std::string & protocol = ""); bool hasViewers(std::string streamName); #define PROMETHEUS_TEXT 0