13f085823Sopenharmony_ci/* 23f085823Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd. 33f085823Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 43f085823Sopenharmony_ci * you may not use this file except in compliance with the License. 53f085823Sopenharmony_ci * You may obtain a copy of the License at 63f085823Sopenharmony_ci * 73f085823Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 83f085823Sopenharmony_ci * 93f085823Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 103f085823Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 113f085823Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 123f085823Sopenharmony_ci * See the License for the specific language governing permissions and 133f085823Sopenharmony_ci * limitations under the License. 143f085823Sopenharmony_ci */ 153f085823Sopenharmony_ci 163f085823Sopenharmony_ci#include "distributed_cfg.h" 173f085823Sopenharmony_ci 183f085823Sopenharmony_ci#include <iostream> 193f085823Sopenharmony_ci#include <fstream> 203f085823Sopenharmony_ci 213f085823Sopenharmony_cinamespace OHOS { 223f085823Sopenharmony_cinamespace DistributeSystemTest { 233f085823Sopenharmony_ciusing namespace std; 243f085823Sopenharmony_ciusing namespace OHOS::HiviewDFX; 253f085823Sopenharmony_ci 263f085823Sopenharmony_ci 273f085823Sopenharmony_ciDistributedCfg::DistributedCfg() 283f085823Sopenharmony_ci{ 293f085823Sopenharmony_ci} 303f085823Sopenharmony_ci 313f085823Sopenharmony_ciDistributedCfg::~DistributedCfg() 323f085823Sopenharmony_ci{ 333f085823Sopenharmony_ci} 343f085823Sopenharmony_ci 353f085823Sopenharmony_cibool DistributedCfg::OpenCfg(std::string fileName) 363f085823Sopenharmony_ci{ 373f085823Sopenharmony_ci ifstream cfgFile; 383f085823Sopenharmony_ci size_t position = 0; 393f085823Sopenharmony_ci std::string strline; 403f085823Sopenharmony_ci 413f085823Sopenharmony_ci cfgFile.open(fileName.c_str()); 423f085823Sopenharmony_ci if (!cfgFile.is_open()) { 433f085823Sopenharmony_ci return false; 443f085823Sopenharmony_ci } 453f085823Sopenharmony_ci 463f085823Sopenharmony_ci std::string key; 473f085823Sopenharmony_ci std::string value; 483f085823Sopenharmony_ci while (getline(cfgFile, strline)) { 493f085823Sopenharmony_ci if (strline == "") { 503f085823Sopenharmony_ci continue; 513f085823Sopenharmony_ci } 523f085823Sopenharmony_ci position = strline.find(":"); 533f085823Sopenharmony_ci if (position == std::string::npos) { 543f085823Sopenharmony_ci continue; 553f085823Sopenharmony_ci } 563f085823Sopenharmony_ci key = strline.substr(0, position); 573f085823Sopenharmony_ci value = strline.substr(position + 1); 583f085823Sopenharmony_ci cfgMap_.insert(std::pair<std::string, std::string>(key, value)); 593f085823Sopenharmony_ci } 603f085823Sopenharmony_ci 613f085823Sopenharmony_ci cfgFile.close(); 623f085823Sopenharmony_ci return true; 633f085823Sopenharmony_ci} 643f085823Sopenharmony_ci 653f085823Sopenharmony_cibool DistributedCfg::GetCfgVal(std::string key, std::string &value) 663f085823Sopenharmony_ci{ 673f085823Sopenharmony_ci std::string iplist = ""; 683f085823Sopenharmony_ci std::map<std::string, std::string>::iterator cfgIterator; 693f085823Sopenharmony_ci cfgIterator = cfgMap_.find(key); 703f085823Sopenharmony_ci if (cfgIterator == cfgMap_.end()) { 713f085823Sopenharmony_ci return false; 723f085823Sopenharmony_ci } 733f085823Sopenharmony_ci value = cfgIterator->second; 743f085823Sopenharmony_ci return true; 753f085823Sopenharmony_ci} 763f085823Sopenharmony_ci 773f085823Sopenharmony_cistd::string DistributedCfg::GetValueInString(std::string str, size_t devNo) 783f085823Sopenharmony_ci{ 793f085823Sopenharmony_ci size_t pos = 0; 803f085823Sopenharmony_ci size_t posend = 0; 813f085823Sopenharmony_ci for (size_t i = 0; i < devNo; i++) { 823f085823Sopenharmony_ci posend = str.find(",", pos); 833f085823Sopenharmony_ci if (posend == std::string::npos) { 843f085823Sopenharmony_ci return ""; 853f085823Sopenharmony_ci } 863f085823Sopenharmony_ci pos = posend + 1; 873f085823Sopenharmony_ci } 883f085823Sopenharmony_ci std::string ipaddr; 893f085823Sopenharmony_ci posend = str.find(",", pos); 903f085823Sopenharmony_ci if (posend != std::string::npos) { 913f085823Sopenharmony_ci ipaddr = str.substr(pos, posend - pos); 923f085823Sopenharmony_ci } else { 933f085823Sopenharmony_ci ipaddr = ""; 943f085823Sopenharmony_ci } 953f085823Sopenharmony_ci return ipaddr; 963f085823Sopenharmony_ci} 973f085823Sopenharmony_ci 983f085823Sopenharmony_cistd::string DistributedCfg::GetDeviceIp(std::string fileName, size_t devNo) 993f085823Sopenharmony_ci{ 1003f085823Sopenharmony_ci devNo--; 1013f085823Sopenharmony_ci if (!OpenCfg(fileName)) { 1023f085823Sopenharmony_ci HiLog::Error(DistributedCfg::LABEL, 1033f085823Sopenharmony_ci "OpenCfg() failed! make sure the filename:%s of major or agent", 1043f085823Sopenharmony_ci fileName.c_str()); 1053f085823Sopenharmony_ci return ""; 1063f085823Sopenharmony_ci } 1073f085823Sopenharmony_ci std::string valueOfIps; 1083f085823Sopenharmony_ci if (!GetCfgVal("agentlist", valueOfIps)) { 1093f085823Sopenharmony_ci HiLog::Error(DistributedCfg::LABEL, 1103f085823Sopenharmony_ci "GetCfgVal() failed! make sure the filename:%s of major or agent", 1113f085823Sopenharmony_ci fileName.c_str()); 1123f085823Sopenharmony_ci return ""; 1133f085823Sopenharmony_ci } 1143f085823Sopenharmony_ci std::string ip = GetValueInString(valueOfIps, devNo); 1153f085823Sopenharmony_ci if (!ip.compare("")) { 1163f085823Sopenharmony_ci HiLog::Error(DistributedCfg::LABEL, 1173f085823Sopenharmony_ci "GetValueOfString() return ""! %zu maybe bigger than the sum of devices_online", 1183f085823Sopenharmony_ci devNo + 1); 1193f085823Sopenharmony_ci return ""; 1203f085823Sopenharmony_ci } 1213f085823Sopenharmony_ci HiLog::Info(DistributedCfg::LABEL, "get %zu device's ip : %s", devNo + 1, ip.c_str()); 1223f085823Sopenharmony_ci return ip; 1233f085823Sopenharmony_ci} 1243f085823Sopenharmony_ci 1253f085823Sopenharmony_cistd::string DistributedCfg::GetDeviceUuid(std::string fileName, size_t devNo) 1263f085823Sopenharmony_ci{ 1273f085823Sopenharmony_ci if (!OpenCfg(fileName)) { 1283f085823Sopenharmony_ci HiLog::Error(DistributedCfg::LABEL, 1293f085823Sopenharmony_ci "OpenCfg() failed! make sure the filename:%s of major or agent", 1303f085823Sopenharmony_ci fileName.c_str()); 1313f085823Sopenharmony_ci return ""; 1323f085823Sopenharmony_ci } 1333f085823Sopenharmony_ci std::string valueOfUuids; 1343f085823Sopenharmony_ci if (!GetCfgVal("devicesuuid", valueOfUuids)) { 1353f085823Sopenharmony_ci HiLog::Error(DistributedCfg::LABEL, 1363f085823Sopenharmony_ci "GetCfgVal() failed! make sure the filename:%s of major or agent", 1373f085823Sopenharmony_ci fileName.c_str()); 1383f085823Sopenharmony_ci return ""; 1393f085823Sopenharmony_ci } 1403f085823Sopenharmony_ci std::string uuid = GetValueInString(valueOfUuids, devNo); 1413f085823Sopenharmony_ci if (!uuid.compare("")) { 1423f085823Sopenharmony_ci printf("device:%zu uuid is null \n", devNo); 1433f085823Sopenharmony_ci HiLog::Error(DistributedCfg::LABEL, 1443f085823Sopenharmony_ci "GetValueOfString() return ""! %zu maybe bigger than the sum of devices_online", 1453f085823Sopenharmony_ci devNo); 1463f085823Sopenharmony_ci return ""; 1473f085823Sopenharmony_ci } 1483f085823Sopenharmony_ci HiLog::Info(DistributedCfg::LABEL, "get %zu device's uuid : %s", devNo, uuid.c_str()); 1493f085823Sopenharmony_ci return uuid; 1503f085823Sopenharmony_ci} 1513f085823Sopenharmony_ci 1523f085823Sopenharmony_cistd::unique_ptr<DistributedCfg>& DistributedCfg::GetInstance() 1533f085823Sopenharmony_ci{ 1543f085823Sopenharmony_ci if (DistributedCfg::getCfg_ == nullptr) { 1553f085823Sopenharmony_ci DistributedCfg::getCfg_ = std::make_unique<DistributedCfg>(); 1563f085823Sopenharmony_ci } 1573f085823Sopenharmony_ci return DistributedCfg::getCfg_; 1583f085823Sopenharmony_ci} 1593f085823Sopenharmony_ci 1603f085823Sopenharmony_cistd::unique_ptr<DistributedCfg> DistributedCfg::getCfg_ = nullptr; 1613f085823Sopenharmony_ci} // namespace DistributeSystemTest 1623f085823Sopenharmony_ci} // namespace OHOS 163