4d199c939a19dd48bdfd564339e120be39054d83
[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 import threading
26
27 @clsinit_copy
28 class NS3BaseCCNDceApplication(NS3BaseDceApplication):
29     _rtype = "abstract::ns3::CCNDceApplication"
30
31     # Lock used to synchronize usage of CcnClientHelper 
32     ccn_client_lock = threading.Lock()
33     _ccn_client_helper_uuid = None
34
35     @property
36     def ccn_client_helper_uuid(self):
37         if not self._ccn_client_helper_uuid:
38             self._ccn_client_helper_uuid = self.simulation.create("CcnClientHelper")
39         return self._ccn_client_helper_uuid
40
41     def _instantiate_object(self):
42         pass
43
44     def _connect_object(self):
45         node = self.node
46         if node.uuid not in self.connected:
47             self._connected.add(node.uuid)
48
49             # Preventing concurrent access to the DceApplicationHelper
50             # from different DceApplication RMs
51             with self.ccn_client_lock:
52                 self.simulation.invoke(
53                         self.ccn_client_helper_uuid, 
54                         "ResetArguments") 
55
56                 self.simulation.invoke(
57                         self.ccn_client_helper_uuid, 
58                         "ResetEnvironment") 
59
60                 self.simulation.invoke(
61                         self.ccn_client_helper_uuid, 
62                         "SetBinary", self.get("binary")) 
63
64                 self.simulation.invoke(
65                         self.ccn_client_helper_uuid, 
66                         "SetStackSize", self.get("stackSize")) 
67
68                 arguments = self.get("arguments")
69                 if arguments:
70                     for arg in map(str.strip, arguments.split(";")):
71                         self.simulation.invoke(
72                                  self.ccn_client_helper_uuid, 
73                                 "AddArgument", arg)
74
75                 environment = self.get("environment")
76                 if environment:
77                     for env in map(str.strip, environment.split(";")):
78                         key, val = env.split("=")
79                         self.simulation.invoke(
80                                 self.ccn_client_helper_uuid, 
81                                "AddEnvironment", key, val)
82
83                 if self.has_attribute("files"):
84                     files = self.get("files")
85                     if files:
86                         for file in map(str.strip, files.split(";")):
87                             remotepath, dcepath = file.split("=")
88                             localpath =  os.path.join(self.simulation.app_home, 
89                                     os.path.basename(remotepath))
90                             self.simulation.invoke(
91                                     self.ccn_client_helper_uuid, 
92                                     "AddFile", localpath, dcepath)
93
94                 if self.has_attribute("stdinFile"):
95                     stdinfile = self.get("stdinFile")
96                     if stdinfile:
97                         # stdinfile might be an empty text that should be set as
98                         # stdin
99                         self.simulation.invoke(
100                                 self.ccn_client_helper_uuid, 
101                                 "SetStdinFile", stdinfile)
102
103                 apps_uuid = self.simulation.invoke(
104                         self.ccn_client_helper_uuid, 
105                         "InstallInNode", self.node.uuid)
106
107                 """
108                 container_uuid = self.simulation.create("NodeContainer")
109                 self.simulation.invoke(container_uuid, "Add", self.node.uuid)
110                 apps_uuid = self.simulation.invoke(
111                         self.ccn_client_helper_uuid, 
112                         "Install", container_uuid)
113                 """
114
115             self._uuid = self.simulation.invoke(apps_uuid, "Get", 0)
116
117             if self.has_changed("StartTime"):
118                 self.simulation.ns3_set(self.uuid, "StartTime", self.get("StartTime"))
119
120             if self.has_changed("StopTime"):
121                 self.simulation.ns3_set(self.uuid, "StopTime", self.get("StopTime"))
122
123