Fixing static lock messup with Ns3 DCE Manager
[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     @classmethod
36     def _register_attributes(cls):
37         binary = Attribute("binary", 
38                 "Name of binary to execute",
39                 flags = Flags.Design)
40
41         stack_size = Attribute("stackSize", 
42                 "Stack Size for DCE",
43                 type = Types.Integer,
44                 default = 1<<20,                
45                 flags = Flags.Design)
46
47         arguments = Attribute("arguments", 
48                 "Semi-colon separated list of arguments for the application",
49                 flags = Flags.Design)
50
51         environment = Attribute("environment", 
52                 "Semi-colon separated list of 'key=value' pairs to set as "
53                 "DCE environment variables.",
54                 flags = Flags.Design)
55
56         use_dlm = Attribute("useDlmLoader",
57                 "Use ns3::DlmLoaderFactory as library loader",
58                 type = Types.Bool,
59                 flags = Flags.Design)
60         
61         starttime = Attribute("StartTime",
62             "Time at which the application will start",
63             default = "+0.0ns",  
64             flags = Flags.Reserved | Flags.Construct)
65
66         stoptime = Attribute("StopTime",
67             "Time at which the application will stop",
68             default = "+0.0ns",  
69             flags = Flags.Reserved | Flags.Construct)
70
71         cls._register_attribute(binary)
72         cls._register_attribute(stack_size)
73         cls._register_attribute(arguments)
74         cls._register_attribute(environment)
75         cls._register_attribute(use_dlm)
76         cls._register_attribute(stoptime)
77         cls._register_attribute(starttime)
78
79     def __init__(self, ec, guid):
80         super(NS3BaseDceApplication, self).__init__(ec, guid)
81         self._pid = None
82
83     @property
84     def pid(self):
85         return self._pid
86
87     def _instantiate_object(self):
88         pass
89
90     def _connect_object(self):
91         node = self.node
92         if node.uuid not in self.connected:
93             self._connected.add(node.uuid)
94
95             # Preventing concurrent access to the DceApplicationHelper
96             # from different DceApplication RMs
97             dce_helper = self.simulation.dce_helper
98
99             with dce_helper.dce_application_lock:
100                 dce_app_uuid = dce_helper.dce_application_uuid
101
102                 self.simulation.invoke(dce_app_uuid, "ResetArguments") 
103
104                 self.simulation.invoke(dce_app_uuid, "ResetEnvironment") 
105
106                 self.simulation.invoke(dce_app_uuid, 
107                         "SetBinary", self.get("binary")) 
108
109                 self.simulation.invoke(dce_app_uuid, 
110                         "SetStackSize", self.get("stackSize")) 
111
112                 arguments = self.get("arguments")
113                 if arguments:
114                     for arg in map(str.strip, arguments.split(";")):
115                         self.simulation.invoke(dce_app_uuid, 
116                             "AddArgument", arg)
117
118                 environment = self.get("environment")
119                 if environment:
120                     for env in map(str.strip, environment.split(";")):
121                         key, val = env.split("=")
122                         self.simulation.invoke(dce_app_uuid, 
123                             "AddEnvironment", key, val)
124
125                 apps_uuid = self.simulation.invoke(dce_app_uuid, 
126                         "InstallInNode", self.node.uuid)
127
128             self._uuid = self.simulation.invoke(apps_uuid, "Get", 0)
129
130             if self.has_changed("StartTime"):
131                 self.simulation.ns3_set(self.uuid, "StartTime", self.get("StartTime"))
132
133             if self.has_changed("StopTime"):
134                 self.simulation.ns3_set(self.uuid, "StopTime", self.get("StopTime"))
135
136     def do_stop(self):
137         if self.state == ResourceState.STARTED:
138             # No need to do anything, simulation.Destroy() will stop every object
139             self.info("Stopping command '%s'" % command)
140             self.simulation.invoke(self.uuid, "Stop")
141             self.set_stopped()
142
143     def do_start(self):
144         if self.simulation.state < ResourceState.STARTED:
145             self.debug("---- RESCHEDULING START ----" )
146             self.ec.schedule(self.reschedule_delay, self.start)
147         else:
148             is_app_started = self.simulation.invoke(self.uuid, "isAppStarted")
149
150             if is_app_started or self.simulation.state > ResourceState.STARTED:
151                 super(NS3BaseApplication, self).do_start()
152                 self._start_time = self.simulation.start_time
153             else:
154                 # Reschedule until dce application is actually started
155                 self.debug("---- RESCHEDULING START ----" )
156                 self.ec.schedule(self.reschedule_delay, self.start)
157
158     def trace(self, name, attr = TraceAttr.ALL, block = 512, offset = 0):
159         self._configure_traces()
160         return super(NS3BaseDceApplication, self).trace(name, attr = attr, 
161                 block = block, offset = offset)
162
163     def _configure_traces(self):
164         if self.pid is not None:
165             return 
166
167         # Using lock to prevent concurrent access to the DceApplicationHelper
168         # from different DceApplication RMs
169         dce_helper = self.simulation.dce_helper
170
171         with dce_helper.dce_application_lock:
172             dce_app_uuid = dce_helper.dce_application_uuid
173
174             self._pid = self.simulation.invoke(dce_app_uuid, 
175                     "GetPid", self.uuid)
176
177         node_id = self.node.node_id 
178         self._trace_filename["stdout"] = "files-%s/var/log/%s/stdout" % (node_id, self.pid)
179         self._trace_filename["stderr"] = "files-%s/var/log/%s/stderr" % (node_id, self.pid)
180         self._trace_filename["status"] = "files-%s/var/log/%s/status" % (node_id, self.pid)
181         self._trace_filename["cmdline"] = "files-%s/var/log/%s/cmdline" % (node_id, self.pid)
182
183