use print() - import print_function - should be fine for both py2 and py3
[nepi.git] / examples / ns3 / local_csma_ping.py
1 #!/usr/bin/env python
2 #
3 #    NEPI, a framework to manage network experiments
4 #    Copyright (C) 2013 INRIA
5 #
6 #    This program is free software: you can redistribute it and/or modify
7 #    it under the terms of the GNU General Public License version 2 as
8 #    published by the Free Software Foundation;
9 #
10 #    This program is distributed in the hope that it will be useful,
11 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
12 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 #    GNU General Public License for more details.
14 #
15 #    You should have received a copy of the GNU General Public License
16 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 #
18 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
19
20 from __future__ import print_function
21
22 from nepi.execution.ec import ExperimentController 
23
24 ec = ExperimentController(exp_id = "ns3-local-csma-ping")
25
26 # Simulation will executed in the local machine
27 node = ec.register_resource("linux::Node")
28 ec.set(node, "hostname", "localhost")
29
30 # Add a simulation resource
31 simu = ec.register_resource("linux::ns3::Simulation")
32 ec.set(simu, "verbose", True)
33 ec.register_connection(simu, node)
34
35 ## Add a ns-3 node with its protocol stack
36 nsnode1 = ec.register_resource("ns3::Node")
37 ec.register_connection(nsnode1, simu)
38
39 ipv4 = ec.register_resource("ns3::Ipv4L3Protocol")
40 ec.register_connection(nsnode1, ipv4)
41 arp = ec.register_resource("ns3::ArpL3Protocol")
42 ec.register_connection(nsnode1, arp)
43 icmp = ec.register_resource("ns3::Icmpv4L4Protocol")
44 ec.register_connection(nsnode1, icmp)
45
46 # Add a csma net device to the node
47 dev1 = ec.register_resource("ns3::CsmaNetDevice")
48 ec.set(dev1, "ip", "10.0.0.1")
49 ec.set(dev1, "prefix", "30")
50 ec.register_connection(nsnode1, dev1)
51 queue1 = ec.register_resource("ns3::DropTailQueue")
52 ec.register_connection(dev1, queue1)
53
54 ## Add another ns-3 node with its protocol stack
55 nsnode2 = ec.register_resource("ns3::Node")
56 ec.register_connection(nsnode2, simu)
57
58 ipv4 = ec.register_resource("ns3::Ipv4L3Protocol")
59 ec.register_connection(nsnode2, ipv4)
60 arp = ec.register_resource("ns3::ArpL3Protocol")
61 ec.register_connection(nsnode2, arp)
62 icmp = ec.register_resource("ns3::Icmpv4L4Protocol")
63 ec.register_connection(nsnode2, icmp)
64
65 # Add a csma net device to the node
66 dev2 = ec.register_resource("ns3::CsmaNetDevice")
67 ec.set(dev2, "ip", "10.0.0.2")
68 ec.set(dev2, "prefix", "30")
69 ec.register_connection(nsnode2, dev2)
70 queue2 = ec.register_resource("ns3::DropTailQueue")
71 ec.register_connection(dev2, queue2)
72
73 # Add a csma channel
74 chan = ec.register_resource("ns3::CsmaChannel")
75 ec.set(chan, "Delay", "0s")
76 ec.register_connection(chan, dev1)
77 ec.register_connection(chan, dev2)
78
79 ### create pinger
80 ping = ec.register_resource("ns3::V4Ping")
81 ec.set (ping, "Remote", "10.0.0.2")
82 ec.set (ping, "Interval", "1s")
83 ec.set (ping, "Verbose", True)
84 ec.set (ping, "StartTime", "0s")
85 ec.set (ping, "StopTime", "20s")
86 ec.register_connection(ping, nsnode1)
87
88 ec.deploy()
89
90 ec.wait_finished([ping])
91
92 stdout = ec.trace(simu, "stdout") 
93
94 ec.shutdown()
95
96 print("PING OUTPUT", stdout)