Ilya Moiseenko | 7407704 | 2011-08-30 16:04:22 -0700 | [diff] [blame] | 1 | #include "ns3/test.h" |
| 2 | #include "ns3/annotated-topology-reader.h" |
| 3 | #include "ns3/ccnx-interest-header.h" |
| 4 | #include "ns3/uinteger.h" |
| 5 | #include "ns3/random-variable.h" |
| 6 | #include <limits> |
| 7 | #include "ns3/ccnx-header-helper.h" |
| 8 | #include "ns3/header.h" |
| 9 | #include "ns3/ccnx-name-components.h" |
| 10 | #include "ns3/nstime.h" |
| 11 | #include "ns3/buffer.h" |
| 12 | #include "ns3/log.h" |
| 13 | |
| 14 | using namespace ns3; |
Alexander Afanasyev | 85a3bca | 2011-08-31 16:51:03 -0700 | [diff] [blame] | 15 | #include <fstream> |
Ilya Moiseenko | 7407704 | 2011-08-30 16:04:22 -0700 | [diff] [blame] | 16 | |
| 17 | NS_LOG_COMPONENT_DEFINE ("InterestHeaderExample"); |
| 18 | |
| 19 | int |
| 20 | main (int argc, char *argv[]) |
| 21 | { |
Alexander Afanasyev | 85a3bca | 2011-08-31 16:51:03 -0700 | [diff] [blame] | 22 | LogComponentEnable ("InterestHeaderExample", LOG_ALL); |
| 23 | |
Ilya Moiseenko | 7407704 | 2011-08-30 16:04:22 -0700 | [diff] [blame] | 24 | NS_LOG_INFO ("Test started"); |
Alexander Afanasyev | 85a3bca | 2011-08-31 16:51:03 -0700 | [diff] [blame] | 25 | |
Ilya Moiseenko | 7407704 | 2011-08-30 16:04:22 -0700 | [diff] [blame] | 26 | CcnxInterestHeader interestHeader; |
Alexander Afanasyev | 85a3bca | 2011-08-31 16:51:03 -0700 | [diff] [blame] | 27 | |
| 28 | Ptr<CcnxNameComponents> testname = Create<CcnxNameComponents> (); |
| 29 | (*testname) ("first") ("second"); |
Ilya Moiseenko | 7407704 | 2011-08-30 16:04:22 -0700 | [diff] [blame] | 30 | interestHeader.SetName(testname); |
Alexander Afanasyev | 85a3bca | 2011-08-31 16:51:03 -0700 | [diff] [blame] | 31 | |
| 32 | uint32_t minSuffixComponents = 20; |
Ilya Moiseenko | 7407704 | 2011-08-30 16:04:22 -0700 | [diff] [blame] | 33 | interestHeader.SetMinSuffixComponents(minSuffixComponents); |
Alexander Afanasyev | 85a3bca | 2011-08-31 16:51:03 -0700 | [diff] [blame] | 34 | |
| 35 | uint32_t maxSuffixComponents = 40; |
| 36 | interestHeader.SetMaxSuffixComponents(maxSuffixComponents); |
| 37 | |
| 38 | Time lifetime = Seconds(661777) + MicroSeconds(1234); |
| 39 | interestHeader.SetInterestLifetime(lifetime); |
| 40 | |
| 41 | bool child = true; |
| 42 | interestHeader.SetChildSelector(child); |
| 43 | |
| 44 | Ptr<CcnxNameComponents> exclude = Create<CcnxNameComponents> (); |
| 45 | (*exclude) ("exclude1") ("exclude2"); |
| 46 | interestHeader.SetExclude(exclude); |
| 47 | |
| 48 | UniformVariable random(1, std::numeric_limits<uint32_t>::max ()); |
| 49 | uint32_t randomNonce = static_cast<uint32_t> (random.GetValue()); |
| 50 | interestHeader.SetNonce(randomNonce); |
| 51 | NS_LOG_INFO ("Source: \n" <<interestHeader); |
Ilya Moiseenko | 7407704 | 2011-08-30 16:04:22 -0700 | [diff] [blame] | 52 | |
| 53 | uint32_t size = interestHeader.GetSerializedSize(); |
Alexander Afanasyev | 85a3bca | 2011-08-31 16:51:03 -0700 | [diff] [blame] | 54 | NS_LOG_INFO ("GetSerializedSize = " << size); |
Ilya Moiseenko | 7407704 | 2011-08-30 16:04:22 -0700 | [diff] [blame] | 55 | //uint32_t size = 5; |
| 56 | //NS_TEST_ASSERT_MSG_EQ (false, true, "GetSize = " << size); |
| 57 | |
Alexander Afanasyev | 85a3bca | 2011-08-31 16:51:03 -0700 | [diff] [blame] | 58 | Buffer buf; |
| 59 | buf.AddAtStart (size); |
Ilya Moiseenko | 7407704 | 2011-08-30 16:04:22 -0700 | [diff] [blame] | 60 | Buffer::Iterator iter = buf.Begin (); |
| 61 | //interestHeader. |
Alexander Afanasyev | 85a3bca | 2011-08-31 16:51:03 -0700 | [diff] [blame] | 62 | interestHeader.Serialize(iter); |
| 63 | |
| 64 | std::ofstream of( "/tmp/file" ); |
| 65 | of.write (reinterpret_cast<const char*> (buf.PeekData ()), size); |
| 66 | of.close (); |
| 67 | |
| 68 | NS_LOG_INFO ("start = " << buf.GetCurrentStartOffset () << " " << |
| 69 | "end = " << buf.GetCurrentEndOffset ()); |
| 70 | |
Ilya Moiseenko | 7407704 | 2011-08-30 16:04:22 -0700 | [diff] [blame] | 71 | iter = buf.Begin (); |
| 72 | CcnxInterestHeader target; |
Alexander Afanasyev | 85a3bca | 2011-08-31 16:51:03 -0700 | [diff] [blame] | 73 | NS_LOG_INFO ("Trying to deserialize"); |
| 74 | std::cout << "\n"; |
| 75 | size = target.Deserialize (iter); |
| 76 | buf.RemoveAtEnd (size); |
| 77 | NS_LOG_INFO ("Deserialized size = " << size); |
| 78 | NS_LOG_INFO ("Target: \n" << target); |
| 79 | } |