Esempio n. 1
0
bool
Glacier2::Application::doMain(Ice::StringSeq& args, const Ice::InitializationData& initData, int& status)
{
    //
    // Reset internal state variables from Ice.Application. The
    // remainder are reset at the end of this method.
    //
    IceInternal::Application::_callbackInProgress = false;
    IceInternal::Application::_destroyed = false;
    IceInternal::Application::_interrupted = false;

    bool restart = false;
    status = 0;

    try
    {
        IceInternal::Application::_communicator = Ice::initialize(args, initData);
        _router = Glacier2::RouterPrx::uncheckedCast(communicator()->getDefaultRouter());
        
        if(!_router)
        {
            Error out(getProcessLogger());
            out << IceInternal::Application::_appName << ": no glacier2 router configured";
            status = 1;
        }
        else
        {
            //
            // The default is to destroy when a signal is received.
            //
            if(IceInternal::Application::_signalPolicy == Ice::HandleSignals)
            {
                destroyOnInterrupt();
            }

            // If createSession throws, we're done.
            try
            {
                _session = createSession();
                _createdSession = true;
            }
            catch(const Ice::LocalException& ex)
            {
                Error out(getProcessLogger());
                out << IceInternal::Application::_appName << ": " << ex;
                status = 1;
            }

            if(_createdSession)
            {
                Ice::Int acmTimeout = 0;
                try
                { 
                    acmTimeout = _router->getACMTimeout();
                }
                catch(const Ice::OperationNotExistException&)
                {
                }
                if(acmTimeout <= 0)
                {
                    acmTimeout = static_cast<Ice::Int>(_router->getSessionTimeout());
                }

                if(acmTimeout > 0)
                {
                    Ice::ConnectionPtr connection = _router->ice_getCachedConnection();
                    assert(connection);
                    connection->setACM(acmTimeout, IceUtil::None, Ice::HeartbeatAlways);
                    connection->setCallback(new ConnectionCallbackI(this));
                }

                _category = _router->getCategoryForClient();
                IceUtilInternal::ArgVector a(args);
                status = runWithSession(a.argc, a.argv);
            }
        }
    }
    // We want to restart on those exceptions which indicate a
    // break down in communications, but not those exceptions that
    // indicate a programming logic error (ie: marshal, protocol
    // failure, etc).
    catch(const RestartSessionException&)
    {
        restart = true;
    }
    catch(const Ice::ConnectionRefusedException& ex)
    {
        Error out(getProcessLogger());
        out << IceInternal::Application::_appName << ": " << ex;
        restart = true;
    }
    catch(const Ice::ConnectionLostException& ex)
    {
        Error out(getProcessLogger());
        out << IceInternal::Application::_appName << ": " << ex;
        restart = true;
    }
    catch(const Ice::UnknownLocalException& ex)
    {
        Error out(getProcessLogger());
        out << IceInternal::Application::_appName << ": " << ex;
        restart = true;
    }
    catch(const Ice::RequestFailedException& ex)
    {
        Error out(getProcessLogger());
        out << IceInternal::Application::_appName << ": " << ex;
        restart = true;
    }
    catch(const Ice::TimeoutException& ex)
    {
        Error out(getProcessLogger());
        out << IceInternal::Application::_appName << ": " << ex;
        restart = true;
    }
    catch(const Ice::LocalException& ex)
    {
        Error out(getProcessLogger());
        out << IceInternal::Application::_appName << ": " << ex;
        status = 1;
    }
    catch(const std::exception& ex)
    {
        Error out(getProcessLogger());
        out << IceInternal::Application::_appName << ": std::exception " << ex;
        status = 1;
    }
    catch(const std::string& ex)
    {
        Error out(getProcessLogger());
        out << IceInternal::Application::_appName << ": c++ exception " << ex;
        status = 1;
    }
    catch(const char* ex)
    {
        Error out(getProcessLogger());
        out << IceInternal::Application::_appName << ": char* exception " << ex;
        status = 1;
    }
    catch(...)
    {
        Error out(getProcessLogger());
        out << IceInternal::Application::_appName << ": unknown exception";
        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(IceInternal::Application::_signalPolicy == HandleSignals)
    {
        ignoreInterrupt();
    }

    {
        IceUtil::Mutex::Lock lock(*IceInternal::Application::mutex);
        while(IceInternal::Application::_callbackInProgress)
        {
            IceInternal::Application::_condVar->wait(lock);
        }
        if(IceInternal::Application::_destroyed)
        {
            IceInternal::Application::_communicator = 0;
        }
        else
        {
            IceInternal::Application::_destroyed = true;
            //
            // And _communicator != 0, meaning will be destroyed
            // next, _destroyed = true also ensures that any
            // remaining callback won't do anything
            //
        }
        IceInternal::Application::_application = 0;
    }

    if(_createdSession && _router)
    {
        try
        {
            _router->destroySession();
        }
        catch(const Ice::ConnectionLostException&)
        {
            // Expected if another thread invoked on an object from the session concurrently.
        }
        catch(const Glacier2::SessionNotExistException&)
        {
            // This can also occur.
        }
        catch(const exception& ex)
        {
            // Not expected.
            Error out(getProcessLogger());
            out << "unexpected exception when destroying the session:\n" << ex;
        }
        _router = 0;
    }

    if(IceInternal::Application::_communicator)
    {
        try
        {
            IceInternal::Application::_communicator->destroy();
        }
        catch(const Ice::LocalException& ex)
        {
            Error out(getProcessLogger());
            out << IceInternal::Application::_appName << ": " << ex;
            status = 1;
        }
        catch(const exception& ex)
        {
            Error out(getProcessLogger());
            out << "unknown exception:\n" << ex;
            status = 1;
        }
        IceInternal::Application::_communicator = 0;
    }

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

    return restart;
}
Esempio n. 2
0
void
SessionHelperI::connected(const Glacier2::RouterPrx& router, const Glacier2::SessionPrx& session)
{
    //
    // Remote invocation should be done without acquiring a mutex lock.
    //
    assert(router);
    Ice::ConnectionPtr conn = router->ice_getCachedConnection();
    string category = router->getCategoryForClient();
    Ice::Int acmTimeout = 0;
    try
    {
        acmTimeout = router->getACMTimeout();
    }
    catch(const Ice::OperationNotExistException&)
    {
    }

    if(acmTimeout <= 0)
    {
        acmTimeout = static_cast<Ice::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)
    {
        _adapter = _communicator->createObjectAdapterWithRouter("", router);
        _adapter->activate();
    }

    bool destroy;
    {
        IceUtil::Mutex::Lock sync(_mutex);
        _router = router;
        destroy = _destroy;

        if(!_destroy)
        {
            //
            // Cache the category.
            //
            _category = category;

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

            if(acmTimeout > 0)
            {
                Ice::ConnectionPtr connection = _router->ice_getCachedConnection();
                assert(connection);
                connection->setACM(acmTimeout, IceUtil::None, Ice::HeartbeatAlways);
                connection->setCallback(new ConnectionCallbackI(this));
            }
        }
    }

    if(destroy)
    {
        //
        // connected() is only called from the ConnectThread so it is ok to
        // call destroyInternal here.
        //
        destroyInternal(new Disconnected(this, _callback));
    }
    else
    {
        dispatchCallback(new Connected(_callback, this), conn);
    }
}