void
  wireDecode(const Block& block)
  {
    if (block.type() != statusCollector::tlv::ScriptPacket)
    {
      std::stringstream error;
      error << "Expected ScriptPacket block, but block is of different type: #" << block.type();
      std::cerr << error.str() << std::endl;
    }
  
    m_block = block;
    m_block.parse();

    Block::element_const_iterator val = m_block.elements_begin();
    Block::element_const_iterator oldVal; 

    while (val != m_block.elements_end())
    {
      oldVal = val;

      if (val != m_block.elements_end() && val->type() == ndn::statusCollector::tlv::ScriptData)
      {
        m_data.assign(reinterpret_cast<const char*>(val->value()), val->value_size());
        ++val;
      }
      if(oldVal == val)
      {
        std::cout << "ERROR!!: About to exit" << std::endl;
        exit(1);
      }
    } 
  }
Esempio n. 2
0
void
EndorseExtension::wireDecode(const Block& endorseWire)
{
  m_wire = endorseWire;
  m_wire.parse();

  // EndorseExtension := ENDORSE-EXTENSION-TYPE TLV-LENGTH
  //                       EntryData+
  //
  // EntryData := ENTRYDATA-TYPE TLV-LENGTH
  //                String
  //

  if (m_wire.type() != tlv::EndorseExtension)
    throw Error("Unexpected TLV number when decoding endorse extension packet");

  // EntryData
  Block::element_const_iterator i = m_wire.elements_begin();
  if (i == m_wire.elements_end())
    throw Error("Missing Entry Data");
  if (i->type() != tlv::EntryData)
    throw Error("Expect Entry Data but get TLV Type " + std::to_string(i->type()));

   while (i != m_wire.elements_end() && i->type() == tlv::EntryData) {
     m_entries.push_back(std::string(reinterpret_cast<const char* >(i->value()),
                                     i->value_size()));
     ++i;
  }

  if (i != m_wire.elements_end()) {
    throw Error("Unexpected element");
  }
}
Esempio n. 3
0
void
Exclude::wireDecode(const Block& wire)
{
  clear();

  if (wire.type() != tlv::Exclude)
    throw tlv::Error("Unexpected TLV type when decoding Exclude");

  m_wire = wire;
  m_wire.parse();

  if (m_wire.elements_size() == 0) {
    throw Error("Exclude element cannot be empty");
  }

  // Exclude ::= EXCLUDE-TYPE TLV-LENGTH Any? (NameComponent (Any)?)+
  // Any     ::= ANY-TYPE TLV-LENGTH(=0)

  Block::element_const_iterator i = m_wire.elements_begin();
  if (i->type() == tlv::Any)
    {
      appendExclude(name::Component(), true);
      ++i;
    }

  while (i != m_wire.elements_end())
    {
      if (i->type() != tlv::NameComponent)
        throw Error("Incorrect format of Exclude filter");

      name::Component excludedComponent(i->value(), i->value_size());
      ++i;

      if (i != m_wire.elements_end())
        {
          if (i->type() == tlv::Any)
            {
              appendExclude(excludedComponent, true);
              ++i;
            }
          else
            {
              appendExclude(excludedComponent, false);
            }
        }
      else
        {
          appendExclude(excludedComponent, false);
        }
    }
}
Esempio n. 4
0
void
MulticastDiscovery::onSuccess(Data& data)
{
  const Block& content = data.getContent();
  content.parse();

  // Get Uri
  Block::element_const_iterator blockValue = content.find(tlv::nfd::Uri);
  if (blockValue == content.elements_end()) {
    m_nextStageOnFailure("Incorrect reply to multicast discovery stage");
    return;
  }
  std::string hubUri(reinterpret_cast<const char*>(blockValue->value()), blockValue->value_size());
  this->connectToHub(hubUri);
}
void
Manifest::decode()
{
    Block content = getContent();
    content.parse();

    // Manifest ::= CONTENT-TLV TLV-LENGTH
    //                Catalogue?
    //                  Name*
    //                KeyValuePair*

    for ( Block::element_const_iterator val = content.elements_begin();
            val != content.elements_end(); ++val)
    {
        if (val->type() == tlv::ManifestCatalogue)
        {
            val->parse();
            for ( Block::element_const_iterator catalogueNameElem = val->elements_begin();
                    catalogueNameElem != val->elements_end(); ++catalogueNameElem)
            {
                if (catalogueNameElem->type() == tlv::Name)
                {
                    Name name(*catalogueNameElem);
                    m_catalogueNames.push_back(name);
                }
            }
        }
        else if (val->type() == tlv::KeyValuePair)
        {
            std::string str((char*)val->value(), val->value_size());

            size_t index = str.find_first_of('=');
            if (index == std::string::npos || index == 0 || (index == str.size() - 1))
                continue;

            std::string key = str.substr(0, index);
            std::string value = str.substr(index + 1, str.size() - index - 1);
            addKeyValuePair(key, value);
        }
    }
}
void
FaceEventNotification::wireDecode(const Block& block)
{
  if (block.type() != tlv::nfd::FaceEventNotification) {
    throw Error("expecting FaceEventNotification block");
  }
  m_wire = block;
  m_wire.parse();
  Block::element_const_iterator val = m_wire.elements_begin();

  if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceEventKind) {
    m_kind = static_cast<FaceEventKind>(readNonNegativeInteger(*val));
    ++val;
  }
  else {
    throw Error("missing required FaceEventKind field");
  }

  if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceId) {
    m_faceId = readNonNegativeInteger(*val);
    ++val;
  }
  else {
    throw Error("missing required FaceId field");
  }

  if (val != m_wire.elements_end() && val->type() == tlv::nfd::Uri) {
    m_remoteUri.assign(reinterpret_cast<const char*>(val->value()), val->value_size());
    ++val;
  }
  else {
    throw Error("missing required Uri field");
  }

  if (val != m_wire.elements_end() && val->type() == tlv::nfd::LocalUri) {
    m_localUri.assign(reinterpret_cast<const char*>(val->value()), val->value_size());
    ++val;
  }
  else {
    throw Error("missing required LocalUri field");
  }

  if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceScope) {
    m_faceScope = static_cast<FaceScope>(readNonNegativeInteger(*val));
    ++val;
  }
  else {
    throw Error("missing required FaceScope field");
  }

  if (val != m_wire.elements_end() && val->type() == tlv::nfd::FacePersistency) {
    m_facePersistency = static_cast<FacePersistency>(readNonNegativeInteger(*val));
    ++val;
  }
  else {
    throw Error("missing required FacePersistency field");
  }

  if (val != m_wire.elements_end() && val->type() == tlv::nfd::LinkType) {
    m_linkType = static_cast<LinkType>(readNonNegativeInteger(*val));
    ++val;
  }
  else {
    throw Error("missing required LinkType field");
  }
}
void
ControlParameters::wireDecode(const Block& block)
{
  if (block.type() != tlv::nfd::ControlParameters) {
    throw Error("expecting TLV-TYPE ControlParameters");
  }
  m_wire = block;
  m_wire.parse();
  Block::element_const_iterator val;

  val = m_wire.find(tlv::Name);
  m_hasFields[CONTROL_PARAMETER_NAME] = val != m_wire.elements_end();
  if (this->hasName()) {
    m_name.wireDecode(*val);
  }

  val = m_wire.find(tlv::nfd::FaceId);
  m_hasFields[CONTROL_PARAMETER_FACE_ID] = val != m_wire.elements_end();
  if (this->hasFaceId()) {
    m_faceId = static_cast<uint64_t>(readNonNegativeInteger(*val));
  }

  val = m_wire.find(tlv::nfd::Uri);
  m_hasFields[CONTROL_PARAMETER_URI] = val != m_wire.elements_end();
  if (this->hasUri()) {
    m_uri.assign(reinterpret_cast<const char*>(val->value()), val->value_size());
  }

  val = m_wire.find(tlv::nfd::LocalControlFeature);
  m_hasFields[CONTROL_PARAMETER_LOCAL_CONTROL_FEATURE] = val != m_wire.elements_end();
  if (this->hasLocalControlFeature()) {
    m_localControlFeature = static_cast<LocalControlFeature>(readNonNegativeInteger(*val));
  }

  val = m_wire.find(tlv::nfd::Origin);
  m_hasFields[CONTROL_PARAMETER_ORIGIN] = val != m_wire.elements_end();
  if (this->hasOrigin()) {
    m_origin = static_cast<uint64_t>(readNonNegativeInteger(*val));
  }

  val = m_wire.find(tlv::nfd::Cost);
  m_hasFields[CONTROL_PARAMETER_COST] = val != m_wire.elements_end();
  if (this->hasCost()) {
    m_cost = static_cast<uint64_t>(readNonNegativeInteger(*val));
  }

  val = m_wire.find(tlv::nfd::Flags);
  m_hasFields[CONTROL_PARAMETER_FLAGS] = val != m_wire.elements_end();
  if (this->hasFlags()) {
    m_flags = static_cast<uint64_t>(readNonNegativeInteger(*val));
  }

  val = m_wire.find(tlv::nfd::Strategy);
  m_hasFields[CONTROL_PARAMETER_STRATEGY] = val != m_wire.elements_end();
  if (this->hasStrategy()) {
    val->parse();
    if (val->elements().empty()) {
      throw Error("expecting Strategy/Name");
    }
    else {
      m_strategy.wireDecode(*val->elements_begin());
    }
  }

  val = m_wire.find(tlv::nfd::ExpirationPeriod);
  m_hasFields[CONTROL_PARAMETER_EXPIRATION_PERIOD] = val != m_wire.elements_end();
  if (this->hasExpirationPeriod()) {
    m_expirationPeriod = time::milliseconds(readNonNegativeInteger(*val));
  }
}
Esempio n. 8
0
void
FaceStatus::wireDecode(const Block& block)
{
  if (block.type() != tlv::nfd::FaceStatus) {
    BOOST_THROW_EXCEPTION(Error("expecting FaceStatus block"));
  }
  m_wire = block;
  m_wire.parse();
  Block::element_const_iterator val = m_wire.elements_begin();

  if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceId) {
    m_faceId = readNonNegativeInteger(*val);
    ++val;
  }
  else {
    BOOST_THROW_EXCEPTION(Error("missing required FaceId field"));
  }

  if (val != m_wire.elements_end() && val->type() == tlv::nfd::Uri) {
    m_remoteUri.assign(reinterpret_cast<const char*>(val->value()), val->value_size());
    ++val;
  }
  else {
    BOOST_THROW_EXCEPTION(Error("missing required Uri field"));
  }

  if (val != m_wire.elements_end() && val->type() == tlv::nfd::LocalUri) {
    m_localUri.assign(reinterpret_cast<const char*>(val->value()), val->value_size());
    ++val;
  }
  else {
    BOOST_THROW_EXCEPTION(Error("missing required LocalUri field"));
  }

  if (val != m_wire.elements_end() && val->type() == tlv::nfd::ExpirationPeriod) {
    m_expirationPeriod = time::milliseconds(readNonNegativeInteger(*val));
    m_hasExpirationPeriod = true;
    ++val;
  }
  else {
    m_hasExpirationPeriod = false;
    // ExpirationPeriod is optional
  }

  if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceScope) {
    m_faceScope = static_cast<FaceScope>(readNonNegativeInteger(*val));
    ++val;
  }
  else {
    BOOST_THROW_EXCEPTION(Error("missing required FaceScope field"));
  }

  if (val != m_wire.elements_end() && val->type() == tlv::nfd::FacePersistency) {
    m_facePersistency = static_cast<FacePersistency>(readNonNegativeInteger(*val));
    ++val;
  }
  else {
    BOOST_THROW_EXCEPTION(Error("missing required FacePersistency field"));
  }

  if (val != m_wire.elements_end() && val->type() == tlv::nfd::LinkType) {
    m_linkType = static_cast<LinkType>(readNonNegativeInteger(*val));
    ++val;
  }
  else {
    BOOST_THROW_EXCEPTION(Error("missing required LinkType field"));
  }

  if (val != m_wire.elements_end() && val->type() == tlv::nfd::NInInterests) {
    m_nInInterests = readNonNegativeInteger(*val);
    ++val;
  }
  else {
    BOOST_THROW_EXCEPTION(Error("missing required NInInterests field"));
  }

  if (val != m_wire.elements_end() && val->type() == tlv::nfd::NInDatas) {
    m_nInDatas = readNonNegativeInteger(*val);
    ++val;
  }
  else {
    BOOST_THROW_EXCEPTION(Error("missing required NInDatas field"));
  }

  if (val != m_wire.elements_end() && val->type() == tlv::nfd::NInNacks) {
    m_nInNacks = readNonNegativeInteger(*val);
    ++val;
  }
  else {
    BOOST_THROW_EXCEPTION(Error("missing required NInNacks field"));
  }

  if (val != m_wire.elements_end() && val->type() == tlv::nfd::NOutInterests) {
    m_nOutInterests = readNonNegativeInteger(*val);
    ++val;
  }
  else {
    BOOST_THROW_EXCEPTION(Error("missing required NOutInterests field"));
  }

  if (val != m_wire.elements_end() && val->type() == tlv::nfd::NOutDatas) {
    m_nOutDatas = readNonNegativeInteger(*val);
    ++val;
  }
  else {
    BOOST_THROW_EXCEPTION(Error("missing required NOutDatas field"));
  }

  if (val != m_wire.elements_end() && val->type() == tlv::nfd::NOutNacks) {
    m_nOutNacks = readNonNegativeInteger(*val);
    ++val;
  }
  else {
    BOOST_THROW_EXCEPTION(Error("missing required NOutNacks field"));
  }

  if (val != m_wire.elements_end() && val->type() == tlv::nfd::NInBytes) {
    m_nInBytes = readNonNegativeInteger(*val);
    ++val;
  }
  else {
    BOOST_THROW_EXCEPTION(Error("missing required NInBytes field"));
  }

  if (val != m_wire.elements_end() && val->type() == tlv::nfd::NOutBytes) {
    m_nOutBytes = readNonNegativeInteger(*val);
    ++val;
  }
  else {
    BOOST_THROW_EXCEPTION(Error("missing required NOutBytes field"));
  }

  if (val != m_wire.elements_end() && val->type() == tlv::nfd::Flags) {
    m_flags = readNonNegativeInteger(*val);
    ++val;
  }
  else {
    BOOST_THROW_EXCEPTION(Error("missing required Flags field"));
  }
}
void
FaceQueryFilter::wireDecode(const Block& block)
{
  //all fields are optional
  if (block.type() != tlv::nfd::FaceQueryFilter) {
    BOOST_THROW_EXCEPTION(Error("expecting FaceQueryFilter block"));
  }

  m_wire = block;
  m_wire.parse();
  Block::element_const_iterator val = m_wire.elements_begin();

  if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceId) {
    m_faceId = readNonNegativeInteger(*val);
    m_hasFaceId = true;
    ++val;
  }
  else {
    m_hasFaceId = false;
  }

  if (val != m_wire.elements_end() && val->type() == tlv::nfd::UriScheme) {
    m_uriScheme.assign(reinterpret_cast<const char*>(val->value()), val->value_size());
    m_hasUriScheme = true;
    ++val;
  }
  else {
    m_hasUriScheme = false;
  }

  if (val != m_wire.elements_end() && val->type() == tlv::nfd::Uri) {
    m_remoteUri.assign(reinterpret_cast<const char*>(val->value()), val->value_size());
    m_hasRemoteUri = true;
    ++val;
  }
  else {
    m_hasRemoteUri = false;
  }

  if (val != m_wire.elements_end() && val->type() == tlv::nfd::LocalUri) {
    m_localUri.assign(reinterpret_cast<const char*>(val->value()), val->value_size());
    m_hasLocalUri = true;
    ++val;
  }
  else {
    m_hasLocalUri = false;
  }

  if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceScope) {
    m_faceScope = static_cast<FaceScope>(readNonNegativeInteger(*val));
    m_hasFaceScope = true;
    ++val;
  }
  else {
    m_hasFaceScope = false;
  }

  if (val != m_wire.elements_end() && val->type() == tlv::nfd::FacePersistency) {
    m_facePersistency = static_cast<FacePersistency>(readNonNegativeInteger(*val));
    m_hasFacePersistency = true;
    ++val;
  }
  else {
    m_hasFacePersistency = false;
  }

  if (val != m_wire.elements_end() && val->type() == tlv::nfd::LinkType) {
    m_linkType = static_cast<LinkType>(readNonNegativeInteger(*val));
    m_hasLinkType = true;
    ++val;
  }
  else {
    m_hasLinkType = false;
  }

}
  void
  wireDecode(const Block& wire)
  {
    m_tx = 0;
    m_rx = 0;
    m_faceId = 0;
    m_linkIp.empty();
    m_timestamp.empty();
    
    
    if (wire.type() != statusCollector::tlv::FaceStatus)
    {
      std::stringstream error;
      error << "Expected FaceStatus Block, but Block is of a different type: #" << wire.type();
      std::cerr << error.str() << std::endl;
    }
    
    m_wire = wire;
    m_wire.parse();
    
    Block::element_const_iterator val = m_wire.elements_begin();
    Block::element_const_iterator oldVal;
    while (val != m_wire.elements_end())
    {
      oldVal = val;

     // std::cout << "Block is of type: #" << m_wire.type() << std::endl;
      if (val != m_wire.elements_end() && val->type() == ndn::statusCollector::tlv::RX)
      {
        m_rx = readNonNegativeInteger(*val);
        ++val;
      }
      if (val != m_wire.elements_end() && val->type() == ndn::statusCollector::tlv::TX)
      {
        m_tx = readNonNegativeInteger(*val);
        ++val;
      }
      
      if (val != m_wire.elements_end() && val->type() == ndn::statusCollector::tlv::FaceId)
      {
        m_faceId = readNonNegativeInteger(*val);
        ++val;
      }
      
      if (val != m_wire.elements_end() && val->type() == ndn::statusCollector::tlv::LinkIp)
      {
        m_linkIp.assign(reinterpret_cast<const char*>(val->value()), val->value_size());
        ++val;
      }
      
      if (val != m_wire.elements_end() && val->type() == ndn::statusCollector::tlv::CurrentTime)
      {
        m_timestamp.assign(reinterpret_cast<const char*>(val->value()), val->value_size());
        ++val;
      }
      
      if(oldVal == val)
      {
        std::cout << "ERROR!!: About to exit" << std::endl;
        exit(1);
      }
    }
  }