ICE 3.4.2
[php5-ice-freebsdport.git] / cs / test / Glacier2 / sessionHelper / Client.cs
blobc1ef8e7ae18d8f6df115ca1e9fb203ea67aa186c
1 // **********************************************************************
2 //
3 // Copyright (c) 2003-2011 ZeroC, Inc. All rights reserved.
4 //
5 // This copy of Ice is licensed to you under the terms described in the
6 // ICE_LICENSE file included in this distribution.
7 //
8 // **********************************************************************
10 using Glacier2;
11 using Test;
12 using System;
13 using System.Collections.Generic;
14 using System.Diagnostics;
15 using System.Reflection;
16 using System.Threading;
18 [assembly: CLSCompliant(true)]
20 [assembly: AssemblyTitle("IceTest")]
21 [assembly: AssemblyDescription("Ice test")]
22 [assembly: AssemblyCompany("ZeroC, Inc.")]
24 public class Client
26 public class App : Ice.Application
28 public App()
30 me = this;
32 _initData = new Ice.InitializationData();
33 _initData.properties = Ice.Util.createProperties();
34 _initData.properties.setProperty("Ice.Default.Router", "Glacier2/router:default -p 12347");
35 #if COMPACT
36 _initData.dispatcher = delegate(Ice.VoidAction action, Ice.Connection connection)
37 #else
38 _initData.dispatcher = delegate(System.Action action, Ice.Connection connection)
39 #endif
41 action();
45 public class SessionCalback1 : Glacier2.SessionCallback
47 public void
48 connected(Glacier2.SessionHelper session)
50 test(false);
53 public void
54 disconnected(Glacier2.SessionHelper session)
56 test(false);
59 public void
60 connectFailed(Glacier2.SessionHelper session, System.Exception exception)
62 try
64 throw exception;
66 catch(Glacier2.PermissionDeniedException)
68 Console.Out.WriteLine("ok");
69 me.lck.Lock();
70 try
72 wakeUp();
74 finally
76 me.lck.Unlock();
79 catch(System.Exception)
81 test(false);
85 public void
86 createdCommunicator(Glacier2.SessionHelper session)
88 test(session.communicator() != null);
92 public class SessionCalback2 : Glacier2.SessionCallback
94 public void
95 connected(Glacier2.SessionHelper session)
97 Console.Out.WriteLine("ok");
98 me.lck.Lock();
99 try
101 wakeUp();
103 finally
105 me.lck.Unlock();
109 public void
110 disconnected(Glacier2.SessionHelper session)
112 Console.Out.WriteLine("ok");
113 me.lck.Lock();
116 wakeUp();
118 finally
120 me.lck.Unlock();
124 public void
125 connectFailed(Glacier2.SessionHelper session, System.Exception ex)
127 Console.Out.WriteLine(ex.ToString());
128 test(false);
131 public void
132 createdCommunicator(Glacier2.SessionHelper session)
134 test(session.communicator() != null);
138 public class SessionCalback3 : Glacier2.SessionCallback
140 public void
141 connected(Glacier2.SessionHelper session)
143 test(false);
146 public void
147 disconnected(Glacier2.SessionHelper session)
149 test(false);
152 public void
153 connectFailed(Glacier2.SessionHelper session, System.Exception exception)
157 throw exception;
159 catch(Ice.ConnectionRefusedException)
161 Console.Out.WriteLine("ok");
162 me.lck.Lock();
165 wakeUp();
167 finally
169 me.lck.Unlock();
172 catch(System.Exception)
174 test(false);
178 public void
179 createdCommunicator(Glacier2.SessionHelper session)
181 test(session.communicator() != null);
185 public Ice.InitializationData getInitData()
187 return _initData;
190 public override int run(string[] args)
192 _factory = new Glacier2.SessionFactoryHelper(_initData, new SessionCalback1());
195 // Test to create a session with wrong userid/password
197 lck.Lock();
200 Console.Out.Write("testing SessionHelper connect with wrong userid/password... ");
201 Console.Out.Flush();
203 _factory.setRouterHost("127.0.0.1");
204 _factory.setPort(12347);
205 _factory.setRouterIdentity(Ice.Util.stringToIdentity("Glacier2/router"));
206 _factory.setSecure(false);
207 _session = _factory.connect("userid", "xxx");
208 while(true)
210 #if COMPACT
211 lck.Wait();
212 break;
213 #else
216 lck.Wait();
217 break;
219 catch(ThreadInterruptedException)
222 #endif
225 finally
227 lck.Unlock();
230 _factory = new Glacier2.SessionFactoryHelper(_initData, new SessionCalback2());
231 lck.Lock();
234 Console.Out.Write("testing SessionHelper connect... ");
235 Console.Out.Flush();
236 _factory.setRouterHost("127.0.0.1");
237 _factory.setPort(12347);
238 _factory.setRouterIdentity(Ice.Util.stringToIdentity("Glacier2/router"));
239 _factory.setSecure(false);
240 _session = _factory.connect("userid", "abc123");
241 while(true)
243 #if COMPACT
244 lck.Wait();
245 break;
246 #else
249 lck.Wait();
250 break;
252 catch(ThreadInterruptedException)
255 #endif
258 Console.Out.Write("testing SessionHelper isConnected after connect... ");
259 Console.Out.Flush();
260 test(_session.isConnected());
261 Console.Out.WriteLine("ok");
263 Console.Out.Write("testing SessionHelper categoryForClient after connect... ");
264 Console.Out.Flush();
267 test(!_session.categoryForClient().Equals(""));
269 catch(Glacier2.SessionNotExistException)
271 test(false);
273 Console.Out.WriteLine("ok");
275 // try
276 // {
277 // test(_session.session() != null);
278 // }
279 // catch(Glacier2.SessionNotExistException ex)
280 // {
281 // test(false);
282 // }
284 Console.Out.Write("testing stringToProxy for server object... ");
285 Console.Out.Flush();
286 Ice.ObjectPrx @base = _session.communicator().stringToProxy("callback:tcp -p 12010");
287 Console.Out.WriteLine("ok");
289 Console.Out.Write("pinging server after session creation... ");
290 Console.Out.Flush();
291 @base.ice_ping();
292 Console.Out.WriteLine("ok");
294 Console.Out.Write("testing checked cast for server object... ");
295 Console.Out.Flush();
296 CallbackPrx twoway = CallbackPrxHelper.checkedCast(@base);
297 test(twoway != null);
298 Console.Out.WriteLine("ok");
300 Console.Out.Write("testing server shutdown... ");
301 Console.Out.Flush();
302 twoway.shutdown();
303 Console.Out.WriteLine("ok");
305 test(_session.communicator() != null);
306 Console.Out.Write("testing SessionHelper destroy... ");
307 Console.Out.Flush();
308 _session.destroy();
309 while(true)
311 #if COMPACT
312 lck.Wait();
313 break;
314 #else
317 lck.Wait();
318 break;
320 catch(ThreadInterruptedException)
323 #endif
326 Console.Out.Write("testing SessionHelper isConnected after destroy... ");
327 Console.Out.Flush();
328 test(_session.isConnected() == false);
329 Console.Out.WriteLine("ok");
331 Console.Out.Write("testing SessionHelper categoryForClient after destroy... ");
332 Console.Out.Flush();
335 test(!_session.categoryForClient().Equals(""));
336 test(false);
338 catch(Glacier2.SessionNotExistException)
341 Console.Out.WriteLine("ok");
343 Console.Out.Write("testing SessionHelper session after destroy... ");
346 _session.session();
347 test(false);
349 catch(Glacier2.SessionNotExistException)
352 Console.Out.WriteLine("ok");
354 Console.Out.Write("testing SessionHelper communicator after destroy... ");
355 Console.Out.Flush();
356 test(_session.communicator() == null);
357 Console.Out.WriteLine("ok");
360 Console.Out.Write("uninstalling router with communicator... ");
361 Console.Out.Flush();
362 communicator().setDefaultRouter(null);
363 Console.Out.WriteLine("ok");
365 Ice.ObjectPrx processBase;
367 Console.Out.Write("testing stringToProxy for process object... ");
368 processBase = communicator().stringToProxy("Glacier2/admin -f Process:tcp -h 127.0.0.1 -p 12348");
369 Console.Out.WriteLine("ok");
373 Ice.ProcessPrx process;
375 Console.Out.Write("testing checked cast for admin object... ");
376 process = Ice.ProcessPrxHelper.checkedCast(processBase);
377 test(process != null);
378 Console.Out.WriteLine("ok");
381 Console.Out.Write("testing Glacier2 shutdown... ");
382 process.shutdown();
385 process.ice_ping();
386 test(false);
388 catch(Ice.LocalException)
390 Console.Out.WriteLine("ok");
393 finally
395 lck.Unlock();
398 _factory = new Glacier2.SessionFactoryHelper(_initData, new SessionCalback3());
399 lck.Lock();
402 Console.Out.Write("testing SessionHelper connect after router shutdown... ");
403 Console.Out.Flush();
405 _factory.setRouterHost("127.0.0.1");
406 _factory.setPort(12347);
407 _factory.setRouterIdentity(Ice.Util.stringToIdentity("Glacier2/router"));
408 _factory.setSecure(false);
409 _session = _factory.connect("userid", "abc123");
410 while(true)
412 #if COMPACT
413 lck.Wait();
414 break;
415 #else
418 lck.Wait();
419 break;
421 catch(ThreadInterruptedException)
424 #endif
427 Console.Out.Write("testing SessionHelper isConnect after connect failure... ");
428 Console.Out.Flush();
429 test(_session.isConnected() == false);
430 Console.Out.WriteLine("ok");
432 Console.Out.Write("testing SessionHelper communicator after connect failure... ");
433 Console.Out.Flush();
434 test(_session.communicator() == null);
435 Console.Out.WriteLine("ok");
437 Console.Out.Write("testing SessionHelper destroy after connect failure... ");
438 Console.Out.Flush();
439 _session.destroy();
440 Console.Out.WriteLine("ok");
442 finally
444 lck.Unlock();
447 return 0;
450 public static void
451 wakeUp()
453 me.lck.Notify();
456 private static void
457 test(bool b)
459 if(!b)
461 throw new Exception();
465 public static App me;
466 public IceUtilInternal.Monitor lck = new IceUtilInternal.Monitor();
467 private Ice.InitializationData _initData;
468 private Glacier2.SessionHelper _session;
469 private Glacier2.SessionFactoryHelper _factory;
472 public static int Main(string[] args)
474 #if !COMPACT
475 Debug.Listeners.Add(new ConsoleTraceListener());
476 #endif
478 App app = new App();
479 app.getInitData().properties.setProperty("Ice.Warn.Connections", "0");
480 return app.main(args, app.getInitData());