Changing reschedule_delay internals
[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
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         use_dlm = Attribute("useDlmLoader",
64                 "Use ns3::DlmLoaderFactory as library loader",
65                 type = Types.Bool,
66                 flags = Flags.Design)
67         
68         starttime = Attribute("StartTime",
69             "Time at which the application will start",
70             default = "+0.0ns",  
71             flags = Flags.Reserved | Flags.Construct)
72
73         stoptime = Attribute("StopTime",
74             "Time at which the application will stop",
75             default = "+0.0ns",  
76             flags = Flags.Reserved | Flags.Construct)
77
78         cls._register_attribute(binary)
79         cls._register_attribute(stack_size)
80         cls._register_attribute(arguments)
81         cls._register_attribute(environment)
82         cls._register_attribute(use_dlm)
83         cls._register_attribute(stoptime)
84         cls._register_attribute(starttime)
85
86     @property
87     def node(self):
88         from nepi.resources.ns3.ns3node import NS3BaseNode
89         nodes = self.get_connected(NS3BaseNode.get_rtype())
90
91         if not nodes: 
92             msg = "DceApplication not connected to node"
93             self.error(msg)
94             raise RuntimeError, msg
95
96         return nodes[0]
97
98     @property
99     def dce_manager_helper_uuid(self):
100         if not self._dce_manager_helper_uuid:
101             self._dce_manager_helper_uuid = self.simulation.create(
102                     "DceManagerHelper")
103
104             if self.get("useDlmLoader"):
105                 self.simulation.invoke(
106                     self._dce_manager_helper_uuid, "SetLoader", 
107                     "ns3::DlmLoaderFactory")
108
109         return self._dce_manager_helper_uuid
110
111     @property
112     def dce_application_helper_uuid(self):
113         if not self._dce_application_helper_uuid:
114             self._dce_application_helper_uuid = self.simulation.create("DceApplicationHelper")
115         return self._dce_application_helper_uuid
116
117     def _instantiate_object(self):
118         pass
119
120     def _connect_object(self):
121         node = self.node
122         if node.uuid not in self.connected:
123             self._connected.add(node.uuid)
124
125             # Preventing concurrent access to the DceApplicationHelper
126             # from different DceApplication RMs
127             with self.dce_application_lock:
128                 self.simulation.invoke(
129                         self.dce_application_helper_uuid, 
130                         "ResetArguments") 
131
132                 self.simulation.invoke(
133                         self.dce_application_helper_uuid, 
134                         "ResetEnvironment") 
135
136                 self.simulation.invoke(
137                         self.dce_application_helper_uuid, 
138                         "SetBinary", self.get("binary")) 
139
140                 self.simulation.invoke(
141                         self.dce_application_helper_uuid, 
142                         "SetStackSize", self.get("stackSize")) 
143
144                 arguments = self.get("arguments")
145                 if arguments:
146                     for arg in map(str.strip, arguments.split(";")):
147                         self.simulation.invoke(
148                                 self.dce_application_helper_uuid, 
149                             "AddArgument", arg)
150
151                 environment = self.get("environment")
152                 if environment:
153                     for env in map(str.strip, environment.split(";")):
154                         key, val = env.split("=")
155                         self.simulation.invoke(
156                                 self.dce_application_helper_uuid, 
157                             "AddEnvironment", key, val)
158
159                 apps_uuid = self.simulation.invoke(
160                         self.dce_application_helper_uuid, 
161                         "InstallInNode", self.node.uuid)
162
163                 """
164                 container_uuid = self.simulation.create("NodeContainer")
165                 self.simulation.invoke(container_uuid, "Add", self.node.uuid)
166                 apps_uuid = self.simulation.invoke(
167                         self.dce_application_helper_uuid, 
168                         "Install", container_uuid)
169                 """
170
171             self._uuid = self.simulation.invoke(apps_uuid, "Get", 0)
172
173             if self.has_changed("StartTime"):
174                 self.simulation.ns3_set(self.uuid, "StartTime", self.get("StartTime"))
175
176             if self.has_changed("StopTime"):
177                 self.simulation.ns3_set(self.uuid, "StopTime", self.get("StopTime"))
178
179     def do_stop(self):
180         if self.state == ResourceState.STARTED:
181             # No need to do anything, simulation.Destroy() will stop every object
182             self.info("Stopping command '%s'" % command)
183             self.simulation.invoke(self.uuid, "Stop")
184             self.set_stopped()
185
186     def do_start(self):
187         if self.simulation.state < ResourceState.STARTED:
188             self.debug("---- RESCHEDULING START ----" )
189             self.ec.schedule(self.reschedule_delay, self.start)
190         else:
191             self._configure_traces()
192             super(NS3BaseApplication, self).do_start()
193             self._start_time = self.simulation.start_time
194
195     def _configure_traces(self):
196         # Waiting until dce application is actually started
197         is_running = False
198         for i in xrange(200):
199             is_running = self.simulation.invoke(self.uuid, "isAppRunning")
200             is_finished = self.simulation.invoke(SIMULATOR_UUID, "isFinished")
201         
202             if is_running or is_finished:
203                 break
204             else:
205                 time.sleep(1)
206         else:
207             if not is_running:
208                 msg = " Application did not start"
209                 self.error(msg)
210                 raise RuntimeError
211
212         # Using lock to prevent concurrent access to the DceApplicationHelper
213         # from different DceApplication RMs
214         with self.dce_application_lock:
215             pid = self.simulation.invoke(self.dce_application_helper_uuid, 
216                     "GetPid", self.uuid)
217             
218         node_id = self.simulation.invoke(self.node.uuid, "GetId")
219         self._trace_filename["stdout"] = "files-%s/var/log/%s/stdout" % (node_id, pid)
220         self._trace_filename["stderr"] = "files-%s/var/log/%s/stderr" % (node_id, pid)
221         self._trace_filename["status"] = "files-%s/var/log/%s/status" % (node_id, pid)
222         self._trace_filename["cmdline"] = "files-%s/var/log/%s/cmdline" % (node_id, pid)
223
224