systematic use of context managers for dealing with files instead of open()/close...
[nepi.git] / examples / omf / testing / nepi_omf6_plexus_ping_with_traces.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 #         Julien Tribino <julien.tribino@inria.fr>
20
21 from nepi.execution.resource import ResourceFactory, ResourceAction, ResourceState
22 from nepi.execution.ec import ExperimentController
23
24 # Create the EC
25 ec = ExperimentController()
26
27 # Create and Configure the Nodes
28
29 node1 = ec.register_resource("omf::Node")
30 ec.set(node1, 'hostname', 'wlab12')
31 ec.set(node1, 'xmppServer', "xmpp-plexus.onelab.eu")
32 ec.set(node1, 'xmppUser', "nepi")
33 ec.set(node1, 'xmppPort', "5222")
34 ec.set(node1, 'xmppPassword', "1234")
35
36 iface1 = ec.register_resource("omf::WifiInterface")
37 ec.set(iface1, 'name', 'wlan0')
38 ec.set(iface1, 'mode', "adhoc")
39 ec.set(iface1, 'hw_mode', "g")
40 ec.set(iface1, 'essid', "ping")
41 ec.set(iface1, 'ip', "192.168.0.12/24")
42
43 node2 = ec.register_resource("omf::Node")
44 ec.set(node2, 'hostname', 'wlab49')
45 ec.set(node2, 'xmppServer', "xmpp-plexus.onelab.eu")
46 ec.set(node2, 'xmppUser', "nepi")
47 ec.set(node2, 'xmppPort', "5222")
48 ec.set(node2, 'xmppPassword', "1234")
49
50 iface2 = ec.register_resource("omf::WifiInterface")
51 ec.set(iface2, 'name', 'wlan0')
52 ec.set(iface2, 'mode', "adhoc")
53 ec.set(iface2, 'hw_mode', "g")
54 ec.set(iface2, 'essid', "ping")
55 ec.set(iface2, 'ip', "192.168.0.49/24")
56
57 chan = ec.register_resource("omf::Channel")
58 ec.set(chan, 'channel', "6")
59
60 # Create and Configure the Application
61 app1 = ec.register_resource("omf::Application")
62 ec.set(app1, 'command', '/bin/ping -c5 192.168.0.49')
63 ec.set(app1, 'env', "")
64
65 app2 = ec.register_resource("omf::Application")
66 ec.set(app2, 'command', '/bin/ping -c5 192.168.0.12')
67 ec.set(app2, 'env', "")
68
69
70 # Connection
71 ec.register_connection(iface1, node1)
72 ec.register_connection(iface2, node2)
73 ec.register_connection(iface1, chan)
74 ec.register_connection(iface2, chan)
75 ec.register_connection(app1, node1)
76 ec.register_connection(app2, node2)
77
78 ec.register_condition([app2], ResourceAction.START, app1, ResourceState.STARTED , "2s")
79 ec.register_condition([app1,app2], ResourceAction.STOP, app2, ResourceState.STARTED , "10s")
80
81
82 # Deploy
83 ec.deploy()
84
85 ec.wait_finished([app1,app2])
86
87 stdout_1 = ec.trace(app1, "stdout")
88 stdout_2 = ec.trace(app2, "stdout")
89
90 # Choose a directory to store the traces, by default
91 # It it the folder ehere you run Nepi.
92  
93 with open("app1.txt", "w") as f:
94     f.write(stdout_1)
95
96 with open("app2.txt", "w") as g:
97     g.write(stdout_2)
98
99 # Stop Experiment
100 ec.shutdown()
101