• 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 #ifndef SP_THREAD_SOCKET_H
16 #define SP_THREAD_SOCKET_H
17 #include <functional>
18 #include "sp_profiler_factory.h"
19 #include "sp_server_socket.h"
20 #include "sp_utils.h"
21 #include "sp_task.h"
22 #include "control_call_cmd.h"
23 namespace OHOS {
24 namespace SmartPerf {
25 class SpThreadSocket {
26 public:
MapToString(std::map<std::string,std::string> dataMap)27     std::string MapToString(std::map<std::string, std::string> dataMap) const
28     {
29         std::string result;
30         int i = 0;
31         std::string splitStr = "";
32         for (auto iter = dataMap.cbegin(); iter != dataMap.cend(); ++iter) {
33             printf("%s = %s\n", iter->first.c_str(), iter->second.c_str());
34             if (i > 0) {
35                 splitStr = "$$";
36             }
37             result += splitStr + iter->first.c_str() + "||" + iter->second.c_str();
38             i++;
39         }
40         return result;
41     }
SplitMsg(const std::string recvBuf)42     std::string SplitMsg(const std::string recvBuf) const
43     {
44         std::vector<std::string> sps;
45         SPUtils::StrSplit(recvBuf, "::", sps);
46         return sps[1];
47     }
48 
Process(ProtoType type)49     void Process(ProtoType type) const
50     {
51         std::cout << "Socket Process called!" << std::endl;
52         SpServerSocket spSocket;
53         spSocket.Init(type);
54         if (type == ProtoType::TCP) {
55             std::cout << "Socket TCP Init called!" << std::endl;
56             while (1) {
57                 int procFd = spSocket.Accept();
58                 std::cout << "Socket TCP procFd: " << procFd << std::endl;
59                 while (procFd > 0) {
60                     spSocket.Recv();
61                     std::string recvStr = spSocket.RecvBuf();
62                     std::cout << "Socket TCP Recv: " << recvStr << std::endl;
63                     // 解析消息 分发处理
64                     DealMsg(recvStr, spSocket);
65                 }
66             }
67         }
68         if (type == ProtoType::UDP) {
69             while (1) {
70                 spSocket.Recvfrom();
71                 HandleMsg(spSocket);
72             }
73         }
74         std::cout << "Socket Process finished!" << std::endl;
75         spSocket.Close();
76     }
77     // TCP
DealMsg(std::string recvStr,SpServerSocket & spSocket)78     void DealMsg(std::string recvStr, SpServerSocket &spSocket) const
79     {
80         // init::-SessionId 12345678 -Interval 1000 -PKG ohos.samples.ecg -c -g -t -p -f -r
81         if (recvStr.find("init::") != std::string::npos) {
82             ErrCode code = SPTask::GetInstance().InitTask(SplitMsg(recvStr));
83             if (code == ErrCode::OK) {
84                 spSocket.Send("init::True");
85             } else if (code == ErrCode::FAILED) {
86                 spSocket.Send("init::False");
87             }
88         } else if (recvStr.find("start::") != std::string::npos) {
89             auto lambdaTask = [&spSocket](std::string data) { spSocket.Send(data); };
90             ErrCode code = SPTask::GetInstance().StartTask(lambdaTask);
91             if (code == ErrCode::OK) {
92                 spSocket.Send("start::True");
93             } else if (code == ErrCode::FAILED) {
94                 spSocket.Send("start::False");
95             }
96         } else if (recvStr.find("stop::") != std::string::npos) {
97             SPTask::GetInstance().StopTask();
98             spSocket.Send("stop::True");
99         } else if (recvStr.find("SP_daemon -editor") != std::string::npos) {
100             std::vector<std::string> vec;
101             int size = recvStr.size();
102             int j = 0;
103             for (int i = 0; i < size; i++) {
104                 if (recvStr[i] == ' ') {
105                     vec.push_back(recvStr.substr(j, i - j));
106                     j = i+1;
107                 }
108             }
109             vec.push_back(recvStr.substr(j, size - j));
110             OHOS::SmartPerf::ControlCallCmd controlCallCmd;
111             std::string result = controlCallCmd.GetResult(vec.size(), vec);
112             spSocket.Send(result);
113         }
114     }
115     // UDP
HandleMsg(SpServerSocket & spSocket)116     void HandleMsg(SpServerSocket &spSocket) const
117     {
118         auto iterator = messageMap.begin();
119         while (iterator != messageMap.end()) {
120             std::string recvBuf = spSocket.RecvBuf();
121             if (SPUtils::IsSubString(recvBuf, iterator->second)) {
122                 SpProfiler *profiler = SpProfilerFactory::GetProfilerItem(iterator->first);
123                 if (profiler == nullptr && (iterator->first == MessageType::SET_PKG_NAME)) {
124                     std::string curPkgName = SplitMsg(recvBuf);
125                     SpProfilerFactory::SetProfilerPkg(curPkgName);
126                     std::string pidCmd = "pidof " + curPkgName;
127                     std::string pidResult;
128                     if (SPUtils::LoadCmd(pidCmd, pidResult)) {
129                         SpProfilerFactory::SetProfilerPid(pidResult);
130                     }
131                     spSocket.Sendto(curPkgName);
132                 } else if (profiler == nullptr && (iterator->first == MessageType::SET_PROCESS_ID)) {
133                     SpProfilerFactory::SetProfilerPid(SplitMsg(recvBuf));
134                 } else if (profiler == nullptr) {
135                     std::string returnStr = iterator->second;
136                     spSocket.Sendto(returnStr);
137                 } else {
138                     std::map<std::string, std::string> data = profiler->ItemData();
139                     std::string sendData = MapToString(data);
140                     spSocket.Sendto(sendData);
141                 }
142                 break;
143             }
144             ++iterator;
145         }
146     }
147 };
148 }
149 }
150 #endif