use print() - import print_function - should be fine for both py2 and py3
[nepi.git] / examples / dce / custom_local_p2p_ccn.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 import os
25
26 def add_ns3_node(ec, simu):
27     node = ec.register_resource("ns3::Node")
28     ec.register_connection(node, simu)
29
30     ipv4 = ec.register_resource("ns3::Ipv4L3Protocol")
31     ec.register_connection(node, ipv4)
32
33     arp = ec.register_resource("ns3::ArpL3Protocol")
34     ec.register_connection(node, arp)
35     
36     icmp = ec.register_resource("ns3::Icmpv4L4Protocol")
37     ec.register_connection(node, icmp)
38
39     udp = ec.register_resource("ns3::UdpL4Protocol")
40     ec.register_connection(node, udp)
41
42     tcp = ec.register_resource("ns3::TcpL4Protocol")
43     ec.register_connection(node, tcp)
44
45     return node
46
47 def add_device(ec, node, ip,  prefix):
48     dev = ec.register_resource("ns3::PointToPointNetDevice")
49     ec.set(dev, "ip", ip)
50     ec.set(dev, "prefix", prefix)
51     ec.register_connection(node, dev)
52
53     queue = ec.register_resource("ns3::DropTailQueue")
54     ec.register_connection(dev, queue)
55
56     return dev
57
58 ec = ExperimentController(exp_id = "dce-custom-ccn")
59
60 node = ec.register_resource("linux::Node")
61 ec.set(node, "hostname", "localhost")
62 ec.set(node, "cleanProcesses", True)
63
64 simu = ec.register_resource("linux::ns3::Simulation")
65 ec.set(simu, "verbose", True)
66 ec.register_connection(simu, node)
67
68 nsnode1 = add_ns3_node(ec, simu)
69 dev1 = add_device(ec, nsnode1, "10.0.0.1", "30")
70 ec.set(dev1, "DataRate", "5Mbps")
71
72 nsnode2 = add_ns3_node(ec, simu)
73 dev2 = add_device(ec, nsnode2, "10.0.0.2", "30")
74 ec.set(dev2, "DataRate", "5Mbps")
75
76 # Create channel
77 chan = ec.register_resource("ns3::PointToPointChannel")
78 ec.set(chan, "Delay", "2ms")
79
80 ec.register_connection(chan, dev1)
81 ec.register_connection(chan, dev2)
82
83 ### create applications
84
85 # Add a LinuxCCNDceApplication to ns-3 node 1 to install custom ccnx sources
86 # and run a CCNx daemon
87 ccnd1 = ec.register_resource("linux::ns3::dce::CCNApplication")
88 # NOTE THAT INSTALLATION MIGHT FAIL IF openjdk-6-jdk is not installed
89 ec.set(ccnd1, "depends", "libpcap0.8-dev openjdk-6-jdk ant1.8 autoconf "
90     "libssl-dev libexpat-dev libpcap-dev libecryptfs0 libxml2-utils auto"
91     "make gawk gcc g++ git-core pkg-config libpcre3-dev openjdk-6-jre-lib")
92 ec.set (ccnd1, "sources", "http://www.ccnx.org/releases/ccnx-0.7.2.tar.gz")
93 ec.set (ccnd1, "build", "tar zxf ${SRC}/ccnx-0.7.2.tar.gz && "
94         "cd ccnx-0.7.2 && "
95         " INSTALL_BASE=${BIN_DCE}/.. ./configure && "
96         " make MORE_LDLIBS='-pie -rdynamic' && "
97         " make install && "
98         " cp ${BIN_DCE}/../bin/ccn* ${BIN_DCE} && "
99         " cd -")
100 ec.set (ccnd1, "binary", "ccnd")
101 ec.set (ccnd1, "stackSize", 1<<20)
102 ec.set (ccnd1, "environment", "CCND_CAP=50000; CCND_DEBUG=7")
103 ec.set (ccnd1, "StartTime", "1s")
104 ec.set (ccnd1, "StopTime", "20s")
105 ec.register_connection(ccnd1, nsnode1)
106
107 # Add a CCN repository to ns-3 node1 manually configuring all
108 # parameters
109 repofile = os.path.join(
110     os.path.dirname(os.path.realpath(__file__)), 
111     "..", "..", "test", "resources", "linux", 
112     "ns3", "ccn", "repoFile1")
113
114 ccnr = ec.register_resource("linux::ns3::dce::CCNApplication")
115 ec.set (ccnr, "binary", "ccnr")
116 ec.set (ccnr, "environment", "CCNR_DIRECTORY=/REPO/")
117 ec.set (ccnr, "files", "%s=/REPO/repoFile1" % repofile) 
118 ec.set (ccnr, "stackSize", 1<<20)
119 ec.set (ccnr, "StartTime", "2s")
120 ec.set (ccnr, "StopTime", "120s")
121 ec.register_connection(ccnr, nsnode1)
122
123 # Add a LinuxCCNDceApplication to ns-3 node 1 to run a CCN
124 # daemon. Note that the CCNx sources and build instructions 
125 # do not need to be specified again (NEPI will take the 
126 # instructions from the first definition).
127 ccnd2 = ec.register_resource("linux::ns3::dce::CCNApplication")
128 ec.set (ccnd2, "binary", "ccnd")
129 ec.set (ccnd2, "stackSize", 1<<20)
130 ec.set (ccnd2, "environment", "CCND_CAP=50000; CCND_DEBUG=7")
131 ec.set (ccnd2, "StartTime", "1s")
132 ec.set (ccnd2, "StopTime", "120s")
133 ec.register_connection(ccnd2, nsnode2)
134
135 # Add DCE application to configure peer CCN faces between
136 # nodes
137 ccndc1 = ec.register_resource("linux::ns3::dce::CCNApplication")
138 ec.set (ccndc1, "binary", "ccndc")
139 ec.set (ccndc1, "arguments", "-v;add;ccnx:/;udp;10.0.0.2")
140 ec.set (ccndc1, "stackSize", 1<<20)
141 ec.set (ccndc1, "StartTime", "2s")
142 ec.set (ccndc1, "StopTime", "120s")
143 ec.register_connection(ccndc1, nsnode1)
144
145 ccndc2 = ec.register_resource("linux::ns3::dce::CCNApplication")
146 ec.set (ccndc2, "binary", "ccndc")
147 ec.set (ccndc2, "arguments", "-v;add;ccnx:/;udp;10.0.0.1")
148 ec.set (ccndc2, "stackSize", 1<<20)
149 ec.set (ccndc2, "StartTime", "2s")
150 ec.set (ccndc2, "StopTime", "120s")
151 ec.register_connection(ccndc2, nsnode2)
152
153 # Add a DCE application to perform a ccncat and retrieve content
154 ccncat = ec.register_resource("linux::ns3::dce::CCNApplication")
155 ec.set (ccncat, "binary", "ccncat")
156 ec.set (ccncat, "arguments", "ccnx:/test/bunny.ts")
157 ec.set (ccncat, "stdinFile", "")
158 ec.set (ccncat, "stackSize", 1<<20)
159 ec.set (ccncat, "StartTime", "4s")
160 ec.set (ccncat, "StopTime", "120s")
161 ec.register_connection(ccncat, nsnode2)
162
163 ec.deploy()
164
165 ec.wait_finished([ccncat])
166
167 stdout = ec.trace(ccncat, "stdout")
168 # convert from bytes to MB
169 print("%0.2f MBytes received" % (len(stdout) / 1024.0 / 1024.0 ))
170
171 ec.shutdown()
172