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