• 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 namespace OHOS {
26 namespace SmartPerf {
SocketStart(const std::string & args)27 int ClientControl::SocketStart(const std::string &args)
28 {
29     std::string messageInit = message + args;
30     int resultId = OHOS::SmartPerf::ClientControl::InitSocket();
31     if (resultId == 1) {
32         LOGE("ClientControl::InitSocket() error(%d)", resultId);
33         return resultId;
34     }
35     send(clientSocket, messageInit.c_str(), strlen(messageInit.c_str()), 0);
36     read(clientSocket, buffer, numBuff);
37     send(clientSocket, message1, strlen(message1), 0);
38     read(clientSocket, buffer, numBuff);
39     char dest[arraySize] = {0};
40     size_t i;
41     for (i = 0; buffer[i] != '\0' && i < arraySize - 1; i++) {
42         dest[i] = buffer[i];
43     }
44     dest[i] = '\0';
45     if (strcmp(dest, "start::True") == 0) {
46         std::cout << "SP_daemon Collection begins" << std::endl;
47         OHOS::SmartPerf::ClientControl::CloseSocket();
48     } else {
49         std::cout << "SP_daemon Collection begins failed" << std::endl;
50         OHOS::SmartPerf::StartUpDelay sd;
51         sd.GetSpTcp();
52     }
53     LOGI("ClientControl::SocketStart() OK");
54     return 0;
55 }
SocketStop()56 int ClientControl::SocketStop()
57 {
58     OHOS::SmartPerf::ClientControl::InitSocket();
59     send(clientSocket, message2, strlen(message2), 0);
60     read(clientSocket, buffer, numBuff);
61     char dest[arraySize] = {0};
62     size_t i;
63     for (i = 0; buffer[i] != '\0' && i < arraySize - 1; i++) {
64         dest[i] = buffer[i];
65     }
66     dest[i] = '\0';
67     if (strcmp(dest, "stop::True") == 0) {
68         std::cout << "SP_daemon Collection ended" << std::endl;
69         std::cout << "Output Path: data/local/tmp/smartperf/1/t_index_info.csv" << std::endl;
70         OHOS::SmartPerf::StartUpDelay sd;
71         sd.GetSpTcp();
72         OHOS::SmartPerf::ClientControl::CloseSocket();
73     } else {
74         std::cout << "SP_daemon Collection ended failed" << std::endl;
75     }
76     LOGI("ClientControl::SocketStop() OK");
77     return 0;
78 }
InitSocket()79 int ClientControl::InitSocket()
80 {
81     clientSocket = socket(AF_INET, SOCK_STREAM, 0);
82     if (clientSocket == -1) {
83         std::cout << "Faild to create socket" << std::endl;
84         LOGE("Faild to create socket");
85         return -1;
86     }
87     serverAddress.sin_family = AF_INET;
88     serverAddress.sin_addr.s_addr = inet_addr("127.0.0.1");
89     serverAddress.sin_port = htons(protNumber);
90     if (connect(clientSocket, reinterpret_cast<struct sockaddr *>(&serverAddress), sizeof(serverAddress)) < 0) {
91         std::cout << "Failed to connect to server" << std::endl;
92         OHOS::SmartPerf::StartUpDelay sd;
93         sd.GetSpTcp();
94         LOGE("Failed to connect to server");
95         return 1;
96     }
97     LOGI("ClientControl::InitSocket() OK");
98     return 0;
99 }
StartSPDaemon() const100 void ClientControl::StartSPDaemon() const
101 {
102     std::string result = "";
103     SPUtils::LoadCmd("SP_daemon", result);
104     sleep(1);
105     LOGI("ClientControl::StartSPDaemon() OK");
106 }
CloseSocket()107 int ClientControl::CloseSocket()
108 {
109     shutdown(clientSocket, SHUT_RD);
110     close(clientSocket);
111     clientSocket = -1;
112     LOGI("ClientControl::CloseSocket() OK");
113     return 0;
114 }
115 }
116 }