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