context: // **********************************************************************
  //
  // Copyright (c) 2003-2017 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 Test;
  using System;
  using System.Reflection;
  using System.Threading;
  
  [assembly: CLSCompliant(true)]
  
  [assembly: AssemblyTitle("IceTest")]
  [assembly: AssemblyDescription("Ice test")]
  [assembly: AssemblyCompany("ZeroC, Inc.")]
  
  public class Client : TestCommon.Application
  {
      public class SessionCallback1 : Glacier2.SessionCallback
      {
          public
          SessionCallback1(Client app)
          {
              _app = app;
          }
  
          public void
          connected(Glacier2.SessionHelper session)
          {
               test(false);
          }
  
          public void
          disconnected(Glacier2.SessionHelper session)
          {
              test(false);
          }
  
          public void
          connectFailed(Glacier2.SessionHelper session, Exception exception)
          {
              try
              {
                  throw exception;
              }
              catch(Glacier2.PermissionDeniedException)
              {
                  Console.Out.WriteLine("ok");
                  _app.wakeUp();
              }
              catch(Exception)
              {
                  test(false);
              }
          }
  
          public void
          createdCommunicator(Glacier2.SessionHelper session)
          {
              test(session.communicator() != null);
          }
  
          private Client _app;
      }
  
      public class SessionCallback2 : Glacier2.SessionCallback
      {
          public
          SessionCallback2(Client app)
          {
              _app = app;
          }
  
          public void
          connected(Glacier2.SessionHelper session)
          {
              Console.Out.WriteLine("ok");
              _app.wakeUp();
          }
  
          public void
          disconnected(Glacier2.SessionHelper session)
          {
              Console.Out.WriteLine("ok");
              _app.wakeUp();
          }
  
          public void
          connectFailed(Glacier2.SessionHelper session, Exception ex)
          {
              Console.Out.WriteLine(ex.ToString());
              test(false);
          }
  
          public void
          createdCommunicator(Glacier2.SessionHelper session)
          {
              test(session.communicator() != null);
          }
  
          private Client _app;
      }
  
      public class SessionCallback3 : Glacier2.SessionCallback
      {
          public
          SessionCallback3(Client app)
          {
              _app = app;
          }
  
          public void
          connected(Glacier2.SessionHelper session)
          {
               test(false);
          }
  
          public void
          disconnected(Glacier2.SessionHelper session)
          {
              test(false);
          }
  
          public void
          connectFailed(Glacier2.SessionHelper session, Exception exception)
          {
              try
              {
                  throw exception;
              }
              catch(Ice.ConnectionRefusedException)
              {
                  Console.Out.WriteLine("ok");
                  _app.wakeUp();
              }
              catch(Exception)
              {
                  test(false);
              }
          }
  
          public void
          createdCommunicator(Glacier2.SessionHelper session)
          {
              test(session.communicator() != null);
          }
  
          private Client _app;
      }
  
      public class SessionCallback4 : Glacier2.SessionCallback
      {
          public
          SessionCallback4(Client app)
          {
              _app = app;
          }
  
          public void
          connected(Glacier2.SessionHelper session)
          {
               test(false);
          }
  
          public void
          disconnected(Glacier2.SessionHelper session)
          {
              test(false);
          }
  
          public void
          connectFailed(Glacier2.SessionHelper session, Exception exception)
          {
              try
              {
                  throw exception;
              }
              catch(Ice.CommunicatorDestroyedException)
              {
                  Console.Out.WriteLine("ok");
                  _app.wakeUp();
              }
              catch(Exception)
              {
                  test(false);
              }
          }
  
          public void
          createdCommunicator(Glacier2.SessionHelper session)
          {
              test(session.communicator() != null);
          }
  
          private Client _app;
      }
  
      public override int run(string[] args)
      {
          string protocol = getTestProtocol();
          string host = getTestHost();
  
          _factory = new Glacier2.SessionFactoryHelper(_initData, new SessionCallback1(this));
  
          //
          // Test to create a session with wrong userid/password
          //
          lock(this)
          {
              Console.Out.Write("testing SessionHelper connect with wrong userid/password... ");
              Console.Out.Flush();
  
              _factory.setProtocol(protocol);
              _session = _factory.connect("userid", "xxx");
              while(true)
              {
                  try
                  {
                      Monitor.Wait(this, 30000);
                      break;
                  }
                  catch(ThreadInterruptedException)
                  {
                  }
              }
              test(!_session.isConnected());
          }
  
          _initData.properties.setProperty("Ice.Default.Router", "");
          _factory = new Glacier2.SessionFactoryHelper(_initData, new SessionCallback4(this));
          lock(this)
          {
              Console.Out.Write("testing SessionHelper connect interrupt... ");
              Console.Out.Flush();
              _factory.setRouterHost(host);
              _factory.setPort(getTestPort(1));
              _factory.setProtocol(protocol);
  
gt:             _session = _factory.connect("userid", "abc123");