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
16 #include "distributed_cfg.h"
17
18 #include <iostream>
19 #include <fstream>
20
21 #include "hilog/log.h"
22
23 namespace OHOS {
24 namespace DistributeSystemTest {
25 using namespace std;
26 static constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, 0, "DistributeTestEnvironment"};
27
DistributedCfg()28 DistributedCfg::DistributedCfg()
29 {
30 }
31
~DistributedCfg()32 DistributedCfg::~DistributedCfg()
33 {
34 }
35
OpenCfg(std::string fileName)36 bool DistributedCfg::OpenCfg(std::string fileName)
37 {
38 ifstream cfgFile;
39 size_t position = 0;
40 std::string strline;
41
42 cfgFile.open(fileName.c_str());
43 if (!cfgFile.is_open()) {
44 return false;
45 }
46
47 std::string key;
48 std::string value;
49 while (getline(cfgFile, strline)) {
50 if (strline == "") {
51 continue;
52 }
53 position = strline.find(":");
54 if (position == std::string::npos) {
55 continue;
56 }
57 key = strline.substr(0, position);
58 value = strline.substr(position + 1);
59 cfgMap_.insert(std::pair<std::string, std::string>(key, value));
60 }
61
62 cfgFile.close();
63 return true;
64 }
65
GetCfgVal(std::string key,std::string & value)66 bool DistributedCfg::GetCfgVal(std::string key, std::string &value)
67 {
68 std::string iplist = "";
69 std::map<std::string, std::string>::iterator cfgIterator;
70 cfgIterator = cfgMap_.find(key);
71 if (cfgIterator == cfgMap_.end()) {
72 return false;
73 }
74 value = cfgIterator->second;
75 return true;
76 }
77
GetValueInString(std::string str,size_t devNo)78 std::string DistributedCfg::GetValueInString(std::string str, size_t devNo)
79 {
80 size_t pos = 0;
81 size_t posend = 0;
82 for (size_t i = 0; i < devNo; i++) {
83 posend = str.find(",", pos);
84 if (posend == std::string::npos) {
85 return "";
86 }
87 pos = posend + 1;
88 }
89 std::string ipaddr;
90 posend = str.find(",", pos);
91 if (posend != std::string::npos) {
92 ipaddr = str.substr(pos, posend - pos);
93 } else {
94 ipaddr = "";
95 }
96 return ipaddr;
97 }
98
GetDeviceIp(std::string fileName,size_t devNo)99 std::string DistributedCfg::GetDeviceIp(std::string fileName, size_t devNo)
100 {
101 devNo--;
102 if (!OpenCfg(fileName)) {
103 HiviewDFX::HiLog::Error(LABEL,
104 "OpenCfg() failed! make sure the filename:%s of major or agent",
105 fileName.c_str());
106 return "";
107 }
108 std::string valueOfIps;
109 if (!GetCfgVal("agentlist", valueOfIps)) {
110 HiviewDFX::HiLog::Error(LABEL,
111 "GetCfgVal() failed! make sure the filename:%s of major or agent",
112 fileName.c_str());
113 return "";
114 }
115 std::string ip = GetValueInString(valueOfIps, devNo);
116 if (!ip.compare("")) {
117 HiviewDFX::HiLog::Error(LABEL,
118 "GetValueOfString() return ""! %zu maybe bigger than the sum of devices_online",
119 devNo + 1);
120 return "";
121 }
122 HiviewDFX::HiLog::Info(LABEL, "get %zu device's ip : %s", devNo + 1, ip.c_str());
123 return ip;
124 }
125
GetDeviceUuid(std::string fileName,size_t devNo)126 std::string DistributedCfg::GetDeviceUuid(std::string fileName, size_t devNo)
127 {
128 if (!OpenCfg(fileName)) {
129 HiviewDFX::HiLog::Error(LABEL,
130 "OpenCfg() failed! make sure the filename:%s of major or agent",
131 fileName.c_str());
132 return "";
133 }
134 std::string valueOfUuids;
135 if (!GetCfgVal("devicesuuid", valueOfUuids)) {
136 HiviewDFX::HiLog::Error(LABEL,
137 "GetCfgVal() failed! make sure the filename:%s of major or agent",
138 fileName.c_str());
139 return "";
140 }
141 std::string uuid = GetValueInString(valueOfUuids, devNo);
142 if (!uuid.compare("")) {
143 printf("device:%zu uuid is null \n", devNo);
144 HiviewDFX::HiLog::Error(LABEL,
145 "GetValueOfString() return ""! %zu maybe bigger than the sum of devices_online",
146 devNo);
147 return "";
148 }
149 HiviewDFX::HiLog::Info(LABEL, "get %zu device's uuid : %s", devNo, uuid.c_str());
150 return uuid;
151 }
152
GetInstance()153 std::unique_ptr<DistributedCfg>& DistributedCfg::GetInstance()
154 {
155 if (DistributedCfg::getCfg_ == nullptr) {
156 DistributedCfg::getCfg_ = std::make_unique<DistributedCfg>();
157 }
158 return DistributedCfg::getCfg_;
159 }
160
161 std::unique_ptr<DistributedCfg> DistributedCfg::getCfg_ = nullptr;
162 } // namespace DistributeSystemTest
163 } // namespace OHOS
164