applied the except and raise fixers to the master branch to close the gap with py3
[nepi.git] / src / nepi / resources / planetlab / ns3 / tuntapfdlink.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.resource import ResourceState, clsinit_copy
20 from nepi.resources.linux.ns3.tuntapfdlink import LinuxTunTapFdLink
21
22 import base64
23 import fcntl
24 import os
25 import socket
26 import struct
27
28 @clsinit_copy
29 class PlanetlabTunTapFdLink(LinuxTunTapFdLink):
30     """ Interconnects a TAP or TUN PlanetLab device to a FdNetDevice
31     """
32     _rtype = "planetlab::ns3::TunTapFdLink"
33
34     def __init__(self, ec, guid):
35         super(PlanetlabTunTapFdLink, self).__init__(ec, guid)
36
37     @property
38     def fdnetdevice(self):
39         if not self._fdnetdevice:
40             from nepi.resources.ns3.ns3fdnetdevice import NS3BaseFdNetDevice
41             devices = self.get_connected(NS3BaseFdNetDevice.get_rtype())
42             if not devices or len(devices) != 1: 
43                 msg = "planetlab::ns3::TunTapFdLink must be connected to exactly one FdNetDevice"
44                 self.error(msg)
45                 raise RuntimeError(msg)
46
47             self._fdnetdevice = devices[0]
48
49             # Set PI headers on
50             self._fdnetdevice.set("EncapsulationMode", "DixPi")
51         
52             simu = self._fdnetdevice.simulation
53             from nepi.resources.planetlab.node import PlanetlabNode
54             nodes = simu.get_connected(PlanetlabNode.get_rtype())
55             self._fd_node = nodes[0]
56         
57         return self._fdnetdevice
58
59     @property
60     def tap(self):
61         if not self._tap:
62             from nepi.resources.planetlab.tap import PlanetlabTap
63             devices = self.get_connected(PlanetlabTap.get_rtype())
64
65             if not devices or len(devices) != 1: 
66                 msg = "planetlab::ns3::TunTapFdLink must be connected to exactly one PlanetlabTap"
67                 self.error(msg)
68                 raise RuntimeError(msg)
69
70             self._tap = devices[0]
71         
72         return self._tap
73
74     def upload_sources(self):
75         scripts = []
76
77         # vif-passfd python script
78         pl_vif_passfd = os.path.join(os.path.dirname(__file__), 
79                 "..", "..",
80                 "linux",
81                 "scripts",
82                 "linux-tap-passfd.py")
83
84         scripts.append(pl_vif_passfd)
85         
86         # Upload scripts
87         scripts = ";".join(scripts)
88
89         self.node.upload(scripts,
90                 os.path.join(self.node.src_dir),
91                 overwrite = False)
92