• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 & buffer,const CmdList & list,const std::string & name)38     explicit CmdExecutor(LogCollector& collector, HilogBuffer& buffer, const CmdList& list, const std::string& name)
39         : m_logCollector(collector), m_hilogBuffer(buffer), m_cmdList(list), m_name(name) {}
40     ~CmdExecutor();
41     void MainLoop(const std::string& sockName);
42 private:
43     void OnAcceptedConnection(std::unique_ptr<Socket> handler);
44     void ClientEventLoop(std::unique_ptr<Socket> handler);
45     void CleanFinishedClients();
46 
47     LogCollector& m_logCollector;
48     HilogBuffer& m_hilogBuffer;
49     CmdList m_cmdList;
50     std::string m_name;
51     std::list<std::unique_ptr<ClientThread>> m_clients;
52     std::mutex m_clientAccess;
53     std::vector<std::thread::id> m_finishedClients;
54     std::mutex m_finishedClientAccess;
55 };
56 } // namespace HiviewDFX
57 } // namespace OHOS
58 #endif
59