-
Notifications
You must be signed in to change notification settings - Fork 215
/
Copy pathclient.py
executable file
·170 lines (148 loc) · 5.48 KB
/
client.py
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!/usr/bin/env python
#
# Copyright (c) ZeroC, Inc. All rights reserved.
#
import sys
import Ice
import Glacier2
Ice.loadSlice('Callback.ice')
import Demo
import uuid
def menu():
print("""
usage:
t: send callback as twoway
o: send callback as oneway
O: send callback as batch oneway
f: flush all batch requests
v: set/reset override context field
F: set/reset fake category
s: shutdown server
x: exit
?: help
""")
class CallbackReceiverI(Demo.CallbackReceiver):
def callback(self, current):
print("received callback")
def run(communicator):
router = Glacier2.RouterPrx.checkedCast(communicator.getDefaultRouter())
session = None
#
# Loop until we have successfully create a session.
#
while not session:
#
# Prompt the user for the credentials to create the session.
#
print("This demo accepts any user-id / password combination.")
sys.stdout.write("user id: ")
sys.stdout.flush()
id = sys.stdin.readline().strip()
sys.stdout.write("password: ")
sys.stdout.flush()
pw = sys.stdin.readline().strip()
#
# Try to create a session and break the loop if succeed,
# otherwise try again after printing the error message.
#
try:
session = router.createSession(id, pw)
break
except Glacier2.PermissionDeniedException as ex:
print("permission denied:\n" + ex.reason)
except Glacier2.CannotCreateSessionException as ex:
print("cannot create session:\n" + ex.reason)
acmTimeout = router.getACMTimeout()
connection = router.ice_getCachedConnection()
assert(connection)
connection.setACM(acmTimeout, Ice.Unset, Ice.ACMHeartbeat.HeartbeatAlways)
# Note: we use sys.stdout.write instead of print because print can't be used in a lambda with Python 2.x
connection.setCloseCallback(lambda conn: sys.stdout.write("The Glacier2 session has been destroyed.\n"))
#
# The Glacier2 router routes bidirectional calls to objects in the client only
# when these objects have the correct Glacier2-issued category. The purpose of
# the callbackReceiverFakeIdent is to demonstrate this.
#
# The Identity name is not checked by the server any value can be used.
#
callbackReceiverIdent = Ice.Identity("callbackReceiver", router.getCategoryForClient())
callbackReceiverFakeIdent = Ice.Identity(str(uuid.uuid4()), "fake")
base = communicator.propertyToProxy('Callback.Proxy')
twoway = Demo.CallbackPrx.checkedCast(base)
oneway = Demo.CallbackPrx.uncheckedCast(twoway.ice_oneway())
batchOneway = Demo.CallbackPrx.uncheckedCast(twoway.ice_batchOneway())
adapter = communicator.createObjectAdapterWithRouter("", router)
adapter.add(CallbackReceiverI(), callbackReceiverIdent)
#
# Callback will never be called for a fake identity.
#
adapter.add(CallbackReceiverI(), callbackReceiverFakeIdent)
twowayR = Demo.CallbackReceiverPrx.uncheckedCast(adapter.createProxy(callbackReceiverIdent))
onewayR = Demo.CallbackReceiverPrx.uncheckedCast(twowayR.ice_oneway())
override = None
fake = False
menu()
#
# Client REPL
#
c = None
while c != 'x':
try:
sys.stdout.write("==> ")
sys.stdout.flush()
c = sys.stdin.readline().strip()
if c == 't':
twoway.initiateCallback(twowayR)
elif c == 'o':
oneway.initiateCallback(onewayR, {"_ovrd": override} if override else {})
elif c == 'O':
context = {}
context["_fwd"] = "O"
if override:
context["_ovrd"] = override
batchOneway.initiateCallback(onewayR, context)
elif c == 'f':
batchOneway.ice_flushBatchRequests()
elif c == 'v':
if not override:
override = "some_value"
print("override context field is now `" + override + "'")
else:
override = None
print("override context field is empty")
elif c == 'F':
fake = not fake
if fake:
twowayR = Demo.CallbackReceiverPrx.uncheckedCast(
twowayR.ice_identity(callbackReceiverFakeIdent))
onewayR = Demo.CallbackReceiverPrx.uncheckedCast(
onewayR.ice_identity(callbackReceiverFakeIdent))
else:
twowayR = Demo.CallbackReceiverPrx.uncheckedCast(twowayR.ice_identity(callbackReceiverIdent))
onewayR = Demo.CallbackReceiverPrx.uncheckedCast(twowayR.ice_identity(callbackReceiverIdent))
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
return 0
#
# Ice.initialize returns an initialized Ice communicator,
# the communicator is destroyed once it goes out of scope.
#
with Ice.initialize(sys.argv, "config.client") as communicator:
#
# The communicator initialization removes all Ice-related arguments from argv
#
if len(sys.argv) > 1:
print(sys.argv[0] + ": too many arguments")
sys.exit(1)
run(communicator)