Adding dummy ns3 fd-net-device test
[nepi.git] / src / nepi / resources / ns3 / ns3wrapper.py
index ebd0d67..eadd71f 100644 (file)
@@ -262,51 +262,60 @@ class NS3Wrapper(object):
 
         if operation == "isRunning":
             result = self.is_running
+
         elif operation == "isFinished":
             result = self.is_finished
+
         elif operation == "isAppRunning":
             result = self._is_app_running(uuid)
-        else:
 
-            if operation == "addStaticRoute":
-                result = self._add_static_route(uuid, *args)
-                
-                ### DUMP
-                self.debuger.dump_invoke(result, uuid, operation, args, kwargs)
+        elif operation == "recvFD":
+            ### passFD operation binds to a different random socket 
+            ### en every execution, so the socket name that could be
+            ### dumped to the debug script using dump_invoke is
+            ### not be valid accross debug executions.
+            result = self._recv_fd(uuid, *args, **kwargs)
 
-            elif operation == "retrieveObject":
-                result = self._retrieve_object(uuid, *args, **kwargs)
-                
-                ### DUMP
-                self.debuger.dump_invoke(result, uuid, operation, args, kwargs)
+        elif operation == "addStaticRoute":
+            result = self._add_static_route(uuid, *args)
+            
+            ### DUMP - result is static, so will be dumped as plain text
+            self.debuger.dump_invoke(result, uuid, operation, args, kwargs)
 
-            else:
-                newuuid = self.make_uuid()
+        elif operation == "retrieveObject":
+            result = self._retrieve_object(uuid, *args, **kwargs)
+       
+            ### DUMP - result is static, so will be dumped as plain text
+            self.debuger.dump_invoke(result, uuid, operation, args, kwargs)
+       
+        else:
+            newuuid = self.make_uuid()
 
-                ### DUMP
-                self.debuger.dump_invoke(newuuid, uuid, operation, args, kwargs)
+            ### DUMP - result is a uuid that encoded an dynamically generated 
+            ### object
+            self.debuger.dump_invoke(newuuid, uuid, operation, args, kwargs)
 
-                if uuid.startswith(SINGLETON):
-                    obj = self._singleton(uuid)
-                else:
-                    obj = self.get_object(uuid)
-                
-                method = getattr(obj, operation)
+            if uuid.startswith(SINGLETON):
+                obj = self._singleton(uuid)
+            else:
+                obj = self.get_object(uuid)
+            
+            method = getattr(obj, operation)
 
-                # arguments starting with 'uuid' identify ns-3 C++
-                # objects and must be replaced by the actual object
-                realargs = self.replace_args(args)
-                realkwargs = self.replace_kwargs(kwargs)
+            # arguments starting with 'uuid' identify ns-3 C++
+            # objects and must be replaced by the actual object
+            realargs = self.replace_args(args)
+            realkwargs = self.replace_kwargs(kwargs)
 
-                result = method(*realargs, **realkwargs)
+            result = method(*realargs, **realkwargs)
 
-                # If the result is an object (not a base value),
-                # then keep track of the object a return the object
-                # reference (newuuid)
-                if not (result is None or type(result) in [
-                        bool, float, long, str, int]):
-                    self._objects[newuuid] = result
-                    result = newuuid
+            # If the result is an object (not a base value),
+            # then keep track of the object a return the object
+            # reference (newuuid)
+            if not (result is None or type(result) in [
+                    bool, float, long, str, int]):
+                self._objects[newuuid] = result
+                result = newuuid
 
         ### DEBUG
         self.logger.debug("RET INVOKE %s%s = %s -> %s(%s, %s) " % (
@@ -649,5 +658,28 @@ class NS3Wrapper(object):
 
         return newuuid
 
+    def _recv_fd(self, uuid):
+        """ Waits on a local address to receive a file descriptor
+        from a local process. The file descriptor is associated
+        to a FdNetDevice to stablish communication between the
+        simulation and what ever process writes on that file descriptor
+        """
+
+        def recvfd(sock, fdnd):
+            (fd, msg) = passfd.recvfd(sock)
+            # Store a reference to the endpoint to keep the socket alive
+            fdnd.SetFileDescriptor(fd)
+        
+        import passfd
+        import socket
+        sock = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
+        sock.bind("")
+        address = sock.getsockname()
+        
+        fdnd = self.get_object(uuid)
+        t = threading.Thread(target=recvfd, args=(sock,fdnd))
+        t.start()
+
+        return address