~certify-web-dev/twisted/certify-trunk

« back to all changes in this revision

Viewing changes to doc/howto/listings/pclients/single-kick.py

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2007-01-17 14:52:35 UTC
  • mfrom: (1.1.5 upstream) (2.1.2 etch)
  • Revision ID: james.westby@ubuntu.com-20070117145235-btmig6qfmqfen0om
Tags: 2.5.0-0ubuntu1
New upstream version, compatible with python2.5.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
from twisted.spread import pb
2
 
 
3
 
class SingleClientWithAKickPerspective(pb.Perspective):
4
 
    """One client may attach to me at a time.
5
 
 
6
 
    If a new client requests to be attached to me, any currently
7
 
    connected perspective will be disconnected.
8
 
    """
9
 
 
10
 
    # This example is from twisted.words.service.Participant.
11
 
 
12
 
    client = None
13
 
 
14
 
    def __getstate__(self):
15
 
        state = styles.Versioned.__getstate__(self)
16
 
        try:
17
 
            del state['client']
18
 
        except KeyError:
19
 
            pass
20
 
        return state
21
 
 
22
 
    def attached(self, client, identity):
23
 
        if self.client is not None:
24
 
            self.detached(client, identity)
25
 
        self.client = client
26
 
        return self
27
 
 
28
 
    def detached(self, client, identity):
29
 
        self.client = None
30
 
 
31
 
        # For the case where 'detached' was called by 'attached' wanting to
32
 
        # kick someone off, is this all we need to do?  I'm afraid not --
33
 
        # no-one ever told the client that it had been detached!  So the
34
 
        # client, which will still have a reference to this perspective until
35
 
        # its broker dies, will continue to execute methods on it, will
36
 
        # continue to get results returned by those methods.  It just won't get
37
 
        # events sent to self.client.  Meanwhile, the newly attached client
38
 
        # will probably be confused, because its Perspective is doing things
39
 
        # the new client did not ask it to do and it thinks it is the only
40
 
        # thing connected.