Exemplo n.º 1
0
        doMain(string[] args, Ice.InitializationData initData, out int status)
        {
            //
            // Reset internal state variables from Ice.Application. The
            // remainder are reset at the end of this method.
            //
            callbackInProgress__ = false;
            destroyed__          = false;
            interrupted__        = false;

            bool restart = false;

            status = 0;

            try
            {
                communicator__ = Ice.Util.initialize(ref args, initData);

                _router = Glacier2.RouterPrxHelper.uncheckedCast(communicator().getDefaultRouter());
                if (_router == null)
                {
                    Ice.Util.getProcessLogger().error(appName__ + ": no Glacier2 router configured");
                    status = 1;
                }
                else
                {
                    //
                    // The default is to destroy when a signal is received.
                    //
                    if (signalPolicy__ == Ice.SignalPolicy.HandleSignals)
                    {
                        destroyOnInterrupt();
                    }

                    //
                    // If createSession throws, we're done.
                    //
                    try
                    {
                        _session        = createSession();
                        _createdSession = true;
                    }
                    catch (Ice.LocalException ex)
                    {
                        Ice.Util.getProcessLogger().error(ex.ToString());
                        status = 1;
                    }

                    if (_createdSession)
                    {
                        int acmTimeout = 0;
                        try
                        {
                            acmTimeout = _router.getACMTimeout();
                        }
                        catch (Ice.OperationNotExistException)
                        {
                        }
                        if (acmTimeout <= 0)
                        {
                            acmTimeout = (int)_router.getSessionTimeout();
                        }
                        if (acmTimeout > 0)
                        {
                            Ice.Connection connection = _router.ice_getCachedConnection();
                            Debug.Assert(connection != null);
                            connection.setACM((int)acmTimeout, Ice.Util.None, Ice.ACMHeartbeat.HeartbeatAlways);
                            connection.setCallback(new ConnectionCallbackI(this));
                        }
                        _category = _router.getCategoryForClient();
                        status    = runWithSession(args);
                    }
                }
            }
            //
            // We want to restart on those exceptions that indicate a
            // break down in communications, but not those exceptions that
            // indicate a programming logic error (i.e., marshal, protocol
            // failure, etc).
            //
            catch (RestartSessionException)
            {
                restart = true;
            }
            catch (Ice.ConnectionRefusedException ex)
            {
                Ice.Util.getProcessLogger().error(ex.ToString());
                restart = true;
            }
            catch (Ice.ConnectionLostException ex)
            {
                Ice.Util.getProcessLogger().error(ex.ToString());
                restart = true;
            }
            catch (Ice.UnknownLocalException ex)
            {
                Ice.Util.getProcessLogger().error(ex.ToString());
                restart = true;
            }
            catch (Ice.RequestFailedException ex)
            {
                Ice.Util.getProcessLogger().error(ex.ToString());
                restart = true;
            }
            catch (Ice.TimeoutException ex)
            {
                Ice.Util.getProcessLogger().error(ex.ToString());
                restart = true;
            }
            catch (Ice.LocalException ex)
            {
                Ice.Util.getProcessLogger().error(ex.ToString());
                status = 1;
            }
            catch (System.Exception ex)
            {
                Ice.Util.getProcessLogger().error("unknown exception:\n" + ex.ToString());
                status = 1;
            }

            //
            // Don't want any new interrupt. And at this point
            // (post-run), it would not make sense to release a held
            // signal to run shutdown or destroy.
            //
            if (signalPolicy__ == Ice.SignalPolicy.HandleSignals)
            {
                ignoreInterrupt();
            }

            lock (mutex__)
            {
                while (callbackInProgress__)
                {
                    System.Threading.Monitor.Wait(mutex__);
                }

                if (destroyed__)
                {
                    communicator__ = null;
                }
                else
                {
                    destroyed__ = true;
                    //
                    // And communicator__ != null, meaning will be
                    // destroyed next, destroyed__ = true also ensures that
                    // any remaining callback won't do anything
                    //
                }
            }

            if (_createdSession && _router != null)
            {
                try
                {
                    _router.destroySession();
                }
                catch (Ice.ConnectionLostException)
                {
                    //
                    // Expected if another thread invoked on an object from the session concurrently.
                    //
                }
                catch (Glacier2.SessionNotExistException)
                {
                    //
                    // This can also occur.
                    //
                }
                catch (System.Exception ex)
                {
                    //
                    // Not expected.
                    //
                    Ice.Util.getProcessLogger().error("unexpected exception when destroying the session:\n" +
                                                      ex.ToString());
                }
                _router = null;
            }

            if (communicator__ != null)
            {
                try
                {
                    communicator__.destroy();
                }
                catch (Ice.LocalException ex)
                {
                    Ice.Util.getProcessLogger().error(ex.ToString());
                    status = 1;
                }
                catch (System.Exception ex)
                {
                    Ice.Util.getProcessLogger().error("unknown exception:\n" + ex.ToString());
                    status = 1;
                }
                communicator__ = null;
            }

            //
            // Reset internal state. We cannot reset the Application state
            // here, since destroyed__ must remain true until we re-run
            // this method.
            //
            _adapter        = null;
            _router         = null;
            _session        = null;
            _createdSession = false;
            _category       = null;

            return(restart);
        }
Exemplo n.º 2
0
        connected(RouterPrx router, SessionPrx session)
        {
            //
            // Remote invocation should be done without acquiring a mutex lock.
            //
            Debug.Assert(router != null);
            Ice.Connection conn       = router.ice_getCachedConnection();
            string         category   = router.getCategoryForClient();
            int            acmTimeout = 0;

            try
            {
                acmTimeout = router.getACMTimeout();
            }
            catch (Ice.OperationNotExistException)
            {
            }

            if (acmTimeout <= 0)
            {
                acmTimeout = (int)router.getSessionTimeout();
            }

            //
            // We create the callback object adapter here because createObjectAdapter internally
            // makes synchronous RPCs to the router. We can't create the OA on-demand when the
            // client calls objectAdapter() or addWithUUID() because they can be called from the
            // GUI thread.
            //
            if (_useCallbacks)
            {
                Debug.Assert(_adapter == null);
                _adapter = _communicator.createObjectAdapterWithRouter("", router);
                _adapter.activate();
            }

            lock (this)
            {
                _router = router;

                if (_destroy)
                {
                    //
                    // Run destroyInternal in a thread because it makes remote invocations.
                    //
                    Thread t = new Thread(new ThreadStart(destroyInternal));
                    t.Start();
                    return;
                }

                //
                // Cache the category.
                //
                _category = category;

                //
                // Assign the session after _destroy is checked.
                //
                _session   = session;
                _connected = true;

                if (acmTimeout > 0)
                {
                    Ice.Connection connection = _router.ice_getCachedConnection();
                    Debug.Assert(connection != null);
                    connection.setACM(acmTimeout, Ice.Util.None, Ice.ACMHeartbeat.HeartbeatAlways);
                    connection.setCallback(new ConnectionCallbackI(this));
                }
            }

            dispatchCallback(() =>
            {
                try
                {
                    _callback.connected(this);
                }
                catch (Glacier2.SessionNotExistException)
                {
                    destroy();
                }
            }, conn);
        }