summaryrefslogtreecommitdiff
path: root/cpp/src/Ice/Outgoing.cpp
blob: 4e99b11507e206c348ca620e2c9157f457d6feb8 (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
225
226
227
228
229
230
231
232
233
234
235
// **********************************************************************
//
// Copyright (c) 2001
// MutableRealms, Inc.
// Huntsville, AL, USA
//
// All Rights Reserved
//
// **********************************************************************

#include <Ice/Outgoing.h>
#include <Ice/Object.h>
#include <Ice/Emitter.h>
#include <Ice/Reference.h>
#include <Ice/LocalException.h>

using namespace std;
using namespace Ice;
using namespace IceInternal;

IceInternal::NonRepeatable::NonRepeatable(const NonRepeatable& ex)
{
    _ex = auto_ptr<LocalException>(ex.get()->clone());
}

IceInternal::NonRepeatable::NonRepeatable(const ::Ice::LocalException& ex)
{
    _ex = auto_ptr<LocalException>(ex.clone());
}

void
IceInternal::NonRepeatable::raise() const
{
    assert(_ex.get());
    _ex.get()->raise();
}

const ::Ice::LocalException*
IceInternal::NonRepeatable::get() const
{
    assert(_ex.get());
    return _ex.get();
}

IceInternal::Outgoing::Outgoing(const EmitterPtr& emitter, const ReferencePtr& reference) :
    _emitter(emitter),
    _reference(reference),
    _state(StateInProgress),
    _is(reference->instance),
    _os(reference->instance)
{
    _emitter->prepareRequest(this);
    _os.write(_reference->identity);
}

IceInternal::Outgoing::~Outgoing()
{
}

bool
IceInternal::Outgoing::invoke()
{
    switch (_reference->mode)
    {
	case Reference::ModeTwoway:
	{
	    bool timedOut = false;
	    
	    {
		JTCSyncT<JTCMonitorT<JTCMutex> > sync(*this);
		
		_emitter->sendRequest(this, false);
		
		Int timeout = _emitter->timeout();
		while (_state == StateInProgress)
		{
		    try
		    {
			if (timeout >= 0)
			{	
			    wait(timeout);
			    if (_state == StateInProgress)
			    {
				_state = StateLocalException;
				_exception = auto_ptr<LocalException>(new TimeoutException(__FILE__, __LINE__));
				timedOut = true;
			    }
			}
			else
			{
			    wait();
			}
		    }
		    catch(const JTCInterruptedException&)
		    {
		    }
		}
	    }
	    
	    if (_exception.get())
	    {
		if (timedOut)
		{
		    //
		    // Must be called outside the synchronization of this
		    // object
		    //
		    _emitter->exception(*_exception.get());
		}

		//
		// A CloseConnectionException indicates graceful
		// server shutdown, and is therefore always repeatable
		// without violating "at-most-once". That's because by
		// sending a close connection message, the server
		// guarantees that all outstanding requests can safely
		// be repeated.
		//
		if(dynamic_cast<const CloseConnectionException*>(_exception.get()))
		{
		    _exception->raise();
		}
		
		//
		// Throw the exception wrapped in a NonRepeatable, to
		// indicate that the request cannot be resent without
		// potentially violating the "at-most-once" principle.
		//
		throw NonRepeatable(*_exception.get());
	    }
	    
	    if (_state == StateException)
	    {
		return false;
	    }

	    if (_state == StateLocationForward)
	    {
		ObjectPrx p;
		_is.read(p);
		throw LocationForward(p);
	    }

	    assert(_state == StateOK);
	    break;
	}
	
	case Reference::ModeOneway:
	case Reference::ModeDatagram:
	{
	    _emitter->sendRequest(this, true);
	    break;
	}
    }

    return true;
}

void
IceInternal::Outgoing::finished(Stream& is)
{
    JTCSyncT<JTCMonitorT<JTCMutex> > sync(*this);
    if (_state == StateInProgress)
    {
	_is.swap(is);
	Byte status;
	_is.read(status);
	switch (static_cast<DispatchStatus>(status))
	{
	    case DispatchOK:
	    {
		_state = StateOK;
		break;
	    }
	    
	    case DispatchException:
	    {
		_state = StateException;
		break;
	    }
	    
	    case DispatchLocationForward:
	    {
		_state = StateLocationForward;
		break;
	    }

	    case DispatchObjectNotExist:
	    {
		_state = StateLocalException;
		_exception = auto_ptr<LocalException>(new ObjectNotExistException(__FILE__, __LINE__));
		break;
	    }
	    
	    case DispatchOperationNotExist:
	    {
		_state = StateLocalException;
		_exception = auto_ptr<LocalException>(new OperationNotExistException(__FILE__, __LINE__));
		break;
	    }
	    
	    default:
	    {
		_state = StateLocalException;
		_exception = auto_ptr<LocalException>(new UnknownReplyStatusException(__FILE__, __LINE__));
		break;
	    }
	}
	notify();
    }
}

void
IceInternal::Outgoing::finished(const LocalException& ex)
{
    JTCSyncT<JTCMonitorT<JTCMutex> > sync(*this);
    if (_state == StateInProgress)
    {
	_state = StateLocalException;
	_exception = auto_ptr<LocalException>(ex.clone());
	notify();
    }
}

Stream*
IceInternal::Outgoing::is()
{
    return &_is;
}

Stream*
IceInternal::Outgoing::os()
{
    return &_os;
}