1 /* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef AW_CXX_DISTRIBUTED_DISTRIBUTED_AGENT_H_ 17 #define AW_CXX_DISTRIBUTED_DISTRIBUTED_AGENT_H_ 18 19 #include <map> 20 #include <vector> 21 #include <thread> 22 #include <iostream> 23 24 #include "distributed.h" 25 #include "distributed_cfg.h" 26 27 #include "hilog/log.h" 28 29 #include "gtest/gtest.h" 30 31 namespace OHOS { 32 namespace DistributeSystemTest { 33 class DistributedAgent { 34 public: 35 DistributedAgent(); 36 virtual ~DistributedAgent(); 37 // deal with testcase logic by struct or xml command. 38 int OnProcessCmd(const std::string &strCommand, int cmdLen, const std::string &strExpectValue, int expectValueLen); 39 int Start(std::string cfgFile); 40 int Stop(); 41 void Join(); 42 static const int RECE_LEN = 10; 43 static const int CMD_LENGTH = 50; 44 static const int HALF_NUM = 2; 45 static constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, 0xD001690, "DistributedAgent"}; 46 47 protected: 48 virtual bool SetUp(); 49 virtual bool TearDown(); OnLocalInit()50 virtual void OnLocalInit() {} 51 virtual int OnProcessCmd(const std::string &strCommand, int cmdLen, const std::string &strArgs, int argsLen, 52 const std::string &strExpectValue, int expectValueLen); 53 virtual int OnProcessMsg(const std::string &strMsg, int msgLen, std::string &strReturnValue, int returnBufLen); 54 virtual int OnNotify(const std::string ¬ifyType, const std::string &msg, int msgLen); 55 virtual void OnNotifyImf(DistributedMsg *pcline); 56 57 private: 58 int agentPort_; 59 int clientSockFd_; 60 std::string agentIpAddr_; 61 std::unique_ptr<std::thread> mpthCmdProcess_; 62 bool mbStop_; 63 DistributedCfg agentCfg_; 64 // init testcase agent to start agent server, only one client connected. 65 int InitAgentServer(); 66 // response the messages from client. 67 int DoCmdServer(int serverSockFd); 68 }; 69 } // namespace DistributeSystemTest 70 } // namespace OHOS 71 72 #endif // AW_CXX_DISTRIBUTED_DISTRIBUTED_AGENT_H_ 73 74