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 CMD_EXECUTOR_H
17 #define CMD_EXECUTOR_H
18 
19 #include <list>
20 #include <memory>
21 #include <atomic>
22 #include <thread>
23 
24 #include <socket.h>
25 #include "log_buffer.h"
26 #include "log_collector.h"
27 #include "service_controller.h"
28 
29 namespace OHOS {
30 namespace HiviewDFX {
31 struct ClientThread {
32     std::thread m_clientThread;
33     std::atomic<bool> m_stopThread;
34 };
35 
36 class CmdExecutor {
37 public:
CmdExecutor(LogCollector & collector,HilogBuffer & hilogBuffer,HilogBuffer & kmsgBuffer,const CmdList & list,const std::string & name)38     explicit CmdExecutor(LogCollector& collector, HilogBuffer& hilogBuffer, HilogBuffer& kmsgBuffer,
39         const CmdList& list, const std::string& name) : m_logCollector(collector), m_hilogBuffer(hilogBuffer),
40         m_kmsgBuffer(kmsgBuffer), m_cmdList(list), m_name(name) {}
41     ~CmdExecutor();
42     void MainLoop(const std::string& sockName);
43 private:
44     void OnAcceptedConnection(std::unique_ptr<Socket> handler);
45     void ClientEventLoop(std::unique_ptr<Socket> handler);
46     void CleanFinishedClients();
47 
48     LogCollector& m_logCollector;
49     HilogBuffer& m_hilogBuffer;
50     HilogBuffer& m_kmsgBuffer;
51     CmdList m_cmdList;
52     std::string m_name;
53     std::list<std::unique_ptr<ClientThread>> m_clients;
54     std::mutex m_clientAccess;
55     std::vector<std::thread::id> m_finishedClients;
56     std::mutex m_finishedClientAccess;
57 };
58 } // namespace HiviewDFX
59 } // namespace OHOS
60 #endif
61