File: twistedEchoClient.py

package info (click to toggle)
pythoncard 0.8.2-2
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 8,452 kB
  • sloc: python: 56,787; makefile: 56; sh: 22
file content (91 lines) | stat: -rw-r--r-- 2,892 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
"""
Twisted PythonCard PbEchoClient
"""

from PythonCard import model, twistedModel
from twisted.cred.credentials import UsernamePassword
from twisted.spread import pb
from twisted.internet import reactor

from PythonCard.templates.dialogs.loginDialog import loginDialog

class DefinedError(pb.Error):
    pass

class EchoClient(model.Background):
    """
    TPC PB Echo GUI Panel
    """
    
    def on_initialize(self, event):
        self.pbfactory = pb.PBClientFactory()
        # KEA the Send button and SendTextField should be disabled
        # until a successful login
        self.components.SendTextField.enabled = False
        self.components.buttonSend.enabled = False

    def on_SendTextField_keyPress(self, event):
        # if user presses return, send text
        if event.keyCode == 13:
            self.sendAndClearText()
        else:
            event.skip()

    # KEA 2004-04-27
    # this should popup a custom dialog
    # to prompt the user for the host, port number,
    # username, and password
    # with defaults of "localhost", pb.portno
    # "guest", and "guest"
    # this dialog is going to be pretty common so we'll stick
    # in PythonCard/templates/dialogs to simplify usage from
    # other twisted apps
    def on_buttonLogin_mouseClick(self, event):
        result = loginDialog(self, port=pb.portno)
        if result.accepted:
            # verify input here?
            host = result.host
            port = result.port
            username = result.username
            password = result.password
            reactor.connectTCP(host, port, self.pbfactory)
            self.pbfactory.login(
                UsernamePassword(username, password)
                    ).addCallbacks(self.loginsuccess,
                                   self.loginfailure)

    def loginsuccess(self, perspective):
        self.statusBar.text = 'Connected'
        self.components.SendTextField.enabled = True
        self.components.buttonSend.enabled = True
        self.components.SendTextField.setFocus()
        self.perspective = perspective

    def loginfailure(self, error):
        self.displaycontent("Error on login: %s" % error)

    def sendAndClearText(self):
        fld = self.components.SendTextField
        self.perspective.callRemote('echo', fld.text
            ).addCallbacks(self.echosuccess,
                           self.echofailure)
        fld.text = ""

    def on_buttonSend_mouseClick(self, event):
        self.sendAndClearText()

    def echosuccess(self, message):
        self.displaycontent(message)

    def echofailure(self, error):
        t = error.trap(DefinedError)
        self.displaycontent("error received"+t)

    def displaycontent(self, text):
        self.components.ReceivedTextArea.appendText(text + "\n")


if __name__ == '__main__':
    app = twistedModel.TwistedApplication(EchoClient)
    app.MainLoop()