Fixes ns-3/DCE
[nepi.git] / src / nepi / resources / ns3 / ns3ccndceapplication.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.ns3dceapplication import NS3BaseDceApplication
23
24 import os
25
26 @clsinit_copy
27 class NS3BaseCCNDceApplication(NS3BaseDceApplication):
28     _rtype = "abstract::ns3::CCNDceApplication"
29
30     @classmethod
31     def _register_attributes(cls):
32         files = Attribute("files", 
33                 "Semi-colon separated list of 'key=value' pairs to set as "
34                 "DCE files (AddFile). The key should be a path to a local file "
35                 "and the key is the path to be set in DCE for that file" ,
36                 flags = Flags.Design)
37
38         stdinfile = Attribute("stdinFile", 
39                 "File to set as StdinFile. The value shoudl be either an empty "
40                 "or a path to a local file ",
41                 flags = Flags.Design)
42
43         cls._register_attribute(files)
44         cls._register_attribute(stdinfile)
45
46     def _instantiate_object(self):
47         pass
48
49     def _connect_object(self):
50         node = self.node
51         if node.uuid not in self.connected:
52             self._connected.add(node.uuid)
53
54             # Preventing concurrent access to the DceApplicationHelper
55             # from different DceApplication RMs
56             with self.simulation.dce_application_lock:
57                 self.simulation.invoke(
58                         self.simulation.ccn_client_helper_uuid, 
59                         "ResetArguments") 
60
61                 self.simulation.invoke(
62                         self.simulation.ccn_client_helper_uuid, 
63                         "ResetEnvironment") 
64
65                 self.simulation.invoke(
66                         self.simulation.ccn_client_helper_uuid, 
67                         "SetBinary", self.get("binary")) 
68
69                 self.simulation.invoke(
70                         self.simulation.ccn_client_helper_uuid, 
71                         "SetStackSize", self.get("stackSize")) 
72
73                 arguments = self.get("arguments")
74                 if arguments:
75                     for arg in map(str.strip, arguments.split(";")):
76                         self.simulation.invoke(
77                                 self.simulation.ccn_client_helper_uuid, 
78                             "AddArgument", arg)
79
80                 environment = self.get("environment")
81                 if environment:
82                     for env in map(str.strip, environment.split(";")):
83                         key, val = env.split("=")
84                         self.simulation.invoke(
85                                 self.simulation.ccn_client_helper_uuid, 
86                             "AddEnvironment", key, val)
87
88                 if self.has_attribute("files"):
89                     files = self.get("files")
90                     if files:
91                         for files in map(str.strip, files.split(";")):
92                             remotepath, dcepath = files.split("=")
93                             localpath = "${SHARE}/" + os.path.basename(remotepath)
94                             self.simulation.invoke(
95                                     self.simulation.ccn_client_helper_uuid, 
96                                 "AddFile", localpath, dcepath)
97
98                 if self.has_attribute("stdinFile"):
99                     stdinfile = self.get("stdinFile")
100                     if stdinfile:
101                         if stdinfile != "":
102                             stdinfile = "${SHARE}/" + os.path.basename(stdinfile)
103         
104                         self.simulation.invoke(
105                                 self.simulation.ccn_client_helper_uuid, 
106                                 "SetStdinFile", stdinfile)
107
108                 apps_uuid = self.simulation.invoke(
109                         self.simulation.ccn_client_helper_uuid, 
110                         "InstallInNode", self.node.uuid)
111
112             self._uuid = self.simulation.invoke(apps_uuid, "Get", 0)
113
114             if self.has_changed("StartTime"):
115                 self.simulation.ns3_set(self.uuid, "StartTime", self.get("StartTime"))
116
117             if self.has_changed("StopTime"):
118                 self.simulation.ns3_set(self.uuid, "StopTime", self.get("StopTime"))
119
120