1 //
2 // Copyright (c) ZeroC, Inc. All rights reserved.
3 //
4 
5 #ifndef ICE_INSTANCE_H
6 #define ICE_INSTANCE_H
7 
8 #include <IceUtil/Config.h>
9 #include <IceUtil/Shared.h>
10 #include <IceUtil/Mutex.h>
11 #include <IceUtil/RecMutex.h>
12 #include <IceUtil/Timer.h>
13 #include <Ice/StringConverter.h>
14 #include <Ice/InstanceF.h>
15 #include <Ice/CommunicatorF.h>
16 #include <Ice/InstrumentationF.h>
17 #include <Ice/TraceLevelsF.h>
18 #include <Ice/DefaultsAndOverridesF.h>
19 #include <Ice/RouterInfoF.h>
20 #include <Ice/LocatorInfoF.h>
21 #include <Ice/ReferenceFactoryF.h>
22 #include <Ice/ProxyFactoryF.h>
23 #include <Ice/ThreadPoolF.h>
24 #include <Ice/ConnectionFactoryF.h>
25 #include <Ice/ACM.h>
26 #include <Ice/ObjectFactory.h>
27 #include <Ice/ObjectAdapterFactoryF.h>
28 #include <Ice/EndpointFactoryManagerF.h>
29 #include <Ice/IPEndpointIF.h>
30 #include <Ice/RetryQueueF.h>
31 #include <Ice/DynamicLibraryF.h>
32 #include <Ice/PluginF.h>
33 #include <Ice/NetworkF.h>
34 #include <Ice/NetworkProxyF.h>
35 #include <Ice/Initialize.h>
36 #include <Ice/ImplicitContextI.h>
37 #include <Ice/FacetMap.h>
38 #include <Ice/Process.h>
39 #include <list>
40 
41 namespace Ice
42 {
43 
44 class CommunicatorI;
45 
46 }
47 
48 namespace IceInternal
49 {
50 
51 class Timer;
52 typedef IceUtil::Handle<Timer> TimerPtr;
53 
54 class MetricsAdminI;
55 ICE_DEFINE_PTR(MetricsAdminIPtr, MetricsAdminI);
56 
57 class RequestHandlerFactory;
58 typedef IceUtil::Handle<RequestHandlerFactory> RequestHandlerFactoryPtr;
59 
60 //
61 // Structure to track warnings for attempts to set socket buffer sizes
62 //
63 struct BufSizeWarnInfo
64 {
65     // Whether send size warning has been emitted
66     bool sndWarn;
67 
68     // The send size for which the warning wwas emitted
69     int sndSize;
70 
71     // Whether receive size warning has been emitted
72     bool rcvWarn;
73 
74     // The receive size for which the warning wwas emitted
75     int rcvSize;
76 };
77 
78 class Instance : public IceUtil::Shared, public IceUtil::Monitor<IceUtil::RecMutex>
79 {
80 public:
81 
82     bool destroyed() const;
initializationData()83     const Ice::InitializationData& initializationData() const { return _initData; }
84     TraceLevelsPtr traceLevels() const;
85     DefaultsAndOverridesPtr defaultsAndOverrides() const;
86     RouterManagerPtr routerManager() const;
87     LocatorManagerPtr locatorManager() const;
88     ReferenceFactoryPtr referenceFactory() const;
89     RequestHandlerFactoryPtr requestHandlerFactory() const;
90     ProxyFactoryPtr proxyFactory() const;
91     OutgoingConnectionFactoryPtr outgoingConnectionFactory() const;
92     ObjectAdapterFactoryPtr objectAdapterFactory() const;
93     ProtocolSupport protocolSupport() const;
94     bool preferIPv6() const;
95     NetworkProxyPtr networkProxy() const;
96     ThreadPoolPtr clientThreadPool();
97     ThreadPoolPtr serverThreadPool();
98     EndpointHostResolverPtr endpointHostResolver();
99     RetryQueuePtr retryQueue();
100     IceUtil::TimerPtr timer();
101     EndpointFactoryManagerPtr endpointFactoryManager() const;
102     DynamicLibraryListPtr dynamicLibraryList() const;
103     Ice::PluginManagerPtr pluginManager() const;
messageSizeMax()104     size_t messageSizeMax() const { return _messageSizeMax; }
batchAutoFlushSize()105     size_t batchAutoFlushSize() const { return _batchAutoFlushSize; }
classGraphDepthMax()106     size_t classGraphDepthMax() const { return _classGraphDepthMax; }
collectObjects()107     bool collectObjects() const { return _collectObjects; }
toStringMode()108     Ice::ToStringMode toStringMode() const { return _toStringMode; }
109     const ACMConfig& clientACM() const;
110     const ACMConfig& serverACM() const;
111 
112     Ice::ObjectPrxPtr createAdmin(const Ice::ObjectAdapterPtr&, const Ice::Identity&);
113     Ice::ObjectPrxPtr getAdmin();
114     void addAdminFacet(const Ice::ObjectPtr&, const std::string&);
115     Ice::ObjectPtr removeAdminFacet(const std::string&);
116     Ice::ObjectPtr findAdminFacet(const std::string&);
117     Ice::FacetMap findAllAdminFacets();
118 
getImplicitContext()119     const Ice::ImplicitContextIPtr& getImplicitContext() const
120     {
121         return _implicitContext;
122     }
123 
124     void setDefaultLocator(const Ice::LocatorPrxPtr&);
125     void setDefaultRouter(const Ice::RouterPrxPtr&);
126 
127     void setLogger(const Ice::LoggerPtr&);
128 #ifdef ICE_CPP11_MAPPING
129     void setThreadHook(std::function<void()>, std::function<void()>);
130 #else
131     void setThreadHook(const Ice::ThreadNotificationPtr&);
132 #endif
133 
getStringConverter()134     const Ice::StringConverterPtr& getStringConverter() const { return _stringConverter; }
getWstringConverter()135     const Ice::WstringConverterPtr& getWstringConverter() const { return _wstringConverter; }
136 
137     BufSizeWarnInfo getBufSizeWarn(Ice::Short type);
138     void setSndBufSizeWarn(Ice::Short type, int size);
139     void setRcvBufSizeWarn(Ice::Short type, int size);
140 
141     void addObjectFactory(const Ice::ObjectFactoryPtr&, const std::string&);
142     Ice::ObjectFactoryPtr findObjectFactory(const std::string&) const;
143 
144     typedef std::map<std::string, Ice::ObjectFactoryPtr> ObjectFactoryMap;
145 
146 private:
147 
148     Instance(const Ice::CommunicatorPtr&, const Ice::InitializationData&);
149     virtual ~Instance();
150     void finishSetup(int&, const char*[], const Ice::CommunicatorPtr&);
151     void destroy();
152     friend class Ice::CommunicatorI;
153 
154     void updateConnectionObservers();
155     void updateThreadObservers();
156     friend class ObserverUpdaterI;
157 
158     void addAllAdminFacets();
159     void setServerProcessProxy(const Ice::ObjectAdapterPtr&, const Ice::Identity&);
160 
161     BufSizeWarnInfo getBufSizeWarnInternal(Ice::Short type);
162 
163     enum State
164     {
165         StateActive,
166         StateDestroyInProgress,
167         StateDestroyed
168     };
169     State _state;
170     Ice::InitializationData _initData;
171     const TraceLevelsPtr _traceLevels; // Immutable, not reset by destroy().
172     const DefaultsAndOverridesPtr _defaultsAndOverrides; // Immutable, not reset by destroy().
173     const size_t _messageSizeMax; // Immutable, not reset by destroy().
174     const size_t _batchAutoFlushSize; // Immutable, not reset by destroy().
175     const size_t _classGraphDepthMax; // Immutable, not reset by destroy().
176     const bool _collectObjects; // Immutable, not reset by destroy().
177     const Ice::ToStringMode _toStringMode; // Immutable, not reset by destroy()
178     ACMConfig _clientACM;
179     ACMConfig _serverACM;
180     RouterManagerPtr _routerManager;
181     LocatorManagerPtr _locatorManager;
182     ReferenceFactoryPtr _referenceFactory;
183     RequestHandlerFactoryPtr _requestHandlerFactory;
184     ProxyFactoryPtr _proxyFactory;
185     OutgoingConnectionFactoryPtr _outgoingConnectionFactory;
186     ObjectAdapterFactoryPtr _objectAdapterFactory;
187     ProtocolSupport _protocolSupport;
188     bool _preferIPv6;
189     NetworkProxyPtr _networkProxy;
190     ThreadPoolPtr _clientThreadPool;
191     ThreadPoolPtr _serverThreadPool;
192     EndpointHostResolverPtr _endpointHostResolver;
193     RetryQueuePtr _retryQueue;
194     TimerPtr _timer;
195     EndpointFactoryManagerPtr _endpointFactoryManager;
196     DynamicLibraryListPtr _dynamicLibraryList;
197     Ice::PluginManagerPtr _pluginManager;
198     const Ice::ImplicitContextIPtr _implicitContext;
199     Ice::StringConverterPtr _stringConverter;
200     Ice::WstringConverterPtr _wstringConverter;
201     bool _adminEnabled;
202     Ice::ObjectAdapterPtr _adminAdapter;
203     Ice::FacetMap _adminFacets;
204     Ice::Identity _adminIdentity;
205     std::set<std::string> _adminFacetFilter;
206     IceInternal::MetricsAdminIPtr _metricsAdmin;
207     std::map<Ice::Short, BufSizeWarnInfo> _setBufSizeWarn;
208     IceUtil::Mutex _setBufSizeWarnMutex;
209     ObjectFactoryMap _objectFactoryMap;
210     mutable ObjectFactoryMap::iterator _objectFactoryMapHint;
211 };
212 
213 class ProcessI : public Ice::Process
214 {
215 public:
216 
217     ProcessI(const Ice::CommunicatorPtr&);
218 
219     virtual void shutdown(const Ice::Current&);
220 #ifdef ICE_CPP11_MAPPING
221     virtual void writeMessage(std::string, Ice::Int, const Ice::Current&);
222 #else
223     virtual void writeMessage(const std::string&, Ice::Int, const Ice::Current&);
224 #endif
225 
226 private:
227 
228     const Ice::CommunicatorPtr _communicator;
229 };
230 
231 }
232 
233 #endif
234