Ejemplo n.º 1
0
  /** \brief respond to specific FaceQuery requests
   *  \retval true the Interest matches one of the defined patterns and is responded
   *  \retval false the Interest is not responded
   */
  bool
  respondFaceQuery(const Interest& interest)
  {
    using ndn::nfd::FacePersistency;
    using ndn::nfd::FaceQueryFilter;
    using ndn::nfd::FaceStatus;

    if (!Name("/localhost/nfd/faces/query").isPrefixOf(interest.getName())) {
      return false;
    }
    BOOST_CHECK_EQUAL(interest.getName().size(), 5);
    FaceQueryFilter filter(interest.getName().at(4).blockFromValue());

    if (filter == FaceQueryFilter().setFaceId(10156)) {
      FaceStatus faceStatus;
      faceStatus.setFaceId(10156)
                .setLocalUri("tcp4://151.26.163.27:22967")
                .setRemoteUri("tcp4://198.57.27.40:6363")
                .setFacePersistency(FacePersistency::FACE_PERSISTENCY_PERSISTENT);
      this->sendDataset(interest.getName(), faceStatus);
      return true;
    }

    if (filter == FaceQueryFilter().setRemoteUri("tcp4://32.121.182.82:6363")) {
      FaceStatus faceStatus;
      faceStatus.setFaceId(2249)
                .setLocalUri("tcp4://30.99.87.98:31414")
                .setRemoteUri("tcp4://32.121.182.82:6363")
                .setFacePersistency(FacePersistency::FACE_PERSISTENCY_PERSISTENT);
      this->sendDataset(interest.getName(), faceStatus);
      return true;
    }

    if (filter == FaceQueryFilter().setFaceId(23728)) {
      this->sendEmptyDataset(interest.getName());
      return true;
    }

    if (filter == FaceQueryFilter().setRemoteUri("udp4://225.131.75.231:56363")) {
      FaceStatus faceStatus1, faceStatus2;
      faceStatus1.setFaceId(6720)
                 .setLocalUri("udp4://202.83.168.28:56363")
                 .setRemoteUri("udp4://225.131.75.231:56363")
                 .setFacePersistency(FacePersistency::FACE_PERSISTENCY_PERMANENT);
      faceStatus2.setFaceId(31066)
                 .setLocalUri("udp4://25.90.26.32:56363")
                 .setRemoteUri("udp4://225.131.75.231:56363")
                 .setFacePersistency(FacePersistency::FACE_PERSISTENCY_PERMANENT);
      this->sendDataset(interest.getName(), faceStatus1, faceStatus2);
      return true;
    }

    return false;
  }
    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);
        }
    }