19 #ifndef INCLUDE_RCF_RCFSERVER_HPP
20 #define INCLUDE_RCF_RCFSERVER_HPP
28 #include <boost/bind.hpp>
29 #include <boost/function.hpp>
30 #include <boost/mpl/bool.hpp>
31 #include <boost/noncopyable.hpp>
32 #include <boost/shared_ptr.hpp>
33 #include <boost/utility/enable_if.hpp>
34 #include <boost/weak_ptr.hpp>
36 #include <RCF/CheckRtti.hpp>
37 #include <RCF/Export.hpp>
38 #include <RCF/GetInterfaceName.hpp>
39 #include <RCF/RcfClient.hpp>
40 #include <RCF/ServerObjectService.hpp>
41 #include <RCF/ServerStub.hpp>
42 #include <RCF/ServerTransport.hpp>
43 #include <RCF/ThreadLibrary.hpp>
44 #include <RCF/ThreadPool.hpp>
46 #if RCF_FEATURE_FILETRANSFER==1
47 #include <RCF/FileTransferService.hpp>
50 #if RCF_FEATURE_PUBSUB==1
51 #include <RCF/PublishingService.hpp>
52 #include <RCF/SubscriptionService.hpp>
57 class ServerTransport;
62 class I_FilterFactoryLookupProvider;
64 class IpServerTransport;
65 class PingBackService;
66 class FileTransferService;
67 class ObjectFactoryService;
69 class SessionTimeoutService;
70 class PublishingService;
71 class SubscriptionService;
72 class SessionObjectFactoryService;
73 class ObjectFactoryService;
74 class CallbackConnectionService;
76 typedef boost::shared_ptr<ServerTransport> ServerTransportPtr;
77 typedef boost::shared_ptr<StubEntry> StubEntryPtr;
78 typedef boost::shared_ptr<I_Service> ServicePtr;
79 typedef boost::shared_ptr<RcfSession> RcfSessionPtr;
80 typedef boost::shared_ptr<I_FilterFactoryLookupProvider> FilterFactoryLookupProviderPtr;
81 typedef boost::shared_ptr<I_RcfClient> RcfClientPtr;
82 typedef boost::shared_ptr<Endpoint> EndpointPtr;
83 typedef boost::shared_ptr<PingBackService> PingBackServicePtr;
84 typedef boost::shared_ptr<FileTransferService> FileTransferServicePtr;
85 typedef boost::shared_ptr<ObjectFactoryService> ObjectFactoryServicePtr;
86 typedef boost::shared_ptr<FilterService> FilterServicePtr;
87 typedef boost::shared_ptr<SessionTimeoutService> SessionTimeoutServicePtr;
88 typedef boost::shared_ptr<PublishingService> PublishingServicePtr;
89 typedef boost::shared_ptr<SubscriptionService> SubscriptionServicePtr;
90 typedef boost::shared_ptr<SessionObjectFactoryService> SessionObjectFactoryServicePtr;
91 typedef boost::shared_ptr<ObjectFactoryService> ObjectFactoryServicePtr;
92 typedef boost::shared_ptr<CallbackConnectionService> CallbackConnectionServicePtr;
93 typedef boost::weak_ptr<RcfSession> RcfSessionWeakPtr;
95 template<
typename Interface>
99 typedef boost::shared_ptr<Subscription> SubscriptionPtr;
101 class SubscriptionParms;
103 class BandwidthQuota;
104 typedef boost::shared_ptr<BandwidthQuota> BandwidthQuotaPtr;
106 class FileDownloadInfo;
107 class FileUploadInfo;
109 class JsonRpcRequest;
110 class JsonRpcResponse;
115 typedef boost::shared_ptr<HttpSession> HttpSessionPtr;
117 typedef boost::function2<void, RcfSessionPtr, ClientTransportAutoPtr> OnCallbackConnectionCreated;
119 class RCF_EXPORT RcfServer : boost::noncopyable
124 RcfServer(
const Endpoint &endpoint);
125 RcfServer(ServicePtr servicePtr);
126 RcfServer(ServerTransportPtr serverTransportPtr);
130 typedef boost::function0<void> JoinFunctor;
131 typedef boost::function1<void, RcfServer&> StartCallback;
143 getServerTransport();
146 getServerTransportService();
149 getServerTransportPtr();
152 getIpServerTransport();
157 ServicePtr servicePtr);
160 ServicePtr servicePtr);
165 getPingBackServicePtr();
167 FileTransferServicePtr
168 getFileTransferServicePtr();
170 SessionTimeoutServicePtr
171 getSessionTimeoutServicePtr();
173 ObjectFactoryServicePtr
174 getObjectFactoryServicePtr();
176 SessionObjectFactoryServicePtr
177 getSessionObjectFactoryServicePtr();
180 getPublishingServicePtr();
182 SubscriptionServicePtr
183 getSubscriptionServicePtr();
185 FilterServicePtr getFilterServicePtr();
187 bool addServerTransport(
188 ServerTransportPtr serverTransportPtr);
190 bool removeServerTransport(
191 ServerTransportPtr serverTransportPtr);
194 findTransportCompatibleWith(ClientTransport & clienetTransport);
196 void setStartCallback(
const StartCallback &startCallback);
199 void invokeStartCallback();
202 ServerBindingPtr bindImpl(
203 const std::string &name,
204 RcfClientPtr rcfClientPtr);
211 SessionPtr createSession();
212 void onReadCompleted(SessionPtr sessionPtr);
213 void onWriteCompleted(SessionPtr sessionPtr);
226 template<
typename Iter>
227 void enumerateSessions(
const Iter & iter)
230 for (std::size_t i=0; i<mServerTransports.size(); ++i)
232 mServerTransports[i]->enumerateSessions(iter);
241 ReadWriteMutex mStubMapMutex;
242 typedef std::map<std::string, StubEntryPtr> StubMap;
245 typedef boost::function<void(const JsonRpcRequest &, JsonRpcResponse &)> JsonRpcMethod;
246 typedef std::map<std::string, JsonRpcMethod> JsonRpcMethods;
247 JsonRpcMethods mJsonRpcMethods;
249 friend class RcfSession;
257 std::vector<ServerTransportPtr> mServerTransports;
258 std::vector<ServicePtr> mServices;
259 FilterServicePtr mFilterServicePtr;
260 PingBackServicePtr mPingBackServicePtr;
261 FileTransferServicePtr mFileTransferServicePtr;
262 SessionTimeoutServicePtr mSessionTimeoutServicePtr;
263 PublishingServicePtr mPublishingServicePtr;
264 SubscriptionServicePtr mSubscriptionServicePtr;
265 CallbackConnectionServicePtr mCallbackConnectionServicePtr;
266 SessionObjectFactoryServicePtr mSessionObjectFactoryServicePtr;
267 ObjectFactoryServicePtr mObjectFactoryServicePtr;
268 ServerObjectServicePtr mServerObjectServicePtr;
270 void startService(ServicePtr servicePtr)
const;
271 void stopService(ServicePtr servicePtr)
const;
272 void resolveServiceThreadPools(ServicePtr servicePtr)
const;
274 friend class AsioNetworkSession;
275 FilterPtr createFilter(
int filterId);
279 StartCallback mStartCallback;
280 Condition mStartEvent;
281 Condition mStopEvent;
283 Mutex mStartStopMutex;
288 void setThreadPool(ThreadPoolPtr threadPoolPtr);
289 ThreadPoolPtr getThreadPool();
290 ServerTransport & addEndpoint(
const Endpoint & endpoint);
293 ThreadPoolPtr mThreadPoolPtr;
297 void waitForStopEvent();
298 void waitForStartEvent();
304 friend class MethodInvocationRequest;
307 boost::uint32_t getRuntimeVersion();
308 void setRuntimeVersion(boost::uint32_t version);
310 boost::uint32_t getArchiveVersion();
311 void setArchiveVersion(boost::uint32_t version);
314 boost::uint32_t mRuntimeVersion;
315 boost::uint32_t mArchiveVersion;
320 template<
typename I1,
typename ImplementationT>
321 ServerBindingPtr bind(ImplementationT & t,
const std::string &name =
"")
323 boost::shared_ptr< I_Deref<ImplementationT> > derefPtr(
324 new DerefObj<ImplementationT>(t) );
326 RcfClientPtr rcfClientPtr = createServerStub(
328 (ImplementationT *) 0, derefPtr);
332 I1::getInterfaceName() :
337 template<
typename InterfaceT>
338 bool unbind(
const std::string &name_ =
"")
340 const std::string &name = (name_ ==
"") ?
341 getInterfaceName((InterfaceT *) NULL) :
344 WriteLock writeLock(mStubMapMutex);
345 mStubMap.erase(name);
349 #if RCF_FEATURE_JSON==1
350 void bindJsonRpc(JsonRpcMethod jsonRpcMethod,
const std::string & jsonRpcName);
351 void unbindJsonRpc(
const std::string & jsonRpcName);
354 void setSupportedTransportProtocols(
355 const std::vector<TransportProtocol> & protocols);
357 const std::vector<TransportProtocol> &
358 getSupportedTransportProtocols()
const;
360 void setCertificate(CertificatePtr certificatePtr);
361 CertificatePtr getCertificate();
363 void setOpenSslCipherSuite(
const std::string & cipherSuite);
364 std::string getOpenSslCipherSuite()
const;
366 void setCaCertificate(CertificatePtr certificatePtr);
367 CertificatePtr getCaCertificate();
370 typedef boost::function<bool(Certificate *)> CertificateValidationCb;
371 void setCertificateValidationCallback(CertificateValidationCb certificateValidationCb);
372 const CertificateValidationCb & getCertificateValidationCallback()
const;
374 void setEnableSchannelCertificateValidation(
const tstring & peerName);
375 tstring getEnableSchannelCertificateValidation()
const;
377 void setSslImplementation(SslImplementation sslImplementation);
378 SslImplementation getSslImplementation()
const;
380 void setSessionTimeoutMs(boost::uint32_t sessionTimeoutMs);
381 boost::uint32_t getSessionTimeoutMs();
383 void setSessionHarvestingIntervalMs(boost::uint32_t sessionHarvestingIntervalMs);
384 boost::uint32_t getSessionHarvestingIntervalMs();
386 void setHttpSessionTimeoutMs(boost::uint32_t httpSessionTimeoutMs);
387 boost::uint32_t getHttpSessionTimeoutMs();
389 void setOnCallbackConnectionCreated(OnCallbackConnectionCreated onCallbackConnectionCreated);
390 OnCallbackConnectionCreated getOnCallbackConnectionCreated();
392 void setOfsMaxNumberOfObjects(boost::uint32_t ofsMaxNumberOfObjects);
393 void setOfsObjectTimeoutS(boost::uint32_t ofsObjectTimeoutS);
394 void setOfsCleanupIntervalS(boost::uint32_t ofsCleanupIntervalS);
395 void setOfsCleanupThreshold(
float ofsCleanupThreshold);
397 boost::uint32_t getOfsMaxNumberOfObjects()
const;
398 boost::uint32_t getOfsObjectTimeoutS()
const;
399 boost::uint32_t getOfsCleanupIntervalS()
const;
400 float getOfsCleanupThreshold()
const;
402 #if RCF_FEATURE_FILETRANSFER==1
404 typedef FileTransferService::OnFileDownloadProgress OnFileDownloadProgress;
405 typedef FileTransferService::OnFileUploadProgress OnFileUploadProgress;
407 void setOnFileDownloadProgress(OnFileDownloadProgress onFileDownloadProgress);
408 void setOnFileUploadProgress(OnFileUploadProgress onFileUploadProgress);
410 void setFileUploadDirectory(
const std::string & uploadDir);
411 std::string getFileUploadDirectory()
const;
413 typedef boost::function1<BandwidthQuotaPtr, RcfSession &> BandwidthQuotaCallback;
414 typedef BandwidthQuotaCallback FileUploadQuotaCallback;
415 typedef BandwidthQuotaCallback FileDownloadQuotaCallback;
417 void setFileUploadBandwidthLimit(boost::uint32_t uploadQuotaBps);
418 boost::uint32_t getFileUploadBandwidthLimit()
const;
420 void setFileUploadCustomBandwidthLimit(FileUploadQuotaCallback uploadQuotaCb);
422 void setFileDownloadBandwidthLimit(boost::uint32_t downloadQuotaBps);
423 boost::uint32_t getFileDownloadBandwidthLimit()
const;
425 void setFileDownloadCustomBandwidthLimit(FileDownloadQuotaCallback downloadQuotaCb);
429 OnFileDownloadProgress mOnFileDownloadProgress;
430 OnFileUploadProgress mOnFileUploadProgress;
432 std::string mFileUploadDirectory;
434 boost::uint32_t mFileUploadQuota;
435 FileUploadQuotaCallback mFileUploadQuotaCb;
437 boost::uint32_t mFileDownloadQuota;
438 FileDownloadQuotaCallback mFileDownloadQuotaCb;
440 friend class FileTransferService;
446 mutable ReadWriteMutex mPropertiesMutex;
448 std::vector<TransportProtocol> mSupportedProtocols;
449 CertificatePtr mCertificatePtr;
450 std::string mOpenSslCipherSuite;
452 CertificatePtr mCaCertificatePtr;
453 CertificateValidationCb mCertificateValidationCb;
454 tstring mSchannelCertificateValidation;
456 SslImplementation mSslImplementation;
458 boost::uint32_t mSessionTimeoutMs;
459 boost::uint32_t mSessionHarvestingIntervalMs;
461 boost::uint32_t mHttpSessionTimeoutMs;
463 OnCallbackConnectionCreated mOnCallbackConnectionCreated;
465 boost::uint32_t mOfsMaxNumberOfObjects;
466 boost::uint32_t mOfsObjectTimeoutS;
467 boost::uint32_t mOfsCleanupIntervalS;
468 float mOfsCleanupThreshold;
472 #if RCF_FEATURE_PUBSUB==1
474 template<
typename Interface>
475 boost::shared_ptr< Publisher<Interface> > createPublisher()
477 PublisherParms parms;
478 return mPublishingServicePtr->createPublisher<Interface>(parms);
481 template<
typename Interface>
482 boost::shared_ptr< Publisher<Interface> > createPublisher(
483 const PublisherParms & parms)
485 return mPublishingServicePtr->createPublisher<Interface>(parms);
488 template<
typename Interface,
typename T>
489 boost::shared_ptr< Subscription > createSubscription(
493 RCF_ASSERT(mStarted);
494 SubscriptionParms parms;
495 parms.setPublisherEndpoint(publisherEp);
496 return mSubscriptionServicePtr->createSubscription<Interface>(t, publisherEp);
499 template<
typename Interface,
typename T>
500 boost::shared_ptr< Subscription > createSubscription(
502 const SubscriptionParms & parms)
504 RCF_ASSERT(mStarted);
505 return mSubscriptionServicePtr->createSubscription<Interface>(t, parms);
512 boost::uint32_t mServerObjectHarvestingIntervalS;
514 Mutex mHttpSessionMapMutex;
515 std::map<std::string, HttpSessionPtr> mHttpSessionMap;
517 friend class HttpSessionFilter;
519 HttpSessionPtr attachHttpSession(
const std::string & httpSessionId,
bool allowCreate, ExceptionPtr & ePtr);
520 void detachHttpSession(HttpSessionPtr httpSessionPtr);
522 friend class ServerObjectService;
523 void harvestHttpSessions();
527 boost::uint32_t getServerObjectHarvestingIntervalS()
const;
528 void setServerObjectHarvestingIntervalS(boost::uint32_t harvestingIntervalS);
531 boost::shared_ptr<T> queryServerObject(
532 const std::string & objectKey)
534 return mServerObjectServicePtr->queryServerObject<T>(objectKey);
538 boost::shared_ptr<T> getServerObject(
539 const std::string & objectKey,
540 boost::uint32_t timeoutMs)
542 return mServerObjectServicePtr->getServerObject<T>(objectKey, timeoutMs);
545 void deleteServerObject(
const std::string & objectKey);
547 StubEntryPtr findStubEntryForToken(
const Token & token);
552 #endif // ! INCLUDE_RCF_RCFSERVER_HPP