Ejemplo n.º 1
0
void FingerTable::initfingers() 
{
	ChordId EmptyId ;
	for (int pos = 1; pos <= getTableLength(); pos++) 
	{
        /*generater start*/
		ChordId start = ChordId(localnode->getChordId().GetId(),constants,"","");
		start.add(ChordId(int(pow(2,pos-1)),constants,"",""));

		/*generater end*/
		ChordId end = ChordId(localnode->getChordId().GetId(),constants,"","");
		if(pos != NBITS) 
			end.add(ChordId(int(pow(2,pos)),constants,"",""));
    
		fingers[pos-1].start = start ;
		fingers[pos-1].interval[0] = start ;
		fingers[pos-1].interval[1] = end ;

		//init the all nodes
		fingers[pos-1].node = EmptyId ;
    }

	setPredecessor(EmptyId) ;
	clearSuccList() ;
}
Ejemplo n.º 2
0
/* Notifies the current node that its predecessor may have changed. */
void Node::notify(int predecessor_candidate_id)
{
  if (pred_id_ == -1 || is_in_interval(predecessor_candidate_id, pred_id_ + 1, id_ - 1)) {
    setPredecessor(predecessor_candidate_id);
    printFingerTable();
  } else {
    XBT_DEBUG("I don't have to change my predecessor to %d", predecessor_candidate_id);
  }
}
Ejemplo n.º 3
0
void Node::join(int known_id)
{
  XBT_INFO("Joining the ring with id %d, knowing node %d", id_, known_id);
  setPredecessor(-1); // no predecessor (yet)

  int successor_id = remoteFindSuccessor(known_id, id_);
  if (successor_id == -1) {
    XBT_INFO("Cannot join the ring.");
  } else {
    setFinger(0, successor_id);
    printFingerTable();
    joined = true;
  }
}
Ejemplo n.º 4
0
/* This function is called when a node receives a message.
 *
 * \param message the message to handle (don't touch it afterward: it will be destroyed, reused or forwarded)
 */
void Node::handleMessage(ChordMessage* message)
{
  switch (message->type) {
  case FIND_SUCCESSOR:
    XBT_DEBUG("Received a 'Find Successor' request from %s for id %d", message->issuer_host_name.c_str(),
        message->request_id);
    // is my successor the successor?
    if (is_in_interval(message->request_id, id_ + 1, fingers_[0])) {
      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->get_cname(), 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::by_name(std::to_string(closest));
      mailbox->put_init(message, 10)->detach(ChordMessage::destroy);
    }
    break;

  case GET_PREDECESSOR:
    XBT_DEBUG("Receiving a 'Get Predecessor' request from %s", message->issuer_host_name.c_str());
    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->get_cname(), message->answer_id);
    message->answer_to->put_init(message, 10)->detach(ChordMessage::destroy);
    break;

  case NOTIFY:
    // someone is telling me that he may be my new predecessor
    XBT_DEBUG("Receiving a 'Notify' request from %s", message->issuer_host_name.c_str());
    notify(message->request_id);
    delete message;
    break;

  case PREDECESSOR_LEAVING:
    // my predecessor is about to quit
    XBT_DEBUG("Receiving a 'Predecessor Leaving' message from %s", message->issuer_host_name.c_str());
    // modify my predecessor
    setPredecessor(message->request_id);
    delete message;
    /*TODO :
      >> notify my new predecessor
      >> send a notify_predecessors !!
     */
    break;

  case SUCCESSOR_LEAVING:
    // my successor is about to quit
    XBT_DEBUG("Receiving a 'Successor Leaving' message from %s", message->issuer_host_name.c_str());
    // modify my successor FIXME : this should be implicit ?
    setFinger(0, message->request_id);
    delete message;
    /* TODO
       >> notify my new successor
       >> update my table & predecessors table */
    break;

  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->get_cname());
    message->answer_to->put_init(message, 10)->detach(ChordMessage::destroy);
    break;

  default:
    XBT_DEBUG("Ignoring unexpected message: %d from %s", message->type, message->issuer_host_name.c_str());
    delete message;
  }
}