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