• 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 "sp_profiler_factory.h"
18 #include "sp_server_socket.h"
19 #include "sp_utils.h"
20 namespace OHOS {
21 namespace SmartPerf {
22 class SpThreadSocket {
23 public:
MapToString(std::map<std::string,std::string> dataMap)24     std::string MapToString(std::map<std::string, std::string> dataMap) const
25     {
26         std::string result;
27         int i = 0;
28         std::string splitStr = "";
29         for (auto iter = dataMap.cbegin(); iter != dataMap.cend(); ++iter) {
30             printf("%s = %s\n", iter->first.c_str(), iter->second.c_str());
31             if (i > 0) {
32                 splitStr = "$$";
33             }
34             result += splitStr + iter->first.c_str() + "||" + iter->second.c_str();
35             i++;
36         }
37         return result;
38     }
ResPkgOrPid(const SpServerSocket & spSocket)39     std::string ResPkgOrPid(const SpServerSocket &spSocket) const
40     {
41         std::vector<std::string> sps;
42         SPUtils::StrSplit(spSocket.RecvBuf(), "::", sps);
43         return sps[1];
44     }
45 
Process()46     void Process() const
47     {
48         SpServerSocket spSocket;
49         spSocket.Init();
50         while (1) {
51             spSocket.Recvfrom();
52             HandleMsg(spSocket);
53         }
54         std::cout << "Socket Process finished!" << std::endl;
55         spSocket.Close();
56     }
HandleMsg(SpServerSocket & spSocket)57     void HandleMsg(SpServerSocket &spSocket) const
58     {
59         auto iterator = messageMap.begin();
60         while (iterator != messageMap.end()) {
61             if (SPUtils::IsSubString(spSocket.RecvBuf(), iterator->second)) {
62                 SpProfiler *profiler = SpProfilerFactory::GetProfilerItem(iterator->first);
63                 if (profiler == nullptr && (iterator->first == MessageType::SET_PKG_NAME)) {
64                     std::string curPkgName = ResPkgOrPid(spSocket);
65                     SpProfilerFactory::SetProfilerPkg(curPkgName);
66                     std::string pidCmd = "pidof " + curPkgName;
67                     std::string pidResult;
68                     if (SPUtils::LoadCmd(pidCmd, pidResult)) {
69                         SpProfilerFactory::SetProfilerPid(pidResult);
70                     }
71                     spSocket.Sendto(curPkgName);
72                 } else if (profiler == nullptr && (iterator->first == MessageType::SET_PROCESS_ID)) {
73                     SpProfilerFactory::SetProfilerPid(ResPkgOrPid(spSocket));
74                 } else if (profiler == nullptr) {
75                     std::string returnStr = iterator->second;
76                     spSocket.Sendto(returnStr);
77                 } else {
78                     std::map<std::string, std::string> data = profiler->ItemData();
79                     std::string sendData = MapToString(data);
80                     spSocket.Sendto(sendData);
81                 }
82                 break;
83             }
84             ++iterator;
85         }
86     }
87 };
88 }
89 }
90 #endif