Alexander Afanasyev | 1ab1aad | 2013-02-28 11:32:21 -0800 | [diff] [blame] | 1 | /* -*- 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 Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 21 | #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 | |
Spyridon Mastorakis | db8280f | 2014-11-21 20:00:17 -0800 | [diff] [blame] | 26 | namespace ns3 { |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 27 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 28 | class PcapWriter { |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 29 | public: |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 30 | PcapWriter(const std::string& file) |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 31 | { |
| 32 | PcapHelper helper; |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 33 | m_pcap = helper.CreateFile(file, std::ios::out, PcapHelper::DLT_PPP); |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 34 | } |
Alexander Afanasyev | 1ab1aad | 2013-02-28 11:32:21 -0800 | [diff] [blame] | 35 | |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 36 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 37 | TracePacket(Ptr<const Packet> packet) |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 38 | { |
| 39 | static PppHeader pppHeader; |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 40 | pppHeader.SetProtocol(0x0077); |
Alexander Afanasyev | 1ab1aad | 2013-02-28 11:32:21 -0800 | [diff] [blame] | 41 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 42 | m_pcap->Write(Simulator::Now(), pppHeader, packet); |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | private: |
| 46 | Ptr<PcapFileWrapper> m_pcap; |
| 47 | }; |
| 48 | |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 49 | int |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 50 | main(int argc, char* argv[]) |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 51 | { |
| 52 | // setting default parameters for PointToPoint links and channels |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 53 | Config::SetDefault("ns3::PointToPointNetDevice::DataRate", StringValue("1Mbps")); |
| 54 | Config::SetDefault("ns3::PointToPointChannel::Delay", StringValue("10ms")); |
| 55 | Config::SetDefault("ns3::DropTailQueue::MaxPackets", StringValue("20")); |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 56 | |
Alexander Afanasyev | 39314e0 | 2013-08-13 10:10:45 -0700 | [diff] [blame] | 57 | // Read optional command-line parameters (e.g., enable visualizer with ./waf --run=<> --visualize |
| 58 | CommandLine cmd; |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 59 | cmd.Parse(argc, argv); |
| 60 | |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 61 | // Creating nodes |
| 62 | NodeContainer nodes; |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 63 | nodes.Create(3); |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 64 | |
| 65 | // Connecting nodes using two links |
| 66 | PointToPointHelper p2p; |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 67 | p2p.Install(nodes.Get(0), nodes.Get(1)); |
| 68 | p2p.Install(nodes.Get(1), nodes.Get(2)); |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 69 | |
Alexander Afanasyev | cf6dc92 | 2012-08-10 16:55:27 -0700 | [diff] [blame] | 70 | // Install NDN stack on all nodes |
| 71 | ndn::StackHelper ndnHelper; |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 72 | ndnHelper.SetDefaultRoutes(true); |
| 73 | ndnHelper.InstallAll(); |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 74 | |
| 75 | // Installing applications |
| 76 | |
| 77 | // Consumer |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 78 | ndn::AppHelper consumerHelper("ns3::ndn::ConsumerCbr"); |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 79 | // Consumer will request /prefix/0, /prefix/1, ... |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 80 | consumerHelper.SetPrefix("/prefix"); |
| 81 | consumerHelper.SetAttribute("Frequency", StringValue("10")); // 10 interests a second |
| 82 | consumerHelper.Install(nodes.Get(0)); // first node |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 83 | |
| 84 | // Producer |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 85 | ndn::AppHelper producerHelper("ns3::ndn::Producer"); |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 86 | // Producer will reply to all requests starting with /prefix |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 87 | producerHelper.SetPrefix("/prefix"); |
| 88 | producerHelper.SetAttribute("PayloadSize", StringValue("1024")); |
| 89 | producerHelper.SetAttribute("Signature", UintegerValue(100)); |
| 90 | producerHelper.SetAttribute("KeyLocator", StringValue("/unique/key/locator")); |
| 91 | producerHelper.Install(nodes.Get(2)); // last node |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 92 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 93 | PcapWriter trace("ndn-simple-trace.pcap"); |
| 94 | Config::ConnectWithoutContext("/NodeList/*/DeviceList/*/$ns3::PointToPointNetDevice/MacTx", |
| 95 | MakeCallback(&PcapWriter::TracePacket, &trace)); |
Alexander Afanasyev | 1ab1aad | 2013-02-28 11:32:21 -0800 | [diff] [blame] | 96 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 97 | Simulator::Stop(Seconds(20.0)); |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 98 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 99 | Simulator::Run(); |
| 100 | Simulator::Destroy(); |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 101 | |
| 102 | return 0; |
| 103 | } |
Spyridon Mastorakis | db8280f | 2014-11-21 20:00:17 -0800 | [diff] [blame] | 104 | |
| 105 | } // namespace ns3 |
| 106 | |
| 107 | int |
| 108 | main(int argc, char* argv[]) |
| 109 | { |
| 110 | return ns3::main(argc, argv); |
| 111 | } |