enable automatic node stack
[nepi.git] / src / nepi / resources / ns3 / ns3node.py
index 3f1ce33..8598979 100644 (file)
@@ -17,6 +17,7 @@
 #
 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
 
+from nepi.execution.attribute import Attribute, Flags, Types
 from nepi.execution.resource import clsinit_copy
 from nepi.resources.ns3.ns3base import NS3Base
 
@@ -24,6 +25,17 @@ from nepi.resources.ns3.ns3base import NS3Base
 class NS3BaseNode(NS3Base):
     _rtype = "abstract::ns3::Node"
 
+    @classmethod
+    def _register_attributes(cls):
+        enablestack = Attribute("enableStack", 
+                "Install network stack in Node, including: ARP, "
+                "IP4, ICMP, UDP and TCP ",
+                type = Types.Bool,
+                default = False,
+                flags = Flags.Design)
+
+        cls._register_attribute(enablestack)
+
     @property
     def simulation(self):
         from nepi.resources.ns3.ns3simulation import NS3Simulation
@@ -43,14 +55,75 @@ class NS3BaseNode(NS3Base):
         if ipv4s: return ipv4s[0]
         return None
 
+    @property
+    def mobility(self):
+        from nepi.resources.ns3.ns3mobilitymodel import NS3BaseMobilityModel
+        mobility = self.get_connected(NS3BaseMobilityModel.get_rtype())
+        if mobility: return mobility[0]
+        return None
+
+    @property
+    def devices(self):
+        from nepi.resources.ns3.ns3netdevice import NS3BaseNetDevice
+        devices = self.get_connected(NS3BaseNetDevice.get_rtype())
+
+        if not devices: 
+            msg = "Node not connected to devices"
+            self.error(msg)
+            raise RuntimeError, msg
+
+        return devices
+
+    @property
+    def dceapplications(self):
+        from nepi.resources.ns3.ns3dceapplication import NS3BaseDceApplication
+        dceapplications = self.get_connected(NS3BaseDceApplication.get_rtype())
+
+        return dceapplications
+
     @property
     def _rms_to_wait(self):
         rms = set()
         rms.add(self.simulation)
+
+        ipv4 = self.ipv4
+        if ipv4:
+            rms.add(ipv4)
+
+        mobility = self.mobility
+        if mobility:
+            rms.add(mobility)
+
         return rms
 
     def _configure_object(self):
-        ### node.AggregateObject(PacketSocketFactory())
-        uuid_packet_socket_factory = self.simulation.create("PacketSocketFactory")
-        self.simulation.invoke(self.uuid, "AggregateObject", uuid_packet_socket_factory)
+        if self.get("enableStack"):
+            uuid_stack_helper = self.simulation.create("InternetStackHelper")
+            self.simulation.invoke(uuid_stack_helper, "Install", self.uuid)
+        else:
+            ### node.AggregateObject(PacketSocketFactory())
+            uuid_packet_socket_factory = self.simulation.create("PacketSocketFactory")
+            self.simulation.invoke(self.uuid, "AggregateObject", uuid_packet_socket_factory)
+
+        dceapplications = self.dceapplications
+        if dceapplications:
+            self._add_dce(dceapplications)
+
+    def _connect_object(self):
+        ipv4 = self.ipv4
+        if ipv4:
+            self.simulation.invoke(self.uuid, "AggregateObject", ipv4.uuid)
+
+        mobility = self.mobility
+        if mobility:
+            self.simulation.invoke(self.uuid, "AggregateObject", mobility.uuid)
+
+    def _add_dce(self, dceapplications):
+        dceapp = dceapplications[0]
+
+        container_uuid = self.simulation.create("NodeContainer")
+        self.simulation.invoke(container_uuid, "Add", self.uuid)
+        with dceapp.dce_manager_lock:
+            self.simulation.invoke(dceapp.dce_manager_helper_uuid, 
+                    "Install", container_uuid)