summaryrefslogtreecommitdiff
path: root/py/demo/Ice/hello/Client.py
blob: acb925d3ab3fdc0459dd722b7ac9aa84051624d5 (plain)
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/env python
# **********************************************************************
#
# Copyright (c) 2003-2014 ZeroC, Inc. All rights reserved.
#
# This copy of Ice is licensed to you under the terms described in the
# ICE_LICENSE file included in this distribution.
#
# **********************************************************************

import sys, traceback, Ice

Ice.loadSlice('Hello.ice')
import Demo

def menu():
    print("""
usage:
t: send greeting as twoway
o: send greeting as oneway
O: send greeting as batch oneway
d: send greeting as datagram
D: send greeting as batch datagram
f: flush all batch requests
T: set a timeout
P: set a server delay
S: switch secure mode on/off
s: shutdown server
x: exit
?: help
""")

class Client(Ice.Application):
    def run(self, args):
        if len(args) > 1:
            print(self.appName() + ": too many arguments")
            return 1

        twoway = Demo.HelloPrx.checkedCast(\
            self.communicator().propertyToProxy('Hello.Proxy').ice_twoway().ice_secure(False))
        if not twoway:
            print(args[0] + ": invalid proxy")
            return 1

        oneway = Demo.HelloPrx.uncheckedCast(twoway.ice_oneway())
        batchOneway = Demo.HelloPrx.uncheckedCast(twoway.ice_batchOneway())
        datagram = Demo.HelloPrx.uncheckedCast(twoway.ice_datagram())
        batchDatagram = Demo.HelloPrx.uncheckedCast(twoway.ice_batchDatagram())

        secure = False
        timeout = -1
        delay = 0

        menu()

        c = None
        while c != 'x':
            try:
                sys.stdout.write("==> ")
                sys.stdout.flush()
                c = sys.stdin.readline().strip()
                if c == 't':
                    twoway.sayHello(delay)
                elif c == 'o':
                    oneway.sayHello(delay)
                elif c == 'O':
                    batchOneway.sayHello(delay)
                elif c == 'd':
                    if secure:
                        print("secure datagrams are not supported")
                    else:
                        datagram.sayHello(delay)
                elif c == 'D':
                    if secure:
                        print("secure datagrams are not supported")
                    else:
                        batchDatagram.sayHello(delay)
                elif c == 'f':
                    self.communicator().flushBatchRequests()
                elif c == 'T':
                    if timeout == -1:
                        timeout = 2000
                    else:
                        timeout = -1

                    twoway = Demo.HelloPrx.uncheckedCast(twoway.ice_invocationTimeout(timeout))
                    oneway = Demo.HelloPrx.uncheckedCast(oneway.ice_invocationTimeout(timeout))
                    batchOneway = Demo.HelloPrx.uncheckedCast(batchOneway.ice_invocationTimeout(timeout))

                    if timeout == -1:
                        print("timeout is now switched off")
                    else:
                        print("timeout is now set to 2000ms")
                elif c == 'P':
                    if delay == 0:
                        delay = 2500
                    else:
                        delay = 0

                    if delay == 0:
                        print("server delay is now deactivated")
                    else:
                        print("server delay is now set to 2500ms")
                elif c == 'S':
                    secure = not secure

                    twoway = Demo.HelloPrx.uncheckedCast(twoway.ice_secure(secure))
                    oneway = Demo.HelloPrx.uncheckedCast(oneway.ice_secure(secure))
                    batchOneway = Demo.HelloPrx.uncheckedCast(batchOneway.ice_secure(secure))
                    datagram = Demo.HelloPrx.uncheckedCast(datagram.ice_secure(secure))
                    batchDatagram = Demo.HelloPrx.uncheckedCast(batchDatagram.ice_secure(secure))

                    if secure:
                        print("secure mode is now on")
                    else:
                        print("secure mode is now off")
                elif c == 's':
                    twoway.shutdown()
                elif c == 'x':
                    pass # Nothing to do
                elif c == '?':
                    menu()
                else:
                    print("unknown command `" + c + "'")
                    menu()
            except KeyboardInterrupt:
                break
            except EOFError:
                break
            except Ice.Exception as ex:
                print(ex)

        return 0

app = Client()
sys.exit(app.main(sys.argv, "config.client"))