some framework for the keeper
authorVolker Birk <vb@pep-project.org>
Sat, 18 Jun 2016 21:26:51 +0200
changeset 86e18b34f4b9cc
parent 83 9b7371990473
child 87 f0ce6a1a7118
some framework for the keeper
pEpCOMServerAdapter.cpp
stdafx.h
     1.1 --- a/pEpCOMServerAdapter.cpp	Wed May 25 13:04:50 2016 +0200
     1.2 +++ b/pEpCOMServerAdapter.cpp	Sat Jun 18 21:26:51 2016 +0200
     1.3 @@ -8,19 +8,102 @@
     1.4  
     1.5  
     1.6  using namespace ATL;
     1.7 +using namespace std;
     1.8  
     1.9 +class CpEpCOMServerAdapterModule;
    1.10 +
    1.11 +static class GateKeeper {
    1.12 +public:
    1.13 +    static const time_t cycle = 7200;   // 7200 sec is 2 h
    1.14 +    static const DWORD waiting = 10000; // 10000 ms is 10 sec
    1.15 +
    1.16 +    static time_t time_diff()
    1.17 +    {
    1.18 +        try {
    1.19 +            static random_device rd;
    1.20 +            static mt19937 gen(rd());
    1.21 +
    1.22 +            uniform_int_distribution<time_t> dist(0, cycle);
    1.23 +
    1.24 +            return dist(gen);
    1.25 +        }
    1.26 +        catch (exception& e) {
    1.27 +            assert(0);
    1.28 +            return 0;
    1.29 +        }
    1.30 +    }
    1.31 +
    1.32 +    GateKeeper(CpEpCOMServerAdapterModule * const self) : _self(self), now(time(NULL)), next(now + time_diff())
    1.33 +    {
    1.34 +
    1.35 +    }
    1.36 +
    1.37 +    void keep();
    1.38 +
    1.39 +    void keep_plugin();
    1.40 +    void keep_updated();
    1.41 +
    1.42 +private:
    1.43 +    time_t now;
    1.44 +    time_t next;
    1.45 +
    1.46 +    CpEpCOMServerAdapterModule * const _self;
    1.47 +};
    1.48 +
    1.49 +void GateKeeper::keep()
    1.50 +{
    1.51 +    while (1) {
    1.52 +        keep_plugin();
    1.53 +
    1.54 +        Sleep(10000);
    1.55 +        
    1.56 +        now = time(NULL);
    1.57 +        assert(now != -1);
    1.58 +        
    1.59 +        if (now > next) {
    1.60 +            next = now + GateKeeper::cycle;
    1.61 +            keep_updated();
    1.62 +        }
    1.63 +    }
    1.64 +}
    1.65 +
    1.66 +void GateKeeper::keep_plugin()
    1.67 +{
    1.68 +
    1.69 +}
    1.70 +
    1.71 +void GateKeeper::keep_updated()
    1.72 +{
    1.73 +
    1.74 +}
    1.75  
    1.76  class CpEpCOMServerAdapterModule : public ATL::CAtlExeModuleT< CpEpCOMServerAdapterModule >
    1.77  {
    1.78 -public :
    1.79 +public:
    1.80 +    CpEpCOMServerAdapterModule() : ATL::CAtlExeModuleT< CpEpCOMServerAdapterModule >(), gatekeeper_thread(NULL)
    1.81 +    {
    1.82 +        gatekeeper_thread = new thread(gatekeeper, this);
    1.83 +        gatekeeper_thread->detach();
    1.84 +    }
    1.85 +
    1.86  	DECLARE_LIBID(LIBID_pEpCOMServerAdapterLib)
    1.87  	DECLARE_REGISTRY_APPID_RESOURCEID(IDR_PEPCOMSERVERADAPTER, "{B3480081-82C0-4EE4-9AA1-3F513C9D78DD}")
    1.88 +
    1.89 +private:
    1.90 +    thread *gatekeeper_thread;
    1.91 +
    1.92 +protected:
    1.93 +    static void gatekeeper(CpEpCOMServerAdapterModule * const self);
    1.94  };
    1.95  
    1.96 +void CpEpCOMServerAdapterModule::gatekeeper(CpEpCOMServerAdapterModule * const self)
    1.97 +{
    1.98 +    GateKeeper keeper(self);
    1.99 +    keeper.keep();
   1.100 +}
   1.101 +
   1.102  CpEpCOMServerAdapterModule _AtlModule;
   1.103  
   1.104 -
   1.105 -
   1.106  //
   1.107  extern "C" int WINAPI _tWinMain(HINSTANCE /*hInstance*/, HINSTANCE /*hPrevInstance*/, 
   1.108  								LPTSTR /*lpCmdLine*/, int nShowCmd)
     2.1 --- a/stdafx.h	Wed May 25 13:04:50 2016 +0200
     2.2 +++ b/stdafx.h	Sat Jun 18 21:26:51 2016 +0200
     2.3 @@ -35,6 +35,7 @@
     2.4  #include <thread>
     2.5  #include <mutex>
     2.6  #include <atomic>
     2.7 +#include <random>
     2.8  
     2.9  #include <assert.h>
    2.10