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