2 # NEPI, a framework to manage network experiments
3 # Copyright (C) 2014 INRIA
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.
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.
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/>.
18 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
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
27 class NS3BasePipeChannel(NS3Base):
28 """ Interconnects two FdNetDevices with a PIPE
30 _rtype = "ns3::PipeChannel"
32 def __init__(self, ec, guid):
33 super(NS3BasePipeChannel, self).__init__(ec, guid)
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"
44 raise RuntimeError, msg
46 self._devices = devices
52 return self.devices[0].node
55 def _rms_to_wait(self):
56 rms = set(self.devices)
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.
64 mode = self.simulator.get("simulatorImplementationType")
65 if mode != "ns3::RealtimeSimulatorImpl":
66 msg = "The simulation must run in real time!!"
68 raise RuntimeError, msg
70 def _connect_object(self):
71 dev1 = self.devices[0]
72 dev2 = self.devices[1]
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)
79 def _send_fd(self, dev, fd):
82 address = self.simulation.invoke(dev.uuid, "recvFD")
83 sock = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
85 passfd.sendfd(sock, fd, '0')
87 self._connected.add(dev.uuid)
88 dev._connected.add(self.uuid)