2014-11-11 19:23:41 +01:00
|
|
|
/*
|
|
|
|
* poller.c: SAT>IP plugin for the Video Disk Recorder
|
|
|
|
*
|
|
|
|
* See the README file for copyright information and how to reach the author.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2014-12-04 20:44:44 +01:00
|
|
|
#define __STDC_FORMAT_MACROS // Required for format specifiers
|
|
|
|
#include <inttypes.h>
|
2014-11-11 19:23:41 +01:00
|
|
|
#include <sys/epoll.h>
|
|
|
|
|
2014-12-03 18:57:23 +01:00
|
|
|
#include "config.h"
|
2014-11-11 19:23:41 +01:00
|
|
|
#include "common.h"
|
2014-12-05 22:14:40 +01:00
|
|
|
#include "log.h"
|
2014-11-11 19:23:41 +01:00
|
|
|
#include "poller.h"
|
|
|
|
|
|
|
|
cSatipPoller *cSatipPoller::instanceS = NULL;
|
|
|
|
|
|
|
|
cSatipPoller *cSatipPoller::GetInstance(void)
|
|
|
|
{
|
|
|
|
if (!instanceS)
|
|
|
|
instanceS = new cSatipPoller();
|
|
|
|
return instanceS;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cSatipPoller::Initialize(void)
|
|
|
|
{
|
2014-12-06 16:02:45 +01:00
|
|
|
debug1("%s", __PRETTY_FUNCTION__);
|
2014-11-11 19:23:41 +01:00
|
|
|
if (instanceS)
|
|
|
|
instanceS->Activate();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void cSatipPoller::Destroy(void)
|
|
|
|
{
|
2014-12-06 16:02:45 +01:00
|
|
|
debug1("%s", __PRETTY_FUNCTION__);
|
2014-11-11 19:23:41 +01:00
|
|
|
if (instanceS)
|
|
|
|
instanceS->Deactivate();
|
|
|
|
}
|
|
|
|
|
|
|
|
cSatipPoller::cSatipPoller()
|
2014-12-05 23:37:55 +01:00
|
|
|
: cThread("SATIP poller"),
|
2014-11-11 19:23:41 +01:00
|
|
|
mutexM(),
|
|
|
|
fdM(epoll_create(eMaxFileDescriptors))
|
|
|
|
{
|
2014-12-06 16:02:45 +01:00
|
|
|
debug1("%s", __PRETTY_FUNCTION__);
|
2014-11-11 19:23:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
cSatipPoller::~cSatipPoller()
|
|
|
|
{
|
2014-12-06 16:02:45 +01:00
|
|
|
debug1("%s", __PRETTY_FUNCTION__);
|
2014-11-11 19:23:41 +01:00
|
|
|
Deactivate();
|
|
|
|
cMutexLock MutexLock(&mutexM);
|
|
|
|
close(fdM);
|
|
|
|
// Free allocated memory
|
|
|
|
}
|
|
|
|
|
|
|
|
void cSatipPoller::Activate(void)
|
|
|
|
{
|
|
|
|
// Start the thread
|
|
|
|
Start();
|
|
|
|
}
|
|
|
|
|
|
|
|
void cSatipPoller::Deactivate(void)
|
|
|
|
{
|
2014-12-06 16:02:45 +01:00
|
|
|
debug1("%s", __PRETTY_FUNCTION__);
|
2014-11-11 19:23:41 +01:00
|
|
|
cMutexLock MutexLock(&mutexM);
|
|
|
|
if (Running())
|
|
|
|
Cancel(3);
|
|
|
|
}
|
|
|
|
|
|
|
|
void cSatipPoller::Action(void)
|
|
|
|
{
|
2014-12-06 16:02:45 +01:00
|
|
|
debug1("%s Entering", __PRETTY_FUNCTION__);
|
2014-11-11 19:23:41 +01:00
|
|
|
struct epoll_event events[eMaxFileDescriptors];
|
2014-11-30 00:00:20 +01:00
|
|
|
uint64_t maxElapsed = 0;
|
2014-11-11 19:23:41 +01:00
|
|
|
// Increase priority
|
|
|
|
SetPriority(-1);
|
|
|
|
// Do the thread loop
|
|
|
|
while (Running()) {
|
|
|
|
int nfds = epoll_wait(fdM, events, eMaxFileDescriptors, -1);
|
2016-06-05 13:19:46 +02:00
|
|
|
ERROR_IF_FUNC((nfds == -1 && errno != EINTR), "epoll_wait() failed", break, ;);
|
2014-11-14 21:28:27 +01:00
|
|
|
for (int i = 0; i < nfds; ++i) {
|
|
|
|
cSatipPollerIf* poll = reinterpret_cast<cSatipPollerIf *>(events[i].data.ptr);
|
2014-11-29 13:44:30 +01:00
|
|
|
if (poll) {
|
2014-11-30 00:00:20 +01:00
|
|
|
uint64_t elapsed;
|
2014-11-29 13:44:30 +01:00
|
|
|
cTimeMs processing(0);
|
2014-11-25 21:04:34 +01:00
|
|
|
poll->Process();
|
2014-11-29 13:44:30 +01:00
|
|
|
elapsed = processing.Elapsed();
|
|
|
|
if (elapsed > maxElapsed) {
|
|
|
|
maxElapsed = elapsed;
|
2014-12-06 16:02:45 +01:00
|
|
|
debug1("%s Processing %s took %" PRIu64 " ms", __PRETTY_FUNCTION__, *(poll->ToString()), maxElapsed);
|
2014-11-29 13:44:30 +01:00
|
|
|
}
|
|
|
|
}
|
2014-11-11 19:23:41 +01:00
|
|
|
}
|
|
|
|
}
|
2014-12-06 16:02:45 +01:00
|
|
|
debug1("%s Exiting", __PRETTY_FUNCTION__);
|
2014-11-11 19:23:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool cSatipPoller::Register(cSatipPollerIf &pollerP)
|
|
|
|
{
|
2014-12-06 16:02:45 +01:00
|
|
|
debug1("%s fd=%d", __PRETTY_FUNCTION__, pollerP.GetFd());
|
2014-11-11 19:23:41 +01:00
|
|
|
cMutexLock MutexLock(&mutexM);
|
2014-11-14 21:28:27 +01:00
|
|
|
|
|
|
|
struct epoll_event ev;
|
|
|
|
ev.events = EPOLLIN | EPOLLET;
|
|
|
|
ev.data.ptr = &pollerP;
|
|
|
|
ERROR_IF_RET(epoll_ctl(fdM, EPOLL_CTL_ADD, pollerP.GetFd(), &ev) == -1, "epoll_ctl(EPOLL_CTL_ADD) failed", return false);
|
2014-12-06 16:02:45 +01:00
|
|
|
debug1("%s Added interface fd=%d", __PRETTY_FUNCTION__, pollerP.GetFd());
|
2014-11-14 21:28:27 +01:00
|
|
|
|
|
|
|
return true;
|
2014-11-11 19:23:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool cSatipPoller::Unregister(cSatipPollerIf &pollerP)
|
|
|
|
{
|
2014-12-06 16:02:45 +01:00
|
|
|
debug1("%s fd=%d", __PRETTY_FUNCTION__, pollerP.GetFd());
|
2014-11-11 19:23:41 +01:00
|
|
|
cMutexLock MutexLock(&mutexM);
|
2014-11-14 21:28:27 +01:00
|
|
|
ERROR_IF_RET((epoll_ctl(fdM, EPOLL_CTL_DEL, pollerP.GetFd(), NULL) == -1), "epoll_ctl(EPOLL_CTL_DEL) failed", return false);
|
2014-12-06 16:02:45 +01:00
|
|
|
debug1("%s Removed interface fd=%d", __PRETTY_FUNCTION__, pollerP.GetFd());
|
2014-11-11 19:23:41 +01:00
|
|
|
|
2014-11-14 21:28:27 +01:00
|
|
|
return true;
|
|
|
|
}
|