+# TODO: convertir HopHandler a ComposedSubHandler
+
+class HopError(HandlerError):
+
+ def __init__(self, hop):
+ self.message = u'Hop error : "%s"' % hop
+
+class HopNotFoundError(HopError):
+
+ def __init__(self, hop):
+ self.message = u'Hop not found : "%s"' % hop
+
+class HopAlreadyExistsError(HopError):
+
+ def __init__(self, hop):
+ self.message = u'Hop already exists : "%s"' % hop
+
+
+class Hop(Sequence):
+
+ def __init__(self, gateway, device):
+ self.gateway = gateway
+ self.device = device
+
+ def as_tuple(self):
+ return (self.gateway, self.device)
+
+ def __cmp__(self, other):
+ if self.gateway == other.gateway \
+ and self.device == other.device:
+ return 0
+ return cmp(id(self), id(other))
+
+class HopHandler(Handler):
+
+ def __init__(self, parent):
+ self.parent = parent
+
+ @handler('Adds a hop : add <gateway> <device>')
+ def add(self, gw, dev):
+ if not dev in self.parent.devices:
+ raise DeviceNotFoundError(device)
+ h = Hop(gw, dev)
+ try:
+ self.parent.hops.index(h)
+ raise HopAlreadyExistsError(gw + '->' + dev)
+ except ValueError:
+ self.parent.hops.append(h)
+
+ @handler(u'Deletes a hop : delete <gateway> <device>')
+ def delete(self, gw, dev):
+ if not dev in self.parent.devices:
+ raise DeviceNotFoundError(device)
+ h = Hop(gw, dev)
+ try:
+ self.parent.hops.remove(h)
+ except ValueError:
+ raise HopNotFoundError(gw + '->' + dev)
+
+ @handler(u'Lists hops : list <dev>')
+ def list(self, device):
+ try:
+ k = self.parent.hops.keys()
+ except ValueError:
+ k = list()
+ return k
+
+ @handler(u'Get information about all hops: show <dev>')
+ def show(self, device):
+ try:
+ k = self.parent.hops.values()
+ except ValueError:
+ k = list()
+ return k
+