use print() - import print_function - should be fine for both py2 and py3
[nepi.git] / examples / dce / custom_local_p2p_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 def add_ns3_node(ec, simu):
25     node = ec.register_resource("ns3::Node")
26     ec.register_connection(node, simu)
27
28     ipv4 = ec.register_resource("ns3::Ipv4L3Protocol")
29     ec.register_connection(node, ipv4)
30
31     arp = ec.register_resource("ns3::ArpL3Protocol")
32     ec.register_connection(node, arp)
33     
34     icmp = ec.register_resource("ns3::Icmpv4L4Protocol")
35     ec.register_connection(node, icmp)
36
37     udp = ec.register_resource("ns3::UdpL4Protocol")
38     ec.register_connection(node, udp)
39
40     tcp = ec.register_resource("ns3::TcpL4Protocol")
41     ec.register_connection(node, tcp)
42
43     return node
44
45 def add_device(ec, node, ip,  prefix):
46     dev = ec.register_resource("ns3::PointToPointNetDevice")
47     ec.set(dev, "ip", ip)
48     ec.set(dev, "prefix", prefix)
49     ec.register_connection(node, dev)
50
51     queue = ec.register_resource("ns3::DropTailQueue")
52     ec.register_connection(dev, queue)
53
54     return dev
55
56 ec = ExperimentController(exp_id = "dce-custom-p2p-ping")
57
58 node = ec.register_resource("linux::Node")
59 ec.set(node, "hostname", "localhost")
60 ec.set(node, "cleanProcesses", True)
61
62 simu = ec.register_resource("linux::ns3::Simulation")
63 ec.set(simu, "verbose", True)
64 ec.register_connection(simu, node)
65
66 nsnode1 = add_ns3_node(ec, simu)
67 dev1 = add_device(ec, nsnode1, "10.0.0.1", "30")
68 ec.set(dev1, "DataRate", "5Mbps")
69
70 nsnode2 = add_ns3_node(ec, simu)
71 dev2 = add_device(ec, nsnode2, "10.0.0.2", "30")
72 ec.set(dev2, "DataRate", "5Mbps")
73
74 # Create channel
75 chan = ec.register_resource("ns3::PointToPointChannel")
76 ec.set(chan, "Delay", "2ms")
77
78 ec.register_connection(chan, dev1)
79 ec.register_connection(chan, dev2)
80
81 ### create applications
82 ping = ec.register_resource("linux::ns3::dce::Application")
83 ec.set (ping, "sources", "http://www.skbuff.net/iputils/iputils-s20101006.tar.bz2")
84 ec.set (ping, "build", "tar xvjf ${SRC}/iputils-s20101006.tar.bz2 && "
85         "cd iputils-s20101006/ && "
86         "sed -i 's/CFLAGS=/CFLAGS+=/g' Makefile && "
87         "make CFLAGS=-fPIC LDFLAGS='-pie -rdynamic' ping && "
88         "cp ping ${BIN_DCE} && cd - ")
89 ec.set (ping, "binary", "ping")
90 ec.set (ping, "stackSize", 1<<20)
91 ec.set (ping, "arguments", "-c 10;-s 1000;10.0.0.2")
92 ec.set (ping, "StartTime", "1s")
93 ec.set (ping, "StopTime", "20s")
94 ec.register_connection(ping, nsnode1)
95
96 ec.deploy()
97
98 ec.wait_finished([ping])
99
100 stdout = ec.trace(ping, "stdout") 
101
102 ec.shutdown()
103
104 print("PING OUTPUT", stdout)
105