<commit_before>/******************************************************************
*
* Round for C++
*
* Copyright (C) <NAME> 2015
*
* This is licensed under BSD-style license, see file COPYING.
*
******************************************************************/

#include <boost/lexical_cast.hpp>

#include <round/Round.h>
#include <round/core/Node.h>
#include <round/core/local/method/SystemMethod.h>

const std::string Round::Node::NAME = ROUNDCC_PRODUCT_NAME;
const std::string Round::Node::VER= ROUNDCC_PRODUCT_VERSION;

Round::Node::Node() {
  setWeakFlag(true);
}

Round::Node::~Node() {
}

bool Round::Node::postMessage(const std::string &method, const std::string &params, std::string *result) {
  NodeRequest nodeReq;
  nodeReq.setMethod(method);
  nodeReq.setParams(params);
  
  NodeResponse nodeRes;
  Error error;
  bool isSuccess = postMessage(&nodeReq, &nodeRes, &error);
  nodeRes.getResult(result);
  
  return isSuccess;
}

bool Round::Node::isAlive(Error *error) {
  std::string currClockStr = boost::lexical_cast<std::string>(getLocalClock());
  SystemEchoRequest nodeReq;
  nodeReq.setParams(currClockStr);
  
  NodeResponse nodeRes;
  if (!postMessage(&nodeReq, &nodeRes, error))
    return false;

  std::string echoResult;
  if (!nodeRes.getResult(&echoResult))
    return false;
  
  return (currClockStr.compare(echoResult) == 0) ? true : false;
}

bool Round::Node::isLeader(Error *error) {
  Cluster cluster;
  if (!getCluster(&cluster, error))
    return false;
  return cluster.isLeaderNode(this);
}

bool Round::Node::getStatus(NodeStatus *status, Error *error) {
  // TODO Not yet implemented
  return false;
}

bool Round::Node::getCluster(Cluster *cluster, Error *error) {
  SystemGetClusterInfoRequest nodeReq;
  NodeResponse nodeRes;
  if (!postMessage(&nodeReq, &nodeRes, error))
    return false;  
  SystemGetClusterInfoResponse sysRes(&nodeRes);
  return sysRes.getCluster(cluster);
}

bool Round::Node::getClusterList(ClusterList *clusterList, Error *error) {
  SystemGetNetworkInfoRequest nodeReq;
  NodeResponse nodeRes;
  if (!postMessage(&nodeReq, &nodeRes, error))
    return false;
  SystemGetNetworkInfoResponse sysRes(&nodeRes);
  return sysRes.getClusters(clusterList);
}

bool Round::Node::findNode(const std::string &nodeHash, Node **node, Error *error) {
  Cluster cluster;
  if (!getCluster(&cluster, error))
    return false;
  *node = cluster.getNodeByHashCode(nodeHash);
  if (!node) {
    // TODO Set error code
    return false;
  }
  return true;
}

bool Round::Node::setRegistry(const Registry reg, Error *error) {
  SystemSetRegistryRequest nodeReq;
  nodeReq.setRegistry(reg);<commit_msg>* Updated Node::findNode() to return a local node when the node hash parameter is null.<commit_after>/******************************************************************
*
* Round for C++
*
* Copyright (C) <NAME> 2015
*
* This is licensed under BSD-style license, see file COPYING.
*
******************************************************************/

#include <boost/lexical_cast.hpp>

#include <round/Round.h>
#include <round/core/Node.h>
#include <round/core/local/method/SystemMethod.h>

const std::string Round::Node::NAME = ROUNDCC_PRODUCT_NAME;
const std::string Round::Node::VER= ROUNDCC_PRODUCT_VERSION;

Round::Node::Node() {
  setWeakFlag(true);
}

Round::Node::~Node() {
}

bool Round::Node::postMessage(const std::string &method, const std::string &params, std::string *result) {
  NodeRequest nodeReq;
  nodeReq.setMethod(method);
  nodeReq.setParams(params);
  
  NodeResponse nodeRes;
  Error error;
  bool isSuccess = postMessage(&nodeReq, &nodeRes, &error);
  nodeRes.getResult(result);
  
  return isSuccess;
}

bool Round::Node::isAlive(Error *error) {
  std::string currClockStr = boost::lexical_cast<std::string>(getLocalClock());
  SystemEchoRequest nodeReq;
  nodeReq.setParams(currClockStr);
  
  NodeResponse nodeRes;
  if (!postMessage(&nodeReq, &nodeRes, error))
    return false;

  std::string echoResult;
  if (!nodeRes.getResult(&echoResult))
    return false;
  
  return (currClockStr.compare(echoResult) == 0) ? true : false;
}

bool Round::Node::isLeader(Error *error) {
  Cluster cluster;
  if (!getCluster(&cluster, error))
    return false;
  return cluster.isLeaderNode(this);
}

bool Round::Node::getStatus(NodeStatus *status, Error *error) {
  // TODO Not yet implemented
  return false;
}

bool Round::Node::getCluster(Cluster *cluster, Error *error) {
  SystemGetClusterInfoRequest nodeReq;
  NodeResponse nodeRes;
  if (!postMessage(&nodeReq, &nodeRes, error))
    return false;  
  SystemGetClusterInfoResponse sysRes(&nodeRes);
  return sysRes.getCluster(cluster);
}

bool Round::Node::getClusterList(ClusterList *clusterList, Error *error) {
  SystemGetNetworkInfoRequest nodeReq;
  NodeResponse nodeRes;
  if (!postMessage(&nodeReq, &nodeRes, error))
    return false;
  SystemGetNetworkInfoResponse sysRes(&nodeRes);
  return sysRes.getClusters(clusterList);
}

bool Round::Node::findNode(const std::string &nodeHash, Node **node, Error *error) {
  if (nodeHash.length() <= 0) {
    *node = this;
    return true;
  }
  
  Cluster cluster;
  if (!getCluster(&cluster, error))
    return false;
  *node = cluster.getNodeByHashCode(nodeHash);
  if (!node) {
    // TODO Set error code
    return false;
  }
  return true;
}

bool Round::Node::setRegistry(const Registry reg, Error *error) {
  SystemSetRegistryRequest nodeReq;
  nodeReq.setRegistry(reg);
{'fraction_non_alphanumeric': 0.17540322580645162, 'fraction_numerical': 0.0020161290322580645, 'mean_word_length': 5.566787003610108, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7114723', 'n_tokens_mistral': 1718, 'n_tokens_neox': 1692, 'n_words': 392}