Alexander Afanasyev | fff8498 | 2013-05-30 09:19:46 -0700 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2013 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-csma.cc |
| 21 | #include "ns3/core-module.h" |
| 22 | #include "ns3/network-module.h" |
| 23 | #include "ns3/csma-module.h" |
| 24 | #include "ns3/ndnSIM-module.h" |
| 25 | |
| 26 | using namespace ns3; |
| 27 | |
| 28 | /** |
| 29 | * This scenario simulates a very simple network topology: |
| 30 | * |
| 31 | * CSMA bus (1Mbps, 10ms) |
| 32 | * +--------------------------+--------------------------+ |
| 33 | * | | | |
| 34 | * |
| 35 | * +----------+ +--------+ +----------+ |
| 36 | * | consumer | | router | | producer | |
| 37 | * +----------+ +--------+ +----------+ |
| 38 | * |
| 39 | * |
| 40 | * Consumer requests data from producer with frequency 10 interests per second |
| 41 | * (interests contain constantly increasing sequence number). |
| 42 | * |
| 43 | * For every received interest, producer replies with a data packet, containing |
| 44 | * 1024 bytes of virtual payload. |
| 45 | * |
| 46 | * To run scenario and see what is happening, use the following command: |
| 47 | * |
| 48 | * NS_LOG=ndn.Consumer:ndn.Producer ./waf --run=ndn-csma |
| 49 | */ |
| 50 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 51 | int |
| 52 | main(int argc, char* argv[]) |
Alexander Afanasyev | fff8498 | 2013-05-30 09:19:46 -0700 | [diff] [blame] | 53 | { |
| 54 | // setting default parameters for PointToPoint links and channels |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 55 | Config::SetDefault("ns3::CsmaChannel::DataRate", StringValue("1Mbps")); |
| 56 | Config::SetDefault("ns3::CsmaChannel::Delay", StringValue("10ms")); |
| 57 | Config::SetDefault("ns3::DropTailQueue::MaxPackets", StringValue("20")); |
Alexander Afanasyev | fff8498 | 2013-05-30 09:19:46 -0700 | [diff] [blame] | 58 | |
| 59 | // Read optional command-line parameters (e.g., enable visualizer with ./waf --run=<> --visualize |
| 60 | CommandLine cmd; |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 61 | cmd.Parse(argc, argv); |
Alexander Afanasyev | fff8498 | 2013-05-30 09:19:46 -0700 | [diff] [blame] | 62 | |
| 63 | // Creating nodes |
| 64 | NodeContainer nodes; |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 65 | nodes.Create(3); |
Alexander Afanasyev | fff8498 | 2013-05-30 09:19:46 -0700 | [diff] [blame] | 66 | |
| 67 | // Connecting nodes using two links |
| 68 | CsmaHelper csma; |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 69 | csma.Install(nodes); |
Alexander Afanasyev | fff8498 | 2013-05-30 09:19:46 -0700 | [diff] [blame] | 70 | |
| 71 | // Install NDN stack on all nodes |
| 72 | ndn::StackHelper ndnHelper; |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 73 | ndnHelper.SetDefaultRoutes(true); |
| 74 | ndnHelper.InstallAll(); |
Alexander Afanasyev | fff8498 | 2013-05-30 09:19:46 -0700 | [diff] [blame] | 75 | |
| 76 | // Installing applications |
| 77 | |
| 78 | // Consumer |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 79 | ndn::AppHelper consumerHelper("ns3::ndn::ConsumerCbr"); |
Alexander Afanasyev | fff8498 | 2013-05-30 09:19:46 -0700 | [diff] [blame] | 80 | // Consumer will request /prefix/0, /prefix/1, ... |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 81 | consumerHelper.SetPrefix("/prefix"); |
| 82 | consumerHelper.SetAttribute("Frequency", StringValue("10")); // 10 interests a second |
| 83 | consumerHelper.Install(nodes.Get(0)); // first node |
Alexander Afanasyev | fff8498 | 2013-05-30 09:19:46 -0700 | [diff] [blame] | 84 | |
| 85 | // Producer |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 86 | ndn::AppHelper producerHelper("ns3::ndn::Producer"); |
Alexander Afanasyev | fff8498 | 2013-05-30 09:19:46 -0700 | [diff] [blame] | 87 | // Producer will reply to all requests starting with /prefix |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 88 | producerHelper.SetPrefix("/prefix"); |
| 89 | producerHelper.SetAttribute("PayloadSize", StringValue("1024")); |
| 90 | producerHelper.Install(nodes.Get(2)); // last node |
Alexander Afanasyev | fff8498 | 2013-05-30 09:19:46 -0700 | [diff] [blame] | 91 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 92 | Simulator::Stop(Seconds(20.0)); |
Alexander Afanasyev | fff8498 | 2013-05-30 09:19:46 -0700 | [diff] [blame] | 93 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 94 | Simulator::Run(); |
| 95 | Simulator::Destroy(); |
Alexander Afanasyev | fff8498 | 2013-05-30 09:19:46 -0700 | [diff] [blame] | 96 | |
| 97 | return 0; |
| 98 | } |