Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 1 | |
| 2 | #include "ns3/core-module.h" |
| 3 | #include "ns3/network-module.h" |
| 4 | #include "ns3/point-to-point-module.h" |
| 5 | #include "ns3/ndnSIM-module.h" |
| 6 | |
| 7 | using namespace ns3; |
| 8 | |
| 9 | class PcapWriter |
| 10 | { |
| 11 | public: |
| 12 | PcapWriter (const std::string &file) |
| 13 | { |
| 14 | PcapHelper helper; |
| 15 | m_pcap = helper.CreateFile (file, std::ios::out, PcapHelper::DLT_PPP); |
| 16 | } |
| 17 | |
| 18 | void |
| 19 | TracePacket (Ptr<const Packet> packet) |
| 20 | { |
| 21 | static PppHeader pppHeader; |
| 22 | pppHeader.SetProtocol (0x0077); |
| 23 | |
| 24 | m_pcap->Write (Simulator::Now (), pppHeader, packet); |
| 25 | } |
| 26 | |
| 27 | private: |
| 28 | Ptr<PcapFileWrapper> m_pcap; |
| 29 | }; |
| 30 | |
| 31 | |
| 32 | int |
| 33 | main (int argc, char *argv[]) |
| 34 | { |
| 35 | // setting default parameters for PointToPoint links and channels |
| 36 | Config::SetDefault ("ns3::PointToPointNetDevice::DataRate", StringValue ("1Mbps")); |
| 37 | Config::SetDefault ("ns3::PointToPointChannel::Delay", StringValue ("10ms")); |
| 38 | Config::SetDefault ("ns3::DropTailQueue::MaxPackets", StringValue ("20")); |
| 39 | |
Alexander Afanasyev | cf6dc92 | 2012-08-10 16:55:27 -0700 | [diff] [blame] | 40 | Config::SetDefault ("ns3::ndn::Producer::SignatureBits", StringValue ("1")); |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 41 | |
| 42 | // Creating nodes |
| 43 | NodeContainer nodes; |
| 44 | nodes.Create (3); |
| 45 | |
| 46 | // Connecting nodes using two links |
| 47 | PointToPointHelper p2p; |
| 48 | p2p.Install (nodes.Get (0), nodes.Get (1)); |
| 49 | p2p.Install (nodes.Get (1), nodes.Get (2)); |
| 50 | |
Alexander Afanasyev | cf6dc92 | 2012-08-10 16:55:27 -0700 | [diff] [blame] | 51 | // Install NDN stack on all nodes |
| 52 | ndn::StackHelper ndnHelper; |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 53 | ndnHelper.SetDefaultRoutes (true); |
| 54 | ndnHelper.InstallAll (); |
| 55 | |
| 56 | // Installing applications |
| 57 | |
| 58 | // Consumer |
Alexander Afanasyev | cf6dc92 | 2012-08-10 16:55:27 -0700 | [diff] [blame] | 59 | ndn::AppHelper consumerHelper ("ns3::ndn::ConsumerCbr"); |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 60 | // Consumer will request /prefix/0, /prefix/1, ... |
| 61 | consumerHelper.SetPrefix ("/prefix"); |
| 62 | consumerHelper.SetAttribute ("Frequency", StringValue ("10")); // 10 interests a second |
| 63 | consumerHelper.Install (nodes.Get (0)); // first node |
| 64 | |
| 65 | // Producer |
Alexander Afanasyev | cf6dc92 | 2012-08-10 16:55:27 -0700 | [diff] [blame] | 66 | ndn::AppHelper producerHelper ("ns3::ndn::Producer"); |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 67 | // Producer will reply to all requests starting with /prefix |
| 68 | producerHelper.SetPrefix ("/prefix"); |
| 69 | producerHelper.SetAttribute ("PayloadSize", StringValue("1024")); |
| 70 | producerHelper.Install (nodes.Get (2)); // last node |
| 71 | |
| 72 | PcapWriter trace ("ndn-simple-trace.pcap"); |
Alexander Afanasyev | cf6dc92 | 2012-08-10 16:55:27 -0700 | [diff] [blame] | 73 | Config::ConnectWithoutContext ("/NodeList/*/$ns3::ndn::L3Protocol/FaceList/*/NdnTx", |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 74 | MakeCallback (&PcapWriter::TracePacket, &trace)); |
| 75 | |
| 76 | Simulator::Stop (Seconds (20.0)); |
| 77 | |
| 78 | Simulator::Run (); |
| 79 | Simulator::Destroy (); |
| 80 | |
| 81 | return 0; |
| 82 | } |
| 83 | |