13f085823Sopenharmony_ci/*
23f085823Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
33f085823Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
43f085823Sopenharmony_ci * you may not use this file except in compliance with the License.
53f085823Sopenharmony_ci * You may obtain a copy of the License at
63f085823Sopenharmony_ci *
73f085823Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
83f085823Sopenharmony_ci *
93f085823Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
103f085823Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
113f085823Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
123f085823Sopenharmony_ci * See the License for the specific language governing permissions and
133f085823Sopenharmony_ci * limitations under the License.
143f085823Sopenharmony_ci */
153f085823Sopenharmony_ci
163f085823Sopenharmony_ci#ifndef AW_CXX_DISTRIBUTED_DISTRIBUTED_AGENT_H_
173f085823Sopenharmony_ci#define AW_CXX_DISTRIBUTED_DISTRIBUTED_AGENT_H_
183f085823Sopenharmony_ci
193f085823Sopenharmony_ci#include <map>
203f085823Sopenharmony_ci#include <vector>
213f085823Sopenharmony_ci#include <thread>
223f085823Sopenharmony_ci#include <iostream>
233f085823Sopenharmony_ci
243f085823Sopenharmony_ci#include "distributed.h"
253f085823Sopenharmony_ci#include "distributed_cfg.h"
263f085823Sopenharmony_ci
273f085823Sopenharmony_ci#include "hilog/log.h"
283f085823Sopenharmony_ci
293f085823Sopenharmony_ci#include "gtest/gtest.h"
303f085823Sopenharmony_ci
313f085823Sopenharmony_cinamespace OHOS {
323f085823Sopenharmony_cinamespace DistributeSystemTest {
333f085823Sopenharmony_ciclass DistributedAgent {
343f085823Sopenharmony_cipublic:
353f085823Sopenharmony_ci    DistributedAgent();
363f085823Sopenharmony_ci    virtual ~DistributedAgent();
373f085823Sopenharmony_ci    // deal with testcase logic by struct or xml command.
383f085823Sopenharmony_ci    int OnProcessCmd(const std::string &strCommand, int cmdLen, const std::string &strExpectValue, int expectValueLen);
393f085823Sopenharmony_ci    int Start(std::string cfgFile);
403f085823Sopenharmony_ci    int Stop();
413f085823Sopenharmony_ci    void Join();
423f085823Sopenharmony_ci    static const int RECE_LEN = 10;
433f085823Sopenharmony_ci    static const int CMD_LENGTH = 50;
443f085823Sopenharmony_ci    static const int HALF_NUM = 2;
453f085823Sopenharmony_ci    static constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, 0xD001690, "DistributedAgent"};
463f085823Sopenharmony_ci
473f085823Sopenharmony_ciprotected:
483f085823Sopenharmony_ci    virtual bool SetUp();
493f085823Sopenharmony_ci    virtual bool TearDown();
503f085823Sopenharmony_ci    virtual void OnLocalInit() {}
513f085823Sopenharmony_ci    virtual int OnProcessCmd(const std::string &strCommand, int cmdLen, const std::string &strArgs, int argsLen,
523f085823Sopenharmony_ci        const std::string &strExpectValue, int expectValueLen);
533f085823Sopenharmony_ci    virtual int OnProcessMsg(const std::string &strMsg, int msgLen, std::string &strReturnValue, int returnBufLen);
543f085823Sopenharmony_ci    virtual int OnNotify(const std::string &notifyType, const std::string &msg, int msgLen);
553f085823Sopenharmony_ci    virtual void OnNotifyImf(DistributedMsg *pcline);
563f085823Sopenharmony_ci
573f085823Sopenharmony_ciprivate:
583f085823Sopenharmony_ci    int agentPort_;
593f085823Sopenharmony_ci    int clientSockFd_;
603f085823Sopenharmony_ci    std::string agentIpAddr_;
613f085823Sopenharmony_ci    std::unique_ptr<std::thread> mpthCmdProcess_;
623f085823Sopenharmony_ci    bool mbStop_;
633f085823Sopenharmony_ci    DistributedCfg agentCfg_;
643f085823Sopenharmony_ci    // init testcase agent to start agent server, only one client connected.
653f085823Sopenharmony_ci    int InitAgentServer();
663f085823Sopenharmony_ci    // response the messages from client.
673f085823Sopenharmony_ci    int DoCmdServer(int serverSockFd);
683f085823Sopenharmony_ci};
693f085823Sopenharmony_ci} // namespace DistributeSystemTest
703f085823Sopenharmony_ci} // namespace OHOS
713f085823Sopenharmony_ci
723f085823Sopenharmony_ci#endif // AW_CXX_DISTRIBUTED_DISTRIBUTED_AGENT_H_
733f085823Sopenharmony_ci
74