Esempio n. 1
0
void
Chat::onData
  (const ptr_lib::shared_ptr<const Interest>& interest,
   const ptr_lib::shared_ptr<Data>& data)
{
  SyncDemo::ChatMessage content;
  content.ParseFromArray(data->getContent().buf(), data->getContent().size());
  if (getNowMilliseconds() - content.timestamp() * 1000.0 < 120000.0) {
    string name = content.from();
    string prefix = data->getName().getPrefix(-2).toUri();
    int sessionNo = ::atoi(data->getName().get(-2).toEscapedString().c_str());
    int sequenceNo = ::atoi(data->getName().get(-1).toEscapedString().c_str());
    ostringstream tempStream;
    tempStream << name << sessionNo;
    string nameAndSession = tempStream.str();

    size_t l = 0;
    //update roster
    while (l < roster_.size()) {
      string tempName2 = roster_[l].substr(0, roster_[l].size() - 10);
      int tempSessionNo = ::atoi(roster_[l].substr(roster_[l].size() - 10, 10).c_str());
      if (name != tempName2 && content.type() != SyncDemo::ChatMessage_ChatMessageType_LEAVE)
        ++l;
      else {
        if (name == tempName2 && sessionNo > tempSessionNo)
          roster_[l] = nameAndSession;
        break;
      }
    }

    if (l == roster_.size()) {
      roster_.push_back(nameAndSession);
      cout << name << ": Join" << endl;
    }

    // Set the alive timeout using the Interest timeout mechanism.
    // TODO: Are we sure using a "/local/timeout" interest is the best future call approach?
    Interest timeout("/local/timeout");
    timeout.setInterestLifetimeMilliseconds(120000);
    face_.expressInterest
      (timeout, dummyOnData,
       bind(&Chat::alive, shared_from_this(), _1, sequenceNo, name, sessionNo, prefix));

    // isRecoverySyncState_ was set by sendInterest.
    // TODO: If isRecoverySyncState_ changed, this assumes that we won't get
    //   data from an interest sent before it changed.
    if (content.type() == SyncDemo::ChatMessage_ChatMessageType_CHAT &&
        !isRecoverySyncState_ && content.from() != screenName_)
      cout << content.from() << ": " << content.data() << endl;
    else if (content.type() == SyncDemo::ChatMessage_ChatMessageType_LEAVE) {
      // leave message
      int n = rosterFind(nameAndSession);
      if (n >= 0 && name != screenName_) {
        roster_.erase(roster_.begin() + n);
        cout << name << ": Leave" << endl;
      }
    }
  }
}
Esempio n. 2
0
 void onData(const ptr_lib::shared_ptr<const Interest>& interest, const ptr_lib::shared_ptr<Data>& data)
 {
   ++callbackCount_;
   cout << "Got data packet with name " << data->getName().toUri() << endl;
   for (size_t i = 0; i < data->getContent().size(); ++i)
     cout << (*data->getContent())[i];
   cout << endl;
 }
Esempio n. 3
0
void 
NdndIdFetcher::operator()(const ptr_lib::shared_ptr<const Interest>& interest, const ptr_lib::shared_ptr<Data>& ndndIdData)
{
  if (ndndIdData->getSignature().getType() == Signature::Sha256WithRsa)
    {
      ndndId_.resize(32);
      ndn_digestSha256(ndndIdData->getContent().value(), ndndIdData->getContent().value_size(), ndndId_.buf());
      onSuccess_();
    }
  else
    onFailure_();
}
/**
 * This is called when the face create command responds to decode the
 * encodedControlResonse as a TLV ControlResponse message containing one
 * ControlParameters. Get the face ID and call registerRoute().
 * @param expressedInterest The interest given to expressInterest.
 * @param responseData The response Data packet whose content is the
 * TLV-encoded ControlResponse.
 * @param prefix The prefix name to register.
 * @param face The Face which is used to sign the command interest and call
 * expressInterest.
 * @param enabled On success or error, set *enabled = false.
 */
static void
processCreateFaceResponse
  (const ptr_lib::shared_ptr<const Interest>& expressedInterest,
   const ptr_lib::shared_ptr<Data>& responseData, const Name& prefix, Face* face,
   bool* enabled)
{
  ndn_message::ControlParametersTypes_ControlParametersResponseMessage
    controlResponseMessage;
  ProtobufTlv::decode(controlResponseMessage, responseData->getContent());
  const ndn_message::ControlParametersTypes_ControlParametersResponse&
    controlResponse = controlResponseMessage.control_response();

  const int lowestErrorCode = 400;
  if (controlResponse.status_code() >= lowestErrorCode) {
    cout << "Face create command got error, code " <<
      controlResponse.status_code() << ": " + controlResponse.status_text() <<
      endl;
    *enabled = false;
    return;
  }
  if (controlResponse.control_parameters_size() != 1) {
    cout << "Face create command response does not have one ControlParameters" <<
      endl;
    *enabled = false;
    return;
  }

  boost::uint64_t faceId = controlResponse.control_parameters(0).face_id();

  cout << "Created face ID " << faceId << endl;
  registerRoute(prefix, faceId, face, enabled);
}
Esempio n. 5
0
void
Node::RegisterResponse::operator()(const ptr_lib::shared_ptr<const Interest>& interest, const ptr_lib::shared_ptr<Data>& responseData)
{
  // Decode responseData->getContent() and check for a success code.
  // TODO: Move this into the TLV code.
  struct ndn_TlvDecoder decoder;
  ndn_TlvDecoder_initialize
    (&decoder, responseData->getContent().buf(),
     responseData->getContent().size());
  ndn_Error error;
  size_t endOffset;
  if ((error = ndn_TlvDecoder_readNestedTlvsStart
      (&decoder, ndn_Tlv_NfdCommand_ControlResponse, &endOffset))) {
    _LOG_DEBUG
      ("Register prefix failed: Error decoding the NFD response: " <<
       ndn_getErrorString(error));
    info_->onRegisterFailed_(info_->prefix_);
    return;
  }
  uint64_t statusCode;
  if ((error = ndn_TlvDecoder_readNonNegativeIntegerTlv
       (&decoder, ndn_Tlv_NfdCommand_StatusCode, &statusCode))) {
    _LOG_DEBUG
      ("Register prefix failed: Error decoding the NFD response: " <<
       ndn_getErrorString(error));
    info_->onRegisterFailed_(info_->prefix_);
    return;
  }

  // Status code 200 is "OK".
  if (statusCode != 200) {
    _LOG_DEBUG
      ("Register prefix failed: Expected NFD status code 200, got: " <<
       statusCode);
    info_->onRegisterFailed_(info_->prefix_);
    return;
  }

  _LOG_DEBUG("Register prefix succeeded with the NFD forwarder for prefix " <<
             info_->prefix_->toUri());
}
/**
 * This is called when the register route command responds to decode the
 * encodedControlResponse as a TLV ControlParametersResponse message
 * containing one ControlParameters. On success, print the ControlParameters
 * values which should be the same as requested.
 * @param expressedInterest The interest given to expressInterest.
 * @param responseData The response Data packet whose content is the TLV-encoded
 * ControlParametersResponse.
 * @param enabled On success or error, set *enabled = false.
 */
static void
processRegisterResponse
  (const ptr_lib::shared_ptr<const Interest>& expressedInterest,
   const ptr_lib::shared_ptr<Data>& responseData, bool* enabled)
{
  // We are finished in all cases.
  *enabled = false;

  ndn_message::ControlParametersTypes_ControlParametersResponseMessage
    decodedControlResponse;
  ProtobufTlv::decode(decodedControlResponse, responseData->getContent());
  const ndn_message::ControlParametersTypes_ControlParametersResponse&
    controlResponse = decodedControlResponse.control_response();

  const boost::uint64_t lowestErrorCode = 400;
  if (controlResponse.status_code() >= lowestErrorCode) {
    cout << "Face create command got error, code " <<
      controlResponse.status_code() <<": " + controlResponse.status_text();
    return;
  }
  if (controlResponse.control_parameters_size() != 1) {
    cout << "Face create command response does not have one ControlParameters";
    return;
  }

  // Success. Print the ControlParameters response.
  const ndn_message::ControlParametersTypes_ControlParameters& controlParameters =
    controlResponse.control_parameters(0);
  string name = "";
  for (size_t i = 0; i < controlParameters.name().component_size(); ++i)
    name += "/" + controlParameters.name().component(i);

  cout << "Successful in name registration: ControlParameters(Name: " << name <<
     ", FaceId: " << controlParameters.face_id() <<
     ", Origin: " << controlParameters.origin() <<
     ", Cost: " << controlParameters.cost() <<
     ", Flags: " << controlParameters.flags() << ")" << endl;
}