Remote Call Framework 3.0
RcfServer.hpp
Go to the documentation of this file.
1 
2 //******************************************************************************
3 // RCF - Remote Call Framework
4 //
5 // Copyright (c) 2005 - 2018, 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.0
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 
269 
271 
273  std::uint32_t getServerObjectHarvestingIntervalS() const;
274 
276  void setServerObjectHarvestingIntervalS(std::uint32_t harvestingIntervalS);
277 
279  template<typename T>
280  std::shared_ptr<T> queryServerObject(
281  const std::string & objectKey)
282  {
283  return mServerObjectServicePtr->queryServerObject<T>(objectKey);
284  }
285 
287  template<typename T>
288  std::shared_ptr<T> getServerObject(
289  const std::string & objectKey,
290  std::uint32_t timeoutMs)
291  {
292  return mServerObjectServicePtr->getServerObject<T>(objectKey, timeoutMs);
293  }
294 
296  void deleteServerObject(const std::string & objectKey);
297 
299 
300 #if RCF_FEATURE_PUBSUB==1
301 
304 
306 
308 
311  template<typename Interface>
312  std::shared_ptr< Publisher<Interface> > createPublisher()
313  {
314  PublisherParms parms;
315  return mPublishingServicePtr->createPublisher<Interface>(parms);
316  }
317 
319 
322  template<typename Interface>
323  std::shared_ptr< Publisher<Interface> > createPublisher(
324  const PublisherParms & parms)
325  {
326  return mPublishingServicePtr->createPublisher<Interface>(parms);
327  }
328 
330 
332  template<typename Interface, typename T>
333  std::shared_ptr< Subscription > createSubscription(
334  T & servantObj,
335  const RCF::Endpoint & publisherEp)
336  {
337  RCF_ASSERT(mStarted);
338  SubscriptionParms parms;
339  parms.setPublisherEndpoint(publisherEp);
340  return mSubscriptionServicePtr->createSubscription<Interface>(servantObj, publisherEp);
341  }
342 
344 
346  template<typename Interface, typename T>
347  std::shared_ptr< Subscription > createSubscription(
348  T & servantObj,
349  const SubscriptionParms & parms)
350  {
351  RCF_ASSERT(mStarted);
352  return mSubscriptionServicePtr->createSubscription<Interface>(servantObj, parms);
353  }
354 
356 
357 #endif
358 
360  void setEnableProxyEndpoints(bool enable);
361 
363  bool getEnableProxyEndpoints() const;
364 
365 #if RCF_FEATURE_FILETRANSFER==1
366 
369 
371 
373  void setUploadBandwidthLimit(std::uint32_t uploadQuotaBps);
374 
376  std::uint32_t getUploadBandwidthLimit() const;
377 
379  void setDownloadBandwidthLimit(std::uint32_t downloadQuotaBps);
380 
382  std::uint32_t getDownloadBandwidthLimit() const;
383 
385  void setUploadDirectory(const Path & uploadDir);
386 
388  Path getUploadDirectory() const;
389 
391  void setDownloadProgressCallback(DownloadProgressCallback downloadProgressCb);
392 
394  void setUploadProgressCallback(UploadProgressCallback uploadProgressCb);
395 
397  void setUploadBandwidthQuotaCallback(UploadBandwidthQuotaCallback uploadQuotaCb);
398 
400  void setDownloadBandwidthQuotaCallback(DownloadBandwidthQuotaCallback downloadQuotaCb);
401 
403 
404 #endif
405 
406 
407 
408  // For internal use.
409  std::uint32_t getRuntimeVersion();
410  void setRuntimeVersion(std::uint32_t version);
411 
413  std::uint32_t getArchiveVersion();
414 
416  void setArchiveVersion(std::uint32_t version);
417 
418  void setOnCallbackConnectionCreated(OnCallbackConnectionCreated onCallbackConnectionCreated);
419  OnCallbackConnectionCreated getOnCallbackConnectionCreated();
420 
421 
422  private:
423 
424  void init();
425 
426  public:
427 
428 
429  I_Service &
430  getServerTransportService();
431 
433  getServerTransportPtr();
434 
435  private:
436 
437  bool addService(
438  ServicePtr servicePtr);
439 
440  bool removeService(
441  ServicePtr servicePtr);
442 
443  public:
444 
445  PingBackServicePtr
446  getPingBackServicePtr();
447 
448  FileTransferServicePtr
449  getFileTransferServicePtr();
450 
451  SessionTimeoutServicePtr
452  getSessionTimeoutServicePtr();
453 
454  PublishingServicePtr
455  getPublishingServicePtr();
456 
457  SubscriptionServicePtr
458  getSubscriptionServicePtr();
459 
460  FilterServicePtr getFilterServicePtr();
461 
462  bool addServerTransport(
463  ServerTransportPtr serverTransportPtr);
464 
465  bool removeServerTransport(
466  ServerTransportPtr serverTransportPtr);
467 
468  ServerTransport *
469  findTransportCompatibleWith(ClientTransport & clientTransport);
470 
471  ServerTransport * queryForTransport(RCF::TransportType transportType);
472 
473  void setStartCallback(const StartCallback &startCallback);
474 
475  private:
476  void invokeStartCallback();
477 
478  private:
479  ServerBindingPtr bindImpl(
480  const std::string &name,
481  RcfClientPtr rcfClientPtr);
482 
483  //*************************************
484  // async io transport interface
485 
486  public:
487  SessionPtr createSession();
488  void onReadCompleted(SessionPtr sessionPtr);
489  void onWriteCompleted(SessionPtr sessionPtr);
490 
491 
492  //*************************************
493  // transports, queues and threads
494 
495  public:
496 
497 
498  template<typename Iter>
499  void enumerateSessions(const Iter & iter)
500  {
501  for (std::size_t i=0; i<mServerTransports.size(); ++i)
502  {
503  mServerTransports[i]->enumerateSessions(iter);
504  }
505  }
506 
507  //*************************************
508  // stub management
509 
510  private:
511  ReadWriteMutex mStubMapMutex;
512  typedef std::map<std::string, RcfClientPtr> StubMap;
513  StubMap mStubMap;
514 
515 
516  typedef std::function<void(const JsonRpcRequest &, JsonRpcResponse &)> JsonRpcMethod;
517  typedef std::map<std::string, JsonRpcMethod> JsonRpcMethods;
518  JsonRpcMethods mJsonRpcMethods;
519 
520  friend class RcfSession;
521 
522 
523 
524  //*************************************
525  // service management
526 
527  private:
528  std::vector<ServerTransportPtr> mServerTransports;
529  std::vector<ServicePtr> mServices;
530  FilterServicePtr mFilterServicePtr;
531  PingBackServicePtr mPingBackServicePtr;
532  FileTransferServicePtr mFileTransferServicePtr;
533  SessionTimeoutServicePtr mSessionTimeoutServicePtr;
534  PublishingServicePtr mPublishingServicePtr;
535  SubscriptionServicePtr mSubscriptionServicePtr;
536  CallbackConnectionServicePtr mCallbackConnectionServicePtr;
537  ProxyEndpointServicePtr mProxyEndpointServicePtr;
538  ServerObjectServicePtr mServerObjectServicePtr;
539 
540  void startService(ServicePtr servicePtr) const;
541  void stopService(ServicePtr servicePtr) const;
542  void resolveServiceThreadPools(ServicePtr servicePtr) const;
543 
544  friend class AsioNetworkSession;
545  FilterPtr createFilter(int filterId);
546 
547  // start/stop functionality
548  StartCallback mStartCallback;
549  Condition mStartEvent;
550  Condition mStopEvent;
551 
552  Mutex mStartStopMutex;
553  bool mStarted;
554 
555  private:
556  ThreadPoolPtr mThreadPoolPtr;
557 
558 
559  // TODO: get rid of this hack
560  private:
561  friend class MethodInvocationRequest;
562 
563  private:
564  std::uint32_t mRuntimeVersion;
565  std::uint32_t mArchiveVersion;
566 
567 
568  public:
569 
570 #if RCF_FEATURE_FILETRANSFER==1
571 
572 
573  Path getUploadPath(const std::string & uploadId);
574 
575  private:
576 
577  DownloadProgressCallback mOnFileDownloadProgress;
578  UploadProgressCallback mOnFileUploadProgress;
579 
580  Path mFileUploadDirectory;
581 
582  std::uint32_t mFileUploadQuota;
583  UploadBandwidthQuotaCallback mFileUploadQuotaCb;
584 
585  std::uint32_t mFileDownloadQuota;
586  DownloadBandwidthQuotaCallback mFileDownloadQuotaCb;
587 
588  friend class FileTransferService;
589 
590 #endif
591 
592  private:
593 
594  mutable ReadWriteMutex mPropertiesMutex;
595 
596  std::vector<TransportProtocol> mSupportedProtocols;
597  CertificatePtr mCertificatePtr;
598  std::string mOpenSslCipherSuite;
599 
600  CertificatePtr mCaCertificatePtr;
601  CertificateValidationCallback mCertificateValidationCb;
602  tstring mSchannelCertificateValidation;
603 
604  SslImplementation mSslImplementation;
605 
606  std::uint32_t mSessionTimeoutMs;
607  std::uint32_t mSessionHarvestingIntervalMs;
608 
609  std::uint32_t mHttpSessionTimeoutMs;
610 
611  OnCallbackConnectionCreated mOnCallbackConnectionCreated;
612 
613 #if RCF_FEATURE_SSPI==1
614  DWORD mSchannelEnabledProtocols = 0;
615  ULONG mSchannelContextRequirements = 0;
616 #endif
617 
618  public:
619 
620 
621  private:
622 
623  std::uint32_t mServerObjectHarvestingIntervalS;
624 
625 #if RCF_FEATURE_HTTP==1
626  Mutex mHttpSessionMapMutex;
627  std::map<std::string, HttpSessionPtr> mHttpSessionMap;
628 
629  friend class HttpSessionFilter;
630 
631  HttpSessionPtr attachHttpSession(const std::string & httpSessionId, bool allowCreate, ExceptionPtr & ePtr);
632  void detachHttpSession(HttpSessionPtr httpSessionPtr);
633 
634  friend class ServerObjectService;
635  void harvestHttpSessions();
636 #endif
637 
638  public:
639 
640 
641  private:
642  friend class ProxyEndpoint;
643  ClientTransportUniquePtr makeProxyEndpointConnection(const std::string& proxyEndpointName);
644 
645  bool mEnableProxyEndpoints = false;
646 
647  };
648 
649 } // namespace RCF
650 
651 #endif // ! INCLUDE_RCF_RCFSERVER_HPP
652 
RCF_FILESYSTEM_NS::path Path
Typedef for standard C++ path type.
Definition: FileSystem.hpp:35
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:134
std::shared_ptr< Subscription > createSubscription(T &servantObj, const SubscriptionParms &parms)
Creates a subscription to a remote RCF publisher.
Definition: RcfServer.hpp:347
Represents a server side session, associated with a client connection.
Definition: RcfSession.hpp:67
std::shared_ptr< Certificate > CertificatePtr
Reference counted wrapper for RCF::Certificate.
Definition: RcfFwd.hpp:108
std::unique_ptr< ClientTransport > ClientTransportUniquePtr
Unique pointer wrapper for RCF::ClientTransport.
Definition: RcfFwd.hpp:43
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:280
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:146
std::function< bool(Certificate *)> CertificateValidationCallback
Describes user-provided callback functions for validating a certificate.
Definition: RcfFwd.hpp:114
std::shared_ptr< Publisher< Interface > > createPublisher()
Creates a publisher instance for the given RCF interface.
Definition: RcfServer.hpp:312
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...
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:288
BandwidthQuotaCallback UploadBandwidthQuotaCallback
Describes user-provided callback functions for assigning custom bandwidth quotas to a RcfSession...
Definition: RcfFwd.hpp:143
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:246
std::shared_ptr< Publisher< Interface > > createPublisher(const PublisherParms &parms)
Creates a publisher instance for the given RCF interface.
Definition: RcfServer.hpp:323
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:137
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:45
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:46
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:333