int main(int argc, char** argv) { printf("Running CSL Version: %s\n", CSL::GetVersionString()); string configFile; struct option longopts[] = { { "config", required_argument, NULL, 'f' }, { 0, 0, 0, 0 } }; int opt; while ((opt = getopt_long(argc, argv, "f:", longopts, NULL)) != -1) { switch (opt) { case 'f': configFile = string(optarg); break; } } if (configFile.empty()) { fprintf(stderr, "Error: Please specify a config file\n"); usage(); return 1; } Config config; if (!config.Load(configFile)) { fprintf(stderr, "Error: Failed to load config file %s\n", configFile.c_str()); return 1; } std::thread httpThread([&](){ HTTPServer* pServer = new HTTPServer(config); pServer->Run(); delete pServer; }); httpThread.join(); return 0; }
int main(int argc, char** argv) { struct event_base* base; struct evhttp* http; struct evhttp_bound_socket* handle; unsigned short port = 15520; if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) return (1); base = event_base_new(); if (!base) { fprintf(stderr, "Couldn't create an event_base: exiting\n"); return 1; } http = evhttp_new(base); evhttp_set_cb(http, "/led/blink", led_blink, NULL); handle = evhttp_bind_socket_with_handle(http, "0.0.0.0", port); //TODO: factor out thread std::thread cmdThread([&]() { //TODO, the locking is to broad at the moment // during executing a command the queue is locked // and therefore no new commands can be added // copy the command and callback and release the lock // would be a solution std::unique_lock<std::mutex> lock(cs_queue); while (!stopThread) { while (!notified) { // loop to avoid spurious wakeups queueCondVar.wait(lock); } while (!cmdQueue.empty()) { std::string cmdOut; t_cmdCB cmdCB = cmdQueue.front(); std::string cmd = std::get<0>(cmdCB); std::string password = std::get<1>(cmdCB); if (!password.empty()) { std::string base64str; std::string unencryptedJson; try { DBB::encryptAndEncodeCommand(cmd, password, base64str); if (!DBB::sendCommand(base64str, cmdOut)) unencryptedJson = "sending command failed"; else DBB::decryptAndDecodeCommand(cmdOut, password, unencryptedJson); } catch (const std::exception& ex) { unencryptedJson = "response decryption failed: "+cmdOut; } cmdOut = unencryptedJson; } else { DBB::sendCommand(cmd, cmdOut); } std::get<2>(cmdCB)(cmdOut); cmdQueue.pop(); } notified = false; } }); //create a thread for the http handling std::thread usbCheckThread([&]() { while(1) { //check devices if (!DBB::isConnectionOpen()) { if (DBB::openConnection()) { #ifdef DBB_ENABLE_QT //TODO, check if this requires locking if (widget) widget->deviceStateHasChanged(true); #endif } else { #ifdef DBB_ENABLE_QT if (widget) widget->deviceStateHasChanged(false); #endif } } std::this_thread::sleep_for(std::chrono::milliseconds(1000)); } }); ECC_Start(); #ifdef DBB_ENABLE_QT #if QT_VERSION > 0x050100 // Generate high-dpi pixmaps QApplication::setAttribute(Qt::AA_UseHighDpiPixmaps); #endif //create a thread for the http handling std::thread httpThread([&]() { event_base_dispatch(base); }); QApplication app(argc, argv); widget = new DBBDaemonGui(0); widget->show(); app.exec(); #else //directly start libevents main run loop event_base_dispatch(base); #endif ECC_Stop(); exit(1); }