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