diff options
Diffstat (limited to 'cpp/include')
-rw-r--r-- | cpp/include/Glacier2/Application.h | 222 | ||||
-rw-r--r-- | cpp/include/Ice/Application.h | 36 |
2 files changed, 255 insertions, 3 deletions
diff --git a/cpp/include/Glacier2/Application.h b/cpp/include/Glacier2/Application.h new file mode 100644 index 00000000000..6519ce7cc88 --- /dev/null +++ b/cpp/include/Glacier2/Application.h @@ -0,0 +1,222 @@ +// ********************************************************************** +// +// Copyright (c) 2003-2009 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. +// +// ********************************************************************** + +#include <exception> + +#include <Ice/Application.h> + +#include <Glacier2/Session.h> +#include <Glacier2/Router.h> + +namespace Glacier2 +{ + +/** + * + * This exception is raised if the session should be restarted. + * + **/ +class GLACIER2_API RestartSessionException : public IceUtil::Exception +{ +public: + + virtual std::string ice_name() const; + virtual Exception* ice_clone() const; + virtual void ice_throw() const; +}; + +/** + * An extension of Ice.Application that makes it easy to write + * Glacier2 applications. + * + * <p> Applications must create a derived class that implements the + * {@link #createSession} and {@link #runWithSession} methods.<p> + * + * The base class invokes {@link #createSession} to create a new + * Glacier2 session and then invokes {@link #runWithSession} in + * which the subclass performs its application logic. The base class + * automatically destroys the session when {@link #runWithSession} + * returns. + * + * If {@link #runWithSession} calls {@link #restart} or raises any of + * the exceptions Ice.ConnectionRefusedException, + * Ice.ConnectionLostException, Ice.UnknownLocalException, + * Ice.RequestFailedException, or Ice.TimeoutException, the base + * class destroys the current session and restarts the application + * with another call to {@link #createSession} followed by + * {@link #runWithSession}. + * + * The application can optionally override the {@link #sessionDestroyed} + * callback method if it needs to take action when connectivity with + * the Glacier2 router is lost. + * + * A program can contain only one instance of this class. + * + * @see Ice.Application + * @see Glacier2.Router + * @see Glacier2.Session + * @see Ice.Communicator + * @see Ice.Logger + * @see #runWithSession + **/ + +class GLACIER2_API Application : public Ice::Application +{ + /** + * Initializes an instance that calls {@link Communicator#shutdown} if + * a signal is received. + **/ +public: + + Application() + { + } + + /** + * Initializes an instance that handles signals according to the signal + * policy. + * + * @param signalPolicy Determines how to respond to signals. + * + * @see SignalPolicy + **/ + Application(Ice::SignalPolicy signalPolicy) : Ice::Application(signalPolicy) + { + } + + + /** + * Called once the communicator has been initialized and the Glacier2 session + * has been established. A derived class must implement <code>runWithSession</code>, + * which is the application's starting method. + * + * @param argc The number of elements in argv. + * + * @param argv The argument vector for the application. <code>Application</code> + * scans the argument vector passed to <code>main</code> for options that are + * specific to the Ice run time and removes them; therefore, the vector passed + * to <code>run</code> is free from Ice-related options and contains only options + * and arguments that are application-specific. + * + * @return The <code>runWithSession</code> method should return zero for successful + * termination, and non-zero otherwise. <code>Application.main</code> returns the + * value returned by <code>runWithSession</code>. + **/ + virtual int runWithSession(int argc, char* argv[]) = 0; + + + /** + * Creates a new Glacier2 session. A call to + * <code>createSession</code> always precedes a call to + * <code>runWithSession</code>. If <code>Ice.LocalException</code> + * is thrown from this method, the application is terminated. + + * @return The Glacier2 session. + **/ + virtual Glacier2::SessionPrx createSession() = 0; + + /** + * Called to restart the application's Glacier2 session. This + * method never returns. + * + * @throws RestartSessionException This exception is always thrown. + **/ + void restart() + { + RestartSessionException ex; + throw ex; + } + + /** + * Called when the base class detects that the session has been destroyed. + * A subclass can override this method to take action after the loss of + * connectivity with the Glacier2 router. + **/ + virtual void sessionDestroyed() + { + } + + /** + * Returns the Glacier2 router proxy + * @return The router proxy. + **/ + static Glacier2::RouterPrx router() + { + return _router; + } + + /** + * Returns the Glacier2 session proxy + * @return The session proxy. + **/ + static Glacier2::SessionPrx session() + { + return _session; + } + + /** + * Returns the category to be used in the identities of all of the client's + * callback objects. Clients must use this category for the router to + * forward callback requests to the intended client. + * @return The category. + * @throws SessionNotExistException No session exists. + **/ + std::string categoryForClient(); + + + /** + * Create a new Ice identity for callback objects with the given + * identity name field. + * @return The identity. + **/ + Ice::Identity createCallbackIdentity(const std::string&); + + /** + * Adds a servant to the callback object adapter's Active Servant Map with a UUID. + * @param servant The servant to add. + * @return The proxy for the servant. + **/ + Ice::ObjectPrx addWithUUID(const Ice::ObjectPtr& servant); + + /** + * Creates an object adapter for callback objects. + * @return The object adapter. + */ + Ice::ObjectAdapterPtr objectAdapter(); + +protected: + + virtual int + doMain(int, char*[], const Ice::InitializationData& initData); + +private: + + bool + doMain(Ice::StringSeq&, const Ice::InitializationData&, int&); + + /** + * Run should not be overridden for Glacier2.Application. Instead + * <code>runWithSession</code> should be used. + */ + int run(int, char**) + { + // This shouldn't be called. + assert(false); + return 0; + } + + + static Ice::ObjectAdapterPtr _adapter; + static Glacier2::RouterPrx _router; + static Glacier2::SessionPrx _session; + static bool _createdSession; +}; + +} + diff --git a/cpp/include/Ice/Application.h b/cpp/include/Ice/Application.h index 1ffc86572e4..f8ac4ebae9b 100644 --- a/cpp/include/Ice/Application.h +++ b/cpp/include/Ice/Application.h @@ -12,10 +12,40 @@ #include <Ice/Ice.h> +#include <IceUtil/Mutex.h> + + namespace Ice { + enum SignalPolicy { HandleSignals, NoSignalHandling } ; + class Application; +} -enum SignalPolicy { HandleSignals, NoSignalHandling } ; +namespace IceInternal +{ + extern ICE_API IceUtil::Mutex* mutex; + extern ICE_API std::auto_ptr<IceUtil::Cond> _condVar; + + // + // Variables than can change while run() and communicator->destroy() are running! + // + extern ICE_API bool _callbackInProgress; + extern ICE_API bool _destroyed; + extern ICE_API bool _interrupted; + + // + // Variables that are immutable during run() and until communicator->destroy() has returned; + // before and after run(), and once communicator->destroy() has returned, we assume that + // only the main thread and CtrlCHandler threads are running. + // + extern ICE_API std::string _appName; + extern ICE_API Ice::CommunicatorPtr _communicator; + extern ICE_API Ice::SignalPolicy _signalPolicy; + extern ICE_API Ice::Application* _application; +} + +namespace Ice +{ class ICE_API Application : private IceUtil::noncopyable { @@ -96,9 +126,9 @@ public: // static bool interrupted(); -private: +protected: - int mainInternal(int, char*[], const Ice::InitializationData&); + virtual int doMain(int, char*[], const Ice::InitializationData&); #if defined(__SUNPRO_CC) // |