Adding dummy ns3 fd-net-device test
[nepi.git] / src / nepi / resources / ns3 / ns3pipechanel.py
1 #
2 #    NEPI, a framework to manage network experiments
3 #    Copyright (C) 2014 INRIA
4 #
5 #    This program is free software: you can redistribute it and/or modify
6 #    it under the terms of the GNU General Public License as published by
7 #    the Free Software Foundation, either version 3 of the License, or
8 #    (at your option) any later version.
9 #
10 #    This program is distributed in the hope that it will be useful,
11 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
12 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 #    GNU General Public License for more details.
14 #
15 #    You should have received a copy of the GNU General Public License
16 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 #
18 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
19
20 from nepi.execution.attribute import Attribute, Flags, Types
21 from nepi.execution.resource import clsinit_copy
22 from nepi.resources.ns3.ns3base import NS3Base
23
24 import socket
25
26 @clsinit_copy
27 class NS3BasePipeChannel(NS3Base):
28     """ Interconnects two FdNetDevices with a PIPE
29     """
30     _rtype = "ns3::PipeChannel"
31
32     def __init__(self, ec, guid):
33         super(NS3BasePipeChannel, self).__init__(ec, guid)
34         self._devices = None
35
36     @property
37     def devices(self):
38         if not self._devices:
39             from nepi.resources.ns3.ns3fdnetdevice import NS3BaseFdNetDevice
40             devices = self.get_connected(NS3BaseFdNetDevice.get_rtype())
41             if not devices or len(devices) != 2: 
42                 msg = "PipeChannel must be connected to exactly to two FdNetDevices"
43                 self.error(msg)
44                 raise RuntimeError, msg
45
46             self._devices = devices
47         
48         return self._devices
49
50     @property
51     def node(self):
52         return self.devices[0].node
53
54     @property
55     def _rms_to_wait(self):
56         rms = set(self.devices)
57         return rms
58
59     def _instantiate_object(self):
60         """ The pipe channel does not really exists as an ns-3 object.
61         Do nothing.
62         """
63         pass
64
65     def _connect_object(self):
66         dev1 = self.devices[0]
67         dev2 = self.devices[1]
68
69         if dev1.uuid not in self.connected and dev2.uuid not in self.connected:
70             (s0, s1) = socket.socketpair(socket.AF_UNIX, socket.SOCK_STREAM, 0)
71             
72             dev1.send_fd(s0)
73
74             self._connected.add(dev1.uuid)
75             dev1._connected.add(self.uuid)
76
77             dev2.send_fd(s1)
78
79             self._connected.add(dev2.uuid)
80             dev2._connected.add(self.uuid)
81
82