summaryrefslogtreecommitdiff
path: root/cpp/src/IceStorm/TwowayProxy.cpp
blob: a7a458069d95e29e5d70cd442965c1d93edbbf3b (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
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
218
219
220
221
222
223
224
// **********************************************************************
//
// Copyright (c) 2003-2005 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.
//
// **********************************************************************

#include <Ice/OutgoingAsync.h> // for Ice::AMI_Object_ice_invoke
#include <IceStorm/TwowayProxy.h>

using namespace std;

IceStorm::TwowayProxy::TwowayProxy(const Ice::ObjectPrx& obj) :
    _obj(obj)
{
}

Ice::ObjectPrx
IceStorm::TwowayProxy::proxy() const
{
    return _obj;
}

class UnorderedInvokeCB : public Ice::AMI_Object_ice_invoke
{
public:

    UnorderedInvokeCB(const IceStorm::UnorderedTwowayProxyPtr& proxy) : _proxy(proxy)
    {
    }

    ~UnorderedInvokeCB()
    {
    }
    
    virtual void
    ice_response(bool, const std::vector<Ice::Byte>&)
    {
    }

    virtual void
    ice_exception(const Ice::Exception& ex)
    {
	try
	{
	    ex.ice_throw();
	}
	catch(const Ice::LocalException& ex)
	{
	    _proxy->exception(ex);
	}
    }

private:

    const IceStorm::UnorderedTwowayProxyPtr _proxy;
};

IceStorm::UnorderedTwowayProxy::UnorderedTwowayProxy(const Ice::ObjectPrx& obj) : 
    TwowayProxy(obj)
{
}

void
IceStorm::UnorderedTwowayProxy::exception(const Ice::LocalException& ex)
{
    IceUtil::Mutex::Lock sync(_mutex);
    _exception.reset(dynamic_cast<Ice::LocalException*>(ex.ice_clone()));
}

void
IceStorm::UnorderedTwowayProxy::deliver(const vector<EventPtr>& v)
{
    //
    // TODO: Use a buffer of AMI callback objects to eliminate the dynamic memory allocation?
    //
    for(vector<EventPtr>::const_iterator p = v.begin(); p != v.end(); ++p)
    {
	_obj->ice_invoke_async(new UnorderedInvokeCB(this), (*p)->op, (*p)->mode, (*p)->data, (*p)->context);
    }
}

class OrderedInvokeCB : public Ice::AMI_Object_ice_invoke
{
public:

    OrderedInvokeCB(const IceStorm::OrderedTwowayProxyPtr& proxy) : _proxy(proxy)
    {
    }

    ~OrderedInvokeCB()
    {
    }
    
    virtual void
    ice_response(bool, const std::vector<Ice::Byte>&)
    {
	_proxy->response();
    }

    virtual void
    ice_exception(const Ice::Exception& ex)
    {
	try
	{
	    ex.ice_throw();
	}
	catch(const Ice::LocalException& ex)
	{
	    _proxy->exception(ex);
	}
    }

private:

    const IceStorm::OrderedTwowayProxyPtr _proxy;
};

IceStorm::OrderedTwowayProxy::OrderedTwowayProxy(const Ice::ObjectPrx& obj) : 
    TwowayProxy(obj)
{
}

void
IceStorm::OrderedTwowayProxy::publish(const EventPtr& event)
{
    EventPtr e;
    {
	IceUtil::Mutex::Lock sync(_mutex);
	
	if(_exception.get())
	{
	    _exception->ice_throw();
	}
	
	_events.push_back(event);
	
	if(_busy)
	{
	    return;
	}

	e = _events.front();
	_events.erase(_events.begin());
	_busy = true;
    }

    try
    {
	assert(e);
	vector<EventPtr> events;
	events.push_back(e);
	deliver(events);
    }
    catch(const Ice::LocalException& ex)
    {
	exception(ex);
	throw;
    }
}

void
IceStorm::OrderedTwowayProxy::exception(const Ice::LocalException& ex)
{
    IceUtil::Mutex::Lock sync(_mutex);
    assert(!_exception.get() && _busy);
    _busy = false;
    _exception.reset(dynamic_cast<Ice::LocalException*>(ex.ice_clone()));
}

void
IceStorm::OrderedTwowayProxy::response()
{
    EventPtr event;
    {
	IceUtil::Mutex::Lock sync(_mutex);
	
	assert(!_exception.get() && _busy);
	
	if(_events.empty())
	{
	    _busy = false;
	    return;
	}

	event = _events.front();
	_events.erase(_events.begin());
    }

    try
    {
	assert(event);
	vector<EventPtr> events;
	events.push_back(event);
	deliver(events);
    }
    catch(const Ice::LocalException& ex)
    {
	exception(ex);
    }
}

void
IceStorm::OrderedTwowayProxy::deliver(const vector<EventPtr>& v)
{
    //
    // Should only ever be called with a queue of length 1 (should
    // only be called by methods local to OrderedTwowayProxy).
    //
    assert(v.size() == 1);

    //
    // TODO: Use a buffer of AMI callback objects to eliminate the
    // dynamic memory allocation? (we could actually use only 2 AMI
    // callback objects there.)
    //
    for(vector<EventPtr>::const_iterator p = v.begin(); p != v.end(); ++p)
    {
	_obj->ice_invoke_async(new OrderedInvokeCB(this), (*p)->op, (*p)->mode, (*p)->data, (*p)->context);
    }
}