Improve:AppManager tcp function.
This commit is contained in:
		
							parent
							
								
									2f6678c7eb
								
							
						
					
					
						commit
						bb0180882c
					
				|  | @ -22,7 +22,7 @@ void TestMissionState::GoInState() | |||
| { | ||||
|     MissionState::GoInState(); | ||||
|     LogInfo(" ========== TestMissionState::GoInState.\n"); | ||||
|     AppParam mAppParam(APP_MANAGER_HTTP_SERVER_IP, APP_MANAGER_HTTP_SERVER_PORT); // TODO:
 | ||||
|     AppParam mAppParam(APP_MANAGER_DEVICE_IP, APP_MANAGER_HTTP_SERVER_PORT, APP_MANAGER_TCP_SERVER_PORT); // TODO:
 | ||||
|     IAppManager::GetInstance()->Init(mAppParam); | ||||
|     std::shared_ptr<VAppMonitor> monitor = | ||||
|         std::dynamic_pointer_cast<TestMissionState>(MissionState::shared_from_this()); | ||||
|  |  | |||
|  | @ -68,7 +68,8 @@ set(CURL_SHARED_LIBS_PATH                  "/mnt/mmc") | |||
| # ------------ build curl + openssl ------------ end | ||||
| 
 | ||||
| # ------------ build AppManager ------------ # | ||||
| # set(APP_MANAGER_HTTP_SERVER_IP                           "localhost") | ||||
| set(APP_MANAGER_HTTP_SERVER_IP                           "192.168.1.29") | ||||
| set(APP_MANAGER_DEVICE_IP                                "localhost") | ||||
| # set(APP_MANAGER_DEVICE_IP                              "192.168.1.29") | ||||
| set(APP_MANAGER_HTTP_SERVER_PORT                         "8080") | ||||
| set(APP_MANAGER_TCP_SERVER_PORT                          "9876") | ||||
| # ------------ build AppManager end ------------ # | ||||
|  | @ -11,6 +11,7 @@ include_directories( | |||
|     ${UTILS_SOURCE_PATH}/Log/include | ||||
|     ${UTILS_SOURCE_PATH}/FxHttpServer/include | ||||
|     ${UTILS_SOURCE_PATH}/WebServer/include | ||||
|     ${UTILS_SOURCE_PATH}/TcpModule/include | ||||
|     ${HAL_SOURCE_PATH}/include | ||||
|     ${EXTERNAL_SOURCE_PATH}/cJSON-1.7.17 | ||||
| ) | ||||
|  | @ -24,7 +25,7 @@ aux_source_directory(./src/Protocol/SixFrame SRC_FILES) | |||
| set(TARGET_NAME AppManager) | ||||
| add_library(${TARGET_NAME} STATIC ${SRC_FILES}) | ||||
| 
 | ||||
| target_link_libraries(${TARGET_NAME} WebServer Hal cjson-static StatusCode Log) | ||||
| target_link_libraries(${TARGET_NAME} WebServer TcpModule Hal cjson-static StatusCode Log) | ||||
| 
 | ||||
| if ("${COMPILE_IMPROVE_SUPPORT}" MATCHES "true") | ||||
| add_custom_target( | ||||
|  |  | |||
|  | @ -1,8 +1,8 @@ | |||
| 
 | ||||
| if (NOT DEFINED APP_MANAGER_HTTP_SERVER_IP) | ||||
|     set(APP_MANAGER_HTTP_SERVER_IP "localhost") | ||||
| if (NOT DEFINED APP_MANAGER_DEVICE_IP) | ||||
|     set(APP_MANAGER_DEVICE_IP "localhost") | ||||
| endif() | ||||
| add_definitions(-DAPP_MANAGER_HTTP_SERVER_IP=\"${APP_MANAGER_HTTP_SERVER_IP}\") | ||||
| add_definitions(-DAPP_MANAGER_DEVICE_IP=\"${APP_MANAGER_DEVICE_IP}\") | ||||
| 
 | ||||
| if (NOT DEFINED APP_MANAGER_HTTP_SERVER_PORT) | ||||
|     message(FATAL_ERROR "You should set http listen port. | ||||
|  | @ -10,3 +10,9 @@ if (NOT DEFINED APP_MANAGER_HTTP_SERVER_PORT) | |||
|     Refer to:${IPC_SDK_PATH}/builde/cmake/toolchain/linux.toolchain.cmake") | ||||
| endif() | ||||
| add_definitions(-DAPP_MANAGER_HTTP_SERVER_PORT=${APP_MANAGER_HTTP_SERVER_PORT}) | ||||
| if (NOT DEFINED APP_MANAGER_TCP_SERVER_PORT) | ||||
|     message(FATAL_ERROR "You should set TCP listen port. | ||||
|     Example: set(APP_MANAGER_TCP_SERVER_PORT  \"8888\") | ||||
|     Refer to:${IPC_SDK_PATH}/builde/cmake/toolchain/linux.toolchain.cmake") | ||||
| endif() | ||||
| add_definitions(-DAPP_MANAGER_TCP_SERVER_PORT=${APP_MANAGER_TCP_SERVER_PORT}) | ||||
|  |  | |||
|  | @ -151,6 +151,24 @@ enum class FileCopy | |||
|     SUPPORT_COPY, | ||||
|     END | ||||
| }; | ||||
| enum class RecordingStatus | ||||
| { | ||||
|     RECORDING_STOP = 0, | ||||
|     RECORDING_START, | ||||
|     END | ||||
| }; | ||||
| enum class MicrophoneStatus | ||||
| { | ||||
|     OFF = 0, | ||||
|     ON, | ||||
|     END | ||||
| }; | ||||
| enum class BatteryStatus | ||||
| { | ||||
|     CHARGING = 0, | ||||
|     NOT_CHARGING, | ||||
|     END | ||||
| }; | ||||
| typedef struct app_get_product_info | ||||
| { | ||||
|     app_get_product_info() | ||||
|  | @ -327,6 +345,18 @@ typedef struct app_get_thumbnail | |||
|     const std::string mFile; | ||||
|     std::string mThumbnail; | ||||
| } AppGetThumbnail; | ||||
| class VAppClient | ||||
| { | ||||
| public: | ||||
|     VAppClient() = default; | ||||
|     virtual ~VAppClient() = default; | ||||
|     virtual void SetRecordingStatus(const RecordingStatus &status); | ||||
|     virtual void SetMicrophoneStatus(const MicrophoneStatus &status); | ||||
|     virtual void SetBatteryStatus(const BatteryStatus &status, const int &batteryCapacity); | ||||
|     virtual void SetSdCardStatus(const SdCardStatus &status); | ||||
|     virtual void DeletedFileMessage(const std::string &file, const StorageFileType &type); | ||||
|     virtual void CreatedFileMessage(const std::string &file, const StorageFileType &type); | ||||
| }; | ||||
| class VAppMonitor | ||||
| { | ||||
| public: | ||||
|  | @ -349,14 +379,16 @@ public: | |||
|     virtual StatusCode AppPlayback(const PlayBackEvent &event); | ||||
|     virtual StatusCode UploadFile(AppUploadFile ¶m); | ||||
|     virtual StatusCode GetThumbnail(AppGetThumbnail ¶m); | ||||
|     virtual StatusCode AppClientConnected(std::shared_ptr<VAppClient> &client); | ||||
| }; | ||||
| typedef struct app_param | ||||
| { | ||||
|     app_param(const char *ip, const int port) : mIP(ip), mPort(port) | ||||
|     app_param(const char *ip, const int &httpPort, const int &tcpPort) : mIP(ip), mHttpPort(httpPort), mTcpPort(tcpPort) | ||||
|     { | ||||
|     } | ||||
|     const char *mIP; | ||||
|     const int mPort; | ||||
|     const int mHttpPort; | ||||
|     const int mTcpPort; | ||||
| } AppParam; | ||||
| class IAppManager | ||||
| { | ||||
|  |  | |||
							
								
								
									
										51
									
								
								middleware/AppManager/src/AppClient.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										51
									
								
								middleware/AppManager/src/AppClient.cpp
									
									
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,51 @@ | |||
| /*
 | ||||
|  * 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 "AppClient.h" | ||||
| #include "IAppProtocolHandle.h" | ||||
| #include "TcpModule.h" | ||||
| AppClient::AppClient(const void *clientObject) : mClientObject(clientObject) | ||||
| { | ||||
| } | ||||
| void AppClient::SetRecordingStatus(const RecordingStatus &status) | ||||
| { | ||||
|     std::shared_ptr<ProtocolPacket> packet = IAppProtocolHandle::GetInstance()->SetRecordingStatus(status); | ||||
|     AcceptClientWrite((void *)mClientObject, packet->mData, packet->mDataLength); | ||||
| } | ||||
| void AppClient::SetMicrophoneStatus(const MicrophoneStatus &status) | ||||
| { | ||||
|     std::shared_ptr<ProtocolPacket> packet = IAppProtocolHandle::GetInstance()->SetMicrophoneStatus(status); | ||||
|     AcceptClientWrite((void *)mClientObject, packet->mData, packet->mDataLength); | ||||
| } | ||||
| void AppClient::SetBatteryStatus(const BatteryStatus &status, const int &batteryCapacity) | ||||
| { | ||||
|     std::shared_ptr<ProtocolPacket> packet = | ||||
|         IAppProtocolHandle::GetInstance()->SetBatteryStatus(status, batteryCapacity); | ||||
|     AcceptClientWrite((void *)mClientObject, packet->mData, packet->mDataLength); | ||||
| } | ||||
| void AppClient::SetSdCardStatus(const SdCardStatus &status) | ||||
| { | ||||
|     std::shared_ptr<ProtocolPacket> packet = IAppProtocolHandle::GetInstance()->SetSdCardStatus(status); | ||||
|     AcceptClientWrite((void *)mClientObject, packet->mData, packet->mDataLength); | ||||
| } | ||||
| void AppClient::DeletedFileMessage(const std::string &file, const StorageFileType &type) | ||||
| { | ||||
|     std::shared_ptr<ProtocolPacket> packet = IAppProtocolHandle::GetInstance()->DeletedFileMessage(file, type); | ||||
|     AcceptClientWrite((void *)mClientObject, packet->mData, packet->mDataLength); | ||||
| } | ||||
| void AppClient::CreatedFileMessage(const std::string &file, const StorageFileType &type) | ||||
| { | ||||
|     std::shared_ptr<ProtocolPacket> packet = IAppProtocolHandle::GetInstance()->CreatedFileMessage(file, type); | ||||
|     AcceptClientWrite((void *)mClientObject, packet->mData, packet->mDataLength); | ||||
| } | ||||
							
								
								
									
										33
									
								
								middleware/AppManager/src/AppClient.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										33
									
								
								middleware/AppManager/src/AppClient.h
									
									
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,33 @@ | |||
| /*
 | ||||
|  * 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 APP_CLIENT_H | ||||
| #define APP_CLIENT_H | ||||
| #include "IAppManager.h" | ||||
| class AppClient : public VAppClient | ||||
| { | ||||
| public: | ||||
|     AppClient(const void *clientObject); | ||||
|     virtual ~AppClient() = default; | ||||
|     void SetRecordingStatus(const RecordingStatus &status) override; | ||||
|     void SetMicrophoneStatus(const MicrophoneStatus &status) override; | ||||
|     void SetBatteryStatus(const BatteryStatus &status, const int &batteryCapacity) override; | ||||
|     void SetSdCardStatus(const SdCardStatus &status) override; | ||||
|     void DeletedFileMessage(const std::string &file, const StorageFileType &type) override; | ||||
|     void CreatedFileMessage(const std::string &file, const StorageFileType &type) override; | ||||
| 
 | ||||
| private: | ||||
|     const void *mClientObject; | ||||
| }; | ||||
| #endif | ||||
|  | @ -13,15 +13,15 @@ | |||
|  * limitations under the License. | ||||
|  */ | ||||
| #include "AppManager.h" | ||||
| #include "AppClient.h" | ||||
| #include "AppManagerMakePtr.h" | ||||
| // #include "FxHttpServer.h"
 | ||||
| #include "IHalCpp.h" | ||||
| #include "ILog.h" | ||||
| #include "TcpModule.h" | ||||
| #include "WebServer.h" | ||||
| AppManager::AppManager() | ||||
| { | ||||
|     // mHttpServerRuning = false;
 | ||||
|     mProtocolHandle = std::make_shared<IAppProtocolHandle>(); | ||||
|     mTcpServer = nullptr; | ||||
| } | ||||
| const StatusCode AppManager::Init(const AppParam ¶m) | ||||
| { | ||||
|  | @ -32,26 +32,53 @@ const StatusCode AppManager::Init(const AppParam ¶m) | |||
|         return CreateStatusCode(STATUS_CODE_NOT_OK); | ||||
|     } | ||||
|     wifi->OpenApMode(); | ||||
|     AppManagerMakePtr::GetInstance()->CreateProtocolHandle(mProtocolHandle); | ||||
|     std::shared_ptr<IAppProtocolHandle> protocolHandle; | ||||
|     AppManagerMakePtr::GetInstance()->CreateProtocolHandle(protocolHandle); | ||||
|     IAppProtocolHandle::GetInstance(&protocolHandle); | ||||
|     HttpServerStart(param); | ||||
|     TcpServerStart(param); | ||||
|     return CreateStatusCode(STATUS_CODE_OK); | ||||
| } | ||||
| const StatusCode AppManager::UnInit(void) | ||||
| { | ||||
|     HttpServerStop(); | ||||
|     mProtocolHandle.reset(); | ||||
|     TcpServerStop(); | ||||
|     std::shared_ptr<IAppProtocolHandle> protocolHandle = std::make_shared<IAppProtocolHandle>(); | ||||
|     IAppProtocolHandle::GetInstance(&protocolHandle); | ||||
|     mAppMonitor.reset(); | ||||
|     return CreateStatusCode(STATUS_CODE_OK); | ||||
| } | ||||
| const StatusCode AppManager::SetAppMonitor(std::shared_ptr<VAppMonitor> &monitor) | ||||
| { | ||||
|     mProtocolHandle->SetAppMonitor(monitor); | ||||
|     IAppProtocolHandle::GetInstance()->SetAppMonitor(monitor); | ||||
|     mAppMonitor = monitor; | ||||
|     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); | ||||
|     IAppProtocolHandle::GetInstance()->RequestHandle(url, urlLength, responseHandle, context); | ||||
| } | ||||
| void AppManager::AppRequestHandleTcp(const char *data, const unsigned int dataLength, ResponseHandle responseHandle, | ||||
|                                      void *context) | ||||
| { | ||||
| } | ||||
| void AppManager::AppClientConnected(const void *client, const char *ip) | ||||
| { | ||||
|     void *object = (void *)client; | ||||
|     mAppClients[object] = std::make_shared<AppClient>(client); | ||||
|     mAppMonitor->AppClientConnected(mAppClients[object]); | ||||
| } | ||||
| void AppManager::AppClientClosed(const void *client) | ||||
| { | ||||
|     void *object = (void *)client; | ||||
|     auto it = mAppClients.find(object); | ||||
|     if (it != mAppClients.end()) { | ||||
|         mAppClients.erase(it); | ||||
|     } | ||||
|     else { | ||||
|         LogError("App client not exit.\n"); | ||||
|     } | ||||
| } | ||||
| void AppManager::HttpServerStart(const AppParam ¶m) | ||||
| { | ||||
|  | @ -63,7 +90,6 @@ void AppManager::HttpServerStart(const AppParam ¶m) | |||
| } | ||||
| void AppManager::HttpServerStop(void) | ||||
| { | ||||
|     // FxHttpServerExit();
 | ||||
|     WebServerExit(); | ||||
|     if (mHttpSever.joinable()) { | ||||
|         mHttpSever.join(); | ||||
|  | @ -81,9 +107,53 @@ void AppManager::HttpServerThread(const AppParam ¶m) | |||
|             appImpl->AppRequestHandle(url, urlLength, responseHandle, context); | ||||
|         } | ||||
|     }; | ||||
|     // FxHttpServerInit(httpHandle, 8080);
 | ||||
|     // FxHttpServerUnInit();
 | ||||
|     WebServerParam web = {.mIp = param.mIP, .mPort = param.mPort, .mHttpRequestHandle = httpHandle}; | ||||
|     WebServerParam web = {.mIp = param.mIP, .mPort = param.mHttpPort, .mHttpRequestHandle = httpHandle}; | ||||
|     WebServerInit(web); | ||||
|     WebServerUnInit(); | ||||
| } | ||||
| void AppManager::TcpServerStart(const AppParam ¶m) | ||||
| { | ||||
|     auto acceptClientFunc = [](void *object, const char *ip) -> bool { | ||||
|         LogInfo("accept client, peer ip: %s\n", ip); | ||||
|         if (nullptr != object) { | ||||
|             std::shared_ptr<IAppManager> app = IAppManager::GetInstance(); | ||||
|             std::shared_ptr<AppManager> appImpl = std::dynamic_pointer_cast<AppManager>(app); | ||||
|             if (appImpl) { | ||||
|                 appImpl->AppClientConnected(object, ip); | ||||
|             } | ||||
|             return true; | ||||
|         } | ||||
|         return false; | ||||
|     }; | ||||
|     auto readFunc = [](const void *data, const ssize_t len, const void *object) -> void { | ||||
|         LogInfo("read data: %s\n", (char *)data); | ||||
|     }; | ||||
|     auto closedFunc = [](const void *object) -> void { | ||||
|         LogInfo("closed.\n"); | ||||
|         std::shared_ptr<IAppManager> app = IAppManager::GetInstance(); | ||||
|         std::shared_ptr<AppManager> appImpl = std::dynamic_pointer_cast<AppManager>(app); | ||||
|         if (appImpl) { | ||||
|             appImpl->AppClientClosed(object); | ||||
|         } | ||||
|     }; | ||||
|     TcpServerParam tcpServerparam = { | ||||
|         .mIp = param.mIP, | ||||
|         .mPort = param.mTcpPort, | ||||
|         .mAcceptClientFunc = acceptClientFunc, | ||||
|         .mClientAcceptParam = | ||||
|             { | ||||
|                 .mReadFunc = readFunc, | ||||
|                 .mClosedFunc = closedFunc, | ||||
|             }, | ||||
|     }; | ||||
|     mTcpServer = CreateTcpServer(tcpServerparam); | ||||
|     if (nullptr == mTcpServer) { | ||||
|         LogError("Create tcp server failed.\n"); | ||||
|     } | ||||
| } | ||||
| void AppManager::TcpServerStop(void) | ||||
| { | ||||
|     if (nullptr != mTcpServer) { | ||||
|         FreeTcpServer(mTcpServer); | ||||
|     } | ||||
| } | ||||
|  | @ -16,6 +16,7 @@ | |||
| #define APP_MANAGER_H | ||||
| #include "IAppManager.h" | ||||
| #include "IAppProtocolHandle.h" | ||||
| #include <map> | ||||
| #include <thread> | ||||
| class AppManager : public IAppManager, public std::enable_shared_from_this<AppManager> | ||||
| { | ||||
|  | @ -26,6 +27,10 @@ public: | |||
|     const StatusCode UnInit(void) override; | ||||
|     const StatusCode SetAppMonitor(std::shared_ptr<VAppMonitor> &monitor) override; | ||||
|     void AppRequestHandle(const char *url, const unsigned int urlLength, ResponseHandle responseHandle, void *context); | ||||
|     void AppRequestHandleTcp(const char *data, const unsigned int dataLength, ResponseHandle responseHandle, | ||||
|                              void *context); | ||||
|     void AppClientConnected(const void *client, const char *ip); | ||||
|     void AppClientClosed(const void *client); | ||||
| 
 | ||||
| private: | ||||
|     void HttpServerStart(const AppParam ¶m); | ||||
|  | @ -33,9 +38,15 @@ private: | |||
|     void HttpServerThread(const AppParam ¶m); | ||||
| 
 | ||||
| private: | ||||
|     // bool mHttpServerRuning;
 | ||||
|     void TcpServerStart(const AppParam ¶m); | ||||
|     void TcpServerStop(void); | ||||
| 
 | ||||
| private: | ||||
|     std::thread mHttpSever; | ||||
|     std::shared_ptr<IAppProtocolHandle> mProtocolHandle; | ||||
|     void *mTcpServer; | ||||
|     std::shared_ptr<VAppMonitor> mAppMonitor; | ||||
|     std::map<void *, std::shared_ptr<VAppClient>> mAppClients; | ||||
| }; | ||||
| 
 | ||||
| #endif | ||||
|  |  | |||
|  | @ -14,6 +14,24 @@ | |||
|  */ | ||||
| #include "IAppManager.h" | ||||
| #include "ILog.h" | ||||
| void VAppClient::SetRecordingStatus(const RecordingStatus &status) | ||||
| { | ||||
| } | ||||
| void VAppClient::SetMicrophoneStatus(const MicrophoneStatus &status) | ||||
| { | ||||
| } | ||||
| void VAppClient::SetBatteryStatus(const BatteryStatus &status, const int &batteryCapacity) | ||||
| { | ||||
| } | ||||
| void VAppClient::SetSdCardStatus(const SdCardStatus &status) | ||||
| { | ||||
| } | ||||
| void VAppClient::DeletedFileMessage(const std::string &file, const StorageFileType &type) | ||||
| { | ||||
| } | ||||
| void VAppClient::CreatedFileMessage(const std::string &file, const StorageFileType &type) | ||||
| { | ||||
| } | ||||
| StatusCode VAppMonitor::GetProductInfo(AppGetProductInfo ¶m) | ||||
| { | ||||
|     return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); | ||||
|  | @ -82,6 +100,10 @@ StatusCode VAppMonitor::GetThumbnail(AppGetThumbnail ¶m) | |||
| { | ||||
|     return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); | ||||
| } | ||||
| StatusCode VAppMonitor::AppClientConnected(std::shared_ptr<VAppClient> &client) | ||||
| { | ||||
|     return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); | ||||
| } | ||||
| std::shared_ptr<IAppManager> &IAppManager::GetInstance(std::shared_ptr<IAppManager> *impl) | ||||
| { | ||||
|     static auto instance = std::make_shared<IAppManager>(); | ||||
|  |  | |||
							
								
								
									
										81
									
								
								middleware/AppManager/src/IAppProtocolHandle.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										81
									
								
								middleware/AppManager/src/IAppProtocolHandle.cpp
									
									
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,81 @@ | |||
| /*
 | ||||
|  * 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 "IAppProtocolHandle.h" | ||||
| #include "ILog.h" | ||||
| protocol_packet::~protocol_packet() | ||||
| { | ||||
|     if (nullptr != mData) { | ||||
|         free(mData); | ||||
|         mData = nullptr; | ||||
|     } | ||||
| } | ||||
| std::shared_ptr<IAppProtocolHandle> &IAppProtocolHandle::GetInstance(std::shared_ptr<IAppProtocolHandle> *impl) | ||||
| { | ||||
|     static auto instance = std::make_shared<IAppProtocolHandle>(); | ||||
|     if (impl) { | ||||
|         if (instance.use_count() == 1) { | ||||
|             LogInfo("Instance changed succeed.\n"); | ||||
|             instance = *impl; | ||||
|         } | ||||
|         else { | ||||
|             LogError("Can't changing the instance becase of using by some one.\n"); | ||||
|         } | ||||
|     } | ||||
|     return instance; | ||||
| } | ||||
| void IAppProtocolHandle::Init(void) | ||||
| { | ||||
| } | ||||
| void IAppProtocolHandle::UnInit(void) | ||||
| { | ||||
| } | ||||
| void IAppProtocolHandle::RequestHandle(const char *url, const unsigned int &urlLength, ResponseHandle responseHandle, | ||||
|                                        void *context) | ||||
| { | ||||
| } | ||||
| void IAppProtocolHandle::RequestHandleTcp(const char *data, const unsigned int &dataLength, | ||||
|                                           ResponseHandle responseHandle, void *context) | ||||
| { | ||||
| } | ||||
| void IAppProtocolHandle::SetAppMonitor(std::shared_ptr<VAppMonitor> &monitor) | ||||
| { | ||||
| } | ||||
| std::shared_ptr<ProtocolPacket> IAppProtocolHandle::SetRecordingStatus(const RecordingStatus &status) | ||||
| { | ||||
|     return std::make_shared<ProtocolPacket>(); | ||||
| } | ||||
| std::shared_ptr<ProtocolPacket> IAppProtocolHandle::SetMicrophoneStatus(const MicrophoneStatus &status) | ||||
| { | ||||
|     return std::make_shared<ProtocolPacket>(); | ||||
| } | ||||
| std::shared_ptr<ProtocolPacket> IAppProtocolHandle::SetBatteryStatus(const BatteryStatus &status, | ||||
|                                                                      const int &batteryCapacity) | ||||
| { | ||||
|     return std::make_shared<ProtocolPacket>(); | ||||
| } | ||||
| std::shared_ptr<ProtocolPacket> IAppProtocolHandle::SetSdCardStatus(const SdCardStatus &status) | ||||
| { | ||||
|     return std::make_shared<ProtocolPacket>(); | ||||
| } | ||||
| std::shared_ptr<ProtocolPacket> IAppProtocolHandle::DeletedFileMessage(const std::string &file, | ||||
|                                                                        const StorageFileType &type) | ||||
| { | ||||
|     return std::make_shared<ProtocolPacket>(); | ||||
| } | ||||
| std::shared_ptr<ProtocolPacket> IAppProtocolHandle::CreatedFileMessage(const std::string &file, | ||||
|                                                                        const StorageFileType &type) | ||||
| { | ||||
|     return std::make_shared<ProtocolPacket>(); | ||||
| } | ||||
|  | @ -14,11 +14,24 @@ | |||
|  */ | ||||
| #ifndef I_APP_PROTOCOL_HANDLE_H | ||||
| #define I_APP_PROTOCOL_HANDLE_H | ||||
| #include "IAppManager.h" | ||||
| #include "StatusCode.h" | ||||
| #include "WebServer.h" | ||||
| #include <iostream> | ||||
| #include <memory> | ||||
| #include <vector> | ||||
| typedef struct protocol_packet | ||||
| { | ||||
|     protocol_packet() : mData(nullptr), mDataLength(0) | ||||
|     { | ||||
|     } | ||||
|     protocol_packet(void *data, const size_t &size) : mData(data), mDataLength(size) | ||||
|     { | ||||
|     } | ||||
|     ~protocol_packet(); | ||||
|     void *mData; | ||||
|     size_t mDataLength; | ||||
| } ProtocolPacket; | ||||
| class VAppDataPacket | ||||
| { | ||||
| public: | ||||
|  | @ -30,18 +43,19 @@ 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) | ||||
|     { | ||||
|     } | ||||
|     virtual void SetAppMonitor(std::shared_ptr<VAppMonitor> &monitor) | ||||
|     { | ||||
|     } | ||||
|     static std::shared_ptr<IAppProtocolHandle> &GetInstance(std::shared_ptr<IAppProtocolHandle> *impl = nullptr); | ||||
|     virtual void Init(void); | ||||
|     virtual void UnInit(void); | ||||
|     virtual void RequestHandle(const char *url, const unsigned int &urlLength, ResponseHandle responseHandle, | ||||
|                                void *context); | ||||
|     virtual void RequestHandleTcp(const char *data, const unsigned int &dataLength, ResponseHandle responseHandle, | ||||
|                                   void *context); | ||||
|     virtual void SetAppMonitor(std::shared_ptr<VAppMonitor> &monitor); | ||||
|     virtual std::shared_ptr<ProtocolPacket> SetRecordingStatus(const RecordingStatus &status); | ||||
|     virtual std::shared_ptr<ProtocolPacket> SetMicrophoneStatus(const MicrophoneStatus &status); | ||||
|     virtual std::shared_ptr<ProtocolPacket> SetBatteryStatus(const BatteryStatus &status, const int &batteryCapacity); | ||||
|     virtual std::shared_ptr<ProtocolPacket> SetSdCardStatus(const SdCardStatus &status); | ||||
|     virtual std::shared_ptr<ProtocolPacket> DeletedFileMessage(const std::string &file, const StorageFileType &type); | ||||
|     virtual std::shared_ptr<ProtocolPacket> CreatedFileMessage(const std::string &file, const StorageFileType &type); | ||||
| }; | ||||
| #endif | ||||
|  | @ -14,13 +14,16 @@ | |||
|  */ | ||||
| #include "SixFrameHandle.h" | ||||
| #include "ILog.h" | ||||
| #include <cstring> | ||||
| #include <sstream> | ||||
| #include <stdio.h> | ||||
| #include <time.h> | ||||
| using std::placeholders::_1; | ||||
| using std::placeholders::_2; | ||||
| using std::placeholders::_3; | ||||
| // using std::placeholders::_4;
 | ||||
| // clang-format off
 | ||||
| const char *TCP_RESULT_MSGID                  = "msgid"; | ||||
| const char *CJSON_INFO_STRING                 = "info"; | ||||
| const char *CJSON_FILES_STRING                = "files"; | ||||
| const char *APP_GET_PRODUCT_INFO              = "/app/getproductinfo"; | ||||
|  | @ -67,7 +70,7 @@ SixFrameHandle::SixFrameHandle() | |||
|     mResquesHandleFunc[APP_GET_THUMBNAIL] = std::bind(&SixFrameHandle::RequestGetThumbnail, this, _1, _2, _3); | ||||
|     // mResquesHandleFunc["favicon.ico"] = std::bind(&SixFrameHandle::DoNothing, this, _1, _2, _);
 | ||||
| } | ||||
| void SixFrameHandle::RequestHandle(const char *url, const unsigned int urlLength, ResponseHandle responseHandle, | ||||
| void SixFrameHandle::RequestHandle(const char *url, const unsigned int &urlLength, ResponseHandle responseHandle, | ||||
|                                    void *context) | ||||
| { | ||||
|     const std::string urlStr2 = url; | ||||
|  | @ -81,9 +84,27 @@ void SixFrameHandle::RequestHandle(const char *url, const unsigned int urlLength | |||
|         command = urlStr2.substr(0, urlStr2.length()); | ||||
|     } | ||||
|     LogInfo("command = %s\n", command.c_str()); | ||||
|     // ExtractParamsFromUrl(urlStr2, paramsMap);
 | ||||
|     RequestHandle2(command, urlStr2, responseHandle, context); | ||||
| } | ||||
| void SixFrameHandle::RequestHandleTcp(const char *data, const unsigned int &dataLength, ResponseHandle responseHandle, | ||||
|                                       void *context) | ||||
| { | ||||
|     cJSON *parsed = nullptr; | ||||
|     std::string command = ""; | ||||
|     parsed = cJSON_Parse(data); | ||||
|     if (nullptr == parsed) { | ||||
|         LogError("cJSON_Parse failed.\n"); | ||||
|         responseHandle("Device run out of memory.", context); | ||||
|         return; | ||||
|     } | ||||
|     cJSON *msgid = cJSON_GetObjectItem(parsed, "msgid"); | ||||
|     if (cJSON_IsString(msgid)) { | ||||
|         command = cJSON_GetStringValue(msgid); | ||||
|     } | ||||
|     LogInfo("command = %s\n", command.c_str()); | ||||
|     RequestTcpHandle2(command, parsed, responseHandle, context); | ||||
|     cJSON_Delete(parsed); | ||||
| } | ||||
| void SixFrameHandle::ExtractParamsFromUrl(const std::string &url, ParseUrlResultFunc resultHandle, | ||||
|                                           std::shared_ptr<VParseUrl> &context) | ||||
| { | ||||
|  | @ -662,6 +683,47 @@ void SixFrameHandle::RequestGetThumbnail(const std::string &url, ResponseHandle | |||
|     // respon.mThumbnail = "./34a396526922a33e97906920dbfef2a5.jpg";
 | ||||
|     responseHandle(respon.mThumbnail.c_str(), context); | ||||
| } | ||||
| void SixFrameHandle::RequestTcpHandle2(const std::string command, const cJSON *const data, | ||||
|                                        ResponseHandle responseHandle, void *context) | ||||
| { | ||||
| } | ||||
| std::shared_ptr<ProtocolPacket> SixFrameHandle::SetRecordingStatus(const RecordingStatus &status) | ||||
| { | ||||
|     cJSON *resultCJSON = cJSON_CreateObject(); | ||||
|     if (nullptr == resultCJSON) { | ||||
|         LogError("cJSON_CreateObject failed.\n"); | ||||
|         return std::make_shared<ProtocolPacket>(); | ||||
|     } | ||||
|     cJSON_AddStringToObject(resultCJSON, TCP_RESULT_MSGID, "rec"); | ||||
|     cJSON *info = cJSON_CreateObject(); | ||||
|     if (nullptr != info) { | ||||
|         cJSON_AddNumberToObject(info, "value", static_cast<int>(status)); | ||||
|         cJSON_AddItemToObject(resultCJSON, CJSON_INFO_STRING, info); | ||||
|     } | ||||
|     AddTimestamp(resultCJSON); | ||||
|     return MakePacket(resultCJSON); | ||||
| } | ||||
| std::shared_ptr<ProtocolPacket> SixFrameHandle::SetMicrophoneStatus(const MicrophoneStatus &status) | ||||
| { | ||||
|     return std::make_shared<ProtocolPacket>(); | ||||
| } | ||||
| std::shared_ptr<ProtocolPacket> SixFrameHandle::SetBatteryStatus(const BatteryStatus &status, | ||||
|                                                                  const int &batteryCapacity) | ||||
| { | ||||
|     return std::make_shared<ProtocolPacket>(); | ||||
| } | ||||
| std::shared_ptr<ProtocolPacket> SixFrameHandle::SetSdCardStatus(const SdCardStatus &status) | ||||
| { | ||||
|     return std::make_shared<ProtocolPacket>(); | ||||
| } | ||||
| std::shared_ptr<ProtocolPacket> SixFrameHandle::DeletedFileMessage(const std::string &file, const StorageFileType &type) | ||||
| { | ||||
|     return std::make_shared<ProtocolPacket>(); | ||||
| } | ||||
| std::shared_ptr<ProtocolPacket> SixFrameHandle::CreatedFileMessage(const std::string &file, const StorageFileType &type) | ||||
| { | ||||
|     return std::make_shared<ProtocolPacket>(); | ||||
| } | ||||
| cJSON *SixFrameHandle::MakeResponseResult(const ResposeResult result, const bool requestSet) | ||||
| { | ||||
|     const char *RESPONSE_RESULT = "result"; | ||||
|  | @ -711,6 +773,29 @@ const char *SixFrameHandle::PrintfFileEvent(const AppGetFileInfo &fileInfo) | |||
|     } | ||||
|     return "unknown event"; | ||||
| } | ||||
| void SixFrameHandle::AddTimestamp(cJSON *json) | ||||
| { | ||||
|     time_t current_time; | ||||
|     current_time = time(nullptr); | ||||
| 
 | ||||
|     if (current_time == -1) { | ||||
|         LogError("Error getting the time\n"); | ||||
|         return; | ||||
|     } | ||||
|     cJSON_AddNumberToObject(json, "time", current_time); | ||||
| } | ||||
| std::shared_ptr<ProtocolPacket> SixFrameHandle::MakePacket(const cJSON *json) | ||||
| { | ||||
|     char *resultStr = nullptr; | ||||
|     resultStr = cJSON_Print(json); | ||||
|     if (nullptr != resultStr) { | ||||
|         return std::make_shared<ProtocolPacket>(resultStr, strlen(resultStr)); | ||||
|     } | ||||
|     else { | ||||
|         LogError("MakePacket failed.\n"); | ||||
|         return std::make_shared<ProtocolPacket>(); | ||||
|     } | ||||
| } | ||||
| void SixFrameHandle::SetAppMonitor(std::shared_ptr<VAppMonitor> &monitor) | ||||
| { | ||||
|     if (monitor) { | ||||
|  |  | |||
|  | @ -51,8 +51,10 @@ public: | |||
|     virtual ~SixFrameHandle() = default; | ||||
|     // virtual void Init(void) {}
 | ||||
|     // virtual void UnInit(void) {}
 | ||||
|     void RequestHandle(const char *url, const unsigned int urlLength, ResponseHandle responseHandle, | ||||
|     void RequestHandle(const char *url, const unsigned int &urlLength, ResponseHandle responseHandle, | ||||
|                        void *context) override; | ||||
|     void RequestHandleTcp(const char *data, const unsigned int &dataLength, ResponseHandle responseHandle, | ||||
|                           void *context) override; | ||||
| 
 | ||||
| private: | ||||
|     void ExtractParamsFromUrl(const std::string &url, ParseUrlResultFunc resultHandle, | ||||
|  | @ -95,10 +97,22 @@ private: | |||
|     std::string RequestGetThumbnailParse(const std::string &url); | ||||
|     void RequestGetThumbnail(const std::string &url, ResponseHandle responseHandle, void *context); | ||||
| 
 | ||||
| private: | ||||
|     void RequestTcpHandle2(const std::string command, const cJSON *const data, ResponseHandle responseHandle, | ||||
|                            void *context); // TODO: delete
 | ||||
|     std::shared_ptr<ProtocolPacket> SetRecordingStatus(const RecordingStatus &status) override; | ||||
|     std::shared_ptr<ProtocolPacket> SetMicrophoneStatus(const MicrophoneStatus &status) override; | ||||
|     std::shared_ptr<ProtocolPacket> SetBatteryStatus(const BatteryStatus &status, const int &batteryCapacity) override; | ||||
|     std::shared_ptr<ProtocolPacket> SetSdCardStatus(const SdCardStatus &status) override; | ||||
|     std::shared_ptr<ProtocolPacket> DeletedFileMessage(const std::string &file, const StorageFileType &type) override; | ||||
|     std::shared_ptr<ProtocolPacket> CreatedFileMessage(const std::string &file, const StorageFileType &type) override; | ||||
| 
 | ||||
| private: | ||||
|     cJSON *MakeResponseResult(const ResposeResult result, const bool requestSet = false); | ||||
|     void ResponseJsonString(cJSON *json, ResponseHandle responseHandle, void *context); | ||||
|     const char *PrintfFileEvent(const AppGetFileInfo &fileInfo); | ||||
|     void AddTimestamp(cJSON *json); | ||||
|     std::shared_ptr<ProtocolPacket> MakePacket(const cJSON *json); | ||||
| 
 | ||||
| protected: | ||||
|     void SetAppMonitor(std::shared_ptr<VAppMonitor> &monitor) override; | ||||
|  |  | |||
|  | @ -24,7 +24,7 @@ namespace AppManagerTest | |||
| class AppManagerTest : public testing::Test, public AppManagerTestTool, public HalTestTool | ||||
| { | ||||
| public: | ||||
|     AppManagerTest() : mAppParam(APP_MANAGER_HTTP_SERVER_IP, APP_MANAGER_HTTP_SERVER_PORT) | ||||
|     AppManagerTest() : mAppParam(APP_MANAGER_DEVICE_IP, APP_MANAGER_HTTP_SERVER_PORT, APP_MANAGER_TCP_SERVER_PORT) | ||||
|     { | ||||
|     } | ||||
|     virtual ~AppManagerTest() | ||||
|  | @ -64,7 +64,8 @@ TEST_F(AppManagerTest, INTEGRATION_AppManager_EXAMPLE_Demo0) | |||
|     IAppManager::GetInstance()->Init(mAppParam); | ||||
|     IAppManager::GetInstance()->SetAppMonitor(monitor); | ||||
|     std::this_thread::sleep_for(std::chrono::milliseconds(100)); | ||||
|     std::this_thread::sleep_for(std::chrono::milliseconds(200000)); | ||||
|     MockAppClientConnect(); | ||||
|     std::this_thread::sleep_for(std::chrono::milliseconds(2000)); | ||||
|     IAppManager::GetInstance()->UnInit(); | ||||
| } | ||||
| // ../output_files/test/bin/AppManagerTest --gtest_filter=AppManagerTest.INTEGRATION_AppManager_EXAMPLE_Demo
 | ||||
|  |  | |||
|  | @ -9,6 +9,7 @@ include_directories( | |||
|     ${UTILS_SOURCE_PATH}/StatusCode/include | ||||
|     ${UTILS_SOURCE_PATH}/Log/include | ||||
|     ${UTILS_SOURCE_PATH}/Servers/include | ||||
|     ${UTILS_SOURCE_PATH}/TcpModule/include | ||||
|     ${MIDDLEWARE_SOURCE_PATH}/AppManager/src | ||||
|     ${MIDDLEWARE_SOURCE_PATH}/AppManager/src/Protocol/SixFrame | ||||
|     ${TEST_SOURCE_PATH} | ||||
|  |  | |||
|  | @ -44,6 +44,9 @@ protected: | |||
|     void MockAppPlayback(void); | ||||
|     void MockMonitorSetFileList(std::vector<AppGetFileList> &files); | ||||
| 
 | ||||
| protected: | ||||
|     void MockAppClientConnect(void); | ||||
| 
 | ||||
| private: | ||||
|     void AppManagerMockInit(std::shared_ptr<IAppManager> &vMock); | ||||
|     void AppMonitorInit(std::shared_ptr<VAppMonitor> &vMock); | ||||
|  | @ -51,6 +54,8 @@ private: | |||
| private: | ||||
|     std::shared_ptr<IAppManager> mAppManagerMock; | ||||
|     std::shared_ptr<VAppMonitor> mAppMonitorMock; | ||||
|     void *mAppClientTool; | ||||
|     std::shared_ptr<VAppClient> mAppClient; | ||||
| 
 | ||||
| public: | ||||
|     static std::shared_ptr<VAppMonitor> MakeMonitorMock(void); | ||||
|  |  | |||
|  | @ -27,14 +27,4 @@ const StatusCode AppManagerTest::SetAppMonitorTrace(std::shared_ptr<VAppMonitor> | |||
| { | ||||
|     LogInfo("AppManagerTest::SetAppMonitorTrace\n"); | ||||
|     return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); | ||||
| } | ||||
| // StatusCode AppManagerTest::GetAllLedsTrace(std::map<std::string, std::shared_ptr<VLedHal>> &allLeds)
 | ||||
| // {
 | ||||
| //     LogInfo("AppManagerTest::GetAllLedsTrace\n");
 | ||||
| //     return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
 | ||||
| // }
 | ||||
| // StatusCode AppManagerTest::GetAllKeysTrace(std::map<std::string, std::shared_ptr<VKeyHal>> &allKeys)
 | ||||
| // {
 | ||||
| //     LogInfo("AppManagerTest::GetAllKeysTrace\n");
 | ||||
| //     return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
 | ||||
| // }
 | ||||
| } | ||||
|  | @ -18,6 +18,7 @@ | |||
| #include "AppMonitorMock.h" | ||||
| #include "ILog.h" | ||||
| #include "ServersMock.h" | ||||
| #include "TcpModule.h" | ||||
| constexpr int ONLY_BE_CALLED_ONCE = 1; | ||||
| void AppManagerTestTool::Init(void) | ||||
| { | ||||
|  | @ -33,6 +34,9 @@ void AppManagerTestTool::UnInit(void) | |||
|     mAppManagerMock.reset(); | ||||
|     mAppMonitorMock.reset(); | ||||
|     CancelOverrideAppManagerMakePtrObject(); | ||||
|     if (nullptr != mAppClientTool) { | ||||
|         FreeTcpClient(mAppClientTool); | ||||
|     } | ||||
| } | ||||
| void AppManagerTestTool::MockGetProductInfo(void) | ||||
| { | ||||
|  | @ -205,6 +209,36 @@ void AppManagerTestTool::MockMonitorSetFileList(std::vector<AppGetFileList> &fil | |||
|         LogError("MockMonitorSetFileList failed, mAppMonitorMock isn't mock object.\n"); | ||||
|     } | ||||
| } | ||||
| void AppManagerTestTool::MockAppClientConnect(void) | ||||
| { | ||||
|     std::shared_ptr<AppMonitorMock> mock = std::dynamic_pointer_cast<AppMonitorMock>(mAppMonitorMock); | ||||
|     if (mock) { | ||||
|         auto appClientConnected = [=](std::shared_ptr<VAppClient> &cliient) { | ||||
|             LogInfo("appClientConnected.\n"); | ||||
|             mAppClient = cliient; | ||||
|         }; | ||||
|         EXPECT_CALL(*mock.get(), AppClientConnectedTrace(_)) | ||||
|             .Times(ONLY_BE_CALLED_ONCE) | ||||
|             .WillOnce( | ||||
|                 DoAll(WithArgs<0>(Invoke(appClientConnected)), Return(CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION)))); | ||||
|     } | ||||
|     auto readFunc = [](const void *data, const ssize_t len, const void *context) -> void { | ||||
|         LogInfo("read data: %s", (char *)data); | ||||
|     }; | ||||
|     auto closedFunc = [](const void *object) -> void { | ||||
|         LogInfo("tcp client closed.\n"); | ||||
|     }; | ||||
|     TcpClientParam param = { | ||||
|         .mIp = APP_MANAGER_DEVICE_IP, | ||||
|         .mPort = APP_MANAGER_TCP_SERVER_PORT, | ||||
|         .mReadFunc = readFunc, | ||||
|         .mClosedFunc = closedFunc, | ||||
|     }; | ||||
|     mAppClientTool = CreateTcpClient(param); | ||||
|     if (nullptr == mAppClientTool) { | ||||
|         LogError("CreateTcpClient failed.\n"); | ||||
|     } | ||||
| } | ||||
| void AppManagerTestTool::AppManagerMockInit(std::shared_ptr<IAppManager> &vMock) | ||||
| { | ||||
|     std::shared_ptr<AppManagerMock> mock = std::dynamic_pointer_cast<AppManagerMock>(vMock); | ||||
|  | @ -268,5 +302,9 @@ void AppManagerTestTool::AppMonitorInit(std::shared_ptr<VAppMonitor> &vMock) | |||
|             .WillRepeatedly(DoAll(Return(CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION)))); | ||||
|         EXPECT_CALL(*mock.get(), UploadFileTrace(_)) | ||||
|             .WillRepeatedly(DoAll(Return(CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION)))); | ||||
|         EXPECT_CALL(*mock.get(), GetThumbnailTrace(_)) | ||||
|             .WillRepeatedly(DoAll(Return(CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION)))); | ||||
|         EXPECT_CALL(*mock.get(), AppClientConnectedTrace(_)) | ||||
|             .WillRepeatedly(DoAll(Return(CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION)))); | ||||
|     } | ||||
| } | ||||
|  | @ -243,4 +243,32 @@ StatusCode AppMonitorTrace::UploadFileTrace(AppUploadFile ¶m) | |||
| { | ||||
|     LogInfo("AppMonitorTrace::UploadFileTrace\n"); | ||||
|     return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); | ||||
| } | ||||
| StatusCode AppMonitorTest::GetThumbnail(AppGetThumbnail ¶m) | ||||
| { | ||||
|     LogInfo("AppMonitorTest::GetThumbnail\n"); | ||||
|     StatusCode code = GetThumbnailTrace(param); | ||||
|     if (StatusCodeEqual(code, "STATUS_CODE_VIRTUAL_FUNCTION")) { | ||||
|         return VAppMonitor::GetThumbnail(param); | ||||
|     } | ||||
|     return code; | ||||
| } | ||||
| StatusCode AppMonitorTrace::GetThumbnailTrace(AppGetThumbnail ¶m) | ||||
| { | ||||
|     LogInfo("AppMonitorTrace::UploadFileTrace\n"); | ||||
|     return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); | ||||
| } | ||||
| StatusCode AppMonitorTest::AppClientConnected(std::shared_ptr<VAppClient> &client) | ||||
| { | ||||
|     LogInfo("AppMonitorTest::AppClientConnected\n"); | ||||
|     StatusCode code = AppClientConnectedTrace(client); | ||||
|     if (StatusCodeEqual(code, "STATUS_CODE_VIRTUAL_FUNCTION")) { | ||||
|         return VAppMonitor::AppClientConnected(client); | ||||
|     } | ||||
|     return code; | ||||
| } | ||||
| StatusCode AppMonitorTrace::AppClientConnectedTrace(std::shared_ptr<VAppClient> &client) | ||||
| { | ||||
|     LogInfo("AppMonitorTrace::AppClientConnected\n"); | ||||
|     return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); | ||||
| } | ||||
|  | @ -39,6 +39,8 @@ protected: | |||
|     virtual StatusCode EnterRecorderTrace(void); | ||||
|     virtual StatusCode AppPlaybackTrace(const PlayBackEvent &event); | ||||
|     virtual StatusCode UploadFileTrace(AppUploadFile ¶m); | ||||
|     virtual StatusCode GetThumbnailTrace(AppGetThumbnail ¶m); | ||||
|     virtual StatusCode AppClientConnectedTrace(std::shared_ptr<VAppClient> &client); | ||||
| }; | ||||
| class AppMonitorTest : public VAppMonitor, virtual public AppMonitorTrace | ||||
| { | ||||
|  | @ -61,6 +63,8 @@ public: | |||
|     StatusCode EnterRecorder(void) override; | ||||
|     StatusCode AppPlayback(const PlayBackEvent &event) override; | ||||
|     StatusCode UploadFile(AppUploadFile ¶m) override; | ||||
|     StatusCode GetThumbnail(AppGetThumbnail ¶m) override; | ||||
|     StatusCode AppClientConnected(std::shared_ptr<VAppClient> &client) override; | ||||
| }; | ||||
| class AppMonitorMock : virtual public AppMonitorTrace | ||||
| { | ||||
|  | @ -83,5 +87,7 @@ public: | |||
|     MOCK_METHOD0(EnterRecorderTrace, StatusCode(void)); | ||||
|     MOCK_METHOD1(AppPlaybackTrace, StatusCode(const PlayBackEvent &)); | ||||
|     MOCK_METHOD1(UploadFileTrace, StatusCode(AppUploadFile &)); | ||||
|     MOCK_METHOD1(GetThumbnailTrace, StatusCode(AppGetThumbnail &)); | ||||
|     MOCK_METHOD1(AppClientConnectedTrace, StatusCode(std::shared_ptr<VAppClient> &)); | ||||
| }; | ||||
| #endif | ||||
|  | @ -49,7 +49,7 @@ std::shared_ptr<ServersMock> &ServersMock::GetInstance(std::shared_ptr<ServersMo | |||
| ServersMock::ServersMock() | ||||
| { | ||||
|     //
 | ||||
|     mServerUrl = APP_MANAGER_HTTP_SERVER_IP ":" + std::to_string(APP_MANAGER_HTTP_SERVER_PORT); | ||||
|     mServerUrl = APP_MANAGER_DEVICE_IP ":" + std::to_string(APP_MANAGER_HTTP_SERVER_PORT); | ||||
| } | ||||
| void ServersMock::Init(void) | ||||
| { | ||||
|  |  | |||
|  | @ -44,7 +44,6 @@ typedef struct tcp_parm | |||
| } TcpClientParam; | ||||
| void *CreateTcpServer(const TcpServerParam param); | ||||
| void FreeTcpServer(void *object); | ||||
| // void AcceptClientSetParam(void *object, const ClientAcceptParam param);
 | ||||
| ssize_t AcceptClientWrite(void *object, const void *buf, const size_t bufLenght); | ||||
| void AcceptClientClose(void *object); | ||||
| void *CreateTcpClient(const TcpClientParam param); | ||||
|  |  | |||
|  | @ -81,6 +81,7 @@ void TcpClientImpl::Readed(const void *data, size_t length) | |||
| } | ||||
| ssize_t TcpClientImpl::Write(const void *buf, const size_t bufLenght) | ||||
| { | ||||
|     std::lock_guard<std::mutex> locker(mMutex); | ||||
|     if (nullptr == mIo) { | ||||
|         LogError("mIo is nullptr.\n"); | ||||
|         return TCP_MODULE_WRITE_ERROR; | ||||
|  | @ -89,6 +90,8 @@ ssize_t TcpClientImpl::Write(const void *buf, const size_t bufLenght) | |||
| } | ||||
| void TcpClientImpl::Closed(void) | ||||
| { | ||||
|     std::lock_guard<std::mutex> locker(mMutex); | ||||
|     mIo = nullptr; | ||||
|     if (nullptr != mParam.mClosedFunc) { | ||||
|         mParam.mClosedFunc(mObjectThis); | ||||
|     } | ||||
|  |  | |||
|  | @ -20,6 +20,7 @@ | |||
| #include "hsocket.h" | ||||
| #include "hssl.h" | ||||
| #include <memory> | ||||
| #include <mutex> | ||||
| #include <thread> | ||||
| class TcpClientImpl : public ITcpClient, public std::enable_shared_from_this<TcpClientImpl> | ||||
| { | ||||
|  | @ -34,6 +35,7 @@ public: | |||
|     void Loop(void); | ||||
| 
 | ||||
| private: | ||||
|     std::mutex mMutex; | ||||
|     hloop_t *mLoop; | ||||
|     hio_t *mIo; | ||||
|     const TcpClientParam mParam; | ||||
|  |  | |||
|  | @ -58,12 +58,6 @@ void FreeTcpServer(void *object) | |||
|         free(((char *)object) - sizeof(ITcpServerHeader)); // TODO: bug?
 | ||||
|     } | ||||
| } | ||||
| // void AcceptClientSetParam(void *object, const ClientAcceptParam param)
 | ||||
| // {
 | ||||
| //     if (TcpClientAcceptObjectCheck(object) == true) {
 | ||||
| //         (*(std::shared_ptr<ITcpClientAccept> *)object)->SetParam(param);
 | ||||
| //     }
 | ||||
| // }
 | ||||
| ssize_t AcceptClientWrite(void *object, const void *buf, const size_t bufLenght) | ||||
| { | ||||
|     if (TcpClientAcceptObjectCheck(object) == true) { | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue
	
	Block a user
	 Fancy code
						Fancy code