예제 #1
0
void
MulticastDiscovery::registerHubDiscoveryPrefix(const ConstBufferPtr& buffer)
{
  std::vector<uint64_t> multicastFaces;

  size_t offset = 0;
  while (offset < buffer->size()) {
    bool isOk = false;
    Block block;
    std::tie(isOk, block) = Block::fromBuffer(buffer, offset);
    if (!isOk) {
      std::cerr << "ERROR: cannot decode FaceStatus TLV" << std::endl;
      break;
    }

    offset += block.size();

    nfd::FaceStatus faceStatus(block);

    ndn::util::FaceUri uri(faceStatus.getRemoteUri());
    if (uri.getScheme() == "udp4") {
      namespace ip = boost::asio::ip;
      boost::system::error_code ec;
      ip::address address = ip::address::from_string(uri.getHost(), ec);

      if (!ec && address.is_multicast()) {
        multicastFaces.push_back(faceStatus.getFaceId());
      }
      else
        continue;
    }
  }

  if (multicastFaces.empty()) {
    m_nextStageOnFailure("No multicast faces available, skipping multicast discovery stage");
  }
  else {
    nfd::ControlParameters parameters;
    parameters
      .setName(LOCALHOP_HUB_DISCOVERY_PREFIX)
      .setCost(1)
      .setExpirationPeriod(time::seconds(30));

    nRequestedRegs = multicastFaces.size();
    nFinishedRegs = 0;

    for (const auto& face : multicastFaces) {
      parameters.setFaceId(face);
      m_controller.start<nfd::RibRegisterCommand>(parameters,
                                                  bind(&MulticastDiscovery::onRegisterSuccess,
                                                       this),
                                                  bind(&MulticastDiscovery::onRegisterFailure,
                                                       this, _1, _2));
    }
  }
}
예제 #2
0
void my_routing::show_interfaces_data(const ndn::ConstBufferPtr& buf)
{
    size_t offset = 0;
    while (offset < buf->size()) {
        bool isOk = false;
        ndn::Block block;
        std::tie(isOk, block) = ndn::Block::fromBuffer(buf, offset);
        if (!isOk) {
            std::cout << "ERROR: cannot decode FaceStatus TLV" << std::endl;
            m_has_error = true;
            break;
        }

        offset += block.size();

        ndn::nfd::FaceStatus faceStatus(block);

        std::cout << "faceid=" << faceStatus.getFaceId()
                  << " remote=" << faceStatus.getRemoteUri()
                  << " local=" << faceStatus.getLocalUri();
        if (faceStatus.hasExpirationPeriod()) {
            std::cout  << " expires="
                       << ndn::time::duration_cast<ndn::time::seconds>(faceStatus.getExpirationPeriod()).count() << "s";
        }
        std::cout << " " << faceStatus.getFaceScope()
                  << " " << faceStatus.getFacePersistency()
                  << " " << faceStatus.getLinkType();
        std::cout << " counters={"
                  << "in={" << faceStatus.getNInInterests() << "i "
                  << faceStatus.getNInDatas() << "d "
                  << faceStatus.getNInBytes() << "B}"
                  << " out={" << faceStatus.getNOutInterests() << "i "
                  << faceStatus.getNOutDatas() << "d "
                  << faceStatus.getNOutBytes() << "B}"
                  << "}";
        std::cout << std::endl;
    }
}
    void
    afterFetchedFaceStatusInformation(const shared_ptr<OBufferStream>& buffer, const Name& remoteName)
    {
        ConstBufferPtr buf = buffer->buf();

        Block block;
        size_t offset = 0;

        std::string currentTime;
        std::tm ctime;
        std::stringstream realEpochTime;
        ndn::time::system_clock::TimePoint realCurrentTime = ndn::time::system_clock::now();
        std::string currentTimeStr = ndn::time::toString(realCurrentTime, "%Y-%m-%dT%H:%M:%S%F");

        strptime(currentTimeStr.c_str(), "%FT%T%Z", &ctime);
        std::string stime(currentTimeStr);
        std::time_t realEpochSeconds = std::mktime(&ctime);
        std::size_t pos = stime.find(".");
        std::string realEpochMilli = stime.substr(pos+1);
        realEpochTime << realEpochSeconds << "." << realEpochMilli;

        CollectorData content;

        currentTime =  realEpochTime.str();

        while (offset < buf->size())
        {
            bool ok = Block::fromBuffer(buf, offset, block);
            if (!ok)
            {
                std::cerr << "ERROR: cannot decode FaceStatus TLV" << std::endl;
                break;
            }

            offset += block.size();

            nfd::FaceStatus faceStatus(block);

            // take only udp4 and tcp4 faces at the moment
            std::string remoteUri = faceStatus.getRemoteUri();
            if(remoteUri.compare(0,4,"tcp4") != 0 &&
                    remoteUri.compare(0,4,"udp4") != 0)
                continue;

            // take the ip from uri (remove tcp4:// and everything after ':'
            std::size_t strPos = remoteUri.find_last_of(":");
            std::string remoteIp = remoteUri.substr(7,strPos - 7);

            std::unordered_set<std::string>::const_iterator got = m_remoteLinks.find(remoteIp);
            // the link is not requested by the server
            if(got == m_remoteLinks.end())
                continue;

            FaceStatus linkStatus;
            linkStatus.setTx(faceStatus.getNOutBytes());
            linkStatus.setRx(faceStatus.getNInBytes());
            linkStatus.setFaceId(faceStatus.getFaceId());
            linkStatus.setLinkIp(remoteIp);
            linkStatus.setTimestamp(currentTime);

            // remove the remoteIP from the list of links to search and add it to the data packet
            m_remoteLinks.erase(remoteIp);
            content.add(linkStatus);

            if (DEBUG)
                std::cout << "about to send back " << linkStatus.getFaceId() << ": " << linkStatus.getRx() << ", " << linkStatus.getTx() << ", " << linkStatus.getLinkIp() << std::endl;
        }

        if (content.size() != 0)
        {
            ndn::shared_ptr<ndn::Data> data = ndn::make_shared<ndn::Data>(remoteName);
            data->setContent(content.wireEncode());
            data->setFreshnessPeriod(time::seconds(0));

            m_keyChain.sign(*data);
            m_face.put(*data);
        }
    }