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