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 | |
| 26 | using namespace ns3; |
| 27 | |
| 28 | class PcapWriter |
| 29 | { |
| 30 | public: |
| 31 | PcapWriter (const std::string &file) |
| 32 | { |
| 33 | PcapHelper helper; |
| 34 | m_pcap = helper.CreateFile (file, std::ios::out, PcapHelper::DLT_PPP); |
| 35 | } |
Alexander Afanasyev | 1ab1aad | 2013-02-28 11:32:21 -0800 | [diff] [blame] | 36 | |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 37 | void |
| 38 | TracePacket (Ptr<const Packet> packet) |
| 39 | { |
| 40 | static PppHeader pppHeader; |
| 41 | pppHeader.SetProtocol (0x0077); |
Alexander Afanasyev | 1ab1aad | 2013-02-28 11:32:21 -0800 | [diff] [blame] | 42 | |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 43 | m_pcap->Write (Simulator::Now (), pppHeader, packet); |
| 44 | } |
| 45 | |
| 46 | private: |
| 47 | Ptr<PcapFileWrapper> m_pcap; |
| 48 | }; |
| 49 | |
| 50 | |
| 51 | int |
| 52 | main (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 Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 59 | // Creating nodes |
| 60 | NodeContainer nodes; |
| 61 | nodes.Create (3); |
| 62 | |
| 63 | // Connecting nodes using two links |
| 64 | PointToPointHelper p2p; |
| 65 | p2p.Install (nodes.Get (0), nodes.Get (1)); |
| 66 | p2p.Install (nodes.Get (1), nodes.Get (2)); |
| 67 | |
Alexander Afanasyev | cf6dc92 | 2012-08-10 16:55:27 -0700 | [diff] [blame] | 68 | // Install NDN stack on all nodes |
| 69 | ndn::StackHelper ndnHelper; |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 70 | ndnHelper.SetDefaultRoutes (true); |
| 71 | ndnHelper.InstallAll (); |
| 72 | |
| 73 | // Installing applications |
| 74 | |
| 75 | // Consumer |
Alexander Afanasyev | cf6dc92 | 2012-08-10 16:55:27 -0700 | [diff] [blame] | 76 | ndn::AppHelper consumerHelper ("ns3::ndn::ConsumerCbr"); |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 77 | // Consumer will request /prefix/0, /prefix/1, ... |
| 78 | consumerHelper.SetPrefix ("/prefix"); |
| 79 | consumerHelper.SetAttribute ("Frequency", StringValue ("10")); // 10 interests a second |
| 80 | consumerHelper.Install (nodes.Get (0)); // first node |
| 81 | |
| 82 | // Producer |
Alexander Afanasyev | cf6dc92 | 2012-08-10 16:55:27 -0700 | [diff] [blame] | 83 | ndn::AppHelper producerHelper ("ns3::ndn::Producer"); |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 84 | // Producer will reply to all requests starting with /prefix |
| 85 | producerHelper.SetPrefix ("/prefix"); |
| 86 | producerHelper.SetAttribute ("PayloadSize", StringValue("1024")); |
| 87 | producerHelper.Install (nodes.Get (2)); // last node |
| 88 | |
| 89 | PcapWriter trace ("ndn-simple-trace.pcap"); |
Alexander Afanasyev | cf6dc92 | 2012-08-10 16:55:27 -0700 | [diff] [blame] | 90 | Config::ConnectWithoutContext ("/NodeList/*/$ns3::ndn::L3Protocol/FaceList/*/NdnTx", |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 91 | MakeCallback (&PcapWriter::TracePacket, &trace)); |
Alexander Afanasyev | 1ab1aad | 2013-02-28 11:32:21 -0800 | [diff] [blame] | 92 | |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 93 | Simulator::Stop (Seconds (20.0)); |
| 94 | |
| 95 | Simulator::Run (); |
| 96 | Simulator::Destroy (); |
| 97 | |
| 98 | return 0; |
| 99 | } |
| 100 | |