blob: 295c1af576131fd633bcaa9136b78a9991e30dda [file] [log] [blame]
Alexander Afanasyeve316ab22015-01-03 21:02:08 -08001## -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*-
2#
3# Copyright (c) 2011-2015 Regents of the University of California.
4#
5# This file is part of ndnSIM. See AUTHORS for complete list of ndnSIM authors and
6# contributors.
7#
8# 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.
11#
12# 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.
15#
16# 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.py
21
22from ns.core import *
23from ns.network import *
24from ns.point_to_point import *
25from ns.ndnSIM import *
26
27#
28# This scenario simulates a very simple network topology:
29#
30#
31# +----------+ 1Mbps +--------+ 1Mbps +----------+
32# | consumer | <------------> | router | <------------> | producer |
33# +----------+ 10ms +--------+ 10ms +----------+
34#
35#
36# Consumer requests data from producer with frequency 10 interests per second
37# (interests contain constantly increasing sequence number).
38#
39# For every received interest, producer replies with a data packet, containing
40# 1024 bytes of virtual payload.
41#
42# To run scenario and see what is happening, use the following command:
43#
44# NS_LOG=ndn.Consumer:ndn.Producer ./waf --pyrun=src/ndnSIM/examples/ndn-simple.py
45#
46
47# Set default parameters for PointToPoint links and channels
48Config.SetDefault("ns3::PointToPointNetDevice::DataRate", StringValue("10Mbps"))
49Config.SetDefault("ns3::PointToPointChannel::Delay", StringValue("10ms"))
50Config.SetDefault("ns3::DropTailQueue::MaxPackets", StringValue("20"))
51
52# Read optional command-line parameters (e.g., enable visualizer with ./waf --pyrun=<> --visualize
53import sys; cmd = CommandLine(); cmd.Parse(sys.argv);
54
55# Creating nodes
56nodes = NodeContainer()
57nodes.Create(3)
58
59# Connecting nodes using two links
60p2p = PointToPointHelper()
61p2p.Install(nodes.Get(0), nodes.Get(1))
62p2p.Install(nodes.Get(1), nodes.Get(2))
63
64# Install NDN stack on all nodes
65ndnHelper = ndn.StackHelper()
66ndnHelper.SetDefaultRoutes(True)
67ndnHelper.InstallAll()
68
69# Choosing forwarding strategy
70ndn.StrategyChoiceHelper.InstallAll("/prefix", "/localhost/nfd/strategy/broadcast")
71
72# Installing applications
73
74# Consumer
75consumerHelper = ndn.AppHelper("ns3::ndn::ConsumerCbr")
76# Consumer will request /prefix/0, /prefix/1, ...
77consumerHelper.SetPrefix("/prefix")
78consumerHelper.SetAttribute("Frequency", StringValue("10")) # 10 interests a second
79consumerHelper.Install(nodes.Get(0)) # first node
80
81# Producer
82producerHelper = ndn.AppHelper("ns3::ndn::Producer")
83# Producer will reply to all requests starting with /prefix
84producerHelper.SetPrefix("/prefix")
85producerHelper.SetAttribute("PayloadSize", StringValue("1024"))
86producerHelper.Install(nodes.Get(2)) # last node
87
88Simulator.Stop(Seconds(20.0))
89
90Simulator.Run()
91Simulator.Destroy()