40b7570e198fe1d6890abade35f08f6b2173cb23
[nepi.git] / examples / ns3 / local_wifi_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 nepi.execution.ec import ExperimentController 
21
22 ec = ExperimentController(exp_id = "ns3-local-wifi-ping")
23
24 # Simulation will executed in the local machine
25 node = ec.register_resource("linux::Node")
26 ec.set(node, "hostname", "localhost")
27
28 # Add a simulation resource
29 simu = ec.register_resource("linux::ns3::Simulation")
30 ec.set(simu, "verbose", True)
31 ec.register_connection(simu, node)
32
33 ## Add a ns-3 node with its protocol stack
34 nsnode1 = ec.register_resource("ns3::Node")
35 ec.register_connection(nsnode1, simu)
36
37 ipv4 = ec.register_resource("ns3::Ipv4L3Protocol")
38 ec.register_connection(nsnode1, ipv4)
39 arp = ec.register_resource("ns3::ArpL3Protocol")
40 ec.register_connection(nsnode1, arp)
41 icmp = ec.register_resource("ns3::Icmpv4L4Protocol")
42 ec.register_connection(nsnode1, icmp)
43
44 # Adding constant mobility to the ns-3 node
45 mobility1 = ec.register_resource("ns3::ConstantPositionMobilityModel") 
46 position1 = "%d:%d:%d" % (0, 0, 0)
47 ec.set(mobility1, "Position", position1)
48 ec.register_connection(nsnode1, mobility1)
49
50 # Add a wifi access point net device to the node
51 dev1 = ec.register_resource("ns3::WifiNetDevice")
52 ec.set(dev1, "ip", "10.0.0.1")
53 ec.set(dev1, "prefix", "30")
54 ec.register_connection(nsnode1, dev1)
55
56 phy1 = ec.register_resource("ns3::YansWifiPhy")
57 ec.set(phy1, "Standard", "WIFI_PHY_STANDARD_80211a")
58 ec.register_connection(dev1, phy1)
59
60 error1 = ec.register_resource("ns3::NistErrorRateModel")
61 ec.register_connection(phy1, error1)
62
63 manager1 = ec.register_resource("ns3::ArfWifiManager")
64 ec.register_connection(dev1, manager1)
65
66 # Mark the node as a wireless access point
67 mac1 = ec.register_resource("ns3::ApWifiMac")
68 ec.set(mac1, "Standard", "WIFI_PHY_STANDARD_80211a")
69 ec.register_connection(dev1, mac1)
70
71 ## Add another ns-3 node with its protocol stack
72 nsnode2 = ec.register_resource("ns3::Node")
73 ec.register_connection(nsnode2, simu)
74
75 ipv4 = ec.register_resource("ns3::Ipv4L3Protocol")
76 ec.register_connection(nsnode2, ipv4)
77 arp = ec.register_resource("ns3::ArpL3Protocol")
78 ec.register_connection(nsnode2, arp)
79 icmp = ec.register_resource("ns3::Icmpv4L4Protocol")
80 ec.register_connection(nsnode2, icmp)
81
82 # Adding constant mobility to the ns-3 node
83 mobility2 = ec.register_resource("ns3::ConstantPositionMobilityModel") 
84 position2 = "%d:%d:%d" % (50, 50, 0)
85 ec.set(mobility2, "Position", position2)
86 ec.register_connection(nsnode2, mobility2)
87
88 # Add a wifi station net device to the node
89 dev2 = ec.register_resource("ns3::WifiNetDevice")
90 ec.set(dev2, "ip", "10.0.0.2")
91 ec.set(dev2, "prefix", "30")
92 ec.register_connection(nsnode2, dev2)
93
94 phy2 = ec.register_resource("ns3::YansWifiPhy")
95 ec.set(phy2, "Standard", "WIFI_PHY_STANDARD_80211a")
96 ec.register_connection(dev2, phy2)
97
98 error2 = ec.register_resource("ns3::NistErrorRateModel")
99 ec.register_connection(phy2, error2)
100
101 manager2 = ec.register_resource("ns3::ArfWifiManager")
102 ec.register_connection(dev2, manager2)
103
104 # Mark the node as a wireless station
105 mac2 = ec.register_resource("ns3::StaWifiMac")
106 ec.set(mac2, "Standard", "WIFI_PHY_STANDARD_80211a")
107 ec.register_connection(dev2, mac2)
108
109 # Add a wifi channel
110 chan = ec.register_resource("ns3::YansWifiChannel")
111 delay = ec.register_resource("ns3::ConstantSpeedPropagationDelayModel")
112 ec.register_connection(chan, delay)
113 loss = ec.register_resource("ns3::LogDistancePropagationLossModel")
114 ec.register_connection(chan, loss)
115 ec.register_connection(chan, phy1)
116 ec.register_connection(chan, phy2)
117
118 ### create pinger
119 ping = ec.register_resource("ns3::V4Ping")
120 ec.set (ping, "Remote", "10.0.0.2")
121 ec.set (ping, "Interval", "1s")
122 ec.set (ping, "Verbose", True)
123 ec.set (ping, "StartTime", "0s")
124 ec.set (ping, "StopTime", "20s")
125 ec.register_connection(ping, nsnode1)
126
127 ec.deploy()
128
129 ec.wait_finished([ping])
130
131 stdout = ec.trace(simu, "stdout") 
132
133 ec.shutdown()
134
135 print "PING OUTPUT", stdout