Added attributes for ns-3 simulator. Realtime mode working
authorAlina Quereilhac <alina.quereilhac@inria.fr>
Mon, 10 Feb 2014 17:28:42 +0000 (18:28 +0100)
committerAlina Quereilhac <alina.quereilhac@inria.fr>
Mon, 10 Feb 2014 17:28:42 +0000 (18:28 +0100)
src/nepi/resources/linux/ns3/ns3simulation.py
src/nepi/resources/ns3/ns3application.py
src/nepi/resources/ns3/ns3base.py
src/nepi/resources/ns3/ns3wrapper.py
test/resources/linux/ns3/ns3simulation.py

index 6fbcb73..f820eab 100644 (file)
@@ -24,9 +24,11 @@ from nepi.execution.resource import ResourceManager, clsinit_copy, \
 from nepi.resources.linux.application import LinuxApplication
 from nepi.util.timefuncs import tnow, tdiffsec
 from nepi.resources.ns3.ns3simulation import NS3Simulation
+from nepi.resources.ns3.ns3wrapper import GLOBAL_VALUE_UUID
 from nepi.resources.linux.ns3.ns3client import LinuxNS3Client
 
 import os
+import time
 
 @clsinit_copy
 class LinuxNS3Simulation(LinuxApplication, NS3Simulation):
@@ -34,6 +36,35 @@ class LinuxNS3Simulation(LinuxApplication, NS3Simulation):
 
     @classmethod
     def _register_attributes(cls):
+        impl_type = Attribute("simulatorImplementationType",
+                "The object class to use as the simulator implementation",
+            allowed = ["ns3::DefaultSimulatorImpl", "ns3::RealtimeSimulatorImpl"],
+            default = "ns3::DefaultSimulatorImpl",
+            type = Types.Enumerate,
+            flags = Flags.Design)
+
+        sched_type = Attribute("schedulerType",
+                "The object class to use as the scheduler implementation",
+                allowed = ["ns3::MapScheduler",
+                            "ns3::ListScheduler",
+                            "ns3::HeapScheduler",
+                            "ns3::MapScheduler",
+                            "ns3::CalendarScheduler"
+                    ],
+            default = "ns3::MapScheduler",
+            type = Types.Enumerate,
+            flags = Flags.Design)
+
+        check_sum = Attribute("checksumEnabled",
+                "A global switch to enable all checksums for all protocols",
+            default = False,
+            type = Types.Bool,
+            flags = Flags.Design)
+
+        stop_time = Attribute("stopTime",
+                "Time to stop the simulation",
+            flags = Flags.Design)
+
         ns_log = Attribute("nsLog",
             "NS_LOG environment variable. " \
                     " Will only generate output if ns-3 is compiled in DEBUG mode. ",
@@ -44,8 +75,11 @@ class LinuxNS3Simulation(LinuxApplication, NS3Simulation):
             type = Types.Bool,
             flags = Flags.Design)
 
+        cls._register_attribute(impl_type)
+        cls._register_attribute(sched_type)
+        cls._register_attribute(check_sum)
+        cls._register_attribute(stop_time)
         cls._register_attribute(ns_log)
-        
         cls._register_attribute(verbose)
 
     def __init__(self, ec, guid):
@@ -124,6 +158,26 @@ class LinuxNS3Simulation(LinuxApplication, NS3Simulation):
         # Run the ns3wrapper 
         self._run_in_background()
 
+    def configure(self):
+        if self._attrs.get("simulatorImplementationType").has_changed():
+            simu_type = self.get("simulatorImplementationType")
+            stype = self.create("StringValue", simu_type)
+            self.invoke(GLOBAL_VALUE_UUID, "Bind", "SimulatorImplementationType", stype)
+
+        if self._attrs.get("checksumEnabled").has_changed():
+            check_sum = self.get("checksumEnabled")
+            btrue = self.create("BooleanValue", check_sum)    
+            self.invoke(GLOBAL_VALUE_UUID, "Bind", "ChecksumEnabled", btrue)
+        
+        if self._attrs.get("schedulerType").has_changed():
+            sched_type = self.get("schedulerType")
+            stype = self.create("StringValue", sched_type)
+            self.invoke(GLOBAL_VALUE_UUID, "Bind", "SchedulerType", btrue)
+        
+        if self._attrs.get("stopTime").has_changed():
+            stop_time = self.get("stopTime")
+            self.stop(time = stop_time)
+
     def do_deploy(self):
         if not self.node or self.node.state < ResourceState.READY:
             self.debug("---- RESCHEDULING DEPLOY ---- node state %s " % self.node.state )
@@ -150,18 +204,30 @@ class LinuxNS3Simulation(LinuxApplication, NS3Simulation):
             self.do_provision()
 
             # Create client
-            self._client = LinuxNS3Client(self) 
-
+            self._client = LinuxNS3Client(self)
+           
+            # Wait until local socket is created
+            for i in [1, 5, 15, 30, 60]:
+                if os.path.exists(self.local_socket):
+                    break
+                time.sleep(i)
+
+            if not os.path.exists(self.local_socket):
+                raise RuntimeError("Problem starting socat")
+
+            self.configure()
+            
             self.set_ready()
 
     def do_start(self):
         """ Starts simulation execution
 
         """
-        self.info("Starting ns-3 simulation")
+        self.info("Starting")
 
         if self.state == ResourceState.READY:
             self._client.start() 
+
             self.set_started()
         else:
             msg = " Failed to execute command '%s'" % command
index aa11a72..46c98ea 100644 (file)
@@ -59,8 +59,9 @@ class NS3BaseApplication(NS3Base):
     def state(self):
         if self._state == ResourceState.STARTED:
             is_running = self.simulation.invoke(self.uuid, "isAppRunning")
+
             if not is_running:
-                self._state = ResourceState.STOPPED
+                self.set_stopped()
 
         return self._state
 
index 358011a..adcbb8b 100644 (file)
@@ -29,8 +29,6 @@ class NS3Base(ResourceManager):
     _rtype = "abstract::ns3::Object"
     _backend_type = "ns3"
 
-    SIMULATOR_UUID = "singleton::Simulator"
-
     def __init__(self, ec, guid):
         super(NS3Base, self).__init__(ec, guid)
         self._uuid = None
index 00245bb..e3a933f 100644 (file)
@@ -25,6 +25,9 @@ import time
 import uuid
 
 SINGLETON = "singleton::"
+SIMULATOR_UUID = "singleton::Simulator"
+CONFIG_UUID = "singleton::Config"
+GLOBAL_VALUE_UUID = "singleton::GlobalValue"
 
 def load_ns3_module():
     import ctypes
@@ -434,7 +437,11 @@ class NS3Wrapper(object):
         start_value = self.get(uuid, "StartTime")
         start_time = self.ns3.Time(start_value)
         
-        if now.Compare(start_time) >= 0 and now.Compare(stop_time) <= 0:
+        self.logger.debug("NOW %s" % now.GetSeconds())
+        self.logger.debug("START TIME %s" % start_value)
+        self.logger.debug("STOP TIME %s" % stop_value)
+
+        if now.Compare(start_time) >= 0 and now.Compare(stop_time) < 0:
             return True
 
         return False
index 53c03a2..f021196 100644 (file)
@@ -41,8 +41,8 @@ class LinuxNS3ClientTest(unittest.TestCase):
         #self.fedora_host = "peeramide.irisa.fr"
         self.fedora_user = "inria_test"
 
-    def test_simple_ping(self):
-        ec = ExperimentController(exp_id = "test-ns3-simu")
+    def ztest_simple_ping(self):
+        ec = ExperimentController(exp_id = "test-ns3-ping")
         
         node = ec.register_resource("LinuxNode")
         ec.set(node, "hostname", self.fedora_host)
@@ -119,6 +119,93 @@ class LinuxNS3ClientTest(unittest.TestCase):
 
         ec.shutdown()
 
+    def test_real_time(self):
+        ec = ExperimentController(exp_id = "test-ns3-real-time")
+        
+        node = ec.register_resource("LinuxNode")
+        ec.set(node, "hostname", self.fedora_host)
+        ec.set(node, "username", self.fedora_user)
+        ec.set(node, "cleanProcesses", True)
+        #ec.set(node, "cleanHome", True)
+
+        simu = ec.register_resource("LinuxNS3Simulation")
+        ec.set(simu, "verbose", True)
+        ec.set(simu, "simulatorImplementationType", "ns3::RealtimeSimulatorImpl")
+        ec.set(simu, "checksumEnabled", True)
+        ec.register_connection(simu, node)
+
+        nsnode1 = ec.register_resource("ns3::Node")
+        ec.register_connection(nsnode1, simu)
+
+        ipv41 = ec.register_resource("ns3::Ipv4L3Protocol")
+        ec.register_connection(nsnode1, ipv41)
+
+        arp1 = ec.register_resource("ns3::ArpL3Protocol")
+        ec.register_connection(nsnode1, arp1)
+        
+        icmp1 = ec.register_resource("ns3::Icmpv4L4Protocol")
+        ec.register_connection(nsnode1, icmp1)
+
+        p1 = ec.register_resource("ns3::PointToPointNetDevice")
+        ec.set(p1, "ip", "10.0.0.1")
+        ec.set(p1, "prefix", "30")
+        ec.register_connection(nsnode1, p1)
+        q1 = ec.register_resource("ns3::DropTailQueue")
+        ec.register_connection(p1, q1)
+
+        nsnode2 = ec.register_resource("ns3::Node")
+        ec.register_connection(nsnode2, simu)
+
+        ipv42 = ec.register_resource("ns3::Ipv4L3Protocol")
+        ec.register_connection(nsnode2, ipv42)
+
+        arp2 = ec.register_resource("ns3::ArpL3Protocol")
+        ec.register_connection(nsnode2, arp2)
+        
+        icmp2 = ec.register_resource("ns3::Icmpv4L4Protocol")
+        ec.register_connection(nsnode2, icmp2)
+
+        p2 = ec.register_resource("ns3::PointToPointNetDevice")
+        ec.set(p2, "ip", "10.0.0.2")
+        ec.set(p2, "prefix", "30")
+        ec.register_connection(nsnode2, p2)
+        q2 = ec.register_resource("ns3::DropTailQueue")
+        ec.register_connection(p2, q2)
+
+        # Create channel
+        chan = ec.register_resource("ns3::PointToPointChannel")
+        ec.set(chan, "Delay", "0s")
+        ec.register_connection(chan, p1)
+        ec.register_connection(chan, p2)
+
+        ### create pinger
+        ping = ec.register_resource("ns3::V4Ping")
+        ec.set (ping, "Remote", "10.0.0.2")
+        ec.set (ping, "Interval", "1s")
+        ec.set (ping, "Verbose", True)
+        ec.set (ping, "StartTime", "0s")
+        ec.set (ping, "StopTime", "20s")
+        ec.register_connection(ping, nsnode1)
+
+        ec.deploy()
+
+        ec.wait_finished([ping])
+       
+        stdout = ec.trace(simu, "stdout") 
+
+        expected = "20 packets transmitted, 20 received, 0% packet loss"
+        self.assertTrue(stdout.find(expected) > -1)
+
+        rm = ec.get_resource(ping)
+        start_time = rm.start_time
+        stop_time = rm.stop_time
+        delta =  stop_time - start_time
+
+        self.assertTrue(delta.seconds >= 20)
+        self.assertTrue(delta.seconds < 25)
+
+        ec.shutdown()
+
 if __name__ == '__main__':
     unittest.main()