pEpCOMServerAdapter.cpp
author Volker Birk <vb@pep-project.org>
Sat, 18 Jun 2016 21:26:51 +0200
changeset 86 e18b34f4b9cc
parent 0 0aa9866d751c
child 87 f0ce6a1a7118
permissions -rw-r--r--
some framework for the keeper
     1 // pEpCOMServerAdapter.cpp : Implementation of WinMain
     2 
     3 
     4 #include "stdafx.h"
     5 #include "resource.h"
     6 #include "pEpCOMServerAdapter_i.h"
     7 #include "xdlldata.h"
     8 
     9 
    10 using namespace ATL;
    11 using namespace std;
    12 
    13 class CpEpCOMServerAdapterModule;
    14 
    15 static class GateKeeper {
    16 public:
    17     static const time_t cycle = 7200;   // 7200 sec is 2 h
    18     static const DWORD waiting = 10000; // 10000 ms is 10 sec
    19 
    20     static time_t time_diff()
    21     {
    22         try {
    23             static random_device rd;
    24             static mt19937 gen(rd());
    25 
    26             uniform_int_distribution<time_t> dist(0, cycle);
    27 
    28             return dist(gen);
    29         }
    30         catch (exception& e) {
    31             assert(0);
    32             return 0;
    33         }
    34     }
    35 
    36     GateKeeper(CpEpCOMServerAdapterModule * const self) : _self(self), now(time(NULL)), next(now + time_diff())
    37     {
    38 
    39     }
    40 
    41     void keep();
    42 
    43     void keep_plugin();
    44     void keep_updated();
    45 
    46 private:
    47     time_t now;
    48     time_t next;
    49 
    50     CpEpCOMServerAdapterModule * const _self;
    51 };
    52 
    53 void GateKeeper::keep()
    54 {
    55     while (1) {
    56         keep_plugin();
    57 
    58         Sleep(10000);
    59         
    60         now = time(NULL);
    61         assert(now != -1);
    62         
    63         if (now > next) {
    64             next = now + GateKeeper::cycle;
    65             keep_updated();
    66         }
    67     }
    68 }
    69 
    70 void GateKeeper::keep_plugin()
    71 {
    72 
    73 }
    74 
    75 void GateKeeper::keep_updated()
    76 {
    77 
    78 }
    79 
    80 class CpEpCOMServerAdapterModule : public ATL::CAtlExeModuleT< CpEpCOMServerAdapterModule >
    81 {
    82 public:
    83     CpEpCOMServerAdapterModule() : ATL::CAtlExeModuleT< CpEpCOMServerAdapterModule >(), gatekeeper_thread(NULL)
    84     {
    85         gatekeeper_thread = new thread(gatekeeper, this);
    86         gatekeeper_thread->detach();
    87     }
    88 
    89 	DECLARE_LIBID(LIBID_pEpCOMServerAdapterLib)
    90 	DECLARE_REGISTRY_APPID_RESOURCEID(IDR_PEPCOMSERVERADAPTER, "{B3480081-82C0-4EE4-9AA1-3F513C9D78DD}")
    91 
    92 private:
    93     thread *gatekeeper_thread;
    94 
    95 protected:
    96     static void gatekeeper(CpEpCOMServerAdapterModule * const self);
    97 };
    98 
    99 void CpEpCOMServerAdapterModule::gatekeeper(CpEpCOMServerAdapterModule * const self)
   100 {
   101     GateKeeper keeper(self);
   102     keeper.keep();
   103 }
   104 
   105 CpEpCOMServerAdapterModule _AtlModule;
   106 
   107 //
   108 extern "C" int WINAPI _tWinMain(HINSTANCE /*hInstance*/, HINSTANCE /*hPrevInstance*/, 
   109 								LPTSTR /*lpCmdLine*/, int nShowCmd)
   110 {
   111     return _AtlModule.WinMain(nShowCmd);
   112 }
   113