blob: 83895add41de309d3e7d69afdef011241cd02be4 [file] [log] [blame]
Ilya Moiseenko74077042011-08-30 16:04:22 -07001#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
14using namespace ns3;
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -070015#include <fstream>
Ilya Moiseenko74077042011-08-30 16:04:22 -070016
17NS_LOG_COMPONENT_DEFINE ("InterestHeaderExample");
18
19int
20main (int argc, char *argv[])
21{
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -070022 LogComponentEnable ("InterestHeaderExample", LOG_ALL);
23
Ilya Moiseenko74077042011-08-30 16:04:22 -070024 NS_LOG_INFO ("Test started");
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -070025
Ilya Moiseenko74077042011-08-30 16:04:22 -070026 CcnxInterestHeader interestHeader;
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -070027
28 Ptr<CcnxNameComponents> testname = Create<CcnxNameComponents> ();
29 (*testname) ("first") ("second");
Ilya Moiseenko74077042011-08-30 16:04:22 -070030 interestHeader.SetName(testname);
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -070031
32 uint32_t minSuffixComponents = 20;
Ilya Moiseenko74077042011-08-30 16:04:22 -070033 interestHeader.SetMinSuffixComponents(minSuffixComponents);
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -070034
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 Moiseenko74077042011-08-30 16:04:22 -070052
53 uint32_t size = interestHeader.GetSerializedSize();
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -070054 NS_LOG_INFO ("GetSerializedSize = " << size);
Ilya Moiseenko74077042011-08-30 16:04:22 -070055 //uint32_t size = 5;
56 //NS_TEST_ASSERT_MSG_EQ (false, true, "GetSize = " << size);
57
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -070058 Buffer buf;
59 buf.AddAtStart (size);
Ilya Moiseenko74077042011-08-30 16:04:22 -070060 Buffer::Iterator iter = buf.Begin ();
61 //interestHeader.
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -070062 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 Moiseenko74077042011-08-30 16:04:22 -070071 iter = buf.Begin ();
72 CcnxInterestHeader target;
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -070073 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}