Nitos OMF ping examples
[nepi.git] / src / nepi / resources / ns3 / ns3dceapplication.py
1 #
2 #    NEPI, a framework to manage network experiments
3 #    Copyright (C) 2014 INRIA
4 #
5 #    This program is free software: you can redistribute it and/or modify
6 #    it under the terms of the GNU General Public License as published by
7 #    the Free Software Foundation, either version 3 of the License, or
8 #    (at your option) any later version.
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.attribute import Attribute, Flags, Types
21 from nepi.execution.resource import clsinit_copy, ResourceState, reschedule_delay
22 from nepi.resources.ns3.ns3application import NS3BaseApplication
23
24 from nepi.resources.ns3.ns3wrapper import SIMULATOR_UUID
25
26 import os
27 import time
28 import threading
29         
30 @clsinit_copy
31 class NS3BaseDceApplication(NS3BaseApplication):
32     _rtype = "abstract::ns3::DceApplication"
33
34     # Lock used to synchronize usage of DceManagerHelper 
35     dce_manager_lock = threading.Lock()
36     # Lock used to synchronize usage of DceApplicationHelper
37     dce_application_lock = threading.Lock()
38    
39     _dce_manager_helper_uuid = None
40     _dce_application_helper_uuid = None
41
42     @classmethod
43     def _register_attributes(cls):
44         binary = Attribute("binary", 
45                 "Name of binary to execute",
46                 flags = Flags.Design)
47
48         stack_size = Attribute("stackSize", 
49                 "Stack Size for DCE",
50                 type = Types.Integer,
51                 default = 1<<20,                
52                 flags = Flags.Design)
53
54         arguments = Attribute("arguments", 
55                 "Semi-colon separated list of arguments for the application",
56                 flags = Flags.Design)
57
58         environment = Attribute("environment", 
59                 "Semi-colon separated list of 'key=value' pairs to set as "
60                 "DCE environment variables.",
61                 flags = Flags.Design)
62
63         """
64         use_dlm = Attribute("useDlmLoader",
65                 "Use ns3::DlmLoaderFactory as library loader",
66                 type = Types.Bool,
67                 flags = Flags.Design)
68         """
69         
70         starttime = Attribute("StartTime",
71             "Time at which the application will start",
72             default = "+0.0ns",  
73             flags = Flags.Reserved | Flags.Construct)
74
75         stoptime = Attribute("StopTime",
76             "Time at which the application will stop",
77             default = "+0.0ns",  
78             flags = Flags.Reserved | Flags.Construct)
79
80         cls._register_attribute(binary)
81         cls._register_attribute(stack_size)
82         cls._register_attribute(arguments)
83         cls._register_attribute(environment)
84         """
85         cls._register_attribute(use_dlm)
86         """
87         cls._register_attribute(stoptime)
88         cls._register_attribute(starttime)
89
90     @property
91     def node(self):
92         from nepi.resources.ns3.ns3node import NS3BaseNode
93         nodes = self.get_connected(NS3BaseNode.get_rtype())
94
95         if not nodes: 
96             msg = "DceApplication not connected to node"
97             self.error(msg)
98             raise RuntimeError, msg
99
100         return nodes[0]
101
102     @property
103     def dce_manager_helper_uuid(self):
104         if not self._dce_manager_helper_uuid:
105             self._dce_manager_helper_uuid = self.simulation.create(
106                     "DceManagerHelper")
107             """
108             if self.get("useDlmLoader"):
109                 self.simulation.invoke(
110                     self._dce_manager_helper_uuid, "SetLoader", 
111                     "ns3::DlmLoaderFactory")
112             """
113         return self._dce_manager_helper_uuid
114
115     @property
116     def dce_application_helper_uuid(self):
117         if not self._dce_application_helper_uuid:
118             self._dce_application_helper_uuid = self.simulation.create("DceApplicationHelper")
119         return self._dce_application_helper_uuid
120
121     def _instantiate_object(self):
122         pass
123
124     def _connect_object(self):
125         node = self.node
126         if node.uuid not in self.connected:
127             self._connected.add(node.uuid)
128
129             # Preventing concurrent access to the DceApplicationHelper
130             # from different DceApplication RMs
131             with self.dce_application_lock:
132                 self.simulation.invoke(
133                         self.dce_application_helper_uuid, 
134                         "ResetArguments") 
135
136                 self.simulation.invoke(
137                         self.dce_application_helper_uuid, 
138                         "ResetEnvironment") 
139
140                 self.simulation.invoke(
141                         self.dce_application_helper_uuid, 
142                         "SetBinary", self.get("binary")) 
143
144                 self.simulation.invoke(
145                         self.dce_application_helper_uuid, 
146                         "SetStackSize", self.get("stackSize")) 
147
148                 arguments = self.get("arguments")
149                 if arguments:
150                     for arg in map(str.strip, arguments.split(";")):
151                         self.simulation.invoke(
152                                 self.dce_application_helper_uuid, 
153                             "AddArgument", arg)
154
155                 environment = self.get("environment")
156                 if environment:
157                     for env in map(str.strip, environment.split(";")):
158                         key, val = env.split("=")
159                         self.simulation.invoke(
160                                 self.dce_application_helper_uuid, 
161                             "AddEnvironment", key, val)
162
163                 apps_uuid = self.simulation.invoke(
164                         self.dce_application_helper_uuid, 
165                         "InstallInNode", self.node.uuid)
166
167                 """
168                 container_uuid = self.simulation.create("NodeContainer")
169                 self.simulation.invoke(container_uuid, "Add", self.node.uuid)
170                 apps_uuid = self.simulation.invoke(
171                         self.dce_application_helper_uuid, 
172                         "Install", container_uuid)
173                 """
174
175             self._uuid = self.simulation.invoke(apps_uuid, "Get", 0)
176
177             if self.has_changed("StartTime"):
178                 self.simulation.ns3_set(self.uuid, "StartTime", self.get("StartTime"))
179
180             if self.has_changed("StopTime"):
181                 self.simulation.ns3_set(self.uuid, "StopTime", self.get("StopTime"))
182
183     def do_stop(self):
184         if self.state == ResourceState.STARTED:
185             # No need to do anything, simulation.Destroy() will stop every object
186             self.info("Stopping command '%s'" % command)
187             self.simulation.invoke(self.uuid, "Stop")
188             self.set_stopped()
189
190     def do_start(self):
191         if self.simulation.state < ResourceState.STARTED:
192             self.debug("---- RESCHEDULING START ----" )
193             self.ec.schedule(reschedule_delay, self.start)
194         else:
195             self._configure_traces()
196             super(NS3BaseApplication, self).do_start()
197             self._start_time = self.simulation.start_time
198
199     def _configure_traces(self):
200         # Waiting until dce application is actually started
201         is_running = False
202         for i in xrange(200):
203             is_running = self.simulation.invoke(self.uuid, "isAppRunning")
204             is_finished = self.simulation.invoke(SIMULATOR_UUID, "isFinished")
205         
206             if is_running or is_finished:
207                 break
208             else:
209                 time.sleep(1)
210         else:
211             if not is_running:
212                 msg = " Application did not start"
213                 self.error(msg)
214                 raise RuntimeError
215
216         # Using lock to prevent concurrent access to the DceApplicationHelper
217         # from different DceApplication RMs
218         with self.dce_application_lock:
219             pid = self.simulation.invoke(self.dce_application_helper_uuid, 
220                     "GetPid", self.uuid)
221             
222         node_id = self.simulation.invoke(self.node.uuid, "GetId")
223         self._trace_filename["stdout"] = "files-%s/var/log/%s/stdout" % (node_id, pid)
224         self._trace_filename["stderr"] = "files-%s/var/log/%s/stderr" % (node_id, pid)
225         self._trace_filename["status"] = "files-%s/var/log/%s/status" % (node_id, pid)
226         self._trace_filename["cmdline"] = "files-%s/var/log/%s/cmdline" % (node_id, pid)
227
228