Merge pull request #616 from psychon/module-jobs2

Add support for CJob cancellation and necessary module support
This commit is contained in:
Alexey Sokolov
2014-08-11 21:09:57 +01:00
10 changed files with 526 additions and 9 deletions
+3
View File
@@ -26,6 +26,7 @@
#endif
#include <utility>
#include "../include/znc/Utils.h"
#include "../include/znc/Threads.h"
#include "../include/znc/Config.h"
#include "../include/znc/Socket.h"
#include "../include/znc/Modules.h"
@@ -115,9 +116,11 @@ class MCString : public std::map<CString, CString> {};
#define u_short unsigned short
#define u_int unsigned int
#include "../include/znc/zncconfig.h"
#include "../include/znc/ZNCString.h"
%include "../include/znc/defines.h"
%include "../include/znc/Utils.h"
%include "../include/znc/Threads.h"
%include "../include/znc/Config.h"
%include "../include/znc/Csocket.h"
%template(ZNCSocketManager) TSocketManager<CZNCSock>;
+3
View File
@@ -17,6 +17,7 @@
%module znc_core %{
#include <utility>
#include "../include/znc/Utils.h"
#include "../include/znc/Threads.h"
#include "../include/znc/Config.h"
#include "../include/znc/Socket.h"
#include "../include/znc/Modules.h"
@@ -123,9 +124,11 @@ class MCString : public std::map<CString, CString> {};
#define u_short unsigned short
#define u_int unsigned int
#include "../include/znc/zncconfig.h"
#include "../include/znc/ZNCString.h"
%include "../include/znc/defines.h"
%include "../include/znc/Utils.h"
%include "../include/znc/Threads.h"
%template(PAuthBase) CSmartPtr<CAuthBase>;
%template(WebSession) CSmartPtr<CWebSession>;
%include "../include/znc/Config.h"
+31
View File
@@ -20,6 +20,36 @@
using std::vector;
class CSampleJob : public CModuleJob {
public:
CSampleJob(CModule *pModule) : CModuleJob(pModule, "sample", "Message the user after a delay") {}
~CSampleJob() {
if (wasCancelled()) {
m_pModule->PutModule("Sample job cancelled");
} else {
m_pModule->PutModule("Sample job destroyed");
}
}
virtual void runThread() {
// Cannot safely use m_pModule in here, because this runs in its
// own thread and such an access would require synchronisation
// between this thread and the main thread!
for (int i = 0; i < 10; i++) {
// Regularly check if we were cancelled
if (wasCancelled())
return;
sleep(1);
}
}
virtual void runMain() {
m_pModule->PutModule("Sample job done");
}
};
class CSampleTimer : public CTimer {
public:
@@ -42,6 +72,7 @@ public:
//AddTimer(new CSampleTimer(this, 300, 0, "Sample", "Sample timer for sample things."));
//AddTimer(new CSampleTimer(this, 5, 20, "Another", "Another sample timer."));
//AddTimer(new CSampleTimer(this, 25000, 5, "Third", "A third sample timer."));
AddJob(new CSampleJob(this));
return true;
}