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