blob: ecbc8eb47f00c23cdac3a88fca375e64eb9e590f [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
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080028class PcapWriter {
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070029public:
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080030 PcapWriter(const std::string& file)
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070031 {
32 PcapHelper helper;
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080033 m_pcap = helper.CreateFile(file, std::ios::out, PcapHelper::DLT_PPP);
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070034 }
Alexander Afanasyev1ab1aad2013-02-28 11:32:21 -080035
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070036 void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080037 TracePacket(Ptr<const Packet> packet)
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070038 {
39 static PppHeader pppHeader;
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080040 pppHeader.SetProtocol(0x0077);
Alexander Afanasyev1ab1aad2013-02-28 11:32:21 -080041
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080042 m_pcap->Write(Simulator::Now(), pppHeader, packet);
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070043 }
44
45private:
46 Ptr<PcapFileWrapper> m_pcap;
47};
48
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070049int
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080050main(int argc, char* argv[])
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070051{
52 // setting default parameters for PointToPoint links and channels
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080053 Config::SetDefault("ns3::PointToPointNetDevice::DataRate", StringValue("1Mbps"));
54 Config::SetDefault("ns3::PointToPointChannel::Delay", StringValue("10ms"));
55 Config::SetDefault("ns3::DropTailQueue::MaxPackets", StringValue("20"));
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070056
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080057 Config::SetGlobal("ndn::WireFormat", StringValue("1"));
Alexander Afanasyev24207622013-07-15 18:27:56 -070058
Alexander Afanasyev39314e02013-08-13 10:10:45 -070059 // Read optional command-line parameters (e.g., enable visualizer with ./waf --run=<> --visualize
60 CommandLine cmd;
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080061 cmd.Parse(argc, argv);
62
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070063 // Creating nodes
64 NodeContainer nodes;
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080065 nodes.Create(3);
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070066
67 // Connecting nodes using two links
68 PointToPointHelper p2p;
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080069 p2p.Install(nodes.Get(0), nodes.Get(1));
70 p2p.Install(nodes.Get(1), nodes.Get(2));
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070071
Alexander Afanasyevcf6dc922012-08-10 16:55:27 -070072 // Install NDN stack on all nodes
73 ndn::StackHelper ndnHelper;
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080074 ndnHelper.SetDefaultRoutes(true);
75 ndnHelper.InstallAll();
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070076
77 // Installing applications
78
79 // Consumer
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080080 ndn::AppHelper consumerHelper("ns3::ndn::ConsumerCbr");
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070081 // Consumer will request /prefix/0, /prefix/1, ...
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080082 consumerHelper.SetPrefix("/prefix");
83 consumerHelper.SetAttribute("Frequency", StringValue("10")); // 10 interests a second
84 consumerHelper.Install(nodes.Get(0)); // first node
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070085
86 // Producer
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080087 ndn::AppHelper producerHelper("ns3::ndn::Producer");
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070088 // Producer will reply to all requests starting with /prefix
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080089 producerHelper.SetPrefix("/prefix");
90 producerHelper.SetAttribute("PayloadSize", StringValue("1024"));
91 producerHelper.SetAttribute("Signature", UintegerValue(100));
92 producerHelper.SetAttribute("KeyLocator", StringValue("/unique/key/locator"));
93 producerHelper.Install(nodes.Get(2)); // last node
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070094
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080095 PcapWriter trace("ndn-simple-trace.pcap");
96 Config::ConnectWithoutContext("/NodeList/*/DeviceList/*/$ns3::PointToPointNetDevice/MacTx",
97 MakeCallback(&PcapWriter::TracePacket, &trace));
Alexander Afanasyev1ab1aad2013-02-28 11:32:21 -080098
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080099 Simulator::Stop(Seconds(20.0));
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700100
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800101 Simulator::Run();
102 Simulator::Destroy();
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700103
104 return 0;
105}