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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
|
// **********************************************************************
//
// Copyright (c) 2003-2009 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.
//
// **********************************************************************
using System;
using System.Text;
namespace Glacier2
{
/// <sumary>
/// A helper class for using Glacier2 with GUI applications.
///
/// Applications should create a session factory for each Glacier2 router to which the application will
/// connect. To connect with the Glacier2 router, call SessionFactory.connect. The callback object is
/// notified of the various life cycle events. Once the session is torn down for whatever reason, the application
/// can use the session factory to create another connection.
/// </sumary>
public class SessionFactoryHelper
{
/// <sumary>
/// Creates a SessionFactory object.
/// </sumary>
/// <param name="callback">The callback object for notifications.</param>
public
SessionFactoryHelper(SessionCallback callback)
{
_callback = callback;
_initData = new Ice.InitializationData();
_initData.properties = Ice.Util.createProperties();
setDefaultProperties();
}
/// <sumary>
/// Creates a SessionFactory object.
/// </sumary>
/// <param name="initData">The initialization data to use when creating the communicator.</param>
/// <param name="callback">The callback object for notifications.</param>
public
SessionFactoryHelper(Ice.InitializationData initData, SessionCallback callback)
{
_callback = callback;
_initData = initData;
setDefaultProperties();
}
/// <sumary>
/// Creates a SessionFactory object.
/// </sumary>
/// <param name="properties">The properties to use when creating the communicator.</param>
/// <param name="callback">The callback object for notifications.</param>
public
SessionFactoryHelper(Ice.Properties properties, SessionCallback callback)
{
_callback = callback;
_initData = new Ice.InitializationData();
_initData.properties = properties;
setDefaultProperties();
}
/// <sumary>
/// Set the router object identity.
/// </sumary>
/// <param name="identity">The Glacier2 router's identity.</param>
public void
setRouterIdentity(Ice.Identity identity)
{
lock(this)
{
_identity = identity;
}
}
/// <sumary>
/// Returns the object identity of the Glacier2 router.
/// </sumary>
/// <returns> The Glacier2 router's identity.</returns>
public Ice.Identity
getRouterIdentity()
{
lock(this)
{
return _identity;
}
}
/// <sumary>
/// Sets the host on which the Glacier2 router runs.
/// </sumary>
/// <param name="hostname">The host name (or IP address) of the router host.</param>
public void
setRouterHost(string hostname)
{
lock(this)
{
_routerHost = hostname;
}
}
/// <sumary>
/// Returns the host on which the Glacier2 router runs.
/// </sumary>
/// <returns>The Glacier2 router host.</returns>
public string
getRouterHost()
{
lock(this)
{
return _routerHost;
}
}
/// <sumary>
/// Sets whether to connect with the Glacier2 router securely.
/// </sumary>
/// <param name="secure">If true, the client connects to the router
/// via SSL; otherwise, the client connects via TCP.</param>
public void
setSecure(bool secure)
{
lock(this)
{
_secure = secure;
}
}
/// <sumary>
/// Returns whether the session factory will establish a secure connection to the Glacier2 router.
/// </sumary>
/// <returns>The secure flag.</returns>
public bool
getSecure()
{
lock(this)
{
return _secure;
}
}
/// <sumary>
/// Sets the connect and connection timeout for the Glacier2 router.
/// </sumary>
/// <param name="timeoutMillisecs">The timeout in milliseconds. A zero
/// or negative timeout value indicates that the router proxy has no
/// associated timeout.</param>
public void
setTimeout(int timeoutMillisecs)
{
lock(this)
{
_timeout = timeoutMillisecs;
}
}
/// <sumary>
/// Returns the connect and connection timeout associated with the Glacier2 router.
/// </sumary>
/// <returns>The timeout.</returns>
public int
getTimeout()
{
lock(this)
{
return _timeout;
}
}
/// <sumary>
/// Sets the Glacier2 router port to connect to.
/// </sumary>
/// <param name="port">The port. If 0, then the default port (4063 for TCP or
/// 4064 for SSL) is used.</param>
public void
setPort(int port)
{
lock(this)
{
_port = port;
}
}
/// <sumary>
/// Returns the Glacier2 router port to connect to.
/// </sumary>
/// <returns>The port.</returns>
public int
getPort()
{
lock(this)
{
return _port == 0 ? (_secure ? GLACIER2_TCP_PORT : GLACIER2_SSL_PORT) : _port;
}
}
/**
* Returns the initialization data used to initialize the communicator.
*
* @return The initialization data.
*/
public Ice.InitializationData
getInitializationData()
{
lock(this)
{
return _initData;
}
}
/// <sumary>
/// Connects to the Glacier2 router using the associated SSL credentials.
///
/// Once the connection is established, SesssionCallback.connected is called on
/// the callback object; upon failure, SessionCallback.connectFailed is called
/// with the exception.
/// </sumary>
/// <returns>The connected session.</returns>
public SessionHelper
connect()
{
lock(this)
{
SessionHelper session = new SessionHelper(_callback, createInitData());
session.connect();
return session;
}
}
/// <sumary>
/// Connect the Glacier2 session using user name and password credentials.
///
/// Once the connection is established, SessionCallback.connected is called on
/// the callback object; upon failure, SessionCallback.connectFailed is called
/// with the exception.
/// </sumary>
/// <param name="username">The user name.</param>
/// <param name="password">The password.</param>
/// <returns>The connected session.</returns>
public SessionHelper
connect( string username, string password)
{
lock(this)
{
SessionHelper session = new SessionHelper(_callback, createInitData());
session.connect(username, password);
return session;
}
}
private Ice.InitializationData
createInitData()
{
// Clone the initialization data and properties.
Ice.InitializationData initData = (Ice.InitializationData)_initData.Clone();
initData.properties = initData.properties.ice_clone_();
if(initData.properties.getProperty("Ice.Default.Router").Length == 0)
{
StringBuilder sb = new StringBuilder();
sb.Append("\"");
sb.Append(Ice.Util.identityToString(_identity));
sb.Append("\"");
sb.Append(":");
if(_secure)
{
sb.Append("ssl -p ");
}
else
{
sb.Append("tcp -p ");
}
if(_port != 0)
{
sb.Append(_port);
}
else
{
if(_secure)
{
sb.Append(GLACIER2_SSL_PORT);
}
else
{
sb.Append(GLACIER2_TCP_PORT);
}
}
sb.Append(" -h ");
sb.Append(_routerHost);
if(_timeout > 0)
{
sb.Append(" -t ");
sb.Append(_timeout);
}
initData.properties.setProperty("Ice.Default.Router", sb.ToString());
}
return initData;
}
private void
setDefaultProperties()
{
_initData.properties.setProperty("Ice.ACM.Client", "0");
_initData.properties.setProperty("Ice.RetryIntervals", "-1");
}
private SessionCallback _callback;
private string _routerHost = "127.0.0.1";
private Ice.InitializationData _initData;
private Ice.Identity _identity = new Ice.Identity("router", "Glacier2");
private bool _secure = true;
private int _port = 0;
private int _timeout = 10000;
private static int GLACIER2_SSL_PORT = 4064;
private static int GLACIER2_TCP_PORT = 4063;
}
}
|