diff options
Diffstat (limited to 'python/src/tcf/services/remote/LocatorProxy.py')
-rw-r--r-- | python/src/tcf/services/remote/LocatorProxy.py | 87 |
1 files changed, 57 insertions, 30 deletions
diff --git a/python/src/tcf/services/remote/LocatorProxy.py b/python/src/tcf/services/remote/LocatorProxy.py index d9963686e..730c095bb 100644 --- a/python/src/tcf/services/remote/LocatorProxy.py +++ b/python/src/tcf/services/remote/LocatorProxy.py @@ -1,5 +1,5 @@ -# ******************************************************************************* -# * Copyright (c) 2011 Wind River Systems, Inc. and others. +# ***************************************************************************** +# * Copyright (c) 2011, 2013 Wind River Systems, Inc. and others. # * All rights reserved. This program and the accompanying materials # * are made available under the terms of the Eclipse Public License v1.0 # * which accompanies this distribution, and is available at @@ -7,26 +7,30 @@ # * # * Contributors: # * Wind River Systems - initial API and implementation -# ******************************************************************************* +# ***************************************************************************** + +from .. import locator +from ... import protocol, peer, channel +from ...channel.Command import Command -from tcf import protocol, peer, channel -from tcf.services import locator -from tcf.channel.Command import Command class Peer(peer.TransientPeer): def __init__(self, parent, attrs): super(Peer, self).__init__(attrs) self.parent = parent + def openChannel(self): assert protocol.isDispatchThread() c = self.parent.openChannel() c.redirect(self.getID()) return c + class ChannelEventListener(channel.EventListener): def __init__(self, proxy): self.proxy = proxy self.channel = proxy.channel + def event(self, name, data): try: args = channel.fromJSONSequence(data) @@ -41,48 +45,58 @@ class ChannelEventListener(channel.EventListener): try: l.peerAdded(_peer) except Exception as x: - protocol.log("Unhandled exception in Locator listener", x) + protocol.log("Unhandled exception in Locator listener", + x) elif name == "peerChanged": assert len(args) == 1 m = args[0] - if not m: raise Exception("Locator service: invalid peerChanged event - no peer ID") + if not m: + raise Exception("Locator service: invalid peerChanged " + + "event - no peer ID") _peer = self.proxy.peers.get(m.get(peer.ATTR_ID)) - if not _peer: return + if not _peer: + return self.proxy.peers[_peer.getID()] = _peer for l in self.proxy.listeners: try: l.peerChanged(_peer) except Exception as x: - protocol.log("Unhandled exception in Locator listener", x) + protocol.log("Unhandled exception in Locator listener", + x) elif name == "peerRemoved": assert len(args) == 1 - id = args[0] - _peer = self.proxy.peers.get(id) - if not _peer: return - del self.proxy.peers[id] + peerID = args[0] + _peer = self.proxy.peers.get(peerID) + if not _peer: + return + del self.proxy.peers[peerID] for l in self.proxy.listeners: try: - l.peerRemoved(id) + l.peerRemoved(peerID) except Exception as x: - protocol.log("Unhandled exception in Locator listener", x) + protocol.log("Unhandled exception in Locator listener", + x) elif name == "peerHeartBeat": assert len(args) == 1 - id = args[0] - _peer = self.proxy.peers.get(id) - if not _peer: return + peerID = args[0] + _peer = self.proxy.peers.get(peerID) + if not _peer: + return for l in self.proxy.listeners: try: - l.peerHeartBeat(id) + l.peerHeartBeat(peerID) except Exception as x: - protocol.log("Unhandled exception in Locator listener", x) + protocol.log("Unhandled exception in Locator listener", + x) else: raise IOError("Locator service: unknown event: " + name) except Exception as x: self.channel.terminate(x) + class LocatorProxy(locator.LocatorService): def __init__(self, channel): - self.channel = channel; + self.channel = channel self.peers = {} self.listeners = [] self.get_peers_done = False @@ -95,9 +109,12 @@ class LocatorProxy(locator.LocatorService): def redirect(self, _peer, done): done = self._makeCallback(done) service = self + class RedirectCommand(Command): def __init__(self): - super(RedirectCommand, self).__init__(service.channel, service, "redirect", [_peer]) + super(RedirectCommand, self).__init__(service.channel, service, + "redirect", [_peer]) + def done(self, error, args): if not error: assert len(args) == 1 @@ -108,11 +125,15 @@ class LocatorProxy(locator.LocatorService): def sync(self, done): done = self._makeCallback(done) service = self + class SyncCommand(Command): def __init__(self): - super(SyncCommand, self).__init__(service.channel, service, "sync", None) + super(SyncCommand, self).__init__(service.channel, service, + "sync", None) + def done(self, error, args): - if error: service.channel.terminate(error) + if error: + service.channel.terminate(error) done.doneSync(self.token) return SyncCommand().token @@ -120,9 +141,13 @@ class LocatorProxy(locator.LocatorService): self.listeners.append(listener) if not self.get_peers_done: service = self + class GetPeersCommand(Command): def __init__(self): - super(GetPeersCommand, self).__init__(service.channel, service, "getPeers", None) + super(GetPeersCommand, self).__init__(service.channel, + service, "getPeers", + None) + def done(self, error, args): if not error: assert len(args) == 2 @@ -133,15 +158,17 @@ class LocatorProxy(locator.LocatorService): c = args[1] if c: for m in c: - id = m.get(peer.ATTR_ID) - if service.peers.get(id): continue; + peerID = m.get(peer.ATTR_ID) + if service.peers.get(peerID): + continue _peer = Peer(service.channel.getRemotePeer(), m) - service.peers[id] = _peer + service.peers[peerID] = _peer for l in service.listeners: try: l.peerAdded(_peer) except Exception as x: - protocol.log("Unhandled exception in Locator listener", x) + protocol.log("Unhandled exception in " + + "Locator listener", x) GetPeersCommand() self.get_peers_done = True |