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