// initialize my node
id_ = std::stoi(args[1]);
+ XBT_DEBUG("Initialize node with id: %d", id_);
+ random.set_seed(id_);
mailbox_ = simgrid::s4u::Mailbox::by_name(std::to_string(id_));
next_finger_to_fix = 0;
fingers_.resize(nb_bits, id_);
void Node::notifyAndQuit()
{
// send the PREDECESSOR_LEAVING to our successor
- ChordMessage* pred_msg = new ChordMessage(PREDECESSOR_LEAVING);
+ auto* pred_msg = new ChordMessage(PREDECESSOR_LEAVING);
pred_msg->request_id = pred_id_;
pred_msg->answer_to = mailbox_;
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);
+ auto* 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_);
void Node::randomLookup()
{
int res = id_;
- int random_index = simgrid::xbt::random::uniform_int(0, nb_bits - 1);
+ int random_index = random.uniform_int(0, nb_bits - 1);
int random_id = fingers_[random_index];
XBT_DEBUG("Making a lookup request for id %d", random_id);
if (random_id != id_)
simgrid::s4u::Mailbox* mailbox = simgrid::s4u::Mailbox::by_name(std::to_string(pred_id_));
simgrid::s4u::Mailbox* return_mailbox = simgrid::s4u::Mailbox::by_name(std::to_string(id_) + "_is_alive");
- ChordMessage* message = new ChordMessage(PREDECESSOR_ALIVE);
+ auto* message = new ChordMessage(PREDECESSOR_ALIVE);
message->request_id = pred_id_;
message->answer_to = return_mailbox;
simgrid::s4u::Mailbox* mailbox = simgrid::s4u::Mailbox::by_name(std::to_string(ask_to));
simgrid::s4u::Mailbox* return_mailbox = simgrid::s4u::Mailbox::by_name(std::to_string(id_) + "_pred");
- ChordMessage* message = new ChordMessage(GET_PREDECESSOR);
+ auto* message = new ChordMessage(GET_PREDECESSOR);
message->request_id = id_;
message->answer_to = return_mailbox;
try {
comm->wait_for(timeout);
- const ChordMessage* answer = static_cast<ChordMessage*>(data);
+ const auto* answer = static_cast<ChordMessage*>(data);
XBT_DEBUG("Received the answer to my 'Get Predecessor' request: the predecessor of node %d is %d", ask_to,
answer->answer_id);
predecessor_id = answer->answer_id;
simgrid::s4u::Mailbox* mailbox = simgrid::s4u::Mailbox::by_name(std::to_string(ask_to));
simgrid::s4u::Mailbox* return_mailbox = simgrid::s4u::Mailbox::by_name(std::to_string(id_) + "_succ");
- ChordMessage* message = new ChordMessage(FIND_SUCCESSOR);
+ auto* message = new ChordMessage(FIND_SUCCESSOR);
message->request_id = id_;
message->answer_to = return_mailbox;
}
/* Notifies a remote node that its predecessor may have changed. */
-void Node::remoteNotify(int notify_id, int predecessor_candidate_id)
+void Node::remoteNotify(int notify_id, int predecessor_candidate_id) const
{
- ChordMessage* message = new ChordMessage(NOTIFY);
+ auto* message = new ChordMessage(NOTIFY);
message->request_id = predecessor_candidate_id;
message->answer_to = nullptr;
while (now < std::min(start_time_ + deadline_, MAX_SIMULATION_TIME)) {
if (comm_receive == nullptr)
comm_receive = mailbox_->get_async(&data);
- if (comm_receive->test()) {
+ bool comm_completed = true;
+ try {
+ if (not comm_receive->test())
+ comm_completed = false;
+ } catch (const simgrid::TimeoutException&) {
+ XBT_DEBUG("Caught a timeout, go ahead.");
+ }
+
+ if (comm_completed) {
if (data != nullptr) {
- ChordMessage* message = static_cast<ChordMessage*>(data);
+ auto* message = static_cast<ChordMessage*>(data);
handleMessage(message);
data = nullptr;
}
now = simgrid::s4u::Engine::get_clock();
}
if (comm_receive != nullptr) {
- if (comm_receive->test())
- delete static_cast<ChordMessage*>(data);
- else
- comm_receive->cancel();
+ try {
+ if (comm_receive->test())
+ delete static_cast<ChordMessage*>(data);
+ else
+ comm_receive->cancel();
+ } catch (const simgrid::TimeoutException&) {
+ XBT_DEBUG("Caught a timeout for last message, nevermind.");
+ }
}
// leave the ring
leave();