1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
|
//
// Copyright (c) ZeroC, Inc. All rights reserved.
//
#ifndef GLACIER2_SESSION_HELPER_H
#define GLACIER2_SESSION_HELPER_H
#include <IceUtil/Shared.h>
#include <IceUtil/Handle.h>
#include <IceUtil/Thread.h>
#include <IceUtil/Mutex.h>
#include <Ice/Initialize.h>
#include <Ice/Properties.h>
#include <Ice/Communicator.h>
#include <Ice/ObjectAdapter.h>
#include <Ice/Ice.h>
#include <Glacier2/Session.h>
#include <Glacier2/Router.h>
#include <map>
#include <string>
namespace Glacier2
{
/** The IANA-registered port number for Glacier2 via SSL. */
const int GLACIER2_SSL_PORT = 4064;
/** The IANA-registered port number for Glacier2 via TCP. */
const int GLACIER2_TCP_PORT = 4063;
/**
* Encapsulates a Glacier2 session and provides much of the same functionality as Glacier2::Application
* but better suited for graphical applications.
* \headerfile Glacier2/Glacier2.h
*/
class GLACIER2_API SessionHelper
#ifndef ICE_CPP11_MAPPING
: public virtual IceUtil::Shared
#endif
{
public:
virtual ~SessionHelper();
/**
* Initiates the destruction of the Glacier2 session, including the communicator created by the SessionHelper.
*/
virtual void destroy() = 0;
/**
* Obtains the communicator created by the SessionHelper.
* @return The communicator object.
* @throws SessionNotExistException if no session is currently active.
*/
virtual Ice::CommunicatorPtr communicator() const = 0;
/**
* Obtains the category that must be used in the identities of all callback objects.
* @return The identity category.
* @throws SessionNotExistException if no session is currently active.
*/
virtual std::string categoryForClient() const = 0;
/**
* Adds a servant to the callback object adapter using a UUID for the identity name.
* Also see Ice::ObjectAdapter::addWithUUID.
* @param servant The servant to add to the callback object adapter's active servant table.
* @return A proxy for the object.
* @throws SessionNotExistException if no session is currently active.
*/
virtual Ice::ObjectPrxPtr addWithUUID(const Ice::ObjectPtr& servant) = 0;
/**
* Obtains a proxy for the Glacier2 session.
* @return The session proxy, or a nil proxy if no session is currently active.
*/
virtual SessionPrxPtr session() const = 0;
/**
* Determines whether the session is active.
* @return True if the session is currently active, false otherwise.
*/
virtual bool isConnected() const = 0;
/**
* Obtains the callback object adapter. This object adapter is only created if the session factory
* was configured to do so using SessionFactoryHelper::setUseCallbacks.
* @return The object adapter, or nil if no object adapter was created.
* @throws SessionNotExistException if no session is currently active.
*/
virtual Ice::ObjectAdapterPtr objectAdapter() = 0;
#ifndef ICE_CPP11_MAPPING
bool operator==(const SessionHelper&) const;
bool operator!=(const SessionHelper&) const;
#endif
};
ICE_DEFINE_PTR(SessionHelperPtr, SessionHelper);
/**
* Allows an application to receive notification about events in the lifecycle of a Glacier2 session.
* \headerfile Glacier2/Glacier2.h
*/
class GLACIER2_API SessionCallback
#ifndef ICE_CPP11_MAPPING
: public virtual IceUtil::Shared
#endif
{
public:
virtual ~SessionCallback();
/**
* Called after successfully initializing a communicator.
* @param session The corresponding session helper.
*/
virtual void createdCommunicator(const SessionHelperPtr& session) = 0;
/**
* Called after successfully establishing the Glacier2 session.
* @param session The corresponding session helper.
*/
virtual void connected(const SessionHelperPtr& session) = 0;
/**
* Called after the Glacier2 session is destroyed.
* @param session The corresponding session helper.
*/
virtual void disconnected(const SessionHelperPtr& session) = 0;
/**
* Called if a failure occurred while attempting to establish a Glacier2 session.
* @param session The corresponding session helper.
* @param ex The exception that caused the failure.
*/
virtual void connectFailed(const SessionHelperPtr& session, const Ice::Exception& ex) = 0;
};
ICE_DEFINE_PTR(SessionCallbackPtr, SessionCallback);
/// \cond INTERNAL
class SessionThreadCallback;
/// \endcond
/**
* Facilitates the creation of new Glacier2 sessions.
* \headerfile Glacier2/Glacier2.h
*/
class GLACIER2_API SessionFactoryHelper
#ifdef ICE_CPP11_MAPPING
: public std::enable_shared_from_this<SessionFactoryHelper>
#else
: public virtual IceUtil::Shared
#endif
{
/// \cond INTERNAL
friend class SessionThreadCallback; // To access thread functions
/// \endcond
public:
/**
* This constructor is useful when your application has no other configuration requirements.
* The constructor allocates an InitializationData object and a new property set.
* @param callback The callback object (must not be nil).
*/
SessionFactoryHelper(const SessionCallbackPtr& callback);
/**
* Use this constructor when you want to provide your own instance of InitializationData.
* @param initData Initialization data for the communicator.
* @param callback The callback object (must not be nil).
*/
SessionFactoryHelper(const Ice::InitializationData& initData, const SessionCallbackPtr& callback);
/**
* This constructor is convenient when you want to supply an initial set of properties.
* @param properties Configuration properties for the communicator.
* @param callback The callback object (must not be nil).
*/
SessionFactoryHelper(const Ice::PropertiesPtr& properties, const SessionCallbackPtr& callback);
~SessionFactoryHelper();
/**
* Blocks until all background threads are terminated.
*/
void destroy();
/**
* Sets the object identity of the Glacier2 router.
* @param identity The router identity.
*/
void setRouterIdentity(const Ice::Identity& identity);
/**
* Obtains the object identity of the Glacier2 router.
* @return The router identity.
*/
Ice::Identity getRouterIdentity() const;
/**
* Sets the host name of the Glacier2 router.
* @param host The router host name.
*/
void setRouterHost(const std::string& host);
/**
* Obtains the host name of the Glacier2 router.
* @return The router host name.
*/
std::string getRouterHost() const;
/// \cond INTERNAL
ICE_DEPRECATED_API("is deprecated, use SessionFactoryHelper::setProtocol instead")
void setSecure(bool);
ICE_DEPRECATED_API("is deprecated, use SessionFactoryHelper::getProtocol instead")
bool getSecure() const;
/// \endcond
/**
* Sets the Ice protocol used for communications with the Glacier2 router.
* @param protocol The protocol name (e.g., "tcp").
*/
void setProtocol(const std::string& protocol);
/**
* Obtains the Ice protocol used for communications with the Glacier2 router.
* @return The protocol name.
*/
std::string getProtocol() const;
/**
* Sets the timeout in milliseconds for the connection to the Glacier2 router.
* @param timeout The timeout in milliseconds.
*/
void setTimeout(int timeout);
/**
* Obtains the timeout in milliseconds for the connection to the Glacier2 router.
* @return The timeout in milliseconds.
*/
int getTimeout() const;
/**
* Sets the port on which the Glacier2 router is listening.
* @param port The router port.
*/
void setPort(int port);
/**
* Obtains the port on which the Glacier2 router is listening.
* @return The router port.
*/
int getPort() const;
/**
* Returns a copy of the InitializationData object that will be used during communicator initialization.
* @return The communicator initialization data.
*/
Ice::InitializationData getInitializationData() const;
/**
* Sets the request context to be used when creating a session.
* @param context The request context.
*/
void setConnectContext(const std::map<std::string, std::string>& context);
/**
* Determines whether the session helper automatically creates an object adapter for callback servants.
* @param b True if the helper should create an object adapter, false otherwise.
*/
void setUseCallbacks(bool b);
/**
* Determines whether the session helper automatically creates an object adapter for callback servants.
* @return True if the helper will create an object adapter, false otherwise.
*/
bool getUseCallbacks() const;
/**
* Initializes a communicator, creates a Glacier2 session using SSL credentials, and returns a new
* SessionHelper object.
*/
SessionHelperPtr connect();
/**
* Initializes a communicator, creates a Glacier2 session using the given username and password,
* and returns a new SessionHelper object.
*/
SessionHelperPtr connect(const std::string& username, const std::string& password);
private:
IceUtil::ThreadPtr addThread(const SessionHelper*, const IceUtil::ThreadPtr&);
Ice::InitializationData createInitData();
std::string getRouterFinderStr();
int getPortInternal() const;
std::string createProxyStr(const Ice::Identity& ident);
void setDefaultProperties();
IceUtil::Mutex _mutex;
std::string _routerHost;
Ice::Identity _identity;
std::string _protocol;
int _port;
int _timeout;
Ice::InitializationData _initData;
SessionCallbackPtr _callback;
std::map<std::string, std::string> _context;
bool _useCallbacks;
std::map<const SessionHelper*, IceUtil::ThreadPtr> _threads;
};
ICE_DEFINE_PTR(SessionFactoryHelperPtr, SessionFactoryHelper);
}
#endif
|