pEpCOMServerAdapter.cpp
author Jorg Knobloch
Fri, 17 Jul 2020 19:20:29 +0200
changeset 450 bd19e2ba0d54
parent 440 f8a0bd27e38e
permissions -rw-r--r--
Used latest icon provided in COM-116.
     1 // pEpCOMServerAdapter.cpp : Implementation of WinMain
     2 
     3 
     4 #include "stdafx.h"
     5 #include "resource.h"
     6 #include "xdlldata.h"
     7 
     8 #include "GateKeeper.h"
     9 #include "pEpCOMServerAdapter.h"
    10 #include "LocalJSONAdapter.h"
    11 #include "CMainWindow.h"
    12 
    13 using namespace ATL;
    14 using namespace std;
    15 
    16 #ifndef NDEBUG
    17 // Stuff for the JSON server follows.
    18 #include <boost/filesystem/path.hpp>
    19 #include <boost/program_options.hpp>
    20 namespace po = boost::program_options;
    21 std::string logfile = "";
    22 #endif
    23 
    24 void CpEpCOMServerAdapterModule::gatekeeper(CpEpCOMServerAdapterModule * self)
    25 {
    26     pEp::GateKeeper keeper(self);
    27     keeper.keep();
    28 }
    29 
    30 CpEpCOMServerAdapterModule _AtlModule;
    31 LocalJSONAdapter* ljs = nullptr;
    32 CMainWindow mainWindow;
    33 
    34 extern "C" int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE /*hPrevInstance*/, 
    35                                 LPTSTR lpCmdLine, int nShowCmd)
    36 {
    37 #ifndef NDEBUG
    38     std::ios::sync_with_stdio(false);
    39 
    40     // Command line options and console output are for debug only.
    41 
    42     // Some Windows acrobatics so we can use this even with a command line
    43     // and get some output onto the console.
    44     if (AttachConsole(ATTACH_PARENT_PROCESS)) {
    45         FILE *f;
    46         freopen_s(&f, "conout$", "w", stdout);
    47         freopen_s(&f, "conout$", "w", stderr);
    48     }
    49 
    50     po::options_description desc("Program options for the JSON Server Adapter");
    51     desc.add_options()
    52         ("help,h", "print this help messages")
    53         ("debug,d", "debug,d ignored, only works in JSON mini adapter")
    54         ("logfile,l", po::value<std::string>(&logfile)->default_value(logfile),
    55             "Name of the logfile. Can be \"stderr\" for log to stderr or empty for no log.")
    56     ;
    57 
    58     po::variables_map vm;
    59     try {
    60         std::vector<std::wstring> args = po::split_winmain(lpCmdLine);
    61         po::store(po::wcommand_line_parser(args).options(desc).run(), vm);
    62         po::notify(vm);
    63     } catch (const po::error& e) {
    64         std::cerr << "Cannot parse command line: " << e.what() << "\n\n" << desc << std::endl;
    65         return 2;
    66     }
    67 
    68     if (vm.count("help")) {
    69         std::cout << desc << "\n";
    70         return 0;
    71     }
    72 
    73     if (logfile.empty()) {
    74         Logger::setDefaultTarget(Logger::Target::None);
    75     } else if (logfile == "stderr") {
    76         Logger::setDefaultTarget(Logger::Target::Console);
    77     } else {
    78         Logger::setDefaultTarget(Logger::Target::File);
    79     }
    80 
    81     Logger::start("JsonAdapter", logfile);
    82 #endif
    83 
    84     _AtlModule.hModule(hInstance);
    85     _AtlModule.start_gatekeeper();
    86 
    87     PEP_SESSION first_session;
    88     PEP_STATUS status = ::init(&first_session, NULL, NULL);
    89 
    90     if (!boost::algorithm::iequals(lpCmdLine, "/regserver")) {
    91         ljs = &LocalJSONAdapter::createInstance();
    92         ljs->startup(pEp::CallbackDispatcher::messageToSend);
    93         pEp::callback_dispatcher.add(JsonAdapter::messageToSend, JsonAdapter::notifyHandshake);
    94         auto mw = mainWindow.Create(HWND_MESSAGE);
    95         assert(mw);
    96     }
    97 
    98     auto rv = _AtlModule.WinMain(nShowCmd);
    99 
   100     if (ljs) {
   101         BOOL r = true;
   102         mainWindow.OnDestroy(0, 0, 0, r);
   103         pEp::callback_dispatcher.remove(JsonAdapter::messageToSend);
   104         ljs->shutdown_now();
   105     }
   106 
   107     ::release(first_session);
   108     ExitProcess(rv);
   109     return rv;
   110 }