c74b24d6cc2265c021d0cd27e63e6f06593d1314
[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, just validate that the simulator is in real time
62         mode, otherwise it is not going to work.
63         """
64         mode = self.simulator.get("simulatorImplementationType")
65         if mode != "ns3::RealtimeSimulatorImpl":
66             msg = "The simulation must run in real time!!"
67                 self.error(msg)
68                 raise RuntimeError, msg
69
70     def _connect_object(self):
71         dev1 = self.devices[0]
72         dev2 = self.devices[1]
73
74         if dev1.uuid not in self.connected and dev2.uuid not in self.connected:
75             (s0, s1) = socket.socketpair(socket.AF_UNIX, socket.SOCK_STREAM, 0)
76             self._send_fd(dev1, s0)
77             self._send_fd(dev2, s1)
78
79     def _send_fd(self, dev, fd):
80         import passfd
81         
82         address = self.simulation.invoke(dev.uuid, "recvFD")
83         sock = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
84         sock.connect(address)
85         passfd.sendfd(sock, fd, '0')
86
87         self._connected.add(dev.uuid)
88         dev._connected.add(self.uuid)
89