namespace kademlia {
static void destroy(void* message)
{
- const Message* msg = static_cast<Message*>(message);
+ const auto* msg = static_cast<Message*>(message);
delete msg->answer_;
delete msg;
}
bool Node::join(unsigned int known_id)
{
const Answer* node_list;
- unsigned int i;
bool got_answer = false;
/* Add the guy we know to our routing table and ourselves. */
XBT_DEBUG("Received an answer from the node I know.");
got_answer = true;
// retrieve the node list and ping them.
- const Message* msg = static_cast<Message*>(received_msg);
+ const auto* msg = static_cast<Message*>(received_msg);
node_list = msg->answer_;
if (node_list) {
for (auto const& contact : node_list->getNodes())
simgrid::s4u::this_actor::sleep_for(1);
} while (not got_answer);
- /* Second step: Send a FIND_NODE to a a random node in buckets */
+ /* Second step: Send a FIND_NODE to a random node in buckets */
unsigned int bucket_id = table.findBucket(known_id)->getId();
xbt_assert(bucket_id <= IDENTIFIER_SIZE);
- for (i = 0; ((bucket_id > i) || (bucket_id + i) <= IDENTIFIER_SIZE) && i < JOIN_BUCKETS_QUERIES; i++) {
+ for (unsigned int i = 0; ((bucket_id > i) || (bucket_id + i) <= IDENTIFIER_SIZE) && i < JOIN_BUCKETS_QUERIES; i++) {
if (bucket_id > i) {
unsigned int id_in_bucket = get_id_in_prefix(id_, bucket_id - i);
findNode(id_in_bucket, false);
* @param id node we are querying
* @param destination node we are trying to find.
*/
-void Node::sendFindNode(unsigned int id, unsigned int destination)
+void Node::sendFindNode(unsigned int id, unsigned int destination) const
{
/* Gets the mailbox to send to */
simgrid::s4u::Mailbox* mailbox = simgrid::s4u::Mailbox::by_name(std::to_string(id));
/* Build the task */
- Message* msg = new Message(id_, destination, simgrid::s4u::Mailbox::by_name(std::to_string(id_)),
- simgrid::s4u::Host::current()->get_cname());
+
+ auto* msg = new Message(id_, destination, simgrid::s4u::Mailbox::by_name(std::to_string(id_)),
+ simgrid::s4u::Host::current()->get_cname());
/* Send the task */
mailbox->put_init(msg, 1)->detach(kademlia::destroy);
* Sends to the best "KADEMLIA_ALPHA" nodes in the "node_list" array a "FIND_NODE" request, to ask them for their best
* nodes
*/
-unsigned int Node::sendFindNodeToBest(const Answer* node_list)
+unsigned int Node::sendFindNodeToBest(const Answer* node_list) const
{
unsigned int i = 0;
unsigned int j = 0;
Bucket* bucket = table.findBucket(id);
// check if the id is already in the bucket.
- auto id_pos = std::find(bucket->nodes.begin(), bucket->nodes.end(), id);
+ auto id_pos = std::find(bucket->nodes_.begin(), bucket->nodes_.end(), id);
- if (id_pos == bucket->nodes.end()) {
+ if (id_pos == bucket->nodes_.end()) {
/* We check if the bucket is full or not. If it is, we evict an old element */
- if (bucket->nodes.size() >= BUCKET_SIZE) {
- bucket->nodes.pop_back();
+ if (bucket->nodes_.size() >= BUCKET_SIZE) {
+ bucket->nodes_.pop_back();
}
- bucket->nodes.push_front(id);
+ bucket->nodes_.push_front(id);
XBT_VERB("I'm adding to my routing table %08x", id);
} else {
// We push the element to the front
- bucket->nodes.erase(id_pos);
- bucket->nodes.push_front(id);
+ bucket->nodes_.erase(id_pos);
+ bucket->nodes_.push_front(id);
XBT_VERB("I'm updating %08x", id);
}
}
*/
Answer* Node::findClosest(unsigned int destination_id)
{
- Answer* answer = new Answer(destination_id);
+ auto* answer = new Answer(destination_id);
/* We find the corresponding bucket for the id */
const Bucket* bucket = table.findBucket(destination_id);
int bucket_id = bucket->getId();
for (int i = 1; answer->getSize() < BUCKET_SIZE && ((bucket_id - i > 0) || (bucket_id + i < IDENTIFIER_SIZE)); i++) {
/* We check the previous buckets */
if (bucket_id - i >= 0) {
- const Bucket* bucket_p = &table.buckets[bucket_id - i];
+ const Bucket* bucket_p = &table.getBucketAt(bucket_id - i);
answer->addBucket(bucket_p);
}
/* We check the next buckets */
if (bucket_id + i <= IDENTIFIER_SIZE) {
- const Bucket* bucket_n = &table.buckets[bucket_id + i];
+ const Bucket* bucket_n = &table.getBucketAt(bucket_id + i);
answer->addBucket(bucket_n);
}
}
receive_comm = mailbox->get_async(&received_msg);
if (receive_comm->test()) {
- const Message* msg = static_cast<Message*>(received_msg);
+ const auto* msg = static_cast<Message*>(received_msg);
// Check if what we have received is what we are looking for.
if (msg->answer_ && msg->answer_->getDestinationId() == id_to_find) {
routingTableUpdate(msg->sender_id_);
XBT_VERB("Received a FIND_NODE from %s (%s), he's trying to find %08x", msg->answer_to_->get_cname(),
msg->issuer_host_name_.c_str(), msg->destination_id_);
// Building the answer to the request
- Message* answer =
+ auto* answer =
new Message(id_, msg->destination_id_, findClosest(msg->destination_id_),
simgrid::s4u::Mailbox::by_name(std::to_string(id_)), simgrid::s4u::Host::current()->get_cname());
// Sending the answer
msg->answer_to_->put_init(answer, 1)->detach(kademlia::destroy);
}
+
+void Node::displaySuccessRate() const
+{
+ XBT_INFO("%u/%u FIND_NODE have succeeded", find_node_success, find_node_success + find_node_failed);
}
+} // namespace kademlia
/**@brief Returns an identifier which is in a specific bucket of a routing table
* @param id id of the routing table owner
* @param prefix id of the bucket where we want that identifier to be
if (prefix == 0) {
return 0;
} else {
- return (1U << ((unsigned int)(prefix - 1))) ^ id;
+ return (1U << (prefix - 1)) ^ id;
}
}