summaryrefslogtreecommitdiff
path: root/py/demo/IceGrid/sessionActivation/Client.py
blob: 78cb1ee613089548da1d464dd27665e295460a8c (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
#!/usr/bin/env python
# **********************************************************************
#
# Copyright (c) 2003-2007 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, threading, Ice, IceGrid

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

def menu():
    print """
usage:
t: send greeting as twoway
x: exit
?: help
"""

class SessionKeepAliveThread(threading.Thread):
    def __init__(self, session, timeout):
        threading.Thread.__init__(self)
        self._session = session
        self._timeout = timeout
        self._terminated = False
        self._cond = threading.Condition()

    def run(self):
        self._cond.acquire()
        try:
            while not self._terminated:
                self._cond.wait(self._timeout)
                if self._terminated:
		    break
                try:
                    self._session.keepAlive()
                except Ice.LocalException, ex:
		    break
        finally:
            self._cond.release()

    def terminate(self):
        self._cond.acquire()
        try:
            self._terminated = True
            self._cond.notify()
        finally:
            self._cond.release()

class Client(Ice.Application):
    def run(self, args):
	status = True
	registry = IceGrid.RegistryPrx.checkedCast(self.communicator().stringToProxy("DemoIceGrid/Registry"))
	if registry == None:
	    print self.appName() + ": could not contact registry"
	    return False

	while True:
	    print "This demo accepts any user-id / password combination."
	    id = raw_input("user id: ").strip()
	    pw = raw_input("password: ").strip()
	    try:
	        session = registry.createSession(id, pw)
		break
	    except IceGrid.PermissionDeniedException, ex:
	        print "permission denied:\n" + ex.reason

	keepAlive = SessionKeepAliveThread(session, registry.getSessionTimeout() / 2)
	keepAlive.start()

	try:
	    hello = Demo.HelloPrx.checkedCast(session.allocateObjectById(self.communicator().stringToIdentity("hello")))

	    menu()

	    c = None
	    while c != 'x':
		try:
		    c = raw_input("==> ")
		    if c == 't':
			hello.sayHello()
		    elif c == 'x':
			pass # Nothing to do
		    elif c == '?':
			menu()
		    else:
			print "unknown command `" + c + "'"
			menu()
		except EOFError:
		    break
		except KeyboardInterrupt:
		    break
	except IceGrid.AllocationException, ex:
	    print self.appName() + ": could not allocate object: " + ex.reason
	    status = False
	except IceGrid.ObjectNotRegisteredException:
	    print self.appName() + ": object not registered with registry"
	    status = False
	except:
            print self.appName() + ": could not allocate object: " + str(sys.exc_info()[0])
            status = False

	#
	# Destroy the keepAlive thread and the sesion object otherwise
	# the session will be kept allocated until the timeout occurs.
	# Destroying the session will release all allocated objects.
	#
	keepAlive.terminate()
	keepAlive.join()
	session.destroy();

	return status

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