X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/b348b0d4cff528bb4562222aa9f7874b8d30626f..479eb3c5eb55f556b5b8c072f092685bc410ad90:/examples/s4u/dht-chord/node.cpp diff --git a/examples/s4u/dht-chord/node.cpp b/examples/s4u/dht-chord/node.cpp index f9ed22a7e8..4a73c0f4e1 100644 --- a/examples/s4u/dht-chord/node.cpp +++ b/examples/s4u/dht-chord/node.cpp @@ -1,9 +1,9 @@ -/* Copyright (c) 2010-2016. The SimGrid Team. All rights reserved. */ +/* Copyright (c) 2010-2017. The SimGrid Team. All rights reserved. */ /* This program is free software; you can redistribute it and/or modify it * under the terms of the license (GNU LGPL) which comes with this package. */ -#include "s4u_dht-chord.hpp" +#include "s4u-dht-chord.hpp" XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(s4u_chord); @@ -40,6 +40,11 @@ static int is_in_interval(int id, int start, int end) return i <= e; } +void ChordMessage::destroy(void* message) +{ + delete static_cast(message); +} + /* Initializes the current node as the first one of the system */ Node::Node(std::vector args) { @@ -47,7 +52,7 @@ Node::Node(std::vector args) // initialize my node id_ = std::stoi(args[1]); - stream = simgrid::s4u::this_actor::host()->extension()->getStream(); + stream = simgrid::s4u::this_actor::getHost()->extension()->getStream(); mailbox_ = simgrid::s4u::Mailbox::byName(std::to_string(id_)); next_finger_to_fix = 0; fingers_ = new int[nb_bits]; @@ -112,7 +117,7 @@ void Node::notifyAndQuit() XBT_DEBUG("Sending a 'PREDECESSOR_LEAVING' to my successor %d", fingers_[0]); try { - simgrid::s4u::this_actor::send(simgrid::s4u::Mailbox::byName(std::to_string(fingers_[0])), pred_msg, 10, timeout); + simgrid::s4u::Mailbox::byName(std::to_string(fingers_[0]))->put(pred_msg, 10, timeout); } catch (xbt_ex& e) { if (e.category == timeout_error) { XBT_DEBUG("Timeout expired when sending a 'PREDECESSOR_LEAVING' to my successor %d", fingers_[0]); @@ -120,18 +125,20 @@ void Node::notifyAndQuit() } } - // send the SUCCESSOR_LEAVING to our predecessor - ChordMessage* succ_msg = new ChordMessage(SUCCESSOR_LEAVING); - succ_msg->request_id = fingers_[0]; - succ_msg->answer_to = mailbox_; - XBT_DEBUG("Sending a 'SUCCESSOR_LEAVING' to my predecessor %d", pred_id_); + if (pred_id_ != -1 && pred_id_ != id_) { + // send the SUCCESSOR_LEAVING to our predecessor (only if I have one that is not me) + ChordMessage* succ_msg = new ChordMessage(SUCCESSOR_LEAVING); + succ_msg->request_id = fingers_[0]; + succ_msg->answer_to = mailbox_; + XBT_DEBUG("Sending a 'SUCCESSOR_LEAVING' to my predecessor %d", pred_id_); - try { - simgrid::s4u::this_actor::send(simgrid::s4u::Mailbox::byName(std::to_string(pred_id_)), succ_msg, 10, timeout); - } catch (xbt_ex& e) { - if (e.category == timeout_error) { - XBT_DEBUG("Timeout expired when sending a 'SUCCESSOR_LEAVING' to my predecessor %d", pred_id_); - delete succ_msg; + try { + simgrid::s4u::Mailbox::byName(std::to_string(pred_id_))->put(succ_msg, 10, timeout); + } catch (xbt_ex& e) { + if (e.category == timeout_error) { + XBT_DEBUG("Timeout expired when sending a 'SUCCESSOR_LEAVING' to my predecessor %d", pred_id_); + delete succ_msg; + } } } } @@ -218,7 +225,7 @@ void Node::checkPredecessor() XBT_DEBUG("Sending a 'Predecessor Alive' request to my predecessor %d", pred_id_); try { - simgrid::s4u::this_actor::send(mailbox, message, 10, timeout); + mailbox->put(message, 10, timeout); } catch (xbt_ex& e) { if (e.category == timeout_error) { XBT_DEBUG("Failed to send the 'Predecessor Alive' request to %d", pred_id_); @@ -228,13 +235,13 @@ void Node::checkPredecessor() } // receive the answer XBT_DEBUG("Sent 'Predecessor Alive' request to %d, waiting for the answer on my mailbox '%s'", pred_id_, - message->answer_to->name()); - simgrid::s4u::Comm& comm = simgrid::s4u::this_actor::irecv(return_mailbox, &data); + message->answer_to->getCname()); + simgrid::s4u::CommPtr comm = return_mailbox->get_async(&data); try { - comm.wait(timeout); + comm->wait(timeout); XBT_DEBUG("Received the answer to my 'Predecessor Alive': my predecessor %d is alive", pred_id_); - delete message; + delete static_cast(data); } catch (xbt_ex& e) { if (e.category == timeout_error) { XBT_DEBUG("Failed to receive the answer to my 'Predecessor Alive' request"); @@ -262,7 +269,7 @@ int Node::remoteGetPredecessor(int ask_to) // send a "Get Predecessor" request to ask_to_id XBT_DEBUG("Sending a 'Get Predecessor' request to %d", ask_to); try { - simgrid::s4u::this_actor::send(mailbox, message, 10, timeout); + mailbox->put(message, 10, timeout); } catch (xbt_ex& e) { if (e.category == timeout_error) { XBT_DEBUG("Failed to send the 'Get Predecessor' request to %d", ask_to); @@ -273,11 +280,11 @@ int Node::remoteGetPredecessor(int ask_to) // receive the answer XBT_DEBUG("Sent 'Get Predecessor' request to %d, waiting for the answer on my mailbox '%s'", ask_to, - message->answer_to->name()); - simgrid::s4u::Comm& comm = simgrid::s4u::this_actor::irecv(return_mailbox, &data); + message->answer_to->getCname()); + simgrid::s4u::CommPtr comm = return_mailbox->get_async(&data); try { - comm.wait(timeout); + comm->wait(timeout); ChordMessage* answer = static_cast(data); XBT_DEBUG("Received the answer to my 'Get Predecessor' request: the predecessor of node %d is %d", ask_to, answer->answer_id); @@ -338,7 +345,7 @@ int Node::remoteFindSuccessor(int ask_to, int id) // send a "Find Successor" request to ask_to_id XBT_DEBUG("Sending a 'Find Successor' request to %d for id %d", ask_to, id); try { - simgrid::s4u::this_actor::send(mailbox, message, 10, timeout); + mailbox->put(message, 10, timeout); } catch (xbt_ex& e) { if (e.category == timeout_error) { XBT_DEBUG("Failed to send the 'Find Successor' request to %d for id %d", ask_to, id_); @@ -348,10 +355,10 @@ int Node::remoteFindSuccessor(int ask_to, int id) } // receive the answer XBT_DEBUG("Sent a 'Find Successor' request to %d for key %d, waiting for the answer", ask_to, id); - simgrid::s4u::Comm& comm = simgrid::s4u::this_actor::irecv(return_mailbox, &data); + simgrid::s4u::CommPtr comm = return_mailbox->get_async(&data); try { - comm.wait(timeout); + comm->wait(timeout); ChordMessage* answer = static_cast(data); XBT_DEBUG("Received the answer to my 'Find Successor' request for id %d: the successor of key %d is %d", answer->request_id, id_, answer->answer_id); @@ -387,15 +394,7 @@ void Node::remoteNotify(int notify_id, int predecessor_candidate_id) // send a "Notify" request to notify_id XBT_DEBUG("Sending a 'Notify' request to %d", notify_id); simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::byName(std::to_string(notify_id)); - try { - // TODO make it a dsend - simgrid::s4u::this_actor::isend(mailbox, message, 10); - } catch (xbt_ex& e) { - if (e.category == timeout_error) { - XBT_DEBUG("Send of 'Notify' failed due to an expired timeout on receiver side"); - delete message; - } - } + mailbox->put_init(message, 10)->detach(ChordMessage::destroy); } /* This function is called periodically. It checks the immediate successor of the current node. */ @@ -404,13 +403,10 @@ void Node::stabilize() XBT_DEBUG("Stabilizing node"); // get the predecessor of my immediate successor - int candidate_id; + int candidate_id = pred_id_; int successor_id = fingers_[0]; - if (successor_id != id_) { + if (successor_id != id_) candidate_id = remoteGetPredecessor(successor_id); - } else { - candidate_id = pred_id_; - } // this node is a candidate to become my new successor if (candidate_id != -1 && is_in_interval(candidate_id, id_ + 1, successor_id - 1)) { @@ -436,29 +432,16 @@ void Node::handleMessage(ChordMessage* message) message->type = FIND_SUCCESSOR_ANSWER; message->answer_id = fingers_[0]; XBT_DEBUG("Sending back a 'Find Successor Answer' to %s (mailbox %s): the successor of %d is %d", - message->issuer_host_name.c_str(), message->answer_to->name(), message->request_id, message->answer_id); - // TODO Replace by dsend - try { - simgrid::s4u::this_actor::isend(message->answer_to, message, 10); - } catch(xbt_ex& e) { - if (e.category == timeout_error) { - XBT_DEBUG("Send of 'Find Successor Answer' failed due du an expired timeout on receiver side"); - } - } + message->issuer_host_name.c_str(), message->answer_to->getCname(), message->request_id, + message->answer_id); + message->answer_to->put_init(message, 10)->detach(ChordMessage::destroy); } else { // otherwise, forward the request to the closest preceding finger in my table int closest = closestPrecedingFinger(message->request_id); XBT_DEBUG("Forwarding the 'Find Successor' request for id %d to my closest preceding finger %d", message->request_id, closest); simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::byName(std::to_string(closest)); - //TODO make it a dsend - try{ - simgrid::s4u::this_actor::isend(mailbox, message, 10); - } catch (xbt_ex& e) { - if (e.category == timeout_error) { - XBT_DEBUG("Forward of 'Find Successor' failed due du an expired timeout on receiver side"); - } - } + mailbox->put_init(message, 10)->detach(ChordMessage::destroy); } break; @@ -467,15 +450,8 @@ void Node::handleMessage(ChordMessage* message) message->type = GET_PREDECESSOR_ANSWER; message->answer_id = pred_id_; XBT_DEBUG("Sending back a 'Get Predecessor Answer' to %s via mailbox '%s': my predecessor is %d", - message->issuer_host_name.c_str(), message->answer_to->name(), message->answer_id); - //TODO make it a dsend - try{ - simgrid::s4u::this_actor::isend(message->answer_to, message, 10); - } catch (xbt_ex& e) { - if (e.category == timeout_error) { - XBT_DEBUG("Send of 'Get Predecessor Answer' failed due du an expired timeout on receiver side"); - } - } + message->issuer_host_name.c_str(), message->answer_to->getCname(), message->answer_id); + message->answer_to->put_init(message, 10)->detach(ChordMessage::destroy); break; case NOTIFY: @@ -511,16 +487,9 @@ void Node::handleMessage(ChordMessage* message) case PREDECESSOR_ALIVE: XBT_DEBUG("Receiving a 'Predecessor Alive' request from %s", message->issuer_host_name.c_str()); message->type = PREDECESSOR_ALIVE_ANSWER; - XBT_DEBUG("Sending back a 'Predecessor Alive Answer' to %s (mailbox %s)", - message->issuer_host_name.c_str(), message->answer_to->name()); - //TODO Make it a dsend - try{ - simgrid::s4u::this_actor::isend(message->answer_to, message, 10); - } catch (xbt_ex& e) { - if (e.category == timeout_error) { - XBT_DEBUG("Send of 'Predecessor Alive' failed due du an expired timeout on receiver side"); - } - } + XBT_DEBUG("Sending back a 'Predecessor Alive Answer' to %s (mailbox %s)", message->issuer_host_name.c_str(), + message->answer_to->getCname()); + message->answer_to->put_init(message, 10)->detach(ChordMessage::destroy); break; default: