• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) Huawei Technologies Co., Ltd. 2023. All rights reserved.
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 #include <iostream>
16 #include <sstream>
17 #include <cstring>
18 #include <arpa/inet.h>
19 #include <unistd.h>
20 #include <sys/socket.h>
21 #include "include/client_control.h"
22 #include "include/sp_utils.h"
23 #include "include/startup_delay.h"
24 #include "include/sp_log.h"
25 #include "include/common.h"
26 namespace OHOS {
27 namespace SmartPerf {
SocketStart(const std::string & args)28 int ClientControl::SocketStart(const std::string &args)
29 {
30     std::string messageInit = message + args;
31     int resultId = OHOS::SmartPerf::ClientControl::InitSocket();
32     if (resultId == 1) {
33         LOGE("ClientControl::InitSocket() error(%d)", resultId);
34         return resultId;
35     }
36     send(clientSocket, messageInit.c_str(), strlen(messageInit.c_str()), 0);
37     LOGD("start-stop messageInit : %s", messageInit.c_str());
38     read(clientSocket, buffer, numBuff);
39     LOGD("start-stop recv : %s", buffer);
40     send(clientSocket, message1, strlen(message1), 0);
41     LOGD("start-stop send : %s", message1);
42     read(clientSocket, buffer, numBuff);
43     LOGD("start-stop recv : %s", buffer);
44     char dest[arraySize] = {0};
45     size_t i;
46     for (i = 0; buffer[i] != '\0' && i < arraySize - 1; i++) {
47         dest[i] = buffer[i];
48     }
49     dest[i] = '\0';
50     if (strcmp(dest, "start::True") == 0) {
51         std::cout << "SP_daemon Collection begins" << std::endl;
52         OHOS::SmartPerf::ClientControl::CloseSocket();
53     } else {
54         std::cout << "SP_daemon Collection begins failed" << std::endl;
55         OHOS::SmartPerf::StartUpDelay sd;
56         sd.GetSpClear();
57     }
58     LOGD("ClientControl::SocketStart() ok");
59     return 0;
60 }
SocketStop()61 int ClientControl::SocketStop()
62 {
63     OHOS::SmartPerf::ClientControl::InitSocket();
64     send(clientSocket, message2, strlen(message2), 0);
65     read(clientSocket, buffer, numBuff);
66     char dest[arraySize] = {0};
67     size_t i;
68     for (i = 0; buffer[i] != '\0' && i < arraySize - 1; i++) {
69         dest[i] = buffer[i];
70     }
71     dest[i] = '\0';
72     if (strcmp(dest, "stop::True") == 0) {
73         std::cout << "SP_daemon Collection ended" << std::endl;
74         std::cout << "Output Path: data/local/tmp/smartperf/1/t_index_info.csv" << std::endl;
75         OHOS::SmartPerf::StartUpDelay sd;
76         OHOS::SmartPerf::ClientControl::CloseSocket();
77         sd.GetSpClear();
78     } else {
79         std::cout << "SP_daemon Collection ended failed" << std::endl;
80     }
81     LOGD("ClientControl::SocketStop() ok");
82     return 0;
83 }
InitSocket()84 int ClientControl::InitSocket()
85 {
86     clientSocket = socket(AF_INET, SOCK_STREAM, 0);
87     if (clientSocket == -1) {
88         LOGE("Faild to create socket");
89         return -1;
90     }
91     serverAddress.sin_family = AF_INET;
92     serverAddress.sin_addr.s_addr = inet_addr("127.0.0.1");
93     serverAddress.sin_port = htons(protNumber);
94     if (connect(clientSocket, reinterpret_cast<struct sockaddr *>(&serverAddress), sizeof(serverAddress)) < 0) {
95         OHOS::SmartPerf::StartUpDelay sd;
96         sd.GetSpClear();
97         LOGE("Failed to connect to server");
98         return 1;
99     }
100     LOGD("ClientControl::SocketInit() ok");
101     return 0;
102 }
StartSPDaemon() const103 void ClientControl::StartSPDaemon() const
104 {
105     std::string result = "";
106     std::string server = CMD_COMMAND_MAP.at(CmdCommand::SERVER);
107     SPUtils::LoadCmd(server, result);
108     sleep(1);
109 }
CloseSocket()110 int ClientControl::CloseSocket()
111 {
112     shutdown(clientSocket, SHUT_RD);
113     close(clientSocket);
114     clientSocket = -1;
115     LOGD("ClientControl::CloseSocket() ok");
116     return 0;
117 }
118 }
119 }