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
|
//
// Copyright (c) ZeroC, Inc. All rights reserved.
//
#include <Ice/Ice.h>
#include <CallbackI.h>
#include <TestHelper.h>
using namespace std;
using namespace Ice;
using namespace Test;
class Cookie : public Ice::LocalObject
{
};
typedef IceUtil::Handle<Cookie> CookiePtr;
template<class T>
class CookieT : public Cookie
{
public:
CookieT(const T& v) : cb(v)
{
}
T cb;
};
template<typename T> CookiePtr newCookie(const T& cb)
{
return new CookieT<T>(cb);
}
template<typename T> const T& getCookie(const CookiePtr& cookie)
{
return dynamic_cast<CookieT<T>* >(cookie.get())->cb;
}
class AsyncCB : public IceUtil::Shared
{
public:
void
responseCallback(const CookiePtr& cookie)
{
getCookie<AMD_Callback_initiateCallbackPtr>(cookie)->ice_response();
}
void
exceptionCallback(const Ice::Exception& ex, const CookiePtr& cookie)
{
getCookie<AMD_Callback_initiateCallbackPtr>(cookie)->ice_exception(ex);
}
void
responseCallbackWithPayload(const CookiePtr& cookie)
{
getCookie<AMD_Callback_initiateCallbackWithPayloadPtr>(cookie)->ice_response();
}
void
exceptionCallbackWithPayload(const Ice::Exception& ex, const CookiePtr& cookie)
{
getCookie<AMD_Callback_initiateCallbackWithPayloadPtr>(cookie)->ice_exception(ex);
}
};
typedef IceUtil::Handle<AsyncCB> AsyncCBPtr;
CallbackReceiverI::CallbackReceiverI() :
_holding(false),
_lastToken(-1),
_callback(0),
_callbackWithPayload(0)
{
}
void
CallbackReceiverI::callback(int token, const Current&)
{
Lock sync(*this);
checkForHold();
if(token != _lastToken)
{
_callback = 0;
_lastToken = token;
}
++_callback;
notifyAll();
}
void
CallbackReceiverI::callbackWithPayload(const Ice::ByteSeq&, const Current&)
{
Lock sync(*this);
checkForHold();
++_callbackWithPayload;
notifyAll();
}
int
CallbackReceiverI::callbackOK(int count, int token)
{
Lock sync(*this);
while(_lastToken != token || _callback < count)
{
wait();
}
_callback -= count;
return _callback;
}
int
CallbackReceiverI::callbackWithPayloadOK(int count)
{
Lock sync(*this);
while(_callbackWithPayload < count)
{
wait();
}
_callbackWithPayload -= count;
return _callbackWithPayload;
}
void
CallbackReceiverI::hold()
{
Lock sync(*this);
_holding = true;
}
void
CallbackReceiverI::activate()
{
Lock sync(*this);
_holding = false;
notifyAll();
}
void
CallbackReceiverI::checkForHold()
{
while(_holding)
{
wait();
}
}
CallbackI::CallbackI()
{
}
void
CallbackI::initiateCallback_async(const AMD_Callback_initiateCallbackPtr& cb,
const CallbackReceiverPrx& proxy, int token, const Current& current)
{
Ice::Context::const_iterator p = current.ctx.find("serverOvrd");
Ice::Context ctx = current.ctx;
if(p != current.ctx.end())
{
ctx["_ovrd"] = p->second;
}
if(proxy->ice_isTwoway())
{
AsyncCBPtr acb = new AsyncCB();
proxy->begin_callback(token, ctx,
newCallback_CallbackReceiver_callback(acb, &AsyncCB::responseCallback, &AsyncCB::exceptionCallback),
newCookie(cb));
}
else
{
proxy->callback(token, ctx);
cb->ice_response();
}
}
void
CallbackI::initiateCallbackWithPayload_async(const AMD_Callback_initiateCallbackWithPayloadPtr& cb,
const CallbackReceiverPrx& proxy,
const Current& current)
{
Ice::Context::const_iterator p = current.ctx.find("serverOvrd");
Ice::Context ctx = current.ctx;
if(p != current.ctx.end())
{
ctx["_ovrd"] = p->second;
}
Ice::ByteSeq seq(1000 * 1024, 0);
if(proxy->ice_isTwoway())
{
AsyncCBPtr acb = new AsyncCB();
proxy->begin_callbackWithPayload(seq, ctx,
newCallback_CallbackReceiver_callbackWithPayload(
acb,
&AsyncCB::responseCallbackWithPayload,
&AsyncCB::exceptionCallbackWithPayload),
newCookie(cb));
}
else
{
proxy->callbackWithPayload(seq, ctx);
cb->ice_response();
}
}
void
CallbackI::shutdown(const Ice::Current& current)
{
current.adapter->getCommunicator()->shutdown();
}
|