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
|
#!/usr/bin/env python
# **********************************************************************
#
# Copyright (c) 2003-2010 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, time, traceback, threading, Ice
Ice.loadSlice('Session.ice')
import Demo
class HelloI(Demo.Hello):
def __init__(self, name, id):
self._name = name
self._id = id
def sayHello(self, c):
print "Hello object #" + str(self._id) + " for session `" + self._name + "' says:\n" + \
"Hello " + self._name + "!"
class SessionI(Demo.Session):
def __init__(self, name):
self._timestamp = time.time()
self._name = name
self._lock = threading.Lock()
self._destroy = False # true if destroy() was called, false otherwise.
self._nextId = 0 # The id of the next hello object. This is used for tracing purposes.
self._objs = [] # List of per-client allocated Hello objects.
print "The session " + self._name + " is now created."
def createHello(self, c):
self._lock.acquire()
try:
if self._destroy:
raise Ice.ObjectNotExistException()
hello = Demo.HelloPrx.uncheckedCast(c.adapter.addWithUUID(HelloI(self._name, self._nextId)))
self._nextId = self._nextId + 1
self._objs.append(hello)
return hello
finally:
self._lock.release()
def refresh(self, c):
self._lock.acquire()
try:
if self._destroy:
raise Ice.ObjectNotExistException()
self._timestamp = time.time()
finally:
self._lock.release()
def getName(self, c):
self._lock.acquire()
try:
if self._destroy:
raise Ice.ObjectNotExistException()
return self._name
finally:
self._lock.release()
def destroy(self, c):
self._lock.acquire()
try:
if self._destroy:
raise Ice.ObjectNotExistException()
self._destroy = True
print "The session " + self._name + " is now destroyed."
try:
c.adapter.remove(c.id)
for p in self._objs:
c.adapter.remove(p.ice_getIdentity())
except Ice.ObjectAdapterDeactivatedException, ex:
# This method is called on shutdown of the server, in
# which case this exception is expected.
pass
self._objs = []
finally:
self._lock.release()
def timestamp(self):
self._lock.acquire()
try:
if self._destroy:
raise Ice.ObjectNotExistException()
return self._timestamp
finally:
self._lock.release()
class SessionProxyPair:
def __init__(self, p, s):
self.proxy = p
self.session = s
class ReapThread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self._timeout = 10
self._terminated = False
self._cond = threading.Condition()
self._sessions = []
def run(self):
self._cond.acquire()
try:
while not self._terminated:
self._cond.wait(1)
if not self._terminated:
for p in self._sessions:
try:
#
# Session destruction may take time in a
# real-world example. Therefore the current time
# is computed for each iteration.
#
if (time.time() - p.session.timestamp()) > self._timeout:
name = p.proxy.getName()
p.proxy.destroy()
print "The session " + name + " has timed out."
self._sessions.remove(p)
except Ice.ObjectNotExistException:
self._sessions.remove(p)
finally:
self._cond.release()
def terminate(self):
self._cond.acquire()
try:
self._terminated = True
self._cond.notify()
self._sessions = []
finally:
self._cond.release()
def add(self, proxy, session):
self._cond.acquire()
try:
self._sessions.append(SessionProxyPair(proxy, session))
finally:
self._cond.release()
class SessionFactoryI(Demo.SessionFactory):
def __init__(self, reaper):
self._reaper = reaper
self._lock = threading.Lock()
def create(self, name, c):
self._lock.acquire()
try:
session = SessionI(name)
proxy = Demo.SessionPrx.uncheckedCast(c.adapter.addWithUUID(session))
self._reaper.add(proxy, session)
return proxy
finally:
self._lock.release()
def shutdown(self, c):
print "Shutting down..."
c.adapter.getCommunicator().shutdown()
class Server(Ice.Application):
def run(self, args):
if len(args) > 1:
print self.appName() + ": too many arguments"
return 1
adapter = self.communicator().createObjectAdapter("SessionFactory")
reaper = ReapThread()
reaper.start()
try:
adapter.add(SessionFactoryI(reaper), self.communicator().stringToIdentity("SessionFactory"))
adapter.activate()
self.communicator().waitForShutdown()
finally:
reaper.terminate()
reaper.join()
return 0
app = Server()
sys.exit(app.main(sys.argv, "config.server"))
|