applied the except and raise fixers to the master branch to close the gap with py3
[nepi.git] / src / nepi / resources / ns3 / ns3netdevice.py
index 87c01df..946f01a 100644 (file)
@@ -3,9 +3,8 @@
 #    Copyright (C) 2014 INRIA
 #
 #    This program is free software: you can redistribute it and/or modify
-#    it under the terms of the GNU General Public License as published by
-#    the Free Software Foundation, either version 3 of the License, or
-#    (at your option) any later version.
+#    it under the terms of the GNU General Public License version 2 as
+#    published by the Free Software Foundation;
 #
 #    This program is distributed in the hope that it will be useful,
 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -19,6 +18,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 +42,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
@@ -50,7 +65,7 @@ class NS3BaseNetDevice(NS3Base):
         if not nodes: 
             msg = "Device not connected to node"
             self.error(msg)
-            raise RuntimeError, msg
+            raise RuntimeError(msg)
 
         return nodes[0]
 
@@ -62,34 +77,76 @@ class NS3BaseNetDevice(NS3Base):
         if not channels: 
             msg = "Device not connected to channel"
             self.error(msg)
-            raise RuntimeError, msg
+            raise RuntimeError(msg)
 
         return channels[0]
 
     @property
-    def _rms_to_wait(self):
-        others = set()
-        
-        node = self.node
-        others.add(node)
+    def queue(self):
+        from nepi.resources.ns3.ns3queue import NS3BaseQueue
+        queue = self.get_connected(NS3BaseQueue.get_rtype())
 
-        ipv4 = node.ipv4
-        if node.ipv4:
-            others.add(ipv4)
+        if not queue: 
+            msg = "Device not connected to queue"
+            self.error(msg)
+            raise RuntimeError(msg)
 
-        others.add(self.channel)
-        return others
+        return queue[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"
+            elif rtype == "ns3::FdNetDevice":
+                classname = "FdNetDeviceHelper"
+
+            self._device_helper_uuid = self.simulation.create(classname)
+
+        return self._device_helper_uuid
+
+    @property
+    def _rms_to_wait(self):
+        rms = set([self.node, self.channel])
+        return rms
 
     def _configure_object(self):
         # Set Mac
+        self._configure_mac_address()
+
+        # Set IP address
+        self._configure_ip_address()
+        
+        # Enable traces
+        self._configure_traces()
+
+    def _configure_mac_address(self):
         mac = self.get("mac")
         if mac:
             mac_uuid = self.simulation.create("Mac48Address", mac)
         else:
             mac_uuid = self.simulation.invoke("singleton::Mac48Address", "Allocate")
+
         self.simulation.invoke(self.uuid, "SetAddress", mac_uuid)
 
-        # Set IP address
+    def _configure_ip_address(self):
         ip = self.get("ip")
         prefix = self.get("prefix")
 
@@ -112,6 +169,42 @@ class NS3BaseNetDevice(NS3Base):
             # TODO!
             pass
 
+    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
         if node and node.uuid not in self.connected:
@@ -122,4 +215,8 @@ class NS3BaseNetDevice(NS3Base):
         if channel and channel.uuid not in self.connected:
             self.simulation.invoke(self.uuid, "Attach", channel.uuid)
             self._connected.add(channel.uuid)
+        
+        # Verify that the device has a queue. If no queue is added a segfault 
+        # error occurs
+        queue = self.queue