Fix #143 - [NS3] Integrate DCE bindings
[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 @clsinit_copy
25 class NS3BaseDceApplication(NS3BaseApplication):
26     _rtype = "abstract::ns3::DceApplication"
27
28     @classmethod
29     def _register_attributes(cls):
30         binary = Attribute("binary", 
31                 "Name of binary to execute",
32                 flags = Flags.Design)
33
34         stack_size = Attribute("stackSize", 
35                 "Stack Size for DCE",
36                 type = Types.Integer,
37                 default = 1<<20,                
38                 flags = Flags.Design)
39
40         arguments = Attribute("arguments", 
41                 "Semi-colon separated list of arguments for the application",
42                 flags = Flags.Design)
43
44         cls._register_attribute(binary)
45         cls._register_attribute(stack_size)
46         cls._register_attribute(arguments)
47
48     @property
49     def node(self):
50         from nepi.resources.ns3.ns3node import NS3BaseNode
51         nodes = self.get_connected(NS3BaseNode.get_rtype())
52
53         if not nodes: 
54             msg = "DceApplication not connected to node"
55             self.error(msg)
56             raise RuntimeError, msg
57
58         if nodes[0].get("enableDCE") == False:
59             raise RuntimeError("DceApplication not connected to DCE enabled node")
60
61         return nodes[0]
62
63     def _connect_object(self):
64         node = self.node
65         if node.uuid not in self.connected:
66             self._connected.add(node.uuid)
67             self.simulation.invoke(self.simulation.dce_application_helper_uuid, 
68                     "ResetArguments") 
69
70             self.simulation.invoke(self.simulation.dce_application_helper_uuid, 
71                     "SetBinary", self.get("binary")) 
72
73             self.simulation.invoke(self.simulation.dce_application_helper_uuid, 
74                     "SetStackSize", self.get("stackSize")) 
75
76             arguments = self.get("arguments") or ""
77             for arg in map(str.strip, arguments.split(";")):
78                 self.simulation.invoke(self.simulation.dce_application_helper_uuid, 
79                     "AddArgument", arg) 
80
81             apps_uuid = self.simulation.invoke(self.simulation.dce_application_helper_uuid, 
82                     "InstallInNode", self.node.uuid)
83
84             #start_time = self.get("StartTime") 
85             #time_uuid = self.simulation.create("Time", start_time)
86             #self.simulation.invoke(apps_uuid, "Start", time_uuid) 
87
88     def do_stop(self):
89         if self.state == ResourceState.STARTED:
90             # No need to do anything, simulation.Destroy() will stop every object
91             self.info("Stopping command '%s'" % command)
92             self.simulation.invoke(self.uuid, "Stop")
93             self.set_stopped()
94
95     def do_start(self):
96         if self.simulation.state < ResourceState.STARTED:
97             self.debug("---- RESCHEDULING START ----" )
98             self.ec.schedule(reschedule_delay, self.start)
99         else:
100             super(NS3BaseApplication, self).do_start()
101             self._start_time = self.simulation.start_time
102