summaryrefslogtreecommitdiff
path: root/java/src/IceInternal/OutgoingAsync.java
blob: d99e6b80fb1e94d57acc47d4ae09117e61b2531a (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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
// **********************************************************************
//
// Copyright (c) 2003
// ZeroC, Inc.
// Billerica, MA, USA
//
// All Rights Reserved.
//
// Ice is free software; you can redistribute it and/or modify it under
// the terms of the GNU General Public License version 2 as published by
// the Free Software Foundation.
//
// **********************************************************************

package IceInternal;

public abstract class OutgoingAsync
{
    public
    OutgoingAsync()
    {
	_is = null;
	_os = null;
    }

    public abstract void ice_exception(Ice.LocalException ex);

    public void
    __setup(Connection connection, Reference ref, String operation, Ice.OperationMode mode, java.util.Map context)
    {
	try
	{
	    _connection = connection;
	    if(_is == null)
	    {
		_is = new BasicStream(ref.instance);
	    }
	    else
	    {
		_is.reset();
	    }
	    if(_os == null)
	    {
		_os = new BasicStream(ref.instance);
	    }
	    else
	    {
		_os.reset();
	    }

	    _connection.prepareRequest(_os);

	    ref.identity.__write(_os);
	    _os.writeStringSeq(ref.facet);
	    _os.writeString(operation);
	    _os.writeByte((byte)mode.value());
	    if(context == null)
	    {
		_os.writeSize(0);
	    }
	    else
	    {
		final int sz = context.size();
		_os.writeSize(sz);
		if(sz > 0)
		{
		    java.util.Iterator i = context.entrySet().iterator();
		    while(i.hasNext())
		    {
			java.util.Map.Entry entry = (java.util.Map.Entry)i.next();
			_os.writeString((String)entry.getKey());
			_os.writeString((String)entry.getValue());
		    }
		}
	    }
	    
	    //
	    // Input and output parameters are always sent in an
	    // encapsulation, which makes it possible to forward
	    // requests as blobs.
	    //
	    _os.startWriteEncaps();
	}
        catch(RuntimeException ex)
	{
	    destroy();
	    throw ex;
	}
    }

    public void
    __invoke()
    {
	try
	{
	    _connection.sendAsyncRequest(this);
	    if(_connection.timeout() >= 0)
	    {
		_absoluteTimeoutMillis = System.currentTimeMillis() + _connection.timeout() * 1000;
	    }
	}
        catch(RuntimeException ex)
	{
	    destroy();
	    throw ex;
	}
    }

    public void
    __finished(BasicStream is)
    {
	try
	{
            _is.swap(is);
            byte status = _is.readByte();
	    
            switch((int)status)
            {
		case DispatchStatus._DispatchOK:
		{
		    //
		    // Input and output parameters are always sent in an
		    // encapsulation, which makes it possible to forward
		    // oneway requests as blobs.
		    //
		    _is.startReadEncaps();
		    __response(true);
		    break;
		}
		
		case DispatchStatus._DispatchUserException:
		{
		    //
		    // Input and output parameters are always sent in an
		    // encapsulation, which makes it possible to forward
		    // oneway requests as blobs.
		    //
		    _is.startReadEncaps();
		    __response(false);
		    break;
		}
		
		case DispatchStatus._DispatchObjectNotExist:
		{
		    Ice.ObjectNotExistException ex = new Ice.ObjectNotExistException();
		    ex.id = new Ice.Identity();
		    ex.id.__read(_is);
		    ex.facet = _is.readStringSeq();
		    ex.operation = _is.readString();
		    ice_exception(ex);
		    break;
		}
		
		case DispatchStatus._DispatchFacetNotExist:
		{
		    Ice.FacetNotExistException ex = new Ice.FacetNotExistException();
		    ex.id = new Ice.Identity();
		    ex.id.__read(_is);
		    ex.facet = _is.readStringSeq();
		    ex.operation = _is.readString();
		    ice_exception(ex);
		    break;
		}
		
		case DispatchStatus._DispatchOperationNotExist:
		{
		    Ice.OperationNotExistException ex = new Ice.OperationNotExistException();
		    ex.id = new Ice.Identity();
		    ex.id.__read(_is);
		    ex.facet = _is.readStringSeq();
		    ex.operation = _is.readString();
		    ice_exception(ex);
		    break;
		}
		
		case DispatchStatus._DispatchUnknownException:
		{
		    Ice.UnknownException ex = new Ice.UnknownException();
		    ex.unknown = _is.readString();
		    ice_exception(ex);
		    break;
		}
		
		case DispatchStatus._DispatchUnknownLocalException:
		{
		    Ice.UnknownLocalException ex = new Ice.UnknownLocalException();
		    ex.unknown = _is.readString();
		    ice_exception(ex);
		    break;
		}
		
		case DispatchStatus._DispatchUnknownUserException:
		{
		    Ice.UnknownUserException ex = new Ice.UnknownUserException();
		    ex.unknown = _is.readString();
		    ice_exception(ex);
		    break;
		}
		
		default:
		{
		    ice_exception(new Ice.UnknownReplyStatusException());
		    break;
		}
	    }
	}
	catch(Exception ex)
	{
	    warning(ex);
	}
	finally
	{
	    destroy();
	}
    }

    public void
    __finished(Ice.LocalException exc)
    {
	try
	{
	    ice_exception(exc);
	}
	catch(Exception ex)
	{
	    warning(ex);
	}
	finally
	{
	    destroy();
	}
    }

    public boolean
    __timedOut()
    {
	if(_connection.timeout() >= 0)
	{
	    return System.currentTimeMillis() >= _absoluteTimeoutMillis;
	}
	else
	{
	    return false;
	}
    }

    public BasicStream
    __is()
    {
        return _is;
    }

    public BasicStream
    __os()
    {
        return _os;
    }

    protected abstract void __response(boolean ok);

    private void
    destroy()
    {
	if(_is != null)
	{
	    _is.destroy();
	    _is = null;
	}
	if(_os != null)
	{
	    _os.destroy();
	    _os = null;
	}
    }

    private void
    warning(Exception ex)
    {
	if(_os.instance().properties().getPropertyAsIntWithDefault("Ice.Warn.AMICallback", 1) > 0)
	{
	    java.io.StringWriter sw = new java.io.StringWriter();
	    java.io.PrintWriter pw = new java.io.PrintWriter(sw);
	    IceUtil.OutputBase out = new IceUtil.OutputBase(pw);
	    out.setUseTab(false);
	    out.print("exception raised by AMI callback:\n");
	    ex.printStackTrace(pw);
	    pw.flush();
	    _os.instance().logger().warning(sw.toString());
	}
    }

    private Connection _connection;
    private long _absoluteTimeoutMillis;
    private BasicStream _is;
    private BasicStream _os;
}