-
Notifications
You must be signed in to change notification settings - Fork 276
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #139 from hyperledger/develop
Develop
- Loading branch information
Showing
26 changed files
with
417 additions
and
168 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
add_subdirectory(smart_contract) | ||
#add_subdirectory(smart_contract) | ||
add_subdirectory(crypto) | ||
add_subdirectory(repository) | ||
add_subdirectory(server) | ||
add_subdirectory(connection) | ||
add_subdirectory(service) | ||
#add_subdirectory(service) | ||
add_subdirectory(protobuf) | ||
add_subdirectory(config) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
set(JSON_PATH "${PROJECT_SOURCE_DIR}/core/vendor/json") | ||
|
||
include_directories( | ||
${JSON_PATH}/src | ||
) | ||
|
||
ADD_LIBRARY(iroha_config_with_json STATIC | ||
iroha_config_with_json.cpp | ||
) | ||
|
||
ADD_LIBRARY(peer_service_with_json STATIC | ||
peer_service_with_json.cpp | ||
) | ||
|
||
target_link_libraries( | ||
iroha_config_with_json | ||
peer_service_with_json | ||
logger | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
Copyright Soramitsu Co., Ltd. 2016 All Rights Reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
#ifndef IROHA_CONFIG_H | ||
#define IROHA_CONFIG_H | ||
|
||
#include "../../util/use_optional.hpp" | ||
#include "../../util/logger.hpp" | ||
#include "../../../core/vendor/json/src/json.hpp" | ||
#include <fstream> // ifstream, ofstream | ||
|
||
using json = nlohmann::json; | ||
|
||
namespace config { | ||
|
||
class IConfig { | ||
protected: | ||
virtual optional<json> openConfig(const std::string &configName) { | ||
if (_configData) { // already content loaded | ||
return _configData; | ||
} | ||
|
||
const auto PathToIROHA_HOME = [](){ | ||
const auto p = getenv("IROHA_HOME"); | ||
return p == nullptr ? "" : std::string(p); | ||
}(); | ||
|
||
if (PathToIROHA_HOME.empty()) { | ||
logger::error("peer with json") << "You must set IROHA_HOME!"; | ||
exit(EXIT_FAILURE); | ||
} | ||
|
||
auto jsonStr = openJSONText(PathToIROHA_HOME + "/" + configName); | ||
|
||
logger::info("peer with json") << "load json is " << jsonStr; | ||
|
||
setConfigData(std::move(jsonStr)); | ||
|
||
return _configData; | ||
} | ||
|
||
virtual std::string openJSONText(const std::string& PathToJSONFile) { | ||
std::ifstream ifs(PathToJSONFile); | ||
if (ifs.fail()) { | ||
logger::error("peer with json") << "Not found: " << PathToJSONFile; | ||
exit(EXIT_FAILURE); | ||
} | ||
|
||
std::istreambuf_iterator<char> it(ifs); | ||
return std::string(it, std::istreambuf_iterator<char>()); | ||
} | ||
|
||
virtual void setConfigData(std::string&& jsonStr) { | ||
try { | ||
_configData = json::parse(std::move(jsonStr)); | ||
} catch(...) { | ||
logger::error("peer with json") << "Bad json!!"; | ||
exit(EXIT_FAILURE); | ||
} | ||
} | ||
|
||
public: | ||
virtual std::string getConfigName() = 0; | ||
|
||
protected: | ||
optional<json> _configData; | ||
}; | ||
} | ||
|
||
#endif // IROHA_CONFIG_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
Copyright Soramitsu Co., Ltd. 2016 All Rights Reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
#include "iroha_config_with_json.hpp" | ||
|
||
namespace config { | ||
IrohaConfigManager::IrohaConfigManager() { } | ||
|
||
std::string IrohaConfigManager::openJSONText(const std::string& PathToJSONFile) { | ||
std::ifstream ifs(PathToJSONFile); | ||
if (ifs.fail()) { | ||
logger::warning("json config") << "Not found: " << PathToJSONFile; | ||
logger::warning("json config") << "All configurable parameters will be set to default"; | ||
return "{}"; | ||
} | ||
|
||
std::istreambuf_iterator<char> it(ifs); | ||
return std::string(it, std::istreambuf_iterator<char>()); | ||
} | ||
|
||
void IrohaConfigManager::setConfigData(std::string&& jsonStr) { | ||
try { | ||
_configData = json::parse(std::move(jsonStr)); | ||
} catch(...) { | ||
logger::warning("json config") << "Bad json!!" << jsonStr; | ||
} | ||
} | ||
|
||
IrohaConfigManager& IrohaConfigManager::getInstance() { | ||
static IrohaConfigManager manager; | ||
return manager; | ||
} | ||
|
||
std::string IrohaConfigManager::getConfigName() { | ||
return "docker/build/config/config.json"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
Copyright Soramitsu Co., Ltd. 2016 All Rights Reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
#ifndef IROHA_CONFIG_WITH_JSON_HPP | ||
#define IROHA_CONFIG_WITH_JSON_HPP | ||
|
||
#include "iroha_config.hpp" | ||
|
||
namespace config { | ||
class IrohaConfigManager: IConfig { | ||
protected: | ||
std::string openJSONText(const std::string& PathToJSONFile); | ||
void setConfigData(std::string&& jsonStr); | ||
|
||
private: | ||
IrohaConfigManager(); | ||
IrohaConfigManager(const IrohaConfigManager&); | ||
IrohaConfigManager& operator=(const IrohaConfigManager&); | ||
|
||
public: | ||
static IrohaConfigManager &getInstance(); | ||
|
||
template <typename T> | ||
T getParam(const std::string ¶m, const T &defaultValue) { | ||
if (auto config = openConfig(getConfigName())) { | ||
return config->value(param, defaultValue); | ||
} | ||
return defaultValue; | ||
} | ||
|
||
virtual std::string getConfigName(); | ||
}; | ||
} | ||
|
||
#endif // IROHA_CONFIG_WITH_JSON_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
Copyright Soramitsu Co., Ltd. 2016 All Rights Reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
#include "peer_service_with_json.hpp" | ||
|
||
namespace config { | ||
PeerServiceConfig::PeerServiceConfig() {} | ||
|
||
PeerServiceConfig& PeerServiceConfig::getInstance() { | ||
static PeerServiceConfig serviceConfig; | ||
return serviceConfig; | ||
} | ||
|
||
std::string PeerServiceConfig::getMyPublicKey() { | ||
if (auto config = openConfig(getConfigName())) { | ||
return (*config)["me"]["publicKey"].get<std::string>(); | ||
} | ||
return ""; | ||
} | ||
|
||
std::string PeerServiceConfig::getPrivateKey() { | ||
if (auto config = openConfig(getConfigName())) { | ||
return (*config)["me"]["privateKey"].get<std::string>(); | ||
} | ||
return ""; | ||
} | ||
|
||
std::string PeerServiceConfig::getMyIp() { | ||
if (auto config = openConfig(getConfigName())) { | ||
return (*config)["me"]["ip"].get<std::string>(); | ||
} | ||
return ""; | ||
} | ||
|
||
std::vector<std::unique_ptr<peer::Node>> PeerServiceConfig::getPeerList() { | ||
std::vector<std::unique_ptr<peer::Node>> nodes; | ||
if (auto config = openConfig(getConfigName())) { | ||
for (const auto& peer : (*config)["group"].get<std::vector<json>>()){ | ||
nodes.push_back(std::make_unique<peer::Node>( | ||
peer["ip"].get<std::string>(), | ||
peer["publicKey"].get<std::string>(), | ||
1 | ||
)); | ||
} | ||
} | ||
return nodes; | ||
} | ||
|
||
std::string PeerServiceConfig::getConfigName() { | ||
return "config/sumeragi.json"; | ||
} | ||
}; |
Oops, something went wrong.