from twisted.spread import pb
class MultipleClientPerspective(pb.Perspective):
"""Many clients may use this Perspective at once."""
def __init__(self, perspectiveName, identityName="Nobody"):
pb.Perspective.__init__(self, perspectiveName, identityName)
self.clients = {}
def attached(self, client, identity):
self.clients[client] = identity
return self
def detached(self, client, identity):
try:
del self.clients[client]
except KeyError:
pass
def sendMessage(self, message):
"""Pass a message to my clients' console.
"""
for client in self.clients.keys():
try:
client.callRemote('message', message)
except pb.DeadReferenceError:
self.detached(client, None)
def __getstate__(self):
state = styles.Versioned.__getstate__(self)
state['clients'] = {}
return state