summaryrefslogtreecommitdiff
path: root/py/demo/Ice/async/Consumer.py
blob: 50c5e314971b1c1d1d62880c87c5cdc37a32ce1f (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
#!/usr/bin/env python
# **********************************************************************
#
# Copyright (c) 2003-2006 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, os, traceback, threading, Ice

slice_dir = os.getenv('ICEPY_HOME', '')
if len(slice_dir) == 0 or not os.path.exists(os.path.join(slice_dir, 'slice')):
    slice_dir = os.getenv('ICE_HOME', '')
if len(slice_dir) == 0 or not os.path.exists(os.path.join(slice_dir, 'slice')):
    slice_dir = os.path.join('/', 'usr', 'share')
if not os.path.exists(os.path.join(slice_dir, 'slice')):
    print sys.argv[0] + ': Slice directory not found. Define ICEPY_HOME or ICE_HOME.'
    sys.exit(1)

Ice.loadSlice('-I' + slice_dir + '/slice Queue.ice')
import Demo

class AMI_Queue_getI:
    def __init__(self, id):
        self._id = id

        requestMutex.acquire()
	requests.append(id)
        requestMutex.release()

    def ice_response(self, message):
        requestMutex.acquire()
        for i in range(0, len(requests)):
	   if requests[i] == self._id:
	       del requests[i]
	       break
        requestMutex.release()

        print message

    def ice_exception(self, ex):
        requestMutex.acquire()
        for i in range(0, len(requests)):
	   if requests[i] == self._id:
	       del requests[i]
	       break
        requestMutex.release()

        print ex

def menu():
    print """
usage:
g: get a message
x: exit
?: help
"""

class Consumer(Ice.Application):
    def run(self, args):
	queue = Demo.QueuePrx.checkedCast(self.communicator().propertyToProxy('Queue.Proxy'))
	if not queue:
	    print args[0] + ": invalid proxy"
	    return False

	menu()

	c = None
	while c != 'x':
	    try:
		c = raw_input("==> ")
		if c == 'g':
		    id = Ice.generateUUID()
		    queue.get_async(AMI_Queue_getI(id), id)
		elif c == 'x':
		    pass # Nothing to do
		elif c == '?':
		    menu()
		else:
		    print "unknown command `" + c + "'"
		    menu()
	    except EOFError:
	        break
	    except KeyboardInterrupt:
		break
	    except Ice.Exception, ex:
	        print ex

        requestMutex.acquire()
	if len(requests) != 0:
	    try:
	        queue.cancel(requests)
	    except Ice.Exception, ex:
	        pass
        requestMutex.release()

	return True


requests = []
requestMutex = threading.Lock()

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