~vishvananda/nova/network-refactor

« back to all changes in this revision

Viewing changes to vendor/Twisted-10.0.0/doc/words/examples/pb_client.py

  • Committer: Jesse Andrews
  • Date: 2010-05-28 06:05:26 UTC
  • Revision ID: git-v1:bf6e6e718cdc7488e2da87b21e258ccc065fe499
initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
 
 
3
# Copyright (c) 2009 Twisted Matrix Laboratories.
 
4
# See LICENSE for details.
 
5
 
 
6
"""
 
7
Simple PB Words client demo
 
8
 
 
9
This connects to a server (host/port specified by argv[1]/argv[2]),
 
10
authenticates with a username and password (given by argv[3] and argv[4]),
 
11
joins a group (argv[5]) sends a simple message, leaves the group, and quits
 
12
the server.
 
13
"""
 
14
 
 
15
import sys
 
16
from twisted.python import log
 
17
from twisted.cred import credentials
 
18
from twisted.words import service
 
19
from twisted.spread import pb
 
20
from twisted.internet import reactor
 
21
 
 
22
class DemoMind(service.PBMind):
 
23
    """An utterly pointless PBMind subclass.
 
24
 
 
25
    This notices messages received and prints them to stdout.  Since
 
26
    the bot never stays in a channel very long, it is exceedingly
 
27
    unlikely this will ever do anything interesting.
 
28
    """
 
29
    def remote_receive(self, sender, recipient, message):
 
30
        print 'Woop', sender, recipient, message
 
31
 
 
32
def quitServer(ignored):
 
33
    """Quit succeeded, shut down the reactor.
 
34
    """
 
35
    reactor.stop()
 
36
 
 
37
def leftGroup(ignored, avatar):
 
38
    """Left the group successfully, quit the server.
 
39
    """
 
40
    q = avatar.quit()
 
41
    q.addCallback(quitServer)
 
42
    return q
 
43
 
 
44
def sentMessage(ignored, group, avatar):
 
45
    """Sent the message successfully, leave the group.
 
46
    """
 
47
    l = group.leave()
 
48
    l.addCallback(leftGroup, avatar)
 
49
    return l
 
50
 
 
51
def joinedGroup(group, avatar):
 
52
    """Joined the group successfully, send a stupid message.
 
53
    """
 
54
    s = group.send({"text": "Hello, monkeys"})
 
55
    s.addCallback(sentMessage, group, avatar)
 
56
    return s
 
57
 
 
58
def loggedIn(avatar, group):
 
59
    """Logged in successfully, join a group.
 
60
    """
 
61
    j = avatar.join(group)
 
62
    j.addCallback(joinedGroup, avatar)
 
63
    return j
 
64
 
 
65
def errorOccurred(err):
 
66
    """Something went awry, log it and shutdown.
 
67
    """
 
68
    log.err(err)
 
69
    try:
 
70
        reactor.stop()
 
71
    except RuntimeError:
 
72
        pass
 
73
 
 
74
def run(host, port, username, password, group):
 
75
    """Create a mind and factory and set things in motion.
 
76
    """
 
77
    m = DemoMind()
 
78
    f = pb.PBClientFactory()
 
79
    f.unsafeTracebacks = True
 
80
    l = f.login(credentials.UsernamePassword(username, password), m)
 
81
    l.addCallback(loggedIn, group)
 
82
    l.addErrback(errorOccurred)
 
83
    reactor.connectTCP(host, int(port), f)
 
84
 
 
85
def main():
 
86
    """
 
87
    Set up logging, have the real main function run, and start the reactor.
 
88
    """
 
89
    if len(sys.argv) != 6:
 
90
        raise SystemExit("Usage: %s host port username password group" % (sys.argv[0],))
 
91
    log.startLogging(sys.stdout)
 
92
 
 
93
    host, port, username, password, group = sys.argv[1:]
 
94
    port = int(port)
 
95
    username = username.decode(sys.stdin.encoding)
 
96
    group = group.decode(sys.stdin.encoding)
 
97
 
 
98
    reactor.callWhenRunning(run, host, port, username, password, group)
 
99
    reactor.run()
 
100
 
 
101
if __name__ == '__main__':
 
102
    main()