blob: e26866b5dfa5176c809bedb71672629504a38376 [file] [log] [blame]
Alexander Afanasyev60a7b622014-12-20 17:04:07 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2011-2015 Regents of the University of California.
Alexander Afanasyev1ab1aad2013-02-28 11:32:21 -08004 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -08005 * This file is part of ndnSIM. See AUTHORS for complete list of ndnSIM authors and
6 * contributors.
Alexander Afanasyev1ab1aad2013-02-28 11:32:21 -08007 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -08008 * ndnSIM is free software: you can redistribute it and/or modify it under the terms
9 * of the GNU General Public License as published by the Free Software Foundation,
10 * either version 3 of the License, or (at your option) any later version.
Alexander Afanasyev1ab1aad2013-02-28 11:32:21 -080011 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -080012 * ndnSIM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
Alexander Afanasyev1ab1aad2013-02-28 11:32:21 -080015 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -080016 * You should have received a copy of the GNU General Public License along with
17 * ndnSIM, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18 **/
19
20// ndn-simple-with-pcap.cpp
21
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070022#include "ns3/core-module.h"
23#include "ns3/network-module.h"
24#include "ns3/point-to-point-module.h"
25#include "ns3/ndnSIM-module.h"
26
Spyridon Mastorakisdb8280f2014-11-21 20:00:17 -080027namespace ns3 {
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070028
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080029class PcapWriter {
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070030public:
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080031 PcapWriter(const std::string& file)
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070032 {
33 PcapHelper helper;
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080034 m_pcap = helper.CreateFile(file, std::ios::out, PcapHelper::DLT_PPP);
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070035 }
Alexander Afanasyev1ab1aad2013-02-28 11:32:21 -080036
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070037 void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080038 TracePacket(Ptr<const Packet> packet)
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070039 {
40 static PppHeader pppHeader;
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080041 pppHeader.SetProtocol(0x0077);
Alexander Afanasyev1ab1aad2013-02-28 11:32:21 -080042
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080043 m_pcap->Write(Simulator::Now(), pppHeader, packet);
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070044 }
45
46private:
47 Ptr<PcapFileWrapper> m_pcap;
48};
49
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070050int
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080051main(int argc, char* argv[])
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070052{
53 // setting default parameters for PointToPoint links and channels
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080054 Config::SetDefault("ns3::PointToPointNetDevice::DataRate", StringValue("1Mbps"));
55 Config::SetDefault("ns3::PointToPointChannel::Delay", StringValue("10ms"));
56 Config::SetDefault("ns3::DropTailQueue::MaxPackets", StringValue("20"));
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070057
Alexander Afanasyev39314e02013-08-13 10:10:45 -070058 // Read optional command-line parameters (e.g., enable visualizer with ./waf --run=<> --visualize
59 CommandLine cmd;
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080060 cmd.Parse(argc, argv);
61
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070062 // Creating nodes
63 NodeContainer nodes;
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080064 nodes.Create(3);
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070065
66 // Connecting nodes using two links
67 PointToPointHelper p2p;
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080068 p2p.Install(nodes.Get(0), nodes.Get(1));
69 p2p.Install(nodes.Get(1), nodes.Get(2));
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070070
Alexander Afanasyevcf6dc922012-08-10 16:55:27 -070071 // Install NDN stack on all nodes
72 ndn::StackHelper ndnHelper;
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080073 ndnHelper.SetDefaultRoutes(true);
74 ndnHelper.InstallAll();
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070075
76 // Installing applications
77
78 // Consumer
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080079 ndn::AppHelper consumerHelper("ns3::ndn::ConsumerCbr");
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070080 // Consumer will request /prefix/0, /prefix/1, ...
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080081 consumerHelper.SetPrefix("/prefix");
82 consumerHelper.SetAttribute("Frequency", StringValue("10")); // 10 interests a second
83 consumerHelper.Install(nodes.Get(0)); // first node
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070084
85 // Producer
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080086 ndn::AppHelper producerHelper("ns3::ndn::Producer");
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070087 // Producer will reply to all requests starting with /prefix
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080088 producerHelper.SetPrefix("/prefix");
89 producerHelper.SetAttribute("PayloadSize", StringValue("1024"));
90 producerHelper.SetAttribute("Signature", UintegerValue(100));
91 producerHelper.SetAttribute("KeyLocator", StringValue("/unique/key/locator"));
92 producerHelper.Install(nodes.Get(2)); // last node
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070093
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080094 PcapWriter trace("ndn-simple-trace.pcap");
95 Config::ConnectWithoutContext("/NodeList/*/DeviceList/*/$ns3::PointToPointNetDevice/MacTx",
96 MakeCallback(&PcapWriter::TracePacket, &trace));
Alexander Afanasyev1ab1aad2013-02-28 11:32:21 -080097
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080098 Simulator::Stop(Seconds(20.0));
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070099
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800100 Simulator::Run();
101 Simulator::Destroy();
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700102
103 return 0;
104}
Spyridon Mastorakisdb8280f2014-11-21 20:00:17 -0800105
106} // namespace ns3
107
108int
109main(int argc, char* argv[])
110{
111 return ns3::main(argc, argv);
112}