Remote Call Framework 3.2
RcfServer.hpp
Go to the documentation of this file.
1 
2 //******************************************************************************
3 // RCF - Remote Call Framework
4 //
5 // Copyright (c) 2005 - 2020, Delta V Software. All rights reserved.
6 // http://www.deltavsoft.com
7 //
8 // RCF is distributed under dual licenses - closed source or GPL.
9 // Consult your particular license for conditions of use.
10 //
11 // If you have not purchased a commercial license, you are using RCF
12 // under GPL terms.
13 //
14 // Version: 3.2
15 // Contact: support <at> deltavsoft.com
16 //
17 //******************************************************************************
18 
20 
21 #ifndef INCLUDE_RCF_RCFSERVER_HPP
22 #define INCLUDE_RCF_RCFSERVER_HPP
23 
24 #include <functional>
25 #include <map>
26 #include <memory>
27 #include <string>
28 #include <vector>
29 
30 #include <RCF/Export.hpp>
31 #include <RCF/RcfClient.hpp>
32 #include <RCF/ServerTransport.hpp>
33 #include <RCF/ThreadLibrary.hpp>
34 
35 #if RCF_FEATURE_FILETRANSFER==1
36 #include <RCF/FileTransferService.hpp>
37 #endif
38 
39 #if RCF_FEATURE_PUBSUB==1
40 #include <RCF/PublishingService.hpp>
41 #include <RCF/SubscriptionService.hpp>
42 #endif
43 
44 #include <RCF/ServerObjectService.hpp>
45 #include <RCF/ServerStub.hpp>
46 
47 namespace RCF {
48 
50 
54  class RCF_EXPORT RcfServer : Noncopyable
55  {
56  public:
57 
58  typedef std::function<void(RcfServer&)> StartCallback;
59 
60  public:
61 
63 
65 
67  RcfServer();
68 
70  RcfServer(const Endpoint &endpoint);
71 
73  RcfServer(ServerTransportPtr serverTransportPtr);
74 
76  ~RcfServer();
77 
79 
82 
84 
86  ServerTransport & addEndpoint(const Endpoint & endpoint);
87 
89  void start();
90 
92  void stop();
93 
95  bool isStarted();
96 
98  void enumerateProxyEndpoints(std::vector<std::string>& endpoints);
99 
101 
105  void setSupportedTransportProtocols(
106  const std::vector<TransportProtocol> & protocols);
107 
109  const std::vector<TransportProtocol> &
110  getSupportedTransportProtocols() const;
111 
115  void setThreadPool(ThreadPoolPtr threadPoolPtr);
116 
118  ThreadPoolPtr getThreadPool();
119 
120 
122 
123  // Waits for the RcfServer to stop. Can be called from any thread.
124  void waitForStopEvent();
125 
126  // Waits for the RcfServer to start. Can be called from any thread.
127  void waitForStartEvent();
128 
130  ServerTransport & getServerTransport();
131 
133  IpServerTransport & getIpServerTransport();
134 
135 
141 
143 
145 
148  template<typename InterfaceT, typename ImplementationT>
149  ServerBindingPtr bind(ImplementationT & servantObj, const std::string &name = "")
150  {
151  std::reference_wrapper<ImplementationT> refWrapper(servantObj);
152 
153  RcfClientPtr rcfClientPtr = createServerStub(
154  (InterfaceT *) 0,
155  (ImplementationT *) 0, refWrapper);
156 
157  return bindImpl(
158  name.empty() ?
159  InterfaceT::getInterfaceName() :
160  name ,
161  rcfClientPtr);
162  }
163 
165  template<typename InterfaceT>
166  bool unbind(const std::string &name = "")
167  {
168  const std::string &name_ = (name == "") ?
169  getInterfaceName((InterfaceT *) NULL) :
170  name;
171 
172  WriteLock writeLock(mStubMapMutex);
173  mStubMap.erase(name_);
174  return true;
175  }
176 
178 
181 
183 
185  void setCertificate(CertificatePtr certificatePtr);
186 
188  CertificatePtr getCertificate();
189 
191  void setOpenSslCipherSuite(const std::string & cipherSuite);
192 
194  std::string getOpenSslCipherSuite() const;
195 
197  void setCaCertificate(CertificatePtr certificatePtr);
198 
200  CertificatePtr getCaCertificate();
201 
203  void setCertificateValidationCallback(CertificateValidationCallback certificateValidationCb);
204 
207  getCertificateValidationCallback() const;
208 
210  void setEnableSchannelCertificateValidation(const tstring & peerName);
211 
213  tstring getEnableSchannelCertificateValidation() const;
214 
216  void setSslImplementation(SslImplementation sslImplementation);
217 
219  SslImplementation getSslImplementation() const;
220 
221 #if RCF_FEATURE_SSPI==1
222  void setSchannelEnabledProtocols(DWORD enabledProtocols);
223  DWORD getSchannelEnabledProtocols() const;
224 
225  void setSchannelContextRequirements(ULONG contextRequirements);
226  ULONG getSchannelContextRequirements() const;
227 #endif
228 
230 
233 
235 
237 
239  void setConnectionIdleTimeoutMs(std::uint32_t idleConnectionTimeoutMs);
240 
242  std::uint32_t getConnectionIdleTimeoutMs();
243 
245 
247  void setConnectionIdleScanIntervalMs(std::uint32_t idleConnectionScanIntervalMs);
248 
250  std::uint32_t getConnectionIdleScanIntervalMs();
251 
253 
256  void setHttpSessionTimeoutMs(std::uint32_t httpSessionTimeoutMs);
257 
259  std::uint32_t getHttpSessionTimeoutMs();
260 
262  void setHttpServerHeader(const std::string & httpServerHeader);
263 
265  std::string getHttpServerHeader() const;
266 
267 #if RCF_FEATURE_HTTP==1
268  void setHttpMessageVerifier(HttpMessageVerifierPtr verifierPtr);
269  HttpMessageVerifierPtr getHttpMessageVerifier() const;
270 #endif
271 
273 
280 
282 
284  std::uint32_t getServerObjectHarvestingIntervalS() const;
285 
287  void setServerObjectHarvestingIntervalS(std::uint32_t harvestingIntervalS);
288 
290  template<typename T>
291  std::shared_ptr<T> queryServerObject(
292  const std::string & objectKey)
293  {
294  return mServerObjectServicePtr->queryServerObject<T>(objectKey);
295  }
296 
298  template<typename T>
299  std::shared_ptr<T> getServerObject(
300  const std::string & objectKey,
301  std::uint32_t timeoutMs)
302  {
303  return mServerObjectServicePtr->getServerObject<T>(objectKey, timeoutMs);
304  }
305 
307  void deleteServerObject(const std::string & objectKey);
308 
310 
311 #if RCF_FEATURE_PUBSUB==1
312 
315 
317 
319 
322  template<typename Interface>
323  std::shared_ptr< Publisher<Interface> > createPublisher()
324  {
325  PublisherParms parms;
326  return mPublishingServicePtr->createPublisher<Interface>(parms);
327  }
328 
330 
333  template<typename Interface>
334  std::shared_ptr< Publisher<Interface> > createPublisher(
335  const PublisherParms & parms)
336  {
337  return mPublishingServicePtr->createPublisher<Interface>(parms);
338  }
339 
341 
343  template<typename Interface, typename T>
344  std::shared_ptr< Subscription > createSubscription(
345  T & servantObj,
346  const RCF::Endpoint & publisherEp)
347  {
348  RCF_ASSERT(mStarted);
349  SubscriptionParms parms;
350  parms.setPublisherEndpoint(publisherEp);
351  return mSubscriptionServicePtr->createSubscription<Interface>(servantObj, publisherEp);
352  }
353 
355 
357  template<typename Interface, typename T>
358  std::shared_ptr< Subscription > createSubscription(
359  T & servantObj,
360  const SubscriptionParms & parms)
361  {
362  RCF_ASSERT(mStarted);
363  return mSubscriptionServicePtr->createSubscription<Interface>(servantObj, parms);
364  }
365 
367 
368 #endif
369 
371  void setEnableProxyEndpoints(bool enable);
372 
374  bool getEnableProxyEndpoints() const;
375 
376 #if RCF_FEATURE_FILETRANSFER==1
377 
380 
382 
384  void setUploadBandwidthLimit(std::uint32_t uploadQuotaBps);
385 
387  std::uint32_t getUploadBandwidthLimit() const;
388 
390  void setDownloadBandwidthLimit(std::uint32_t downloadQuotaBps);
391 
393  std::uint32_t getDownloadBandwidthLimit() const;
394 
396  void setUploadDirectory(const Path & uploadDir);
397 
399  Path getUploadDirectory() const;
400 
402  void setDownloadProgressCallback(DownloadProgressCallback downloadProgressCb);
403 
405  void setUploadProgressCallback(UploadProgressCallback uploadProgressCb);
406 
408  void setUploadBandwidthQuotaCallback(UploadBandwidthQuotaCallback uploadQuotaCb);
409 
411  void setDownloadBandwidthQuotaCallback(DownloadBandwidthQuotaCallback downloadQuotaCb);
412 
414 
415 #endif
416 
417 
418 
419  // For internal use.
420  std::uint32_t getRuntimeVersion();
421  void setRuntimeVersion(std::uint32_t version);
422 
424  std::uint32_t getArchiveVersion();
425 
427  void setArchiveVersion(std::uint32_t version);
428 
429  void setOnCallbackConnectionCreated(OnCallbackConnectionCreated onCallbackConnectionCreated);
430  OnCallbackConnectionCreated getOnCallbackConnectionCreated();
431 
432 
433  private:
434 
435  void init();
436 
437  public:
438 
439 
440  I_Service &
441  getServerTransportService();
442 
444  getServerTransportPtr();
445 
446  private:
447 
448  bool addService(
449  ServicePtr servicePtr);
450 
451  bool removeService(
452  ServicePtr servicePtr);
453 
454  public:
455 
456  PingBackServicePtr
457  getPingBackServicePtr();
458 
459  FileTransferServicePtr
460  getFileTransferServicePtr();
461 
462  SessionTimeoutServicePtr
463  getSessionTimeoutServicePtr();
464 
465  PublishingServicePtr
466  getPublishingServicePtr();
467 
468  SubscriptionServicePtr
469  getSubscriptionServicePtr();
470 
471  FilterServicePtr getFilterServicePtr();
472 
473  bool addServerTransport(
474  ServerTransportPtr serverTransportPtr);
475 
476  bool removeServerTransport(
477  ServerTransportPtr serverTransportPtr);
478 
479  ServerTransport *
480  findTransportCompatibleWith(ClientTransport & clientTransport);
481 
482  ServerTransport * queryForTransport(RCF::TransportType transportType);
483 
484  void setStartCallback(const StartCallback &startCallback);
485 
486  private:
487  void invokeStartCallback();
488 
489  private:
490  ServerBindingPtr bindImpl(
491  const std::string &name,
492  RcfClientPtr rcfClientPtr);
493 
494  //*************************************
495  // async io transport interface
496 
497  public:
498  SessionPtr createSession();
499  void onReadCompleted(SessionPtr sessionPtr);
500  void onWriteCompleted(SessionPtr sessionPtr);
501 
502 
503  //*************************************
504  // transports, queues and threads
505 
506  public:
507 
508 
509  template<typename Iter>
510  void enumerateSessions(const Iter & iter)
511  {
512  for (std::size_t i=0; i<mServerTransports.size(); ++i)
513  {
514  mServerTransports[i]->enumerateSessions(iter);
515  }
516  }
517 
518  //*************************************
519  // stub management
520 
521  private:
522  ReadWriteMutex mStubMapMutex;
523  typedef std::map<std::string, RcfClientPtr> StubMap;
524  StubMap mStubMap;
525 
526 
527  typedef std::function<void(const JsonRpcRequest &, JsonRpcResponse &)> JsonRpcMethod;
528  typedef std::map<std::string, JsonRpcMethod> JsonRpcMethods;
529  JsonRpcMethods mJsonRpcMethods;
530 
531  friend class RcfSession;
532 
533 
534 
535  //*************************************
536  // service management
537 
538  private:
539  std::vector<ServerTransportPtr> mServerTransports;
540  std::vector<ServicePtr> mServices;
541  FilterServicePtr mFilterServicePtr;
542  PingBackServicePtr mPingBackServicePtr;
543  FileTransferServicePtr mFileTransferServicePtr;
544  SessionTimeoutServicePtr mSessionTimeoutServicePtr;
545  PublishingServicePtr mPublishingServicePtr;
546  SubscriptionServicePtr mSubscriptionServicePtr;
547  CallbackConnectionServicePtr mCallbackConnectionServicePtr;
548  ProxyEndpointServicePtr mProxyEndpointServicePtr;
549  ServerObjectServicePtr mServerObjectServicePtr;
550 
551  void startService(ServicePtr servicePtr) const;
552  void stopService(ServicePtr servicePtr) const;
553  void resolveServiceThreadPools(ServicePtr servicePtr) const;
554 
555  friend class AsioNetworkSession;
556  FilterPtr createFilter(int filterId);
557 
558  // start/stop functionality
559  StartCallback mStartCallback;
560  Condition mStartEvent;
561  Condition mStopEvent;
562 
563  Mutex mStartStopMutex;
564  bool mStarted;
565 
566  private:
567  ThreadPoolPtr mThreadPoolPtr;
568 
569 
570  // TODO: get rid of this hack
571  private:
572  friend class MethodInvocationRequest;
573 
574  private:
575  std::uint32_t mRuntimeVersion;
576  std::uint32_t mArchiveVersion;
577 
578 
579  public:
580 
581 #if RCF_FEATURE_FILETRANSFER==1
582 
583 
584  Path getUploadPath(const std::string & uploadId);
585 
586  private:
587 
588  DownloadProgressCallback mOnFileDownloadProgress;
589  UploadProgressCallback mOnFileUploadProgress;
590 
591  Path mFileUploadDirectory;
592 
593  std::uint32_t mFileUploadQuota;
594  UploadBandwidthQuotaCallback mFileUploadQuotaCb;
595 
596  std::uint32_t mFileDownloadQuota;
597  DownloadBandwidthQuotaCallback mFileDownloadQuotaCb;
598 
599  friend class FileTransferService;
600 
601 #endif
602 
603  private:
604 
605  mutable ReadWriteMutex mPropertiesMutex;
606 
607  std::vector<TransportProtocol> mSupportedProtocols;
608  CertificatePtr mCertificatePtr;
609  std::string mOpenSslCipherSuite;
610 
611  CertificatePtr mCaCertificatePtr;
612  CertificateValidationCallback mCertificateValidationCb;
613  tstring mSchannelCertificateValidation;
614 
615  SslImplementation mSslImplementation;
616 
617  std::uint32_t mSessionTimeoutMs;
618  std::uint32_t mSessionHarvestingIntervalMs;
619 
620  std::uint32_t mHttpSessionTimeoutMs;
621 
622  std::string mHttpServerHeader;
623 
624  OnCallbackConnectionCreated mOnCallbackConnectionCreated;
625 
626 #if RCF_FEATURE_SSPI==1
627  DWORD mSchannelEnabledProtocols = 0;
628  ULONG mSchannelContextRequirements = 0;
629 #endif
630 
631  public:
632 
633 
634  private:
635 
636  std::uint32_t mServerObjectHarvestingIntervalS;
637 
638  friend class HttpSessionFilter;
639 
640  HttpSessionPtr attachHttpSession(const std::string & httpSessionId, bool allowCreate, ExceptionPtr & ePtr);
641  void detachHttpSession(HttpSessionPtr httpSessionPtr);
642 
643  friend class ServerObjectService;
644  void harvestHttpSessions();
645 
646 #if RCF_FEATURE_HTTP==1
647  Mutex mHttpSessionMapMutex;
648  std::map<std::string, HttpSessionPtr> mHttpSessionMap;
649 
650  HttpMessageVerifierPtr mHttpMessageVerifierPtr;
651 
652 #endif
653 
654  public:
655 
656 
657  private:
658  friend class ProxyEndpoint;
659  ClientTransportUniquePtr makeProxyEndpointConnection(const std::string& proxyEndpointName);
660 
661  bool mEnableProxyEndpoints = false;
662 
663  };
664 
665 } // namespace RCF
666 
667 #endif // ! INCLUDE_RCF_RCFSERVER_HPP
668 
RCF_FILESYSTEM_NS::path Path
Typedef for standard C++ path type.
Definition: FileSystem.hpp:32
RCF_EXPORT std::uint32_t getArchiveVersion()
Gets the RCF archive version number.
std::function< void(RcfSession &, FileDownloadInfo &)> DownloadProgressCallback
Describes user-provided callback functions for server-side monitoring of a file download.
Definition: RcfFwd.hpp:135
std::shared_ptr< Subscription > createSubscription(T &servantObj, const SubscriptionParms &parms)
Creates a subscription to a remote RCF publisher.
Definition: RcfServer.hpp:358
Represents a server side session, associated with a client connection.
Definition: RcfSession.hpp:65
std::shared_ptr< Certificate > CertificatePtr
Reference counted wrapper for RCF::Certificate.
Definition: RcfFwd.hpp:109
std::unique_ptr< ClientTransport > ClientTransportUniquePtr
Unique pointer wrapper for RCF::ClientTransport.
Definition: RcfFwd.hpp:44
std::shared_ptr< T > queryServerObject(const std::string &objectKey)
Queries for a server object under the given key. Returns an empty std::shared_ptr if no object is fou...
Definition: RcfServer.hpp:291
Base class for all client transports.
Definition: ClientTransport.hpp:75
Base class for all server transports.
Definition: ServerTransport.hpp:122
void setPublisherEndpoint(const Endpoint &publisherEp)
Sets the network endpoint of the publishing server.
BandwidthQuotaCallback DownloadBandwidthQuotaCallback
Describes user-provided callback functions for assigning custom bandwidth quotas to a RcfSession...
Definition: RcfFwd.hpp:147
std::function< bool(Certificate *)> CertificateValidationCallback
Describes user-provided callback functions for validating a certificate.
Definition: RcfFwd.hpp:115
std::shared_ptr< Publisher< Interface > > createPublisher()
Creates a publisher instance for the given RCF interface.
Definition: RcfServer.hpp:323
RCF_EXPORT void setArchiveVersion(std::uint32_t version)
Sets the RCF archive version number. Applies to all RCF clients and servers within the current proces...
std::shared_ptr< HttpMessageVerifier > HttpMessageVerifierPtr
Reference counted wrapper for RCF::HttpMessageVerifier.
Definition: HttpFrameFilter.hpp:61
RCF_EXPORT std::uint32_t getRuntimeVersion()
Gets the RCF runtime version number.
std::shared_ptr< T > getServerObject(const std::string &objectKey, std::uint32_t timeoutMs)
Queries or creates a server object under the given key. If creating the object, the timeout value is ...
Definition: RcfServer.hpp:299
BandwidthQuotaCallback UploadBandwidthQuotaCallback
Describes user-provided callback functions for assigning custom bandwidth quotas to a RcfSession...
Definition: RcfFwd.hpp:144
SslImplementation
Describes which SSL implementation to use.
Definition: Enums.hpp:85
ServerBindingPtr bind(ImplementationT &servantObj, const std::string &name="")
Creates a servant binding, exposing the servant object to remote calls through the RCF interface Inte...
Definition: RcfServer.hpp:149
std::shared_ptr< ServerBinding > ServerBindingPtr
Reference counted wrapper for RCF::ServerBinding.
Definition: RcfFwd.hpp:249
std::shared_ptr< Publisher< Interface > > createPublisher(const PublisherParms &parms)
Creates a publisher instance for the given RCF interface.
Definition: RcfServer.hpp:334
Provides RCF server-side functionality.
Definition: RcfServer.hpp:54
bool unbind(const std::string &name="")
Removes a servant binding from the RcfServer.
Definition: RcfServer.hpp:166
Base class for all network endpoint types.
Definition: Endpoint.hpp:41
std::function< void(RcfSession &, FileUploadInfo &)> UploadProgressCallback
Describes user-provided callback functions for server-side monitoring of a file upload.
Definition: RcfFwd.hpp:138
Base class for IP-based server transports. Provides IP-related functionality.
Definition: IpServerTransport.hpp:37
General configuration of a subscription.
Definition: SubscriptionService.hpp:91
Definition: AmiIoHandler.hpp:24
TransportType
Describes the transport types used by a RCF connection.
Definition: Enums.hpp:34
Represents a proxy endpoint.
Definition: ProxyEndpoint.hpp:46
General configuration of a publisher.
Definition: PublishingService.hpp:38
RCF_EXPORT void setRuntimeVersion(std::uint32_t version)
Sets the RCF runtime version number. Applies to all RCF clients and servers within the current proces...
std::shared_ptr< ServerTransport > ServerTransportPtr
Unique pointer wrapper for RCF::ServerTransport.
Definition: RcfFwd.hpp:47
RCF_EXPORT bool init(RcfConfigT *=nullptr)
Reference-counted initialization of RCF library. May be called multiple times (see deinit())...
std::shared_ptr< Subscription > createSubscription(T &servantObj, const RCF::Endpoint &publisherEp)
Creates a subscription to a remote RCF publisher.
Definition: RcfServer.hpp:344