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