summaryrefslogtreecommitdiff
path: root/cpp/src/Glacier2/Blobject.cpp
blob: 7f6812a0bbafa3244c2ec5bc7ab2f66ebba66268 (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
// **********************************************************************
//
// Copyright (c) 2003-2004 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 <Glacier2/Blobject.h>

using namespace std;
using namespace Ice;
using namespace Glacier2;

static const string serverForwardContext = "Glacier2.Server.ForwardContext";
static const string clientForwardContext = "Glacier2.Client.ForwardContext";
static const string serverUnbuffered = "Glacier2.Server.Unbuffered";
static const string clientUnbuffered = "Glacier2.Client.Unbuffered";
static const string serverAlwaysBatch = "Glacier2.Server.AlwaysBatch";
static const string clientAlwaysBatch = "Glacier2.Client.AlwaysBatch";
static const string serverThreadStackSize = "Glacier2.Server.ThreadStackSize";
static const string clientThreadStackSize = "Glacier2.Client.ThreadStackSize";
static const string serverTraceRequest = "Glacier2.Server.Trace.Request";
static const string clientTraceRequest = "Glacier2.Client.Trace.Request";

Glacier2::Blobject::Blobject(const CommunicatorPtr& communicator, bool reverse) :
    _communicator(communicator),
    _properties(_communicator->getProperties()),
    _logger(_communicator->getLogger()),
    _reverse(reverse),
    _forwardContext(_reverse ?
		    _properties->getPropertyAsInt(serverForwardContext) > 0 :
		    _properties->getPropertyAsInt(clientForwardContext) > 0),
    _unbuffered(_reverse ?
		_properties->getPropertyAsInt(serverUnbuffered) > 0 :
		_properties->getPropertyAsInt(clientUnbuffered) > 0),
    _alwaysBatch(_reverse ?
		 _properties->getPropertyAsInt(serverAlwaysBatch) > 0 :
		 _properties->getPropertyAsInt(clientAlwaysBatch) > 0),
    _requestTraceLevel(_reverse ?
		       _properties->getPropertyAsInt(serverTraceRequest) :
		       _properties->getPropertyAsInt(clientTraceRequest))
{
    if(!_unbuffered)
    {
	Int threadStackSize = _reverse ?
	    _properties->getPropertyAsInt(serverThreadStackSize) :
	    _properties->getPropertyAsInt(clientThreadStackSize);
	
	_requestQueue = new RequestQueue(_communicator, _reverse);
	_requestQueue->start(static_cast<size_t>(threadStackSize));
    }
}

Glacier2::Blobject::~Blobject()
{
    assert(!_requestQueue);
}

void
Glacier2::Blobject::destroy()
{
    if(_requestQueue)
    {
	_requestQueue->destroy();
	_requestQueue = 0;
    }
}

void
Glacier2::Blobject::invoke(ObjectPrx& proxy, const AMD_Object_ice_invokePtr& amdCB, const vector<Byte>& inParams,
			   const Current& current)
{
    //
    // Set the correct facet on the proxy.
    //
    if(!current.facet.empty())
    {
	proxy = proxy->ice_newFacet(current.facet);
    }

    //
    // Modify the proxy according to the _fwd context field.
    //
    Context::const_iterator p = current.ctx.find("_fwd");
    if(p != current.ctx.end())
    {
	for(unsigned int i = 0; i < p->second.length(); ++i)
	{
	    char option = p->second[i];
	    switch(option)
	    {
		case 't':
		{
		    proxy = proxy->ice_twoway();
		    break;
		}
		
		case 'o':
		{
		    if(_alwaysBatch && !_unbuffered)
		    {
			proxy = proxy->ice_batchOneway();
		    }
		    else
		    {
			proxy = proxy->ice_oneway();
		    }
		    break;
		}
		
		case 'd':
		{
		    if(_alwaysBatch && !_unbuffered)
		    {
			proxy = proxy->ice_batchDatagram();
		    }
		    else
		    {
			proxy = proxy->ice_datagram();
		    }
		    break;
		}
		
		case 'O':
		{
		    if(!_unbuffered)
		    {
			proxy = proxy->ice_batchOneway();
		    }
		    else
		    {
			proxy = proxy->ice_oneway();
		    }
		    break;
		}
		
		case 'D':
		{
		    if(!_unbuffered)
		    {
			proxy = proxy->ice_batchDatagram();
		    }
		    else
		    {
			proxy = proxy->ice_datagram();
		    }
		    break;
		}
		
		case 's':
		{
		    proxy = proxy->ice_secure(true);
		    break;
		}
		
		case 'z':
		{
		    proxy = proxy->ice_compress(true);
		    break;
		}
		
		default:
		{
		    Warning out(_logger);
		    out << "unknown forward option `" << option << "'";
		    break;
		}
	    }
	}
    }
    
    if(_unbuffered)
    {
	//
	// If we are in unbuffered mode, we send the request directly.
	//

	if(_requestTraceLevel >= 1)
	{
	    Trace out(_logger, "Glacier2");
	    if(_reverse)
	    {
		out << "reverse ";
	    }
	    out << "routing (unbuffered)";
	    out << "\nproxy = " << _communicator->proxyToString(proxy);
	    out << "\noperation = " << current.operation;
	    out << "\ncontext = ";
	    Context::const_iterator q = current.ctx.begin();
	    while(q != current.ctx.end())
	    {
		out << q->first << '/' << q->second;
		if(++q != current.ctx.end())
		{
		    out << ", ";
		}
	    }
	}

	bool ok;
	vector<Byte> outParams;

	try
	{
	    if(_forwardContext)
	    {
		ok = proxy->ice_invoke(current.operation, current.mode, inParams, outParams, current.ctx);
	    }
	    else
	    {
		ok = proxy->ice_invoke(current.operation, current.mode, inParams, outParams);
	    }
	}
	catch(const LocalException& ex)
	{
	    amdCB->ice_exception(ex);
	    return;
	}
	
	amdCB->ice_response(ok, outParams);
    }
    else
    {    
	//
	// If we are not in unbuffered mode, we create a new request
	// and add it to the request queue. If the request is twoway,
	// we use AMI.
	//

	_requestQueue->addRequest(new Request(proxy, inParams, current, _forwardContext, amdCB));
    }
}