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