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