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;
114 typedef boost::function2<void, RcfSessionPtr, ClientTransportAutoPtr> OnCallbackConnectionCreated;
116 class RCF_EXPORT RcfServer : boost::noncopyable
121 RcfServer(
const Endpoint &endpoint);
122 RcfServer(ServicePtr servicePtr);
123 RcfServer(ServerTransportPtr serverTransportPtr);
127 typedef boost::function0<void> JoinFunctor;
128 typedef boost::function1<void, RcfServer&> StartCallback;
140 getServerTransport();
143 getServerTransportService();
146 getServerTransportPtr();
149 getIpServerTransport();
154 ServicePtr servicePtr);
157 ServicePtr servicePtr);
162 getPingBackServicePtr();
164 FileTransferServicePtr
165 getFileTransferServicePtr();
167 SessionTimeoutServicePtr
168 getSessionTimeoutServicePtr();
170 ObjectFactoryServicePtr
171 getObjectFactoryServicePtr();
173 SessionObjectFactoryServicePtr
174 getSessionObjectFactoryServicePtr();
177 getPublishingServicePtr();
179 SubscriptionServicePtr
180 getSubscriptionServicePtr();
182 bool addServerTransport(
183 ServerTransportPtr serverTransportPtr);
185 bool removeServerTransport(
186 ServerTransportPtr serverTransportPtr);
188 ServerTransport & findTransportCompatibleWith(ClientTransport & clienetTransport);
190 void setStartCallback(
const StartCallback &startCallback);
193 void invokeStartCallback();
196 ServerBindingPtr bindImpl(
197 const std::string &name,
198 RcfClientPtr rcfClientPtr);
205 SessionPtr createSession();
206 void onReadCompleted(SessionPtr sessionPtr);
207 void onWriteCompleted(SessionPtr sessionPtr);
220 template<
typename Iter>
221 void enumerateSessions(
const Iter & iter)
224 for (std::size_t i=0; i<mServerTransports.size(); ++i)
226 mServerTransports[i]->enumerateSessions(iter);
235 ReadWriteMutex mStubMapMutex;
236 typedef std::map<std::string, StubEntryPtr> StubMap;
239 typedef boost::function<void(const JsonRpcRequest &, JsonRpcResponse &)> JsonRpcMethod;
240 typedef std::map<std::string, JsonRpcMethod> JsonRpcMethods;
241 JsonRpcMethods mJsonRpcMethods;
243 friend class RcfSession;
251 std::vector<ServerTransportPtr> mServerTransports;
252 std::vector<ServicePtr> mServices;
253 FilterServicePtr mFilterServicePtr;
254 PingBackServicePtr mPingBackServicePtr;
255 FileTransferServicePtr mFileTransferServicePtr;
256 SessionTimeoutServicePtr mSessionTimeoutServicePtr;
257 PublishingServicePtr mPublishingServicePtr;
258 SubscriptionServicePtr mSubscriptionServicePtr;
259 CallbackConnectionServicePtr mCallbackConnectionServicePtr;
260 SessionObjectFactoryServicePtr mSessionObjectFactoryServicePtr;
261 ObjectFactoryServicePtr mObjectFactoryServicePtr;
262 ServerObjectServicePtr mServerObjectServicePtr;
264 void startService(ServicePtr servicePtr)
const;
265 void stopService(ServicePtr servicePtr)
const;
266 void resolveServiceThreadPools(ServicePtr servicePtr)
const;
268 friend class AsioSessionState;
269 FilterPtr createFilter(
int filterId);
273 StartCallback mStartCallback;
274 Condition mStartEvent;
275 Condition mStopEvent;
277 Mutex mStartStopMutex;
282 void setThreadPool(ThreadPoolPtr threadPoolPtr);
283 ThreadPoolPtr getThreadPool();
284 ServerTransport & addEndpoint(
const Endpoint & endpoint);
287 ThreadPoolPtr mThreadPoolPtr;
291 void waitForStopEvent();
292 void waitForStartEvent();
298 friend class MethodInvocationRequest;
301 boost::uint32_t getRuntimeVersion();
302 void setRuntimeVersion(boost::uint32_t version);
304 boost::uint32_t getArchiveVersion();
305 void setArchiveVersion(boost::uint32_t version);
308 boost::uint32_t mRuntimeVersion;
309 boost::uint32_t mArchiveVersion;
314 template<
typename I1,
typename ImplementationT>
315 ServerBindingPtr bind(ImplementationT & t,
const std::string &name =
"")
317 boost::shared_ptr< I_Deref<ImplementationT> > derefPtr(
318 new DerefObj<ImplementationT>(t) );
320 RcfClientPtr rcfClientPtr = createServerStub(
322 (ImplementationT *) 0, derefPtr);
326 I1::getInterfaceName() :
331 template<
typename I1,
typename I2,
typename ImplementationT>
332 ServerBindingPtr bind(ImplementationT & t,
const std::string &name =
"")
334 boost::shared_ptr< I_Deref<ImplementationT> > derefPtr(
335 new DerefObj<ImplementationT>(t) );
337 RcfClientPtr rcfClientPtr = createServerStub(
339 (ImplementationT *) 0, derefPtr);
342 RcfClientPtr toMergePtr = createServerStub(
344 (ImplementationT *) 0,
347 rcfClientPtr->getServerStub().merge(toMergePtr);
351 I1::getInterfaceName() :
356 template<
typename I1,
typename I2,
typename I3,
typename ImplementationT>
357 ServerBindingPtr bind(ImplementationT & t,
const std::string &name =
"")
359 boost::shared_ptr< I_Deref<ImplementationT> > derefPtr(
360 new DerefObj<ImplementationT>(t) );
362 RcfClientPtr rcfClientPtr = createServerStub( (I1 *) 0, (ImplementationT *) 0, derefPtr);
364 RcfClientPtr toMergePtr = createServerStub(
366 (ImplementationT *) 0,
369 rcfClientPtr->getServerStub().merge(toMergePtr);
372 RcfClientPtr toMergePtr = createServerStub(
374 (ImplementationT *) 0,
377 rcfClientPtr->getServerStub().merge(toMergePtr);
382 I1::getInterfaceName() :
387 template<
typename I1,
typename I2,
typename I3,
typename I4,
typename ImplementationT>
388 ServerBindingPtr bind(ImplementationT & t,
const std::string &name =
"")
390 boost::shared_ptr< I_Deref<ImplementationT> > derefPtr(
391 new DerefObj<ImplementationT>(t) );
393 RcfClientPtr rcfClientPtr = createServerStub(
395 (ImplementationT *) 0,
399 RcfClientPtr toMergePtr = createServerStub(
401 (ImplementationT *) 0,
404 rcfClientPtr->getServerStub().merge(toMergePtr);
408 RcfClientPtr toMergePtr = createServerStub(
410 (ImplementationT *) 0, derefPtr);
412 rcfClientPtr->getServerStub().merge(toMergePtr);
416 RcfClientPtr toMergePtr = createServerStub(
418 (ImplementationT *) 0,
421 rcfClientPtr->getServerStub().merge(toMergePtr);
426 I1::getInterfaceName() :
431 template<
typename InterfaceT>
432 bool unbind(
const std::string &name_ =
"")
434 const std::string &name = (name_ ==
"") ?
435 getInterfaceName((InterfaceT *) NULL) :
438 WriteLock writeLock(mStubMapMutex);
439 mStubMap.erase(name);
443 #if RCF_FEATURE_JSON==1
444 void bindJsonRpc(JsonRpcMethod jsonRpcMethod,
const std::string & jsonRpcName);
445 void unbindJsonRpc(
const std::string & jsonRpcName);
448 void setSupportedTransportProtocols(
449 const std::vector<TransportProtocol> & protocols);
451 const std::vector<TransportProtocol> &
452 getSupportedTransportProtocols()
const;
454 void setCertificate(CertificatePtr certificatePtr);
455 CertificatePtr getCertificate();
457 void setOpenSslCipherSuite(
const std::string & cipherSuite);
458 std::string getOpenSslCipherSuite()
const;
460 void setCaCertificate(CertificatePtr certificatePtr);
461 CertificatePtr getCaCertificate();
464 typedef boost::function<bool(Certificate *)> CertificateValidationCb;
465 void setCertificateValidationCallback(CertificateValidationCb certificateValidationCb);
466 const CertificateValidationCb & getCertificateValidationCallback()
const;
468 void setEnableSchannelCertificateValidation(
const tstring & peerName);
469 tstring getEnableSchannelCertificateValidation()
const;
471 void setSslImplementation(SslImplementation sslImplementation);
472 SslImplementation getSslImplementation()
const;
474 void setSessionTimeoutMs(boost::uint32_t sessionTimeoutMs);
475 boost::uint32_t getSessionTimeoutMs();
477 void setSessionHarvestingIntervalMs(boost::uint32_t sessionHarvestingIntervalMs);
478 boost::uint32_t getSessionHarvestingIntervalMs();
480 void setOnCallbackConnectionCreated(OnCallbackConnectionCreated onCallbackConnectionCreated);
481 OnCallbackConnectionCreated getOnCallbackConnectionCreated();
483 void setOfsMaxNumberOfObjects(boost::uint32_t ofsMaxNumberOfObjects);
484 void setOfsObjectTimeoutS(boost::uint32_t ofsObjectTimeoutS);
485 void setOfsCleanupIntervalS(boost::uint32_t ofsCleanupIntervalS);
486 void setOfsCleanupThreshold(
float ofsCleanupThreshold);
488 boost::uint32_t getOfsMaxNumberOfObjects()
const;
489 boost::uint32_t getOfsObjectTimeoutS()
const;
490 boost::uint32_t getOfsCleanupIntervalS()
const;
491 float getOfsCleanupThreshold()
const;
493 #if RCF_FEATURE_FILETRANSFER==1
495 typedef FileTransferService::OnFileDownloadProgress OnFileDownloadProgress;
496 typedef FileTransferService::OnFileUploadProgress OnFileUploadProgress;
498 void setOnFileDownloadProgress(OnFileDownloadProgress onFileDownloadProgress);
499 void setOnFileUploadProgress(OnFileUploadProgress onFileUploadProgress);
501 void setFileUploadDirectory(
const std::string & uploadDir);
502 std::string getFileUploadDirectory()
const;
504 typedef boost::function1<BandwidthQuotaPtr, RcfSession &> BandwidthQuotaCallback;
505 typedef BandwidthQuotaCallback FileUploadQuotaCallback;
506 typedef BandwidthQuotaCallback FileDownloadQuotaCallback;
508 void setFileUploadBandwidthLimit(boost::uint32_t uploadQuotaBps);
509 boost::uint32_t getFileUploadBandwidthLimit()
const;
511 void setFileUploadCustomBandwidthLimit(FileUploadQuotaCallback uploadQuotaCb);
513 void setFileDownloadBandwidthLimit(boost::uint32_t downloadQuotaBps);
514 boost::uint32_t getFileDownloadBandwidthLimit()
const;
516 void setFileDownloadCustomBandwidthLimit(FileDownloadQuotaCallback downloadQuotaCb);
520 OnFileDownloadProgress mOnFileDownloadProgress;
521 OnFileUploadProgress mOnFileUploadProgress;
523 std::string mFileUploadDirectory;
525 boost::uint32_t mFileUploadQuota;
526 FileUploadQuotaCallback mFileUploadQuotaCb;
528 boost::uint32_t mFileDownloadQuota;
529 FileDownloadQuotaCallback mFileDownloadQuotaCb;
531 friend class FileTransferService;
537 mutable ReadWriteMutex mPropertiesMutex;
539 std::vector<TransportProtocol> mSupportedProtocols;
540 CertificatePtr mCertificatePtr;
541 std::string mOpenSslCipherSuite;
543 CertificatePtr mCaCertificatePtr;
544 CertificateValidationCb mCertificateValidationCb;
545 tstring mSchannelCertificateValidation;
547 SslImplementation mSslImplementation;
549 boost::uint32_t mSessionTimeoutMs;
550 boost::uint32_t mSessionHarvestingIntervalMs;
552 OnCallbackConnectionCreated mOnCallbackConnectionCreated;
554 boost::uint32_t mOfsMaxNumberOfObjects;
555 boost::uint32_t mOfsObjectTimeoutS;
556 boost::uint32_t mOfsCleanupIntervalS;
557 float mOfsCleanupThreshold;
561 #if RCF_FEATURE_PUBSUB==1
563 template<
typename Interface>
564 boost::shared_ptr< Publisher<Interface> > createPublisher()
566 PublisherParms parms;
567 return mPublishingServicePtr->createPublisher<Interface>(parms);
570 template<
typename Interface>
571 boost::shared_ptr< Publisher<Interface> > createPublisher(
572 const PublisherParms & parms)
574 return mPublishingServicePtr->createPublisher<Interface>(parms);
577 template<
typename Interface,
typename T>
578 boost::shared_ptr< Subscription > createSubscription(
582 RCF_ASSERT(mStarted);
583 SubscriptionParms parms;
584 parms.setPublisherEndpoint(publisherEp);
585 return mSubscriptionServicePtr->createSubscription<Interface>(t, publisherEp);
588 template<
typename Interface,
typename T>
589 boost::shared_ptr< Subscription > createSubscription(
591 const SubscriptionParms & parms)
593 RCF_ASSERT(mStarted);
594 return mSubscriptionServicePtr->createSubscription<Interface>(t, parms);
601 boost::uint32_t mServerObjectHarvestingIntervalS;
606 boost::uint32_t getServerObjectHarvestingIntervalS()
const;
607 void setServerObjectHarvestingIntervalS(boost::uint32_t harvestingIntervalS);
610 boost::shared_ptr<T> queryServerObject(
611 const std::string & objectKey,
612 boost::uint32_t timeoutMs)
614 return mServerObjectServicePtr->queryServerObject<T>(objectKey, timeoutMs);
618 boost::shared_ptr<T> getServerObject(
619 const std::string & objectKey,
620 boost::uint32_t timeoutMs)
622 return mServerObjectServicePtr->getServerObject<T>(objectKey, timeoutMs);
625 void deleteServerObject(
const std::string & objectKey);
630 #endif // ! INCLUDE_RCF_RCFSERVER_HPP