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;
      }
    }
  }
}
Beispiel #2
0
void
Node::expressInterestHelper
  (uint64_t pendingInterestId,
   const ptr_lib::shared_ptr<const Interest>& interestCopy,
   const OnData& onData, const OnTimeout& onTimeout, WireFormat* wireFormat,
   Face* face)
{
  ptr_lib::shared_ptr<PendingInterest> pendingInterest(new PendingInterest
    (pendingInterestId, interestCopy, onData, onTimeout));
  pendingInterestTable_.push_back(pendingInterest);
  if (interestCopy->getInterestLifetimeMilliseconds() >= 0.0)
    // Set up the timeout.
    face->callLater
      (interestCopy->getInterestLifetimeMilliseconds(),
       bind(&Node::processInterestTimeout, this, pendingInterest));

  // Special case: For timeoutPrefix_ we don't actually send the interest.
  if (!timeoutPrefix_.match(interestCopy->getName())) {
    Blob encoding = interestCopy->wireEncode(*wireFormat);
    if (encoding.size() > getMaxNdnPacketSize())
      throw runtime_error
        ("The encoded interest size exceeds the maximum limit getMaxNdnPacketSize()");
    transport_->send(*encoding);
  }
}
Beispiel #3
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;
 }
void
Chat::onInterest
  (const ptr_lib::shared_ptr<const Name>& prefix,
   const ptr_lib::shared_ptr<const Interest>& interest, Face& face,
   uint64_t interestFilterId,
   const ptr_lib::shared_ptr<const InterestFilter>& filter)
{
  SyncDemo::ChatMessage content;
  int sequenceNo = ::atoi(interest->getName().get(chatPrefix_.size() + 1).toEscapedString().c_str());
  for (int i = messageCache_.size() - 1; i >= 0; --i) {
    if (messageCache_[i]->getSequenceNo() == sequenceNo) {
      if (messageCache_[i]->getMessageType() != SyncDemo::ChatMessage_ChatMessageType_CHAT) {
        content.set_from(screenName_);
        content.set_to(chatRoom_);
        content.set_type((SyncDemo::ChatMessage_ChatMessageType)messageCache_[i]->getMessageType());
        content.set_timestamp(::round(messageCache_[i]->getTime() / 1000.0));
      }
      else {
        content.set_from(screenName_);
        content.set_to(chatRoom_);
        content.set_type((SyncDemo::ChatMessage_ChatMessageType)messageCache_[i]->getMessageType());
        content.set_data(messageCache_[i]->getMessage());
        content.set_timestamp(::round(messageCache_[i]->getTime() / 1000.0));
      }
      break;
    }
  }

  if (content.from().size() != 0) {
    ptr_lib::shared_ptr<vector<uint8_t> > array(new vector<uint8_t>(content.ByteSize()));
    content.SerializeToArray(&array->front(), array->size());
    Data data(interest->getName());
    data.setContent(Blob(array, false));
    keyChain_.sign(data, certificateName_);
    try {
      face.putData(data);
    }
    catch (std::exception& e) {
      cout << "Error sending the chat data " << e.what() << endl;
    }
  }
}
Beispiel #5
0
Name
SecPublicInfo::getDefaultCertificateName()
{
  if(!static_cast<bool>(defaultCertificate_))
    refreshDefaultCertificate();

  if(!static_cast<bool>(defaultCertificate_))
    return Name();

  return defaultCertificate_->getName();
}
ptr_lib::shared_ptr<ValidationRequest>
SelfVerifyPolicyManager::checkVerificationPolicy
  (const ptr_lib::shared_ptr<Interest>& interest, int stepCount,
   const OnVerifiedInterest& onVerified,
   const OnVerifyInterestFailed& onVerifyFailed, WireFormat& wireFormat)
{
  // Decode the last two name components of the signed interest
  ptr_lib::shared_ptr<Signature> signature =
    wireFormat.decodeSignatureInfoAndValue
      (interest->getName().get(-2).getValue().buf(),
       interest->getName().get(-2).getValue().size(),
       interest->getName().get(-1).getValue().buf(),
       interest->getName().get(-1).getValue().size());

  // wireEncode returns the cached encoding if available.
  if (verify(signature.get(), interest->wireEncode()))
    onVerified(interest);
  else
    onVerifyFailed(interest);
  
  // No more steps, so return a null ValidationRequest.
  return ptr_lib::shared_ptr<ValidationRequest>();
}
Beispiel #7
0
  void
  onInterest(const ptr_lib::shared_ptr<const Name> &name, const ptr_lib::shared_ptr<const Interest> &interest)
  {
    std::cout << "<< I: " << *interest << std::endl;
    
    ndn::Data data(ndn::Name(interest->getName()).append("testApp").appendVersion());
    data.setFreshnessPeriod(1000); // 10 sec

    data.setContent((const uint8_t*)"HELLO KITTY", sizeof("HELLO KITTY"));

    keyChain_.sign(data);

    std::cout << ">> D: " << data << std::endl;
    face_.put(data);
  }
Beispiel #8
0
 void onTimeout(const ptr_lib::shared_ptr<const Interest>& interest)
 {
   ++callbackCount_;
   cout << "Time out for interest " << interest->getName().toUri() << endl;
 }
Beispiel #9
0
void
MemoryContentCache::operator()
  (const ptr_lib::shared_ptr<const Name>& prefix,
   const ptr_lib::shared_ptr<const Interest>& interest, Face& face,
   boost::uint64_t interestFilterId,
   const ptr_lib::shared_ptr<const InterestFilter>& filter)
{
  doCleanup();

  const Name::Component* selectedComponent = 0;
  Blob selectedEncoding;
  // We need to iterate over both arrays.
  size_t totalSize = staleTimeCache_.size() + noStaleTimeCache_.size();
  for (size_t i = 0; i < totalSize; ++i) {
    const Content* content;
    if (i < staleTimeCache_.size())
      content = staleTimeCache_[i].get();
    else
      // We have iterated over the first array. Get from the second.
      content = noStaleTimeCache_[i - staleTimeCache_.size()].get();

    if (interest->matchesName(content->getName())) {
      if (interest->getChildSelector() < 0) {
        // No child selector, so send the first match that we have found.
        face.send(*content->getDataEncoding());
        return;
      }
      else {
        // Update selectedEncoding based on the child selector.
        const Name::Component* component;
        if (content->getName().size() > interest->getName().size())
          component = &content->getName().get(interest->getName().size());
        else
          component = &emptyComponent_;

        bool gotBetterMatch = false;
        if (!selectedEncoding)
          // Save the first match.
          gotBetterMatch = true;
        else {
          if (interest->getChildSelector() == 0) {
            // Leftmost child.
            if (*component < *selectedComponent)
              gotBetterMatch = true;
          }
          else {
            // Rightmost child.
            if (*component > *selectedComponent)
              gotBetterMatch = true;
          }
        }

        if (gotBetterMatch) {
          selectedComponent = component;
          selectedEncoding = content->getDataEncoding();
        }
      }
    }
  }

  if (selectedEncoding)
    // We found the leftmost or rightmost child.
    face.send(*selectedEncoding);
  else {
    // Call the onDataNotFound callback (if defined).
    map<string, OnInterestCallback>::iterator onDataNotFound =
      onDataNotFoundForPrefix_.find(prefix->toUri());
    if (onDataNotFound != onDataNotFoundForPrefix_.end() &&
        onDataNotFound->second)
      onDataNotFound->second(prefix, interest, face, interestFilterId, filter);
  }
}