Fixing up DCE
[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 import os
25 import threading
26         
27 @clsinit_copy
28 class NS3BaseDceApplication(NS3BaseApplication):
29     _rtype = "abstract::ns3::DceApplication"
30
31     # Lock used to synchronize usage of DceManagerHelper 
32     dce_manager_lock = threading.Lock()
33     # Lock used to synchronize usage of DceApplicationHelper
34     dce_application_lock = threading.Lock()
35    
36     _dce_manager_helper_uuid = None
37     _dce_application_helper_uuid = None
38
39     @classmethod
40     def _register_attributes(cls):
41         binary = Attribute("binary", 
42                 "Name of binary to execute",
43                 flags = Flags.Design)
44
45         stack_size = Attribute("stackSize", 
46                 "Stack Size for DCE",
47                 type = Types.Integer,
48                 default = 1<<20,                
49                 flags = Flags.Design)
50
51         arguments = Attribute("arguments", 
52                 "Semi-colon separated list of arguments for the application",
53                 flags = Flags.Design)
54
55         environment = Attribute("environment", 
56                 "Semi-colon separated list of 'key=value' pairs to set as "
57                 "DCE environment variables.",
58                 flags = Flags.Design)
59
60         starttime = Attribute("StartTime",
61             "Time at which the application will start",
62             default = "+0.0ns",  
63             flags = Flags.Reserved | Flags.Construct)
64
65         stoptime = Attribute("StopTime",
66             "Time at which the application will stop",
67             default = "+0.0ns",  
68             flags = Flags.Reserved | Flags.Construct)
69
70         cls._register_attribute(binary)
71         cls._register_attribute(stack_size)
72         cls._register_attribute(arguments)
73         cls._register_attribute(environment)
74         cls._register_attribute(stoptime)
75         cls._register_attribute(starttime)
76
77     @property
78     def node(self):
79         from nepi.resources.ns3.ns3node import NS3BaseNode
80         nodes = self.get_connected(NS3BaseNode.get_rtype())
81
82         if not nodes: 
83             msg = "DceApplication not connected to node"
84             self.error(msg)
85             raise RuntimeError, msg
86
87         return nodes[0]
88
89     @property
90     def dce_manager_helper_uuid(self):
91         if not self._dce_manager_helper_uuid:
92             self._dce_manager_helper_uuid = self.simulation.create("DceManagerHelper")
93         return self._dce_manager_helper_uuid
94
95     @property
96     def dce_application_helper_uuid(self):
97         if not self._dce_application_helper_uuid:
98             self._dce_application_helper_uuid = self.simulation.create("DceApplicationHelper")
99         return self._dce_application_helper_uuid
100
101     def _instantiate_object(self):
102         pass
103
104     def _connect_object(self):
105         node = self.node
106         if node.uuid not in self.connected:
107             self._connected.add(node.uuid)
108
109             # Preventing concurrent access to the DceApplicationHelper
110             # from different DceApplication RMs
111             with self.dce_application_lock:
112                 self.simulation.invoke(
113                         self.dce_application_helper_uuid, 
114                         "ResetArguments") 
115
116                 self.simulation.invoke(
117                         self.dce_application_helper_uuid, 
118                         "ResetEnvironment") 
119
120                 self.simulation.invoke(
121                         self.dce_application_helper_uuid, 
122                         "SetBinary", self.get("binary")) 
123
124                 self.simulation.invoke(
125                         self.dce_application_helper_uuid, 
126                         "SetStackSize", self.get("stackSize")) 
127
128                 arguments = self.get("arguments")
129                 if arguments:
130                     for arg in map(str.strip, arguments.split(";")):
131                         self.simulation.invoke(
132                                 self.dce_application_helper_uuid, 
133                             "AddArgument", arg)
134
135                 environment = self.get("environment")
136                 if environment:
137                     for env in map(str.strip, environment.split(";")):
138                         key, val = env.split("=")
139                         self.simulation.invoke(
140                                 self.dce_application_helper_uuid, 
141                             "AddEnvironment", key, val)
142
143                 apps_uuid = self.simulation.invoke(
144                         self.dce_application_helper_uuid, 
145                         "InstallInNode", self.node.uuid)
146
147                 """
148                 container_uuid = self.simulation.create("NodeContainer")
149                 self.simulation.invoke(container_uuid, "Add", self.node.uuid)
150                 apps_uuid = self.simulation.invoke(
151                         self.dce_application_helper_uuid, 
152                         "Install", container_uuid)
153                 """
154
155             self._uuid = self.simulation.invoke(apps_uuid, "Get", 0)
156
157             if self.has_changed("StartTime"):
158                 self.simulation.ns3_set(self.uuid, "StartTime", self.get("StartTime"))
159
160             if self.has_changed("StopTime"):
161                 self.simulation.ns3_set(self.uuid, "StopTime", self.get("StopTime"))
162
163     def do_stop(self):
164         if self.state == ResourceState.STARTED:
165             # No need to do anything, simulation.Destroy() will stop every object
166             self.info("Stopping command '%s'" % command)
167             self.simulation.invoke(self.uuid, "Stop")
168             self.set_stopped()
169
170     def do_start(self):
171         if self.simulation.state < ResourceState.STARTED:
172             self.debug("---- RESCHEDULING START ----" )
173             self.ec.schedule(reschedule_delay, self.start)
174         else:
175             self._configure_traces()
176             super(NS3BaseApplication, self).do_start()
177             self._start_time = self.simulation.start_time
178
179     def _configure_traces(self):
180         # Preventing concurrent access to the DceApplicationHelper
181         # from different DceApplication RMs
182         with self.dce_application_lock:
183             pid = self.simulation.invoke(self.dce_application_helper_uuid, 
184                     "GetPid", self.uuid)
185
186         node_id = self.simulation.invoke(self.node.uuid, "GetId")
187         self._trace_filename["stdout"] = "files-%s/var/log/%s/stdout" % (node_id, pid)
188         self._trace_filename["stderr"] = "files-%s/var/log/%s/stderr" % (node_id, pid)
189         self._trace_filename["status"] = "files-%s/var/log/%s/status" % (node_id, pid)
190         self._trace_filename["cmdline"] = "files-%s/var/log/%s/cmdline" % (node_id, pid)
191
192