Adding trace support for ns3 RMs
[nepi.git] / src / nepi / resources / ns3 / ns3netdevice.py
index 87c01df..e3af336 100644 (file)
@@ -19,6 +19,7 @@
 
 from nepi.execution.attribute import Attribute, Flags
 from nepi.execution.resource import clsinit_copy
+from nepi.execution.trace import Trace
 from nepi.resources.ns3.ns3base import NS3Base
 
 import ipaddr
@@ -42,6 +43,21 @@ class NS3BaseNetDevice(NS3Base):
         cls._register_attribute(ip)
         cls._register_attribute(prefix)
 
+    @classmethod
+    def _register_traces(cls):
+        pcap = Trace("pcap", "Dump traffic sniffed on the network device in Pcap format")
+        promisc_pcap = Trace("promiscPcap", "Dump traffic sniffed in promiscuous mode on the network device in Pcap format")
+        ascii = Trace("ascii", "Dump traffic sniffed on the network device in Ascii format")
+
+        cls._register_trace(pcap)
+        cls._register_trace(promisc_pcap)
+        cls._register_trace(ascii)
+
+    def __init__(self, ec, guid):
+        super(NS3BaseNetDevice, self).__init__(ec, guid)
+        self._ascii_helper_uuid = None
+        self._device_helper_uuid = None
+
     @property
     def node(self):
         from nepi.resources.ns3.ns3node import NS3BaseNode
@@ -66,6 +82,33 @@ class NS3BaseNetDevice(NS3Base):
 
         return channels[0]
 
+    @property
+    def ascii_helper_uuid(self):
+        if not self._ascii_helper_uuid:
+            self._ascii_helper_uuid = self.simulation.create("AsciiTraceHelper")
+        return self._ascii_helper_uuid
+
+    @property
+    def device_helper_uuid(self):
+        if not self._device_helper_uuid:
+            rtype = self.get_rtype()
+            if rtype == "ns3::PointToPointNetDevice":
+                classname = "PointToPointHelper"
+            elif rtype == "ns3::CsmaNetDevice":
+                classname = "CsmaHelper"
+            elif rtype == "ns3::EmuNetDevice":
+                classname = "EmuHelper"
+            elif rtype == "ns3::FdNetDevice":
+                classname = "FdNetDeviceHelper"
+            elif rtype in [ "ns3::BaseStationNetDevice", "SubscriberStationNetDevice" ]:
+                classname = "WimaxHelper"
+            elif rtype == "ns3::WifiNetDevice":
+                classname = "YansWifiPhyHelper"
+
+            self._device_helper_uuid = self.simulation.create(classname)
+
+        return self._device_helper_uuid
+
     @property
     def _rms_to_wait(self):
         others = set()
@@ -111,6 +154,45 @@ class NS3BaseNetDevice(NS3Base):
             # IPv6
             # TODO!
             pass
+        
+        # Enable traces
+        self._configure_traces()
+
+    def _configure_traces(self):
+        if self.trace_enabled("pcap"):
+            helper_uuid = self.device_helper_uuid
+
+            filename = "trace-pcap-netdev-%d.pcap" % self.guid
+            self._trace_filename["pcap"] = filename
+
+            filepath = self.simulation.trace_filepath(filename)
+
+            self.simulation.invoke(helper_uuid, "EnablePcap", filepath, 
+                    self.uuid, promiscuous = False, explicitFilename = True)
+
+        if self.trace_enabled("promiscPcap"):
+            helper_uuid = self.device_helper_uuid
+
+            filename = "trace-promisc-pcap-netdev-%d.pcap" % self.guid
+            self._trace_filename["promiscPcap"] = filename
+
+            filepath = self.simulation.trace_filepath(filename)
+
+            self.simulation.invoke(helper_uuid, "EnablePcap", filepath, 
+                    self.uuid, promiscuous = True, explicitFilename = True)
+
+        if self.trace_enabled("ascii"):
+            helper_uuid = self.device_helper_uuid
+            ascii_helper_uuid = self.ascii_helper_uuid
+
+            filename = "trace-ascii-netdev-%d.tr" % self.guid
+            self._trace_filename["ascii"] = filename
+
+            filepath = self.simulation.trace_filepath(filename)
+            stream_uuid = self.simulation.invoke(ascii_helper_uuid, 
+                    "CreateFileStream", filepath) 
+            self.simulation.invoke(helper_uuid, "EnableAscii", stream_uuid,
+                    self.uuid)
 
     def _connect_object(self):
         node = self.node