README moves to markdown
[nepi.git] / src / nepi / resources / linux / ns3 / ns3simulation.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.trace import Trace, TraceAttr
21 from nepi.execution.resource import ResourceManager, clsinit_copy, \
22         ResourceState, ResourceFactory
23 from nepi.resources.linux.application import LinuxApplication
24 from nepi.util.timefuncs import tnow, tdiffsec
25 from nepi.resources.ns3.ns3simulation import NS3Simulation
26 from nepi.resources.ns3.ns3wrapper import SIMULATOR_UUID, GLOBAL_VALUE_UUID, \
27         IPV4_GLOBAL_ROUTING_HELPER_UUID
28 from nepi.resources.linux.ns3.ns3client import LinuxNS3Client
29
30 import os
31 import time
32 import threading
33
34 @clsinit_copy
35 class LinuxNS3Simulation(LinuxApplication, NS3Simulation):
36     _rtype = "linux::ns3::Simulation"
37
38     @classmethod
39     def _register_attributes(cls):
40         impl_type = Attribute("simulatorImplementationType",
41                 "The object class to use as the simulator implementation",
42             allowed = ["ns3::DefaultSimulatorImpl", "ns3::RealtimeSimulatorImpl"],
43             default = "ns3::DefaultSimulatorImpl",
44             type = Types.Enumerate,
45             flags = Flags.Design)
46
47         sched_type = Attribute("schedulerType",
48                 "The object class to use as the scheduler implementation",
49                 allowed = ["ns3::MapScheduler",
50                             "ns3::ListScheduler",
51                             "ns3::HeapScheduler",
52                             "ns3::MapScheduler",
53                             "ns3::CalendarScheduler"
54                     ],
55             default = "ns3::MapScheduler",
56             type = Types.Enumerate,
57             flags = Flags.Design)
58
59         check_sum = Attribute("checksumEnabled",
60                 "A global switch to enable all checksums for all protocols",
61             default = False,
62             type = Types.Bool,
63             flags = Flags.Design)
64
65         ns_log = Attribute("nsLog",
66             "NS_LOG environment variable. " \
67                     " Will only generate output if ns-3 is compiled in DEBUG mode. ",
68             flags = Flags.Design)
69
70         verbose = Attribute("verbose",
71             "True to output debugging info from the ns3 client-server communication",
72             type = Types.Bool,
73             flags = Flags.Design)
74
75         enable_dump = Attribute("enableDump",
76             "Enable dumping the remote executed ns-3 commands to a script "
77             "in order to later reproduce and debug the experiment",
78             type = Types.Bool,
79             default = False,
80             flags = Flags.Design)
81
82         build_mode = Attribute("buildMode",
83             "Mode used to build ns-3 with waf. One if: debug, release, oprimized ",
84             default = "optimized", 
85             allowed = ["debug", "release", "optimized"],
86             type = Types.Enumerate,
87             flags = Flags.Design)
88
89         ns3_version = Attribute("ns3Version",
90             "Version of ns-3 to install from nsam repo",
91             default = "ns-3.20", 
92             #default = "ns-3-dev", 
93             flags = Flags.Design)
94
95         pybindgen_version = Attribute("pybindgenVersion",
96             "Version of pybindgen to install from bazar repo",
97             default = "868", 
98             #default = "876", 
99             flags = Flags.Design)
100
101         dce_version = Attribute("dceVersion",
102             "Version of dce to install from nsam repo (tag branch for repo)",
103             #default = "dce-1.3", 
104             default = "dce-dev", 
105             flags = Flags.Design)
106
107         populate_routing_tables = Attribute("populateRoutingTables",
108             "Invokes  Ipv4GlobalRoutingHelper.PopulateRoutingTables() ",
109             default = False,
110             type = Types.Bool,
111             flags = Flags.Design)
112
113         stoptime = Attribute("StopTime",
114             "Time at which the simulation will stop",
115             flags = Flags.Design)
116
117         cls._register_attribute(impl_type)
118         cls._register_attribute(sched_type)
119         cls._register_attribute(check_sum)
120         cls._register_attribute(ns_log)
121         cls._register_attribute(enable_dump)
122         cls._register_attribute(verbose)
123         cls._register_attribute(build_mode)
124         cls._register_attribute(ns3_version)
125         cls._register_attribute(pybindgen_version)
126         cls._register_attribute(dce_version)
127         cls._register_attribute(populate_routing_tables)
128         cls._register_attribute(stoptime)
129
130     def __init__(self, ec, guid):
131         LinuxApplication.__init__(self, ec, guid)
132         NS3Simulation.__init__(self)
133
134         self._client = None
135         self._home = "ns3-simu-%s" % self.guid
136         self._socket_name = "ns3-%s.sock" % os.urandom(4).encode('hex')
137         self._enable_dce = None
138         self._dce_helper = None
139
140     @property
141     def socket_name(self):
142         return self._socket_name
143
144     @property
145     def remote_socket(self):
146         return os.path.join(self.run_home, self.socket_name)
147
148     def trace(self, name, attr = TraceAttr.ALL, block = 512, offset = 0):
149         # stout needs to get flushed on the ns-3 server side, else we will 
150         # get an empty stream. We try twice to retrieve the stream
151         # if we get empty stdout since the stream might not be
152         # flushed immediately.
153         if name.endswith("stdout"):
154             self._client.flush() 
155             result = LinuxApplication.trace(self, name, attr, block, offset)
156             if result:
157                 return result
158             # Let the stream be flushed
159             time.sleep(1)
160
161         return LinuxApplication.trace(self, name, attr, block, offset)
162
163     def upload_sources(self):
164         self.node.mkdir(os.path.join(self.node.src_dir, "ns3wrapper"))
165
166         # upload ns3 wrapper python script
167         ns3_wrapper = os.path.join(os.path.dirname(__file__), "..", "..", "ns3", 
168                 "ns3wrapper.py")
169
170         self.node.upload(ns3_wrapper,
171                 os.path.join(self.node.src_dir, "ns3wrapper", "ns3wrapper.py"),
172                 overwrite = False)
173
174         # upload ns3 wrapper debug python script
175         ns3_wrapper_debug = os.path.join(os.path.dirname(__file__), "..", "..", "ns3", 
176                 "ns3wrapper_debug.py")
177
178         self.node.upload(ns3_wrapper_debug,
179                 os.path.join(self.node.src_dir, "ns3wrapper", "ns3wrapper_debug.py"),
180                 overwrite = False)
181
182         # upload ns3_server python script
183         ns3_server = os.path.join(os.path.dirname(__file__), "..", "..", "ns3",
184                 "ns3server.py")
185
186         self.node.upload(ns3_server,
187                 os.path.join(self.node.src_dir, "ns3wrapper", "ns3server.py"),
188                 overwrite = False)
189
190         if self.node.use_rpm:
191             # upload pygccxml sources
192             pygccxml_tar = os.path.join(os.path.dirname(__file__), "dependencies",
193                     "%s.tar.gz" % self.pygccxml_version)
194
195             self.node.upload(pygccxml_tar,
196                     os.path.join(self.node.src_dir, "%s.tar.gz" % self.pygccxml_version),
197                     overwrite = False)
198
199         # Upload user defined ns-3 sources
200         self.node.mkdir(os.path.join(self.node.src_dir, "ns-3"))
201         src_dir = os.path.join(self.node.src_dir, "ns-3")
202
203         super(LinuxNS3Simulation, self).upload_sources(src_dir = src_dir)
204     
205     def upload_extra_sources(self, sources = None, src_dir = None):
206         return super(LinuxNS3Simulation, self).upload_sources(
207                 sources = sources, 
208                 src_dir = src_dir)
209
210     def upload_start_command(self):
211         command = self.get("command")
212         env = self.get("env")
213
214         # We want to make sure the ccnd is running
215         # before the experiment starts.
216         # Run the command as a bash script in background,
217         # in the host ( but wait until the command has
218         # finished to continue )
219         env = self.replace_paths(env)
220         command = self.replace_paths(command)
221
222         shfile = os.path.join(self.app_home, "start.sh")
223         self.node.upload_command(command, 
224                     shfile = shfile,
225                     env = env,
226                     overwrite = True)
227
228         # Run the ns3wrapper 
229         self._run_in_background()
230
231         # Wait until the remote socket is created
232         self.wait_remote_socket()
233
234     def configure(self):
235         if self.has_changed("simulatorImplementationType"):
236             simu_type = self.get("simulatorImplementationType")
237             stype = self.create("StringValue", simu_type)
238             self.invoke(GLOBAL_VALUE_UUID, "Bind", "SimulatorImplementationType", stype)
239
240         if self.has_changed("checksumEnabled"):
241             check_sum = self.get("checksumEnabled")
242             btrue = self.create("BooleanValue", check_sum)    
243             self.invoke(GLOBAL_VALUE_UUID, "Bind", "ChecksumEnabled", btrue)
244         
245         if self.has_changed("schedulerType"):
246             sched_type = self.get("schedulerType")
247             stype = self.create("StringValue", sched_type)
248             self.invoke(GLOBAL_VALUE_UUID, "Bind", "SchedulerType", btrue)
249         
250     def do_deploy(self):
251         if not self.node or self.node.state < ResourceState.READY:
252             self.debug("---- RESCHEDULING DEPLOY ---- node state %s " % self.node.state )
253             
254             # ccnd needs to wait until node is deployed and running
255             self.ec.schedule(self.reschedule_delay, self.deploy)
256         else:
257             if not self.get("command"):
258                 self.set("command", self._start_command)
259             
260             if not self.get("depends"):
261                 self.set("depends", self._dependencies)
262
263             if self.get("sources"):
264                 sources = self.get("sources")
265                 source = sources.split(" ")[0]
266                 basename = os.path.basename(source)
267                 version = ( basename.strip().replace(".tar.gz", "")
268                     .replace(".tar","")
269                     .replace(".gz","")
270                     .replace(".zip","") )
271
272                 self.set("ns3Version", version)
273                 self.set("sources", source)
274
275             if not self.get("build"):
276                 self.set("build", self._build)
277
278             if not self.get("install"):
279                 self.set("install", self._install)
280
281             if not self.get("env"):
282                 self.set("env", self._environment)
283
284             self.do_discover()
285             self.do_provision()
286
287             # Create client
288             self._client = LinuxNS3Client(self)
289
290             self.configure()
291             
292             self.set_ready()
293
294     def do_start(self):
295         """ Starts simulation execution
296
297         """
298         self.info("Starting")
299
300         if self.state == ResourceState.READY:
301             if self.get("populateRoutingTables") == True:
302                 self.invoke(IPV4_GLOBAL_ROUTING_HELPER_UUID, "PopulateRoutingTables")
303
304             time = self.get("StopTime")
305             if time:
306                 self._client.stop(time=time) 
307
308             self._client.start()
309
310             self.set_started()
311         else:
312             msg = " Failed to execute command '%s'" % command
313             self.error(msg, out, err)
314             raise RuntimeError(msg)
315
316     def do_stop(self):
317         """ Stops simulation execution
318
319         """
320         if self.state == ResourceState.STARTED:
321             if not self.get("StopTime"):
322                 self._client.stop() 
323             self.set_stopped()
324
325     def do_release(self):
326         self.info("Releasing resource")
327
328         tear_down = self.get("tearDown")
329         if tear_down:
330             self.node.execute(tear_down)
331
332         self.do_stop()
333         self._client.shutdown()
334         LinuxApplication.do_stop(self)
335         
336         super(LinuxApplication, self).do_release()
337
338     @property
339     def state(self):
340         super(LinuxApplication, self).state
341         
342         if self._state == ResourceState.STARTED:
343             try:
344                 is_finished = self.invoke(SIMULATOR_UUID, "isFinished")
345                 
346                 if is_finished:
347                     self.set_stopped()
348             except:
349                 msg = "Simulator failed. Can not retrieve state"
350                 out = ""
351
352                 import traceback
353                 err = traceback.format_exc()
354                 self.error(msg, out, err)
355                 self.do_fail()
356
357         return self._state
358
359     @property
360     def enable_dce(self):
361         if self._enable_dce is None:
362             from nepi.resources.ns3.ns3dceapplication import NS3BaseDceApplication
363             rclass = ResourceFactory.get_resource_type(
364                     NS3BaseDceApplication.get_rtype())
365             
366             self._enable_dce = False
367             for guid in self.ec.resources:
368                 rm = self.ec.get_resource(guid)
369                 if isinstance(rm, rclass):
370                     self._enable_dce = True
371
372                     from nepi.resources.ns3.ns3dcehelper import NS3DceHelper
373                     self._dce_helper = NS3DceHelper(self)
374                     break
375
376         return self._enable_dce
377
378     @property
379     def dce_helper(self):
380         return self._dce_helper
381         
382     @property
383     def _start_command(self):
384         command = [] 
385
386         command.append("PYTHONPATH=$PYTHONPATH:${SRC}/ns3wrapper/")
387         
388         command.append("python ${SRC}/ns3wrapper/ns3server.py -S %s" % \
389                 os.path.basename(self.remote_socket) )
390
391         ns_log = self.get("nsLog")
392         if ns_log:
393             command.append("-L '%s'" % ns_log)
394
395         if self.get("enableDump"):
396             command.append("-D")
397
398         if self.get("verbose"):
399             command.append("-v")
400
401         command = " ".join(command)
402         return command
403
404     @property
405     def _dependencies(self):
406         if self.node.use_rpm:
407             return ( " gcc gcc-c++ python python-devel mercurial bzr tcpdump socat gccxml unzip")
408         elif self.node.use_deb:
409             return ( " gcc g++ python python-dev mercurial bzr tcpdump socat gccxml python-pygccxml unzip")
410         return ""
411
412     @property
413     def ns3_repo(self):
414         return "http://code.nsnam.org"
415
416     @property
417     def pygccxml_version(self):
418         return "pygccxml-1.0.0"
419
420     @property
421     def dce_repo(self):
422         return "http://code.nsnam.org/ns-3-dce"
423         #eturn "http://code.nsnam.org/epmancini/ns-3-dce"
424
425     @property
426     def dce_version(self):
427         dce_version = self.get("dceVersion")
428         return dce_version or "dce-dev"
429
430     @property
431     def ns3_build_location(self):
432         location = "${BIN}/ns-3/%(ns3_version)s%(dce_version)s/%(build_mode)s/build" \
433                     % {
434                         "ns3_version": self.get("ns3Version"),
435                         "dce_version": "-%s" % self.get("dceVersion") \
436                                 if self.enable_dce else "", 
437                         "build_mode": self.get("buildMode"),
438                       }
439
440         return location
441  
442
443     @property
444     def ns3_src_location(self):
445         location = "${SRC}/ns-3/%(ns3_version)s" \
446                     % {
447                         "ns3_version": self.get("ns3Version"),
448                       }
449
450         return location
451  
452     @property
453     def dce_src_location(self):
454         location = "${SRC}/ns-3-dce/%(dce_version)s" \
455                    % {
456                         "dce_version": self.get("dceVersion"),
457                      }
458
459         return location
460
461     @property
462     def _clone_ns3_command(self):
463         source = self.get("sources")
464         
465         if not source:
466             clone_ns3_cmd = "hg clone %(ns3_repo)s/%(ns3_version)s %(ns3_src)s" \
467                     % {
468                         "ns3_version": self.get("ns3Version"),
469                         "ns3_repo": self.ns3_repo,  
470                         "ns3_src": self.ns3_src_location,
471                       }
472         else:
473             if source.find(".tar.gz") > -1:
474                 clone_ns3_cmd = ( 
475                             "tar xzf ${SRC}/ns-3/%(basename)s " 
476                             " --strip-components=1 -C %(ns3_src)s"
477                             ) % {
478                                 "basename": os.path.basename(source),
479                                 "ns3_src": self.ns3_src_location,
480                                 }
481             elif source.find(".tar") > -1:
482                 clone_ns3_cmd = ( 
483                             "tar xf ${SRC}/ns-3/%(basename)s " 
484                             " --strip-components=1 -C %(ns3_src)s"
485                             ) % {
486                                 "basename": os.path.basename(source),
487                                 "ns3_src": self.ns3_src_location,
488                                 }
489             elif source.find(".zip") > -1:
490                 basename = os.path.basename(source)
491                 bare_basename = basename.replace(".zip", "") 
492
493                 clone_ns3_cmd = ( 
494                             "unzip ${SRC}/ns-3/%(basename)s && "
495                             "mv ${SRC}/ns-3/%(bare_basename)s %(ns3_src)s"
496                             ) % {
497                                 "bare_basename": basename_name,
498                                 "basename": basename,
499                                 "ns3_src": self.ns3_src_location,
500                                 }
501
502         return clone_ns3_cmd
503
504     @property
505     def _clone_dce_command(self):
506         clone_dce_cmd = " echo 'DCE will not be built' "
507
508         if self.enable_dce:
509             dce_version = self.dce_version
510             dce_tag = ""
511             if dce_version != "dce-dev":
512                 dce_tag = "-r %s" % dce_version
513
514             clone_dce_cmd = (
515                         # DCE installation
516                         # Test if dce is alredy cloned
517                         " ( "
518                         "  ( "
519                         "    ( test -d %(dce_src)s ) "
520                         "   && echo 'dce binaries found, nothing to do'"
521                         "  ) "
522                         " ) "
523                         "  || " 
524                         # Get dce source code
525                         " ( "
526                         "   mkdir -p %(dce_src)s && "
527                         "   hg clone %(dce_repo)s %(dce_tag)s %(dce_src)s"
528                         " ) "
529                      ) % {
530                             "dce_repo": self.dce_repo,
531                             "dce_tag": dce_tag,
532                             "dce_src": self.dce_src_location,
533                          }
534
535         return clone_dce_cmd
536
537     @property
538     def _build(self):
539         # If the user defined local sources for ns-3, we uncompress the sources
540         # on the remote sources directory. Else we clone ns-3 from the official repo.
541         clone_ns3_cmd = self._clone_ns3_command
542         clone_dce_cmd = self._clone_dce_command
543
544         ns3_build_cmd = (
545                 # NS3 installation
546                 "( "
547                 " ( "
548                 # Test if ns-3 is alredy cloned
549                 "  ((( test -d %(ns3_src)s ) || "
550                 "    ( test -d ${NS3BINDINGS:='None'} && test -d ${NS3LIBRARIES:='None'})) "
551                 "  && echo 'ns-3 binaries found, nothing to do' )"
552                 " ) "
553                 "  || " 
554                 # If not, install ns-3 and its dependencies
555                 " (   "
556                 # Install pygccxml
557                 "   (   "
558                 "     ( "
559                 "       python -c 'import pygccxml' && "
560                 "       echo 'pygccxml not found' "
561                 "     ) "
562                 "      || "
563                 "     ( "
564                 "       tar xf ${SRC}/%(pygccxml_version)s.tar.gz -C ${SRC} && "
565                 "       cd ${SRC}/%(pygccxml_version)s && "
566                 "       python setup.py build && "
567                 "       sudo -S python setup.py install "
568                 "     ) "
569                 "   ) " 
570                 # Install pybindgen
571                 "  && "
572                 "   (   "
573                 "     ( "
574                 "       test -d ${SRC}/pybindgen/%(pybindgen_version)s && "
575                 "       echo 'binaries found, nothing to do' "
576                 "     ) "
577                 "      || "
578                 # If not, clone and build
579                 "      ( cd ${SRC} && "
580                 "        mkdir -p ${SRC}/pybindgen && "
581                 "        bzr checkout lp:pybindgen -r %(pybindgen_version)s ${SRC}/pybindgen/%(pybindgen_version)s && "
582                 "        cd ${SRC}/pybindgen/%(pybindgen_version)s && "
583                 "        ./waf configure && "
584                 "        ./waf "
585                 "      ) "
586                 "   ) " 
587                 " && "
588                 # Get ns-3 source code
589                 "  ( "
590                 "     mkdir -p %(ns3_src)s && "
591                 "     %(clone_ns3_cmd)s "
592                 "  ) "
593                 " ) "
594                 ") "
595                 " && "
596                 "( "
597                 "   %(clone_dce_cmd)s "
598                 ") "
599              ) % { 
600                     "ns3_src": self.ns3_src_location,
601                     "pybindgen_version": self.get("pybindgenVersion"),
602                     "pygccxml_version": self.pygccxml_version,
603                     "clone_ns3_cmd": clone_ns3_cmd,
604                     "clone_dce_cmd": clone_dce_cmd,
605                  }
606
607         return ns3_build_cmd
608
609     @property
610     def _install_dce_command(self):
611         install_dce_cmd = " echo 'DCE will not be installed'"
612
613         if self.enable_dce:
614             install_dce_cmd = (
615                         " ( "
616                         "   ((test -d %(ns3_build)s/bin_dce ) && "
617                         "    echo 'dce binaries found, nothing to do' )"
618                         " ) "
619                         " ||" 
620                         " (   "
621                          # If not, copy build to dce
622                         "  cd %(dce_src)s && "
623                         "  rm -rf %(dce_src)s/build && "
624                         "  ./waf configure %(enable_opt)s --with-pybindgen=${SRC}/pybindgen/%(pybindgen_version)s "
625                         "  --prefix=%(ns3_build)s --with-ns3=%(ns3_build)s && "
626                         "  ./waf build && "
627                         "  ./waf install && "
628                         "  [ ! -e %(ns3_build)s/lib/python/site-packages/ns/dce.so ] && "
629                         "   mv %(ns3_build)s/lib*/python*/site-packages/ns/dce.so %(ns3_build)s/lib/python/site-packages/ns/ "
630                         " )"
631                 ) % { 
632                     "pybindgen_version": self.get("pybindgenVersion"),
633                     "enable_opt": "--enable-opt" if  self.get("buildMode") == "optimized" else "",
634                     "ns3_build": self.ns3_build_location,
635                     "dce_src": self.dce_src_location,
636                      }
637
638         return install_dce_cmd
639
640     @property
641     def _install(self):
642         install_dce_cmd = self._install_dce_command
643
644         install_ns3_cmd = (
645                  # Test if ns-3 is alredy installed
646                 "("
647                 " ( "
648                 "  ( ( (test -d %(ns3_build)s/lib ) || "
649                 "    (test -d ${NS3BINDINGS:='None'} && test -d ${NS3LIBRARIES:='None'}) ) && "
650                 "    echo 'binaries found, nothing to do' )"
651                 " ) "
652                 " ||" 
653                 " (   "
654                  # If not, copy ns-3 build to bin
655                 "  mkdir -p %(ns3_build)s && "
656                 "  cd %(ns3_src)s && "
657                 "  rm -rf %(ns3_src)s/build && "
658                 "  ./waf configure -d %(build_mode)s --with-pybindgen=${SRC}/pybindgen/%(pybindgen_version)s "
659                 "  --prefix=%(ns3_build)s && "
660                 "  ./waf build && "
661                 "  ./waf install && "
662                 "  mv %(ns3_build)s/lib*/python* %(ns3_build)s/lib/python "
663                 " )"
664                 ") "
665                 " && "
666                 "( "
667                 "   %(install_dce_cmd)s "
668                 ") "
669               ) % { 
670                     "pybindgen_version": self.get("pybindgenVersion"),
671                     "build_mode": self.get("buildMode"),
672                     "install_dce_cmd": install_dce_cmd,
673                     "ns3_build": self.ns3_build_location,
674                     "ns3_src": self.ns3_src_location,
675                  }
676
677         return install_ns3_cmd
678
679     @property
680     def _environment(self):
681         env = []
682         env.append("PYTHONPATH=$PYTHONPATH:${NS3BINDINGS:=%(ns3_build)s/lib/python/site-packages}" % { 
683                     "ns3_build": self.ns3_build_location
684                  })
685         # If NS3LIBRARIES is defined and not empty, assign its value, 
686         # if not assign ns3_build_home/lib/ to NS3LIBRARIES and LD_LIBARY_PATH
687         env.append("LD_LIBRARY_PATH=${NS3LIBRARIES:=%(ns3_build)s/lib}" % { 
688                     "ns3_build": self.ns3_build_location
689                  })
690         env.append("DCE_PATH=$NS3LIBRARIES/../bin_dce")
691         env.append("DCE_ROOT=$NS3LIBRARIES/..")
692
693         return " ".join(env) 
694
695     def replace_paths(self, command):
696         """
697         Replace all special path tags with shell-escaped actual paths.
698         """
699         return ( command
700             .replace("${USR}", self.node.usr_dir)
701             .replace("${LIB}", self.node.lib_dir)
702             .replace("${BIN}", self.node.bin_dir)
703             .replace("${SRC}", self.node.src_dir)
704             .replace("${SHARE}", self.node.share_dir)
705             .replace("${EXP}", self.node.exp_dir)
706             .replace("${EXP_HOME}", self.node.exp_home)
707             .replace("${APP_HOME}", self.app_home)
708             .replace("${RUN_HOME}", self.run_home)
709             .replace("${NODE_HOME}", self.node.node_home)
710             .replace("${HOME}", self.node.home_dir)
711             # If NS3LIBRARIES is defined and not empty, use that value, 
712             # if not use ns3_build_home/lib/
713             .replace("${BIN_DCE}", "${NS3LIBRARIES-%s/lib}/../bin_dce" % \
714                     self.ns3_build_location)
715             )
716
717     def valid_connection(self, guid):
718         # TODO: Validate!
719         return True
720
721     def wait_remote_socket(self):
722         """ Waits until the remote socket is created
723         """
724         command = " [ -e %s ] && echo 'DONE' " % self.remote_socket
725
726         for i in range(200):
727             (out, err), proc = self.node.execute(command, retry = 1, 
728                     with_lock = True)
729
730             if out.find("DONE") > -1:
731                 break
732         else:
733             raise RuntimeError("Remote socket not found at %s" % \
734                     self.remote_socket)
735     
736