/* * Copyright (c) 2023 Fancy Code. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "AppManager.h" #include "AppManagerMakePtr.h" // #include "FxHttpServer.h" #include "ILog.h" #include "WebServer.h" AppManager::AppManager() { // // mHttpServerRuning = false; } const StatusCode AppManager::Init(void) { AppManagerMakePtr::GetInstance()->CreateProtocolHandle(mProtocolHandle); HttpServerStart(); return CreateStatusCode(STATUS_CODE_OK); } const StatusCode AppManager::UnInit(void) { HttpServerStop(); mProtocolHandle.reset(); return CreateStatusCode(STATUS_CODE_OK); } void AppManager::AppRequestHandle(const char *url, const unsigned int urlLength, ResponseHandle responseHandle, void *context) { // mProtocolHandle->RequestHandle(url, urlLength, responseHandle, context); } void AppManager::HttpServerStart(void) { auto httpServerThread = [](std::shared_ptr app) { LogInfo("AppManager httpServer started.\n"); app->HttpServerThread(); }; mHttpSever = std::thread(httpServerThread, shared_from_this()); } void AppManager::HttpServerStop(void) { // FxHttpServerExit(); WebServerExit(); if (mHttpSever.joinable()) { mHttpSever.join(); } } void AppManager::HttpServerThread(void) { std::shared_ptr app = shared_from_this(); auto httpHandle = [](const char *url, const unsigned int urlLength, ResponseHandle responseHandle, void *context) -> void { // LogInfo("url = %s\n", url); std::shared_ptr app = IAppManager::GetInstance(); std::shared_ptr appImpl = std::dynamic_pointer_cast(app); if (appImpl) { appImpl->AppRequestHandle(url, urlLength, responseHandle, context); } }; // FxHttpServerInit(httpHandle, 8080); // FxHttpServerUnInit(); WebServerParam web = { .mIp = APP_MANAGER_HTTP_SERVER_IP, .mPort = APP_MANAGER_HTTP_SERVER_PORT, .mHttpRequestHandle = httpHandle}; WebServerInit(web); WebServerUnInit(); }