Skip to content

Commit

Permalink
rename prefix "_" -> surfix "_"
Browse files Browse the repository at this point in the history
  • Loading branch information
satellitex committed Jun 15, 2017
1 parent f5986ca commit 38d9737
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 75 deletions.
32 changes: 16 additions & 16 deletions core/peer_service/change_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ namespace peer_service{
// This scope is validation
namespace validation {
bool add(const Node &peer){
if (monitor::isExistIP(peer._ip))
if (monitor::isExistIP(peer.ip_))
return false;
if (monitor::isExistPublicKey(peer._public_key))
if (monitor::isExistPublicKey(peer.public_key_))
return false;
return true;
}
Expand Down Expand Up @@ -75,53 +75,53 @@ namespace peer_service{
// This scope is runtime
namespace runtime {
bool add(const Node &peer){
if (monitor::isExistIP(peer._ip))
if (monitor::isExistIP(peer.ip_))
return false;
if (monitor::isExistPublicKey(peer._public_key))
if (monitor::isExistPublicKey(peer.public_key_))
return false;
_peer_list.emplace_back( std::make_shared<Node>(peer) );
peer_list_.emplace_back( std::make_shared<Node>(peer) );
return true;
}
bool remove(const std::string &publicKey){
if (!monitor::isExistPublicKey(publicKey))
return false;
_peer_list.erase( monitor::findPeerPublicKey(publicKey) );
peer_list_.erase( monitor::findPeerPublicKey(publicKey) );
return true;
}
bool setTrust(const std::string &publicKey, const double &trust){
if (!monitor::isExistPublicKey(publicKey))
return false;
monitor::findPeerPublicKey(publicKey)->get()->_trust = trust;
monitor::findPeerPublicKey(publicKey)->get()->trust_ = trust;
return true;
}
bool changeTrust(const std::string &publicKey, const double &trust){
if (!monitor::isExistPublicKey(publicKey))
return false;
monitor::findPeerPublicKey(publicKey)->get()->_trust += trust;
monitor::findPeerPublicKey(publicKey)->get()->trust_ += trust;
return true;
}
bool setActive(const std::string &publicKey, const State state){
if (!monitor::isExistPublicKey(publicKey))
return false;

monitor::findPeerPublicKey(publicKey)->get()->_state = state;
monitor::findPeerPublicKey(publicKey)->get()->state_ = state;
update();
}


void update(){
std::unordered_set<std::string> tmp_active;

for( auto it = _active_peer_list.begin(); it != _active_peer_list.end(); it++ ){
if( it->get()->_state != ACTIVE )
it = _active_peer_list.erase(it);
for( auto it = active_peer_list_.begin(); it != active_peer_list_.end(); it++ ){
if( it->get()->state_ != ACTIVE )
it = active_peer_list_.erase(it);
else
tmp_active.insert( it->get()->_public_key );
tmp_active.insert( it->get()->public_key_ );
}

for( auto it = _peer_list.begin(); it != _peer_list.end(); it++ ){
if( it->get()->_state == ACTIVE && !tmp_active.count(it->get()->_public_key) )
_active_peer_list.emplace_back( *it );
for( auto it = peer_list_.begin(); it != peer_list_.end(); it++ ){
if( it->get()->state_ == ACTIVE && !tmp_active.count(it->get()->public_key_) )
active_peer_list_.emplace_back( *it );
}
}
}
Expand Down
36 changes: 18 additions & 18 deletions core/peer_service/monitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@ namespace peer_service{
return getActivePeerAt(0);
}
std::string getCurrentLeaderIp(){
return getActivePeerAt(0)->_ip;
return getActivePeerAt(0)->ip_;
}


void initialize(){
if( !_peer_list.empty() ) return;
if( !peer_list_.empty() ) return;
// TODO Read config.json

// At First myself only
_peer_list.emplace_back(
peer_list_.emplace_back(
std::make_shared<Node>(
self_state::getIp(),
self_state::getPublicKey(),
Expand All @@ -52,61 +52,61 @@ namespace peer_service{
}

Nodes getAllPeerList(){
return _peer_list;
return peer_list_;
}
std::shared_ptr<Node> getPeerAt(unsigned int index){
try {
return _peer_list.at(index);
return peer_list_.at(index);
} catch( const std::out_of_range& oor ){
// TODO Out ot Range Exception
}
}
std::vector<std::string> getAllIpList(){
std::vector<std::string> ret;
for( auto v : _peer_list )
ret.emplace_back( v->_ip );
for( auto v : peer_list_ )
ret.emplace_back( v->ip_ );
return ret;
}

Nodes getActivePeerList(){
return _active_peer_list;
return active_peer_list_;
}
std::shared_ptr<Node> getActivePeerAt(unsigned int index){
try {
return _active_peer_list.at(index);
return active_peer_list_.at(index);
} catch( const std::out_of_range& oor ){
// TODO Out ot Range Exception
}
}
std::vector<std::string> getActiveIpList(){
std::vector<std::string> ret;
for( auto v : _active_peer_list )
ret.emplace_back( v->_ip );
for( auto v : active_peer_list_ )
ret.emplace_back( v->ip_ );
return ret;
}
int getActivePeerSize(){
return _active_peer_list.size();
return active_peer_list_.size();
}



bool isExistIP(const std::string &ip){
return findPeerIP(ip) != _peer_list.end();
return findPeerIP(ip) != peer_list_.end();
}
bool isExistPublicKey(const std::string &publicKey){
return findPeerPublicKey(publicKey) != _peer_list.end();
return findPeerPublicKey(publicKey) != peer_list_.end();
}

Nodes::iterator findPeerIP(const std::string &ip){
initialize();
return std::find_if(_peer_list.begin(), _peer_list.end(),
[&ip](const auto &p) { return p->_ip == ip; });
return std::find_if(peer_list_.begin(), peer_list_.end(),
[&ip](const auto &p) { return p->ip_ == ip; });
}
Nodes::iterator findPeerPublicKey(const std::string &publicKey){
initialize();
return std::find_if(
_peer_list.begin(), _peer_list.end(),
[&publicKey](const auto &p) { return p->_public_key == publicKey; });
peer_list_.begin(), peer_list_.end(),
[&publicKey](const auto &p) { return p->public_key_ == publicKey; });
}

}; // namespace monitor
Expand Down
38 changes: 19 additions & 19 deletions core/peer_service/peer_service.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,40 +32,40 @@ namespace peer_service {
inline static const std::string defaultPubKey() { return ""; }

struct Node {
std::string _ip;
std::string _public_key;
std::string _name;
double _trust;
State _state;
std::string ip_;
std::string public_key_;
std::string name_;
double trust_;
State state_;

Node(std::string ip = defaultIP(),
std::string public_key = defaultPubKey(),
std::string name = defaultName(),
double trust = 100.0,
State state = PREPARE)
: _ip(ip),
_public_key(public_key),
_name(name),
_trust(trust),
_state(state) {}
: ip_(ip),
public_key_(public_key),
name_(name),
trust_(trust),
state_(state) {}

Node(const Node &p) :
_ip(p._ip),
_public_key(p._public_key),
_name(p._name),
_trust(p._trust),
_state(p._state) {}
ip_(p.ip_),
public_key_(p.public_key_),
name_(p.name_),
trust_(p.trust_),
state_(p.state_) {}


bool isDefaultIP() const { return _ip == defaultIP(); }
bool isDefaultPubKey() const { return _public_key == defaultPubKey(); }
bool isDefaultIP() const { return ip_ == defaultIP(); }
bool isDefaultPubKey() const { return public_key_ == defaultPubKey(); }
};

using Nodes = std::vector<std::shared_ptr<Node>>;


Nodes _peer_list;
Nodes _active_peer_list;
Nodes peer_list_;
Nodes active_peer_list_;

} // namespace peer_service

Expand Down
44 changes: 22 additions & 22 deletions core/peer_service/self_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,27 +29,27 @@ limitations under the License.
namespace peer_service {
namespace self_state {

std::string _ip;
std::string _public_key;
std::string _private_key;
std::string _name;
double _trust;
std::string ip_;
std::string public_key_;
std::string private_key_;
std::string name_;
double trust_;

uint64_t _active_time;
State _state;
uint64_t active_time_;
State state_;

void initializeMyKey() {
if (_public_key.empty() || _private_key.empty()) {
if (public_key_.empty() || private_key_.empty()) {
crypto::signature::KeyPair keyPair = crypto::signature::generateKeyPair();
_public_key = crypto::base64::encode(keyPair.publicKey);
_private_key = crypto::base64::encode(keyPair.privateKey);
public_key_ = crypto::base64::encode(keyPair.publicKey);
private_key_ = crypto::base64::encode(keyPair.privateKey);
}
}

void initializeMyIp() {
std::string interface = "eth0"; // TODO : temporary "eth0"

if (_ip.empty()) {
if (ip_.empty()) {
int sockfd;
struct ifreq ifr;

Expand All @@ -58,31 +58,31 @@ namespace peer_service {
strncpy(ifr.ifr_name, interface.c_str(), IFNAMSIZ - 1);
ioctl(sockfd, SIOCGIFADDR, &ifr);
close(sockfd);
_ip = inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr);
ip_ = inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr);
}
}


std::string getPublicKey() {
initializeMyKey();
return _public_key;
return public_key_;
}
std::string getPrivateKey() {
initializeMyKey();
return _private_key;
return private_key_;
}
std::string getIp() {
initializeMyIp();
return _ip;
return ip_;
}
std::string getName() {
return _name;//TODO : initialize name
return name_;//TODO : initialize name
}
State getState() { return _state; }
State getState() { return state_; }


bool isLeader() {
return monitor::getCurrentLeader()->_public_key == _public_key;
return monitor::getCurrentLeader()->public_key_ == public_key_;
}

double getTrust() {
Expand All @@ -91,12 +91,12 @@ namespace peer_service {



uint64_t getActiveTime() { return _active_time; }
uint64_t getActiveTime() { return active_time_; }

void activate() {
_state = ACTIVE;
_active_time = ::common::datetime::unixtime();
state_ = ACTIVE;
active_time_ = ::common::datetime::unixtime();
}
void stop() { _state = PREPARE; }
void stop() { state_ = PREPARE; }
};
};

0 comments on commit 38d9737

Please sign in to comment.