Skip to content

Commit

Permalink
implement change_state
Browse files Browse the repository at this point in the history
  • Loading branch information
satellitex committed Jun 15, 2017
1 parent d21f4de commit a6e9496
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 12 deletions.
40 changes: 30 additions & 10 deletions core/peer_service/change_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,61 +87,81 @@ namespace peer_service{
return false;
if (monitor::isExistPublicKey(peer.public_key_))
return false;
if ( peer.getState()!= PREPARE )
return false;

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) );
auto it = monitor::findPeerPublicKey(publicKey);
if( it->get()->getState() != PREPARE )

peer_list_.erase( it );
return true;
}
bool setTrust(const std::string &publicKey, const double &trust){
if (!monitor::isExistPublicKey(publicKey))
return false;

auto node = monitor::findPeerPublicKey(publicKey)->get();
auto node = *monitor::findPeerPublicKey(publicKey);
node->setTrust(trust);
if( node->getState() == ACTIVE )
detail::changeActive(publicKey);
detail::changeActive(node);
return true;
}
bool changeTrust(const std::string &publicKey, const double &trust){
if (!monitor::isExistPublicKey(publicKey))
return false;

auto node = monitor::findPeerPublicKey(publicKey)->get();
auto node = *monitor::findPeerPublicKey(publicKey);
node->setTrust(node->getTrust() + trust);
if( node->getState() == ACTIVE )
detail::changeActive(publicKey);
detail::changeActive(node);
return true;
}
bool setActive(const std::string &publicKey, const State state){
if (!monitor::isExistPublicKey(publicKey))
return false;

auto node = monitor::findPeerPublicKey(publicKey)->get();
auto node = *monitor::findPeerPublicKey(publicKey);
if( node->getState() == PREPARE ){
if( state == ACTIVE ) // PRPARE -> ACTIVE
detail::insertActive(node);
} else if( node->getState() == ACTIVE ) {
if( state == PREPARE ) // ACTIVE -> PREPARE
detail::eraseActive(node);
detail::eraseActive(publicKey);
}
node->state_ = state;
}

};

namespace detail {
void insertActive( const Node& node ){

void insertActive( const std::shared_ptr<Node> node ){
for( auto it = active_peer_list_.begin(); it != active_peer_list_.end(); it++ ) {
if( *node > *(*it) ) {
active_peer_list_.insert(it, node);
break;
}
}
}
void eraseActive( const Node& node ){

void eraseActive( const std::string& publicKey ){
for( auto it = active_peer_list_.begin(); it != active_peer_list_.end(); it++ ) {
if( (*it)->getPublicKey() == publicKey ) {
active_peer_list_.erase(it);
break;
}
}
}
void changeActive( std::string publicKey ) {

void changeActive( const std::shared_ptr<Node> node ) {
eraseActive( node->getPublicKey() );
insertActive( node );
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions core/peer_service/change_state.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ namespace peer_service{
// This scope is detail implement change peer_list_ and active_peer_list_
namespace detail {
void insertActive( const std::shared_ptr<Node> node );
void eraseActive( const std::shared_ptr<Node> node );
void changeActive( std::string publicKey );
void eraseActive( const std::string& publicKey );
void changeActive( const std::shared_ptr<Node> node );
}
};
};
Expand Down
1 change: 1 addition & 0 deletions core/peer_service/monitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ namespace peer_service{
self_state::getPublicKey(),
self_state::getName(),
self_state::getTrust(),
self_state::getActiveTime(),
self_state::getState()
)
);
Expand Down
14 changes: 14 additions & 0 deletions core/peer_service/peer_service.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ limitations under the License.
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <common/datetime.hpp>

namespace peer_service {

Expand All @@ -36,12 +38,14 @@ namespace peer_service {
std::string public_key_;
std::string name_;
double trust_;
uint64_t created_;
State state_;

Node(std::string ip = defaultIP(),
std::string public_key = defaultPubKey(),
std::string name = defaultName(),
double trust = 100.0,
uint64_t created_ = common::datetime::unixtime(),
State state = PREPARE)
: ip_(ip),
public_key_(public_key),
Expand All @@ -54,22 +58,32 @@ namespace peer_service {
public_key_(p.public_key_),
name_(p.name_),
trust_(p.trust_),
created_(p.created_),
state_(p.state_) {}

void setIp(std::string ip = defaultIP()){ ip_ = ip; }
void setPublicKey(std::string public_key = defaultPubKey()){ public_key_ = public_key; }
void setName(std::string name = defaultName()){ name_ = name; }
void setTrust(double trust = 100.0){ trust_ = trust; }
void setCreated(uint64_t created){ created_ = created; }
void setState(State state = PREPARE){ state_ = state; }

std::string getIp() const { return ip_; }
std::string getPublicKey() const { return public_key_; }
std::string getName() const { return name_; }
double getTrust() const { return trust_; }
uint64_t getCreated() const { return created_; }
State getState() const { return state_; }

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



bool operator > (const Node& node) const {
return ( std::abs(trust_ - node.trust_) < -1e5 ) ? created_ < node.created_ : trust_ > node.trust_;
}

};

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

0 comments on commit a6e9496

Please sign in to comment.