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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
|
# **********************************************************************
#
# Copyright (c) 2003-2015 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.
#
# **********************************************************************
"""
Glacier2 module
"""
import threading, traceback, copy
#
# Import the Python extension.
#
import Ice
Ice.updateModule("Glacier2")
import Glacier2_Router_ice
import Glacier2_Session_ice
import Glacier2_PermissionsVerifier_ice
import Glacier2_SSLInfo_ice
import Glacier2_Metrics_ice
class SessionNotExistException(Exception):
def __init__(self):
pass
class RestartSessionException(Exception):
def __init__(self):
pass
class SessionPingThread(threading.Thread):
def __init__(self, app, router, period):
threading.Thread.__init__(self)
self._app = app
self._router = router
self._period = period
self._done = False
self._cond = threading.Condition()
def run(self):
self._cond.acquire()
try:
while not self._done:
self._router.begin_refreshSession(self.response, self.exception)
if not self._done:
self._cond.wait(self._period)
finally:
self._cond.release()
def done(self):
self._cond.acquire()
try:
if not self._done:
self._done = True
self._cond.notify()
finally:
self._cond.release()
def response(self):
#
# Ignore successful call to refreshSession.
#
pass
def exception(self, ex):
#
# Here the session has gone. The thread terminates, and we notify the
# application that the session has been destroyed.
#
self.done()
self._app.sessionDestroyed()
class ConnectionCallbackI(Ice.ConnectionCallback):
def __init__(self, app):
self._app = app
def heartbeat(self, conn):
pass
def closed(self, conn):
self._app.sessionDestroyed()
class Application(Ice.Application):
def __init__(self, signalPolicy=0): # HandleSignals=0
'''The constructor accepts an optional argument indicating
whether to handle signals. The value should be either
Application.HandleSignals (the default) or
Application.NoSignalHandling.
'''
if type(self) == Application:
raise RuntimeError("Glacier2.Application is an abstract class")
Ice.Application.__init__(self, signalPolicy)
Application._adapter = None
Application._router = None
Application._session = None
Application._createdSession = False
Application._category = None
def run(self, args):
raise RuntimeError('run should not be called on Glacier2.Application - call runWithSession instead')
def runWithSession(self, args):
raise RuntimeError('runWithSession() not implemented')
def createSession(self, args):
raise RuntimeError('createSession() not implemented')
def restart(self):
raise RestartSessionException()
def sessionDestroyed(self):
pass
def router(self):
return Application._router
router = classmethod(router)
def session(self):
return Application._session
session = classmethod(session)
def categoryForClient(self):
if Application._router == None:
raise SessionNotExistException()
return Application._category
def createCallbackIdentity(self, name):
return Ice.Identity(name, self.categoryForClient())
def addWithUUID(self, servant):
return self.objectAdapter().add(servant, self.createCallbackIdentity(Ice.generateUUID()))
def objectAdapter(self):
if Application._router == None:
raise SessionNotExistException()
if Application._adapter == None:
Application._adapter = self.communicator().createObjectAdapterWithRouter("", Application._router)
Application._adapter.activate()
return Application._adapter
def doMainInternal(self, args, initData):
# Reset internal state variables from Ice.Application. The
# remainder are reset at the end of this method.
Ice.Application._callbackInProgress = False
Ice.Application._destroyed = False
Ice.Application._interrupted = False
restart = False
status = 0
ping = None
try:
Ice.Application._communicator = Ice.initialize(args, initData)
Application._router = RouterPrx.uncheckedCast(Ice.Application.communicator().getDefaultRouter())
if Application._router == None:
Ice.getProcessLogger().error("no glacier2 router configured")
status = 1
else:
#
# The default is to destroy when a signal is received.
#
if Ice.Application._signalPolicy == Ice.Application.HandleSignals:
Ice.Application.destroyOnInterrupt()
# If createSession throws, we're done.
try:
Application._session = self.createSession()
Application._createdSession = True
except Ice.LocalException:
Ice.getProcessLogger().error(traceback.format_exc())
status = 1
if Application._createdSession:
acmTimeout = 0
try:
acmTimeout = Application._router.getACMTimeout()
except(Ice.OperationNotExistException):
pass
if acmTimeout > 0:
connection = Application._router.ice_getCachedConnection()
assert(connection)
connection.setACM(acmTimeout, Ice.Unset, Ice.ACMHeartbeat.HeartbeatAlways)
connection.setCallback(ConnectionCallbackI(self))
else:
timeout = Application._router.getSessionTimeout()
if timeout > 0:
ping = SessionPingThread(self, Application._router, timeout / 2)
ping.start()
Application._category = Application._router.getCategoryForClient()
status = self.runWithSession(args)
# We want to restart on those exceptions which indicate a
# break down in communications, but not those exceptions that
# indicate a programming logic error (ie: marshal, protocol
# failure, etc).
except(RestartSessionException):
restart = True
except(Ice.ConnectionRefusedException, Ice.ConnectionLostException, Ice.UnknownLocalException, \
Ice.RequestFailedException, Ice.TimeoutException):
Ice.getProcessLogger().error(traceback.format_exc())
restart = True
except:
Ice.getProcessLogger().error(traceback.format_exc())
status = 1
#
# Don't want any new interrupt and at this point (post-run),
# it would not make sense to release a held signal to run
# shutdown or destroy.
#
if Ice.Application._signalPolicy == Ice.Application.HandleSignals:
Ice.Application.ignoreInterrupt()
Ice.Application._condVar.acquire()
while Ice.Application._callbackInProgress:
Ice.Application._condVar.wait()
if Ice.Application._destroyed:
Ice.Application._communicator = None
else:
Ice.Application._destroyed = True
#
# And _communicator != None, meaning will be destroyed
# next, _destroyed = True also ensures that any
# remaining callback won't do anything
#
Ice.Application._condVar.release()
if ping:
ping.done()
ping.join()
if Application._createdSession and Application._router:
try:
Application._router.destroySession()
except (Ice.ConnectionLostException, SessionNotExistException):
pass
except:
Ice.getProcessLogger().error("unexpected exception when destroying the session " + \
traceback.format_exc())
Application._router = None
if Ice.Application._communicator:
try:
Ice.Application._communicator.destroy()
except:
getProcessLogger().error(traceback.format_exc())
status = 1
Ice.Application._communicator = None
# Reset internal state. We cannot reset the Application state
# here, since _destroyed must remain true until we re-run
# this method.
Application._adapter = None
Application._router = None
Application._session = None
Application._createdSession = False
Application._category = None
return (restart, status)
def doMain(self, args, initData):
# Set the default properties for all Glacier2 applications.
initData.properties.setProperty("Ice.RetryIntervals", "-1")
restart = True
ret = 0
while restart:
# A copy of the initialization data and the string seq
# needs to be passed to doMainInternal, as these can be
# changed by the application.
id = copy.copy(initData)
if id.properties:
id.properties = id.properties.clone()
argsCopy = args[:]
(restart, ret) = self.doMainInternal(argsCopy, initData)
return ret
|