From 8c1a1045a3d9c3064b1aeb4720a5751e10d02c83 Mon Sep 17 00:00:00 2001 From: Fancy code <258828110.@qq.com> Date: Wed, 28 Feb 2024 22:08:58 -0800 Subject: [PATCH] Backup:AppManager module. --- middleware/AppManager/CMakeLists.txt | 1 + middleware/AppManager/include/IAppManager.h | 10 +- middleware/AppManager/src/AppManager.cpp | 20 +++- middleware/AppManager/src/AppManager.h | 3 + .../AppManager/src/AppManagerMakePtr.cpp | 8 ++ middleware/AppManager/src/AppManagerMakePtr.h | 2 + .../AppManager/src/IAppProtocolHandle.h | 40 ++++++++ middleware/AppManager/src/Protocol/README.md | 3 + .../src/Protocol/SixFrame/SixFrameHandle.cpp | 91 +++++++++++++++++++ .../src/Protocol/SixFrame/SixFrameHandle.h | 45 +++++++++ .../src/Protocol/SixFrame/SixFrameMakePtr.cpp | 14 +++ .../src/Protocol/SixFrame/SixFrameMakePtr.h | 29 ++++++ utils/FxHttpServer/include/FxHttpServer.h | 2 +- utils/FxHttpServer/src/FxHttpServer.c | 12 ++- 14 files changed, 269 insertions(+), 11 deletions(-) create mode 100644 middleware/AppManager/src/IAppProtocolHandle.h create mode 100644 middleware/AppManager/src/Protocol/README.md create mode 100644 middleware/AppManager/src/Protocol/SixFrame/SixFrameHandle.cpp create mode 100644 middleware/AppManager/src/Protocol/SixFrame/SixFrameHandle.h create mode 100644 middleware/AppManager/src/Protocol/SixFrame/SixFrameMakePtr.cpp create mode 100644 middleware/AppManager/src/Protocol/SixFrame/SixFrameMakePtr.h diff --git a/middleware/AppManager/CMakeLists.txt b/middleware/AppManager/CMakeLists.txt index 49c11d1c..b92e02be 100644 --- a/middleware/AppManager/CMakeLists.txt +++ b/middleware/AppManager/CMakeLists.txt @@ -16,6 +16,7 @@ include_directories( #) aux_source_directory(./src SRC_FILES) +aux_source_directory(./src/Protocol/SixFrame SRC_FILES) set(TARGET_NAME AppManager) add_library(${TARGET_NAME} STATIC ${SRC_FILES}) diff --git a/middleware/AppManager/include/IAppManager.h b/middleware/AppManager/include/IAppManager.h index cfc80171..ad529bc5 100644 --- a/middleware/AppManager/include/IAppManager.h +++ b/middleware/AppManager/include/IAppManager.h @@ -12,14 +12,20 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#ifndef IDEVICEMANAGER_H -#define IDEVICEMANAGER_H +#ifndef I_APP_EMANAGER_H +#define I_APP_EMANAGER_H #include "StatusCode.h" #include #include #include bool CreateAppManagerModule(void); bool DestroyAppManagerModule(void); +class VAppMonitor +{ +public: + VAppMonitor() = default; + virtual ~VAppMonitor() = default; +}; class IAppManager { public: diff --git a/middleware/AppManager/src/AppManager.cpp b/middleware/AppManager/src/AppManager.cpp index 8a107203..40f10128 100644 --- a/middleware/AppManager/src/AppManager.cpp +++ b/middleware/AppManager/src/AppManager.cpp @@ -13,6 +13,7 @@ * limitations under the License. */ #include "AppManager.h" +#include "AppManagerMakePtr.h" #include "FxHttpServer.h" #include "ILog.h" AppManager::AppManager() @@ -22,14 +23,22 @@ AppManager::AppManager() } 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) { @@ -47,13 +56,16 @@ void AppManager::HttpServerStop(void) } void AppManager::HttpServerThread(void) { - // typedef void (*HttpHandleCallback)(const char *, const unsigned int, ResponseHandle, 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); + // 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); + FxHttpServerInit(httpHandle, 8080); FxHttpServerUnInit(); } diff --git a/middleware/AppManager/src/AppManager.h b/middleware/AppManager/src/AppManager.h index 46b4117e..d807222f 100644 --- a/middleware/AppManager/src/AppManager.h +++ b/middleware/AppManager/src/AppManager.h @@ -15,6 +15,7 @@ #ifndef APP_MANAGER_H #define APP_MANAGER_H #include "IAppManager.h" +#include "IAppProtocolHandle.h" #include class AppManager : public IAppManager, public std::enable_shared_from_this { @@ -23,6 +24,7 @@ public: virtual ~AppManager() = default; const StatusCode Init(void) override; const StatusCode UnInit(void) override; + void AppRequestHandle(const char *url, const unsigned int urlLength, ResponseHandle responseHandle, void *context); private: void HttpServerStart(void); @@ -32,6 +34,7 @@ private: private: // bool mHttpServerRuning; std::thread mHttpSever; + std::shared_ptr mProtocolHandle; }; #endif diff --git a/middleware/AppManager/src/AppManagerMakePtr.cpp b/middleware/AppManager/src/AppManagerMakePtr.cpp index a556ce48..76a77516 100644 --- a/middleware/AppManager/src/AppManagerMakePtr.cpp +++ b/middleware/AppManager/src/AppManagerMakePtr.cpp @@ -15,8 +15,10 @@ #include "AppManagerMakePtr.h" #include "AppManager.h" #include "ILog.h" +extern bool CreateProtocolHandleImpl(); bool CreateAppManagerModule(void) { + CreateProtocolHandleImpl(); // TODO: not good for gtest. auto instance = std::make_shared(); StatusCode code = AppManagerMakePtr::GetInstance()->CreateAppManager(instance); if (IsCodeOK(code)) { @@ -52,4 +54,10 @@ const StatusCode AppManagerMakePtr::CreateAppManager(std::shared_ptr(); impl = tmp; return CreateStatusCode(STATUS_CODE_OK); +} +const StatusCode AppManagerMakePtr::CreateProtocolHandle(std::shared_ptr &impl) +{ + auto tmp = std::make_shared(); + impl = tmp; + return CreateStatusCode(STATUS_CODE_OK); } \ No newline at end of file diff --git a/middleware/AppManager/src/AppManagerMakePtr.h b/middleware/AppManager/src/AppManagerMakePtr.h index a00112df..13e8d0f6 100644 --- a/middleware/AppManager/src/AppManagerMakePtr.h +++ b/middleware/AppManager/src/AppManagerMakePtr.h @@ -15,6 +15,7 @@ #ifndef DEVICE_MANAGER_MAKE_PTR_H #define DEVICE_MANAGER_MAKE_PTR_H #include "IAppManager.h" +#include "IAppProtocolHandle.h" #include "StatusCode.h" #include class AppManagerMakePtr @@ -24,5 +25,6 @@ public: virtual ~AppManagerMakePtr() = default; static std::shared_ptr &GetInstance(std::shared_ptr *impl = nullptr); virtual const StatusCode CreateAppManager(std::shared_ptr &impl); + virtual const StatusCode CreateProtocolHandle(std::shared_ptr &impl); }; #endif \ No newline at end of file diff --git a/middleware/AppManager/src/IAppProtocolHandle.h b/middleware/AppManager/src/IAppProtocolHandle.h new file mode 100644 index 00000000..6d48e5bf --- /dev/null +++ b/middleware/AppManager/src/IAppProtocolHandle.h @@ -0,0 +1,40 @@ +/* + * 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. + */ +#ifndef I_APP_PROTOCOL_HANDLE_H +#define I_APP_PROTOCOL_HANDLE_H +#include "FxHttpServer.h" +#include "StatusCode.h" +#include +#include +#include +class VAppDataPacket +{ +public: + VAppDataPacket() = default; + virtual ~VAppDataPacket() = default; +}; +class IAppProtocolHandle +{ +public: + IAppProtocolHandle() = default; + virtual ~IAppProtocolHandle() = default; + virtual void Init(void) {} + virtual void UnInit(void) {} + virtual void RequestHandle(const char *url, const unsigned int urlLength, ResponseHandle responseHandle, + void *context) + { + } +}; +#endif \ No newline at end of file diff --git a/middleware/AppManager/src/Protocol/README.md b/middleware/AppManager/src/Protocol/README.md new file mode 100644 index 00000000..ba57168f --- /dev/null +++ b/middleware/AppManager/src/Protocol/README.md @@ -0,0 +1,3 @@ +# 1. 协议目录说明 + +   可配置选型不同的对接协议。 \ No newline at end of file diff --git a/middleware/AppManager/src/Protocol/SixFrame/SixFrameHandle.cpp b/middleware/AppManager/src/Protocol/SixFrame/SixFrameHandle.cpp new file mode 100644 index 00000000..9bc9d4d9 --- /dev/null +++ b/middleware/AppManager/src/Protocol/SixFrame/SixFrameHandle.cpp @@ -0,0 +1,91 @@ +/* + * 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 "SixFrameHandle.h" +#include "ILog.h" +#include +#include +using std::placeholders::_1; +using std::placeholders::_2; +using std::placeholders::_3; +// using std::placeholders::_4; +SixFrameHandle::SixFrameHandle() +{ + // mResquesHandleFunc["set"] = std::bind(&SixFrameHandle::test, this, _1, _2, _3); + mResquesHandleFunc["app/set"] = std::bind(&SixFrameHandle::test, this, _1, _2, _3); + mResquesHandleFunc["favicon.ico"] = std::bind(&SixFrameHandle::DoNothing, this, _1, _2, _3); +} +void SixFrameHandle::RequestHandle(const char *url, const unsigned int urlLength, ResponseHandle responseHandle, + void *context) +{ + std::multimap paramsMap; + const std::string urlStr = url; + size_t queryStartPos = urlStr.find("HTTP"); + if (queryStartPos != std::string::npos && queryStartPos + 1 < urlStr.length()) { + const std::string urlStr2 = urlStr.substr(0, queryStartPos - 1); + LogInfo("URL = %s\n", urlStr2.c_str()); + queryStartPos = urlStr2.find('?'); + std::string command = ""; + if (queryStartPos != std::string::npos && queryStartPos + 1 < urlStr2.length()) { + command = urlStr2.substr(1, queryStartPos - 1); + } + else { + command = urlStr2.substr(1, urlStr2.length()); + } + LogInfo("command = %s\n", command.c_str()); + ExtractParamsFromUrl(urlStr2, paramsMap); + RequestHandle(command, paramsMap, responseHandle, context); + } +} +void SixFrameHandle::ExtractParamsFromUrl(const std::string &url, std::multimap ¶msMap) +{ + size_t queryStartPos = url.find('?'); + if (queryStartPos != std::string::npos && queryStartPos + 1 < url.length()) { + std::string paramsStr = url.substr(queryStartPos + 1); + + std::istringstream iss(paramsStr); + std::string token; + while (getline(iss, token, '&')) { + size_t equalSignPos = token.find('='); + if (equalSignPos != std::string::npos) { + std::string key = token.substr(0, equalSignPos); + std::string value = token.substr(equalSignPos + 1); + LogInfo("url get [%s] = %s\n", key.c_str(), value.c_str()); + paramsMap.insert({key, value}); + } + } + } +} +void SixFrameHandle::test(std::multimap ¶msMap, ResponseHandle responseHandle, + void *context) +{ + // + LogInfo("sssssssssssssssssssssssssssssssss\n"); +} +void SixFrameHandle::RequestHandle(const std::string command, std::multimap ¶msMap, + ResponseHandle responseHandle, void *context) +{ + auto result = mResquesHandleFunc.find(command); + if (result != mResquesHandleFunc.end()) { + (*result).second(paramsMap, responseHandle, context); + } + else { + LogError("Unknown command.\n"); + } +} +void SixFrameHandle::DoNothing(std::multimap ¶msMap, ResponseHandle responseHandle, + void *context) +{ + // +} \ No newline at end of file diff --git a/middleware/AppManager/src/Protocol/SixFrame/SixFrameHandle.h b/middleware/AppManager/src/Protocol/SixFrame/SixFrameHandle.h new file mode 100644 index 00000000..42d2901f --- /dev/null +++ b/middleware/AppManager/src/Protocol/SixFrame/SixFrameHandle.h @@ -0,0 +1,45 @@ +/* + * 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. + */ +#ifndef SIX_FRAME_HANDLE_H +#define SIX_FRAME_HANDLE_H +#include "IAppProtocolHandle.h" +#include "StatusCode.h" +#include +#include +#include +#include +#include +using ResquesHandleFunc = std::function &, ResponseHandle, void *)>; +class SixFrameHandle : public IAppProtocolHandle +{ +public: + SixFrameHandle(); + virtual ~SixFrameHandle() = default; + // virtual void Init(void) {} + // virtual void UnInit(void) {} + void RequestHandle(const char *url, const unsigned int urlLength, ResponseHandle responseHandle, + void *context) override; + +private: + void ExtractParamsFromUrl(const std::string &url, std::multimap ¶msMap); + void test(std::multimap ¶msMap, ResponseHandle responseHandle, void *context); + void RequestHandle(const std::string command, std::multimap ¶msMap, + ResponseHandle responseHandle, void *context); + void DoNothing(std::multimap ¶msMap, ResponseHandle responseHandle, void *context); + +private: + std::map mResquesHandleFunc; +}; +#endif \ No newline at end of file diff --git a/middleware/AppManager/src/Protocol/SixFrame/SixFrameMakePtr.cpp b/middleware/AppManager/src/Protocol/SixFrame/SixFrameMakePtr.cpp new file mode 100644 index 00000000..526e9e65 --- /dev/null +++ b/middleware/AppManager/src/Protocol/SixFrame/SixFrameMakePtr.cpp @@ -0,0 +1,14 @@ +#include "SixFrameMakePtr.h" +#include "SixFrameHandle.h" +bool CreateProtocolHandleImpl(void) +{ + std::shared_ptr instance = std::make_shared(); + AppManagerMakePtr::GetInstance(&instance); + return true; +} +const StatusCode SixFrameMakePtr::CreateProtocolHandle(std::shared_ptr &impl) +{ + auto tmp = std::make_shared(); + impl = tmp; + return CreateStatusCode(STATUS_CODE_OK); +} \ No newline at end of file diff --git a/middleware/AppManager/src/Protocol/SixFrame/SixFrameMakePtr.h b/middleware/AppManager/src/Protocol/SixFrame/SixFrameMakePtr.h new file mode 100644 index 00000000..74a682bf --- /dev/null +++ b/middleware/AppManager/src/Protocol/SixFrame/SixFrameMakePtr.h @@ -0,0 +1,29 @@ +/* + * 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. + */ +#ifndef SIX_FRAME_MAKE_PTR_H +#define SIX_FRAME_MAKE_PTR_H +#include "AppManagerMakePtr.h" +#include "IAppManager.h" +#include "StatusCode.h" +#include +bool CreateProtocolHandleImpl(void); +class SixFrameMakePtr : public AppManagerMakePtr +{ +public: + SixFrameMakePtr() = default; + virtual ~SixFrameMakePtr() = default; + const StatusCode CreateProtocolHandle(std::shared_ptr &impl) override; +}; +#endif \ No newline at end of file diff --git a/utils/FxHttpServer/include/FxHttpServer.h b/utils/FxHttpServer/include/FxHttpServer.h index 00a9c178..ae1647a6 100644 --- a/utils/FxHttpServer/include/FxHttpServer.h +++ b/utils/FxHttpServer/include/FxHttpServer.h @@ -20,7 +20,7 @@ extern "C" { #endif typedef void (*ResponseHandle)(const char *, void *); typedef void (*HttpHandleCallback)(const char *, const unsigned int, ResponseHandle, void *); -StatusCode FxHttpServerInit(HttpHandleCallback httpHandle); +StatusCode FxHttpServerInit(HttpHandleCallback httpHandle, const int port); StatusCode FxHttpServerExit(void); StatusCode FxHttpServerUnInit(void); #ifdef __cplusplus diff --git a/utils/FxHttpServer/src/FxHttpServer.c b/utils/FxHttpServer/src/FxHttpServer.c index d68cb577..be3813c5 100644 --- a/utils/FxHttpServer/src/FxHttpServer.c +++ b/utils/FxHttpServer/src/FxHttpServer.c @@ -37,16 +37,20 @@ static void handle_request(struct http_request_s *request) gHttpHandle(url.buf, url.len, response_handle, response); http_respond(request, response); } -StatusCode FxHttpServerInit(HttpHandleCallback httpHandle) +StatusCode FxHttpServerInit(HttpHandleCallback httpHandle, const int port) { - server = http_server_init(8080, handle_request); - poll_server = http_server_init(8081, handle_request); + // poll_server = http_server_init(port + 1, handle_request); if (NULL == httpHandle) { LogError("FxHttpServerInit failed. Callback function is nullptr.\n"); return CreateStatusCode(STATUS_CODE_NOT_OK); } + server = http_server_init(port, handle_request); + if (!server) { + LogError("http_server_init failed.\n"); + return CreateStatusCode(STATUS_CODE_NOT_OK); + } gHttpHandle = httpHandle; - http_server_listen_poll(poll_server); + // http_server_listen_poll(poll_server); http_server_listen(server); return CreateStatusCode(STATUS_CODE_OK); }