Backup:AppManager module.
This commit is contained in:
parent
359eec1914
commit
8c1a1045a3
|
@ -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})
|
||||
|
|
|
@ -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 <iostream>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
bool CreateAppManagerModule(void);
|
||||
bool DestroyAppManagerModule(void);
|
||||
class VAppMonitor
|
||||
{
|
||||
public:
|
||||
VAppMonitor() = default;
|
||||
virtual ~VAppMonitor() = default;
|
||||
};
|
||||
class IAppManager
|
||||
{
|
||||
public:
|
||||
|
|
|
@ -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<AppManager> 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<AppManager> 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<IAppManager> app = IAppManager::GetInstance();
|
||||
std::shared_ptr<AppManager> appImpl = std::dynamic_pointer_cast<AppManager>(app);
|
||||
if (appImpl) {
|
||||
appImpl->AppRequestHandle(url, urlLength, responseHandle, context);
|
||||
}
|
||||
};
|
||||
FxHttpServerInit(httpHandle);
|
||||
FxHttpServerInit(httpHandle, 8080);
|
||||
FxHttpServerUnInit();
|
||||
}
|
||||
|
|
|
@ -15,6 +15,7 @@
|
|||
#ifndef APP_MANAGER_H
|
||||
#define APP_MANAGER_H
|
||||
#include "IAppManager.h"
|
||||
#include "IAppProtocolHandle.h"
|
||||
#include <thread>
|
||||
class AppManager : public IAppManager, public std::enable_shared_from_this<AppManager>
|
||||
{
|
||||
|
@ -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<IAppProtocolHandle> mProtocolHandle;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
@ -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<IAppManager>();
|
||||
StatusCode code = AppManagerMakePtr::GetInstance()->CreateAppManager(instance);
|
||||
if (IsCodeOK(code)) {
|
||||
|
@ -53,3 +55,9 @@ const StatusCode AppManagerMakePtr::CreateAppManager(std::shared_ptr<IAppManager
|
|||
impl = tmp;
|
||||
return CreateStatusCode(STATUS_CODE_OK);
|
||||
}
|
||||
const StatusCode AppManagerMakePtr::CreateProtocolHandle(std::shared_ptr<IAppProtocolHandle> &impl)
|
||||
{
|
||||
auto tmp = std::make_shared<IAppProtocolHandle>();
|
||||
impl = tmp;
|
||||
return CreateStatusCode(STATUS_CODE_OK);
|
||||
}
|
|
@ -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 <memory>
|
||||
class AppManagerMakePtr
|
||||
|
@ -24,5 +25,6 @@ public:
|
|||
virtual ~AppManagerMakePtr() = default;
|
||||
static std::shared_ptr<AppManagerMakePtr> &GetInstance(std::shared_ptr<AppManagerMakePtr> *impl = nullptr);
|
||||
virtual const StatusCode CreateAppManager(std::shared_ptr<IAppManager> &impl);
|
||||
virtual const StatusCode CreateProtocolHandle(std::shared_ptr<IAppProtocolHandle> &impl);
|
||||
};
|
||||
#endif
|
40
middleware/AppManager/src/IAppProtocolHandle.h
Normal file
40
middleware/AppManager/src/IAppProtocolHandle.h
Normal file
|
@ -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 <iostream>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
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
|
3
middleware/AppManager/src/Protocol/README.md
Normal file
3
middleware/AppManager/src/Protocol/README.md
Normal file
|
@ -0,0 +1,3 @@
|
|||
# 1. 协议目录说明
|
||||
|
||||
   可配置选型不同的对接协议。
|
|
@ -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 <sstream>
|
||||
#include <stdio.h>
|
||||
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<std::string, std::string> 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<std::string, std::string> ¶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<std::string, std::string> ¶msMap, ResponseHandle responseHandle,
|
||||
void *context)
|
||||
{
|
||||
//
|
||||
LogInfo("sssssssssssssssssssssssssssssssss\n");
|
||||
}
|
||||
void SixFrameHandle::RequestHandle(const std::string command, std::multimap<std::string, std::string> ¶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<std::string, std::string> ¶msMap, ResponseHandle responseHandle,
|
||||
void *context)
|
||||
{
|
||||
//
|
||||
}
|
45
middleware/AppManager/src/Protocol/SixFrame/SixFrameHandle.h
Normal file
45
middleware/AppManager/src/Protocol/SixFrame/SixFrameHandle.h
Normal file
|
@ -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 <functional>
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
using ResquesHandleFunc = std::function<void(std::multimap<std::string, std::string> &, 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<std::string, std::string> ¶msMap);
|
||||
void test(std::multimap<std::string, std::string> ¶msMap, ResponseHandle responseHandle, void *context);
|
||||
void RequestHandle(const std::string command, std::multimap<std::string, std::string> ¶msMap,
|
||||
ResponseHandle responseHandle, void *context);
|
||||
void DoNothing(std::multimap<std::string, std::string> ¶msMap, ResponseHandle responseHandle, void *context);
|
||||
|
||||
private:
|
||||
std::map<std::string, ResquesHandleFunc> mResquesHandleFunc;
|
||||
};
|
||||
#endif
|
|
@ -0,0 +1,14 @@
|
|||
#include "SixFrameMakePtr.h"
|
||||
#include "SixFrameHandle.h"
|
||||
bool CreateProtocolHandleImpl(void)
|
||||
{
|
||||
std::shared_ptr<AppManagerMakePtr> instance = std::make_shared<SixFrameMakePtr>();
|
||||
AppManagerMakePtr::GetInstance(&instance);
|
||||
return true;
|
||||
}
|
||||
const StatusCode SixFrameMakePtr::CreateProtocolHandle(std::shared_ptr<IAppProtocolHandle> &impl)
|
||||
{
|
||||
auto tmp = std::make_shared<SixFrameHandle>();
|
||||
impl = tmp;
|
||||
return CreateStatusCode(STATUS_CODE_OK);
|
||||
}
|
|
@ -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 <memory>
|
||||
bool CreateProtocolHandleImpl(void);
|
||||
class SixFrameMakePtr : public AppManagerMakePtr
|
||||
{
|
||||
public:
|
||||
SixFrameMakePtr() = default;
|
||||
virtual ~SixFrameMakePtr() = default;
|
||||
const StatusCode CreateProtocolHandle(std::shared_ptr<IAppProtocolHandle> &impl) override;
|
||||
};
|
||||
#endif
|
|
@ -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
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user