示例#1
0
        /// <summary>
        /// Force the communicator to sever it's connection.
        /// </summary>
        public void Disconnect()
        {
            lock (this)
            {
                try
                {
                    if (pinger != null)
                    {
                        pinger.Running = false;
                    }

                    if (keepAliveTimer != null)
                    {
                        keepAliveTimer.Dispose();
                    }

                    if (session != null)
                    {
                        session.Disconnect_async(new Disconnect_Callback());
                    }

                    if (router != null)
                    {
                        router.destroySession_async(new RouterDestroySession_Callback());
                    }

                    if (logger != null)
                    {
                        logger.Close();
                    }

                    DestroyCommunicator();
                }
                catch (System.Exception e)
                {
                    if (logger != null)
                    {
                        logger.Error(
                            "Unexpected error in the Disconnect() method: {0}", e);
                    }
                    else
                    {
                        Console.Error.WriteLine(e);
                    }
                }
                finally
                {
                    Connected            = false;
                    pinger               = null;
                    keepAliveTimer       = null;
                    session              = null;
                    router               = null;
                    factory              = null;
                    tankList             = null;
                    tankListNeedsRefresh = true;
                    // weaponList = null;
                    logger = null;
                }
            }
        }
示例#2
0
        /// <summary>
        /// Connect to the main server using the stored properties.
        /// </summary>
        /// <returns>True if the connection was successful; false otherwise.</returns>
        public bool Connect()
        {
            if (Connected)
            {
                // Already connected -- so sever the connection.
                Disconnect();
            }

            if (logger == null)
            {
                if (LogFile == null)
                {
                    LogFile = "MasterCommunicator.log";
                }

                logger = new Logger(LogFile);
            }

            bool result = false;

            lock (this)
            {
                try
                {
                    logger.Info("Connect() Attempting to connect...");

                    CreateCommunicator();
                    factory = CreateSessionFactoryProxy();

                    Connected = true;
                    Running   = true;

                    logger.Info("Connect() Connection attempt successful.");

                    result = true;
                }
                catch (Ice.Exception e)
                {
                    logger.Error("Connect() Ice.Exception: {0}", e);
                }
                catch (System.Exception e)
                {
                    logger.Error("Connect() System.Exception: {0}", e);
                }
            }

            return(result);
        }
示例#3
0
        /// <summary>
        /// Create a proxy pointing to the session factory, which gives the program access to
        /// login methods. If Glacier2 is enabled, it goes through a Glacier2 router.
        /// </summary>
        /// <returns></returns>
        private SessionFactoryPrx CreateSessionFactoryProxy()
        {
            SessionFactoryPrx sessionFactoryProxy = null;

            if (UseGlacier2)
            {
                /*Ice.ObjectPrx routerProxy = communicator.stringToProxy(String.Format(
                 *  "VTank/router:{0} -h {1} -p {2} -t {3}",
                 *      (Secure ? "tcp" : "tcp"), // TODO: We'll eventually use "ssl" for secure connections.
                 *      Host, Port, Timeout));*/
                Ice.ObjectPrx routerProxy = communicator.getDefaultRouter();

                router = Glacier2.RouterPrxHelper.checkedCast(routerProxy);
                if (router == null)
                {
                    // Not a valid Glacier2 router.
                    throw new InvalidHostException(
                              "The target host does not use a Glacier2 router.");
                }

                Glacier2.SessionPrx sessionProxy = router.createSession("MasterCommunicator", "");
                sessionFactoryProxy = SessionFactoryPrxHelper.uncheckedCast(sessionProxy);
            }
            else
            {
                communicator.setDefaultRouter(null);
                sessionFactoryProxy = SessionFactoryPrxHelper.uncheckedCast(
                    communicator.stringToProxy(String.Format(
                                                   "SessionFactory:{0} -h {1} -p {2} -t {3}",
                                                   (Secure ? "tcp" : "tcp"), // TODO: We'll eventually use "ssl" for secure connections.
                                                   Host, Port, Timeout)));
            }

            // Pinging once will initiate the connection and cause an exception to be thrown if
            // the target host is invalid. This is only useful for non-router connections.
            sessionFactoryProxy.ice_ping();

            logger.Info("SessionFactoryProxy created successfully.");

            return(sessionFactoryProxy);
        }
示例#4
0
        public override int run(string[] args)
        {
            if (args.Length > 0)
            {
                Console.Error.WriteLine(appName() + ": too many arguments");
                return(1);
            }

            string name;

            do
            {
                Console.Out.Write("Please enter your name ==> ");
                Console.Out.Flush();

                name = Console.In.ReadLine();
                if (name == null)
                {
                    return(1);
                }
                name = name.Trim();
            }while(name.Length == 0);

            Ice.ObjectPrx     basePrx = communicator().propertyToProxy("SessionFactory.Proxy");
            SessionFactoryPrx factory = SessionFactoryPrxHelper.checkedCast(basePrx);

            if (factory == null)
            {
                Console.Error.WriteLine("invalid proxy");
                return(1);
            }

            SessionPrx session = factory.create(name);

            SessionRefreshThread refresh = new SessionRefreshThread(communicator().getLogger(), 5000, session);
            Thread refreshThread         = new Thread(new ThreadStart(refresh.run));

            refreshThread.Start();

            List <HelloPrx> hellos = new List <HelloPrx>();

            menu();

            try
            {
                bool destroy  = true;
                bool shutdown = false;
                while (true)
                {
                    Console.Out.Write("==> ");
                    Console.Out.Flush();
                    string line = Console.In.ReadLine();
                    if (line == null)
                    {
                        break;
                    }
                    if (line.Length > 0 && Char.IsDigit(line[0]))
                    {
                        int index = Int32.Parse(line);
                        if (index < hellos.Count)
                        {
                            HelloPrx hello = hellos[index];
                            hello.sayHello();
                        }
                        else
                        {
                            Console.Out.WriteLine("Index is too high. " + hellos.Count +
                                                  " hello objects exist so far.\n" +
                                                  "Use `c' to create a new hello object.");
                        }
                    }
                    else if (line.Equals("c"))
                    {
                        hellos.Add(session.createHello());
                        Console.Out.WriteLine("Created hello object " + (hellos.Count - 1));
                    }
                    else if (line.Equals("s"))
                    {
                        destroy  = false;
                        shutdown = true;
                        break;
                    }
                    else if (line.Equals("x"))
                    {
                        break;
                    }
                    else if (line.Equals("t"))
                    {
                        destroy = false;
                        break;
                    }
                    else if (line.Equals("?"))
                    {
                        menu();
                    }
                    else
                    {
                        Console.Out.WriteLine("Unknown command `" + line + "'.");
                        menu();
                    }
                }

                //
                // The refresher thread must be terminated before destroy is
                // called, otherwise it might get ObjectNotExistException. refresh
                // is set to 0 so that if session.destroy() raises an exception
                // the thread will not be re-terminated and re-joined.
                //
                refresh.terminate();
                refreshThread.Join();
                refresh = null;

                if (destroy)
                {
                    session.destroy();
                }
                if (shutdown)
                {
                    factory.shutdown();
                }
            }
            catch (System.Exception)
            {
                //
                // The refresher thread must be terminated in the event of a
                // failure.
                //
                if (refresh != null)
                {
                    refresh.terminate();
                    refreshThread.Join();
                    refresh = null;
                }
                throw;
            }

            return(0);
        }