blob: 606562407bec0816039fd71879df3dd224c2d37b [file] [log] [blame]
Alexander Afanasyev1ab1aad2013-02-28 11:32:21 -08001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2012 University of California, Los Angeles
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation;
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
19 */
20// ndn-simple-with-pcap.cc
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070021#include "ns3/core-module.h"
22#include "ns3/network-module.h"
23#include "ns3/point-to-point-module.h"
24#include "ns3/ndnSIM-module.h"
25
26using namespace ns3;
27
28class PcapWriter
29{
30public:
31 PcapWriter (const std::string &file)
32 {
33 PcapHelper helper;
34 m_pcap = helper.CreateFile (file, std::ios::out, PcapHelper::DLT_PPP);
35 }
Alexander Afanasyev1ab1aad2013-02-28 11:32:21 -080036
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070037 void
38 TracePacket (Ptr<const Packet> packet)
39 {
40 static PppHeader pppHeader;
41 pppHeader.SetProtocol (0x0077);
Alexander Afanasyev1ab1aad2013-02-28 11:32:21 -080042
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070043 m_pcap->Write (Simulator::Now (), pppHeader, packet);
44 }
45
46private:
47 Ptr<PcapFileWrapper> m_pcap;
48};
49
50
51int
52main (int argc, char *argv[])
53{
54 // setting default parameters for PointToPoint links and channels
55 Config::SetDefault ("ns3::PointToPointNetDevice::DataRate", StringValue ("1Mbps"));
56 Config::SetDefault ("ns3::PointToPointChannel::Delay", StringValue ("10ms"));
57 Config::SetDefault ("ns3::DropTailQueue::MaxPackets", StringValue ("20"));
58
Alexander Afanasyev7b923f32013-07-16 13:23:04 -070059 Config::SetGlobal ("ndn::WireFormat", StringValue ("1"));
Alexander Afanasyev24207622013-07-15 18:27:56 -070060
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070061 // Creating nodes
62 NodeContainer nodes;
63 nodes.Create (3);
64
65 // Connecting nodes using two links
66 PointToPointHelper p2p;
67 p2p.Install (nodes.Get (0), nodes.Get (1));
68 p2p.Install (nodes.Get (1), nodes.Get (2));
69
Alexander Afanasyevcf6dc922012-08-10 16:55:27 -070070 // Install NDN stack on all nodes
71 ndn::StackHelper ndnHelper;
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070072 ndnHelper.SetDefaultRoutes (true);
73 ndnHelper.InstallAll ();
74
75 // Installing applications
76
77 // Consumer
Alexander Afanasyevcf6dc922012-08-10 16:55:27 -070078 ndn::AppHelper consumerHelper ("ns3::ndn::ConsumerCbr");
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070079 // Consumer will request /prefix/0, /prefix/1, ...
80 consumerHelper.SetPrefix ("/prefix");
81 consumerHelper.SetAttribute ("Frequency", StringValue ("10")); // 10 interests a second
82 consumerHelper.Install (nodes.Get (0)); // first node
83
84 // Producer
Alexander Afanasyevcf6dc922012-08-10 16:55:27 -070085 ndn::AppHelper producerHelper ("ns3::ndn::Producer");
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070086 // Producer will reply to all requests starting with /prefix
87 producerHelper.SetPrefix ("/prefix");
88 producerHelper.SetAttribute ("PayloadSize", StringValue("1024"));
Alexander Afanasyeve7221482013-07-15 18:24:37 -070089 producerHelper.SetAttribute ("Signature", UintegerValue (100));
90 producerHelper.SetAttribute ("KeyLocator", StringValue ("/unique/key/locator"));
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070091 producerHelper.Install (nodes.Get (2)); // last node
92
93 PcapWriter trace ("ndn-simple-trace.pcap");
Alexander Afanasyeve7221482013-07-15 18:24:37 -070094 Config::ConnectWithoutContext ("/NodeList/*/DeviceList/*/$ns3::PointToPointNetDevice/MacTx",
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070095 MakeCallback (&PcapWriter::TracePacket, &trace));
Alexander Afanasyev1ab1aad2013-02-28 11:32:21 -080096
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070097 Simulator::Stop (Seconds (20.0));
98
99 Simulator::Run ();
100 Simulator::Destroy ();
101
102 return 0;
103}
104