summaryrefslogtreecommitdiff
path: root/py/test/Ice/blobject/RouterI.py
blob: e54e55ad76aef3d53d645fee504bb603ebfd2816 (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
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
# **********************************************************************
#
# 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 threading, Ice

class CallQueue(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)
        self._condVar = threading.Condition()
        self._queue = []
        self._destroy = False

    def add(self, call):
        self._condVar.acquire()
        self._queue.append(call)
        self._condVar.notify()
        self._condVar.release()

    def destroy(self):
        self._condVar.acquire()
        self._destroy = True
        self._condVar.notify()
        self._condVar.release()

    def run(self):
        while True:
            self._condVar.acquire()
            while len(self._queue) == 0 and not self._destroy:
                self._condVar.wait()
            if self._destroy:
                self._condVar.release()
                break
            call = self._queue.pop()
            self._condVar.release()
            call.execute()

class AsyncCallback(object):
    def __init__(self, cb):
        self._cb = cb

    def response(self, ok, results):
        self._cb.ice_response(ok, results)

    def exception(self, ex):
        self._cb.ice_exception(ex)

class BlobjectCall(object):
    def __init__(self, proxy, amdCallback, inParams, curr):
        self._proxy = proxy
        self._amdCallback = amdCallback
        self._inParams = inParams
        self._curr = curr

    def execute(self):
        proxy = self._proxy
        if len(self._curr.facet) > 0:
            proxy = self._proxy.ice_facet(self._curr.facet)

        if self._curr.ctx.has_key("_fwd") and self._curr.ctx["_fwd"] == "o":
            proxy = proxy.ice_oneway()
            try:
                ok, out = proxy.ice_invoke(self._curr.operation, self._curr.mode, self._inParams, self._curr.ctx)
                self._amdCallback.ice_response(ok, out)
            except Ice.Exception, e:
                self._amdCallback.ice_exception(e)
        else:
            cb = AsyncCallback(self._amdCallback)
            proxy.begin_ice_invoke(self._curr.operation, self._curr.mode, self._inParams, cb.response, cb.exception,
                                   None, self._curr.ctx)

class BlobjectAsyncI(Ice.BlobjectAsync):
    def __init__(self):
        self._queue = CallQueue()
        self._queue.start()
        self._objects = {}
        self._lock = threading.Lock()

    def ice_invoke_async(self, amdCallback, inParams, curr):
        self._lock.acquire()
        proxy = self._objects[curr.id]
        assert proxy
        self._lock.release()
        self._queue.add(BlobjectCall(proxy, amdCallback, inParams, curr))

    def add(self, proxy):
        self._lock.acquire()
        self._objects[proxy.ice_getIdentity()] = proxy.ice_facet("").ice_twoway().ice_router(None)
        self._lock.release()

    def destroy(self):
        self._lock.acquire()
        self._queue.destroy()
        self._queue.join()
        self._lock.release()

class BlobjectI(Ice.Blobject):
    def __init__(self):
        self._objects = {}
        self._lock = threading.Lock()

    def ice_invoke(self, inParams, curr):
        self._lock.acquire()
        proxy = self._objects[curr.id]
        self._lock.release()

        if len(curr.facet) > 0:
            proxy = proxy.ice_facet(curr.facet)

        try:
            if curr.ctx.has_key("_fwd") and curr.ctx["_fwd"] == "o":
                proxy = proxy.ice_oneway()
                return proxy.ice_invoke(curr.operation, curr.mode, inParams, curr.ctx)
            else:
                return proxy.ice_invoke(curr.operation, curr.mode, inParams, curr.ctx)
        except Ice.Exception, e:
            raise

    def add(self, proxy):
        self._lock.acquire()
        self._objects[proxy.ice_getIdentity()] = proxy.ice_facet("").ice_twoway().ice_router(None)
        self._lock.release()

    def destroy(self):
        pass

class ServantLocatorI(Ice.ServantLocator):
    def __init__(self, blobject):
        self._blobject = blobject

    def locate(self, current):
        return self._blobject # and the cookie

    def finished(self, current, object, cookie):
        pass

    def deactivate(self, s):
        pass

class RouterI(Ice.Router):
    def __init__(self, communicator, sync):
        self._adapter = communicator.createObjectAdapterWithEndpoints("forward", "default -h 127.0.0.1")
        if sync:
            self._blobject = BlobjectI()
        else:
            self._blobject = BlobjectAsyncI()
        self._adapter.addServantLocator(ServantLocatorI(self._blobject), "")
        self._blobjectProxy = self._adapter.addWithUUID(self._blobject)
        proxy = Ice.RouterPrx.uncheckedCast(self._adapter.addWithUUID(self))
        communicator.setDefaultRouter(proxy)
        self._adapter.activate()

    def useSync(self, sync):
        self._locator.useSync(sync)

    def getClientProxy(self, current):
        return self._blobjectProxy

    def getServerProxy(self, current):
        assert false

    def addProxy(self, proxy, current):
        self._blobject.add(proxy)

    def addProxies(self, proxies, current):
        for p in proxies:
            self._blobject.add(p)

    def destroy(self):
        self._blobject.destroy()