"Ethernet devices"

import dev

# Interface cards
class GenericEtherCard(dev.Interface):
	def can_connect(self, other):
		"An Ethernet card connects a host and an Ethernet cable"
		if len(self.ifaces) == 0:
			return (isinstance(other, EtherCable) or
					isinstance(other, dev.Host))
		elif len(self.ifaces) == 1:
			return ((isinstance(other, EtherCable) and
				isinstance(self.ifaces[0], dev.Host)) or
				(isinstance(other, dev.Host) and
				isinstance(self.ifaces[0], EtherCable)))
		else:
			return 0
class EtherCard(GenericEtherCard):
	pass
class FastEtherCard(GenericEtherCard):
	pass
class GigabitEtherCard(GenericEtherCard):
	pass

# Cabling
class EtherCable(dev.Cable):
	def can_connect(self, other):
		"An Ethernet cable connects two Ethernet cards"
		return (isinstance(other, GenericEtherCard) and 
				len(self.ifaces) < 2)

# Miscellaneous devices
class EtherRepeater(dev.Repeater, GenericEtherCard):
	def can_connect(self, other):
		"An Ethernet repeater connects two Ethernet cables"
		return isinstance(other, EtherCable) and len(self.ifaces) < 2
class EtherHub(dev.Repeater, GenericEtherCard):
	def can_connect(self, other):
		"An Ethernet hub connects Ethernet cables"
		return isinstance(other, EtherCable)
class EtherBridge(dev.NetDevice, GenericEtherCard):
	def can_connect(self, other):
		"An Ethernet bridge connects two Ethernet cables"
		return isinstance(other, EtherCable) and len(self.ifaces) < 2
class EtherSwitch(dev.Bridge, GenericEtherCard):
	def can_connect(self, other):
		"An Ethernet switch connects Ethernet cables"
		return isinstance(other, EtherCable)
