12498b56bSopenharmony_ci/*
22498b56bSopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
32498b56bSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
42498b56bSopenharmony_ci * you may not use this file except in compliance with the License.
52498b56bSopenharmony_ci * You may obtain a copy of the License at
62498b56bSopenharmony_ci *
72498b56bSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
82498b56bSopenharmony_ci *
92498b56bSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
102498b56bSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
112498b56bSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
122498b56bSopenharmony_ci * See the License for the specific language governing permissions and
132498b56bSopenharmony_ci * limitations under the License.
142498b56bSopenharmony_ci */
152498b56bSopenharmony_ci
162498b56bSopenharmony_ci#ifndef CMD_EXECUTOR_H
172498b56bSopenharmony_ci#define CMD_EXECUTOR_H
182498b56bSopenharmony_ci
192498b56bSopenharmony_ci#include <list>
202498b56bSopenharmony_ci#include <memory>
212498b56bSopenharmony_ci#include <atomic>
222498b56bSopenharmony_ci#include <thread>
232498b56bSopenharmony_ci
242498b56bSopenharmony_ci#include <socket.h>
252498b56bSopenharmony_ci#include "log_buffer.h"
262498b56bSopenharmony_ci#include "log_collector.h"
272498b56bSopenharmony_ci#include "service_controller.h"
282498b56bSopenharmony_ci
292498b56bSopenharmony_cinamespace OHOS {
302498b56bSopenharmony_cinamespace HiviewDFX {
312498b56bSopenharmony_cistruct ClientThread {
322498b56bSopenharmony_ci    std::thread m_clientThread;
332498b56bSopenharmony_ci    std::atomic<bool> m_stopThread;
342498b56bSopenharmony_ci};
352498b56bSopenharmony_ci
362498b56bSopenharmony_ciclass CmdExecutor {
372498b56bSopenharmony_cipublic:
382498b56bSopenharmony_ci    explicit CmdExecutor(LogCollector& collector, HilogBuffer& hilogBuffer, HilogBuffer& kmsgBuffer,
392498b56bSopenharmony_ci        const CmdList& list, const std::string& name) : m_logCollector(collector), m_hilogBuffer(hilogBuffer),
402498b56bSopenharmony_ci        m_kmsgBuffer(kmsgBuffer), m_cmdList(list), m_name(name) {}
412498b56bSopenharmony_ci    ~CmdExecutor();
422498b56bSopenharmony_ci    void MainLoop(const std::string& sockName);
432498b56bSopenharmony_ciprivate:
442498b56bSopenharmony_ci    void OnAcceptedConnection(std::unique_ptr<Socket> handler);
452498b56bSopenharmony_ci    void ClientEventLoop(std::unique_ptr<Socket> handler);
462498b56bSopenharmony_ci    void CleanFinishedClients();
472498b56bSopenharmony_ci
482498b56bSopenharmony_ci    LogCollector& m_logCollector;
492498b56bSopenharmony_ci    HilogBuffer& m_hilogBuffer;
502498b56bSopenharmony_ci    HilogBuffer& m_kmsgBuffer;
512498b56bSopenharmony_ci    CmdList m_cmdList;
522498b56bSopenharmony_ci    std::string m_name;
532498b56bSopenharmony_ci    std::list<std::unique_ptr<ClientThread>> m_clients;
542498b56bSopenharmony_ci    std::mutex m_clientAccess;
552498b56bSopenharmony_ci    std::vector<std::thread::id> m_finishedClients;
562498b56bSopenharmony_ci    std::mutex m_finishedClientAccess;
572498b56bSopenharmony_ci};
582498b56bSopenharmony_ci} // namespace HiviewDFX
592498b56bSopenharmony_ci} // namespace OHOS
602498b56bSopenharmony_ci#endif
61