README moves to markdown
[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 version 2 as
7 #    published by the Free Software Foundation;
8 #
9 #    This program is distributed in the hope that it will be useful,
10 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
11 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 #    GNU General Public License for more details.
13 #
14 #    You should have received a copy of the GNU General Public License
15 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 #
17 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
18
19 from nepi.execution.attribute import Attribute, Flags, Types
20 from nepi.execution.resource import clsinit_copy, ResourceState
21 from nepi.resources.ns3.ns3application import NS3BaseApplication
22 from nepi.execution.trace import TraceAttr
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     @classmethod
35     def _register_attributes(cls):
36         binary = Attribute("binary", 
37                 "Name of binary to execute",
38                 flags = Flags.Design)
39
40         stack_size = Attribute("stackSize", 
41                 "Stack Size for DCE",
42                 type = Types.Integer,
43                 default = 1<<20,                
44                 flags = Flags.Design)
45
46         arguments = Attribute("arguments", 
47                 "Semi-colon separated list of arguments for the application",
48                 flags = Flags.Design)
49
50         environment = Attribute("environment", 
51                 "Semi-colon separated list of 'key=value' pairs to set as "
52                 "DCE environment variables.",
53                 flags = Flags.Design)
54
55         use_dlm = Attribute("useDlmLoader",
56                 "Use ns3::DlmLoaderFactory as library loader",
57                 type = Types.Bool,
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(use_dlm)
75         cls._register_attribute(stoptime)
76         cls._register_attribute(starttime)
77
78     def __init__(self, ec, guid):
79         super(NS3BaseDceApplication, self).__init__(ec, guid)
80         self._pid = None
81
82     @property
83     def pid(self):
84         return self._pid
85
86     def _instantiate_object(self):
87         pass
88
89     def _connect_object(self):
90         node = self.node
91         if node.uuid not in self.connected:
92             self._connected.add(node.uuid)
93
94             # Preventing concurrent access to the DceApplicationHelper
95             # from different DceApplication RMs
96             dce_helper = self.simulation.dce_helper
97
98             with dce_helper.dce_application_lock:
99                 dce_app_uuid = dce_helper.dce_application_uuid
100
101                 self.simulation.invoke(dce_app_uuid, "ResetArguments") 
102
103                 self.simulation.invoke(dce_app_uuid, "ResetEnvironment") 
104
105                 self.simulation.invoke(dce_app_uuid, 
106                         "SetBinary", self.get("binary")) 
107
108                 self.simulation.invoke(dce_app_uuid, 
109                         "SetStackSize", self.get("stackSize")) 
110
111                 arguments = self.get("arguments")
112                 if arguments:
113                     for arg in map(str.strip, arguments.split(";")):
114                         self.simulation.invoke(dce_app_uuid, 
115                             "AddArgument", arg)
116
117                 environment = self.get("environment")
118                 if environment:
119                     for env in map(str.strip, environment.split(";")):
120                         key, val = env.split("=")
121                         self.simulation.invoke(dce_app_uuid, 
122                             "AddEnvironment", key, val)
123
124                 apps_uuid = self.simulation.invoke(dce_app_uuid, 
125                         "InstallInNode", self.node.uuid)
126
127             self._uuid = self.simulation.invoke(apps_uuid, "Get", 0)
128
129             if self.has_changed("StartTime"):
130                 self.simulation.ns3_set(self.uuid, "StartTime", self.get("StartTime"))
131
132             if self.has_changed("StopTime"):
133                 self.simulation.ns3_set(self.uuid, "StopTime", self.get("StopTime"))
134
135     def do_stop(self):
136         if self.state == ResourceState.STARTED:
137             # No need to do anything, simulation.Destroy() will stop every object
138             self.info("Stopping command '%s'" % command)
139             self.simulation.invoke(self.uuid, "Stop")
140             self.set_stopped()
141
142     def do_start(self):
143         if self.simulation.state < ResourceState.STARTED:
144             self.debug("---- RESCHEDULING START ----" )
145             self.ec.schedule(self.reschedule_delay, self.start)
146         else:
147             is_app_started = self.simulation.invoke(self.uuid, "isAppStarted")
148
149             if is_app_started or self.simulation.state > ResourceState.STARTED:
150                 super(NS3BaseApplication, self).do_start()
151                 self._start_time = self.simulation.start_time
152             else:
153                 # Reschedule until dce application is actually started
154                 self.debug("---- RESCHEDULING START ----" )
155                 self.ec.schedule(self.reschedule_delay, self.start)
156
157     def trace(self, name, attr = TraceAttr.ALL, block = 512, offset = 0):
158         self._configure_traces()
159         return super(NS3BaseDceApplication, self).trace(name, attr = attr, 
160                 block = block, offset = offset)
161
162     def _configure_traces(self):
163         if self.pid is not None:
164             return 
165
166         # Using lock to prevent concurrent access to the DceApplicationHelper
167         # from different DceApplication RMs
168         dce_helper = self.simulation.dce_helper
169
170         with dce_helper.dce_application_lock:
171             dce_app_uuid = dce_helper.dce_application_uuid
172
173             self._pid = self.simulation.invoke(dce_app_uuid, 
174                     "GetPid", self.uuid)
175
176         node_id = self.node.node_id 
177         self._trace_filename["stdout"] = "files-%s/var/log/%s/stdout" % (node_id, self.pid)
178         self._trace_filename["stderr"] = "files-%s/var/log/%s/stderr" % (node_id, self.pid)
179         self._trace_filename["status"] = "files-%s/var/log/%s/status" % (node_id, self.pid)
180         self._trace_filename["cmdline"] = "files-%s/var/log/%s/cmdline" % (node_id, self.pid)
181
182