2014-07-12 22:18:25 +03:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
# Copyright (C) 2014 GNS3 Technologies Inc.
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
|
|
|
class Adapter(object):
|
2015-01-20 14:24:00 +02:00
|
|
|
|
2014-07-12 22:18:25 +03:00
|
|
|
"""
|
|
|
|
Base class for adapters.
|
|
|
|
|
|
|
|
:param interfaces: number of interfaces supported by this adapter.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, interfaces=1):
|
|
|
|
|
|
|
|
self._interfaces = interfaces
|
|
|
|
|
|
|
|
self._ports = {}
|
2015-01-31 21:01:23 +02:00
|
|
|
for port_number in range(0, interfaces):
|
|
|
|
self._ports[port_number] = None
|
2014-07-12 22:18:25 +03:00
|
|
|
|
|
|
|
def removable(self):
|
|
|
|
"""
|
|
|
|
Returns True if the adapter can be removed from a slot
|
|
|
|
and False if not.
|
|
|
|
|
|
|
|
:returns: boolean
|
|
|
|
"""
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
2015-01-31 21:01:23 +02:00
|
|
|
def port_exists(self, port_number):
|
2014-07-12 22:18:25 +03:00
|
|
|
"""
|
|
|
|
Checks if a port exists on this adapter.
|
|
|
|
|
|
|
|
:returns: True is the port exists,
|
|
|
|
False otherwise.
|
|
|
|
"""
|
|
|
|
|
2015-01-31 21:01:23 +02:00
|
|
|
if port_number in self._ports:
|
2014-07-12 22:18:25 +03:00
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2015-01-31 21:01:23 +02:00
|
|
|
def add_nio(self, port_number, nio):
|
2014-07-12 22:18:25 +03:00
|
|
|
"""
|
|
|
|
Adds a NIO to a port on this adapter.
|
|
|
|
|
2015-01-31 21:01:23 +02:00
|
|
|
:param port_number: port number (integer)
|
2014-07-12 22:18:25 +03:00
|
|
|
:param nio: NIO instance
|
|
|
|
"""
|
|
|
|
|
2015-01-31 21:01:23 +02:00
|
|
|
self._ports[port_number] = nio
|
2014-07-12 22:18:25 +03:00
|
|
|
|
2015-01-31 21:01:23 +02:00
|
|
|
def remove_nio(self, port_number):
|
2014-07-12 22:18:25 +03:00
|
|
|
"""
|
|
|
|
Removes a NIO from a port on this adapter.
|
|
|
|
|
2015-01-31 21:01:23 +02:00
|
|
|
:param port_number: port number (integer)
|
2014-07-12 22:18:25 +03:00
|
|
|
"""
|
|
|
|
|
2015-01-31 21:01:23 +02:00
|
|
|
self._ports[port_number] = None
|
2014-07-12 22:18:25 +03:00
|
|
|
|
2015-01-31 21:01:23 +02:00
|
|
|
def get_nio(self, port_number):
|
2014-07-12 22:18:25 +03:00
|
|
|
"""
|
|
|
|
Returns the NIO assigned to a port.
|
|
|
|
|
2015-01-31 21:01:23 +02:00
|
|
|
:params port_number: port number (integer)
|
2014-07-12 22:18:25 +03:00
|
|
|
|
|
|
|
:returns: NIO instance
|
|
|
|
"""
|
|
|
|
|
2015-01-31 21:01:23 +02:00
|
|
|
return self._ports[port_number]
|
2014-07-12 22:18:25 +03:00
|
|
|
|
|
|
|
@property
|
|
|
|
def ports(self):
|
|
|
|
"""
|
|
|
|
Returns port to NIO mapping
|
|
|
|
|
|
|
|
:returns: dictionary port -> NIO
|
|
|
|
"""
|
|
|
|
|
|
|
|
return self._ports
|
|
|
|
|
|
|
|
@property
|
|
|
|
def interfaces(self):
|
|
|
|
"""
|
|
|
|
Returns the number of interfaces supported by this adapter.
|
|
|
|
|
|
|
|
:returns: number of interfaces
|
|
|
|
"""
|
|
|
|
|
|
|
|
return self._interfaces
|