RCFProto
 All Classes Functions Typedefs
RcfServer.hpp
1 
2 //******************************************************************************
3 // RCF - Remote Call Framework
4 //
5 // Copyright (c) 2005 - 2013, 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: 2.0
15 // Contact: support <at> deltavsoft.com
16 //
17 //******************************************************************************
18 
19 #ifndef INCLUDE_RCF_RCFSERVER_HPP
20 #define INCLUDE_RCF_RCFSERVER_HPP
21 
22 #include <map>
23 #include <memory>
24 #include <set>
25 #include <string>
26 #include <vector>
27 
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>
35 
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>
45 
46 #if RCF_FEATURE_FILETRANSFER==1
47 #include <RCF/FileTransferService.hpp>
48 #endif
49 
50 #if RCF_FEATURE_PUBSUB==1
51 #include <RCF/PublishingService.hpp>
52 #include <RCF/SubscriptionService.hpp>
53 #endif
54 
55 namespace RCF {
56 
57  class ServerTransport;
58  class StubEntry;
59  class I_Service;
60  class RcfSession;
61  class Endpoint;
62  class I_FilterFactoryLookupProvider;
63  class I_RcfClient;
64  class IpServerTransport;
65  class PingBackService;
66  class FileTransferService;
67  class ObjectFactoryService;
68  class FilterService;
69  class SessionTimeoutService;
70  class PublishingService;
71  class SubscriptionService;
72  class SessionObjectFactoryService;
73  class ObjectFactoryService;
74  class CallbackConnectionService;
75 
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;
94 
95  template<typename Interface>
96  class Publisher;
97 
98  class Subscription;
99  typedef boost::shared_ptr<Subscription> SubscriptionPtr;
100 
101  class SubscriptionParms;
102 
103  class BandwidthQuota;
104  typedef boost::shared_ptr<BandwidthQuota> BandwidthQuotaPtr;
105 
106  class FileDownloadInfo;
107  class FileUploadInfo;
108 
109  class JsonRpcRequest;
110  class JsonRpcResponse;
111 
112  class SspiFilter;
113 
114  class HttpSession;
115  typedef boost::shared_ptr<HttpSession> HttpSessionPtr;
116 
117  typedef boost::function2<void, RcfSessionPtr, ClientTransportAutoPtr> OnCallbackConnectionCreated;
118 
119  class RCF_EXPORT RcfServer : boost::noncopyable
120  {
121  public:
122 
123  RcfServer();
124  RcfServer(const Endpoint &endpoint);
125  RcfServer(ServicePtr servicePtr);
126  RcfServer(ServerTransportPtr serverTransportPtr);
127 
128  ~RcfServer();
129 
130  typedef boost::function0<void> JoinFunctor;
131  typedef boost::function1<void, RcfServer&> StartCallback;
132 
133  void start();
134  void stop();
135 
136  private:
137 
138  void init();
139 
140  public:
141 
142  ServerTransport &
143  getServerTransport();
144 
145  I_Service &
146  getServerTransportService();
147 
148  ServerTransportPtr
149  getServerTransportPtr();
150 
151  IpServerTransport &
152  getIpServerTransport();
153 
154  private:
155 
156  bool addService(
157  ServicePtr servicePtr);
158 
159  bool removeService(
160  ServicePtr servicePtr);
161 
162  public:
163 
164  PingBackServicePtr
165  getPingBackServicePtr();
166 
167  FileTransferServicePtr
168  getFileTransferServicePtr();
169 
170  SessionTimeoutServicePtr
171  getSessionTimeoutServicePtr();
172 
173  ObjectFactoryServicePtr
174  getObjectFactoryServicePtr();
175 
176  SessionObjectFactoryServicePtr
177  getSessionObjectFactoryServicePtr();
178 
179  PublishingServicePtr
180  getPublishingServicePtr();
181 
182  SubscriptionServicePtr
183  getSubscriptionServicePtr();
184 
185  FilterServicePtr getFilterServicePtr();
186 
187  bool addServerTransport(
188  ServerTransportPtr serverTransportPtr);
189 
190  bool removeServerTransport(
191  ServerTransportPtr serverTransportPtr);
192 
193  ServerTransport &
194  findTransportCompatibleWith(ClientTransport & clienetTransport);
195 
196  void setStartCallback(const StartCallback &startCallback);
197 
198  private:
199  void invokeStartCallback();
200 
201  private:
202  ServerBindingPtr bindImpl(
203  const std::string &name,
204  RcfClientPtr rcfClientPtr);
205 
206  //*************************************
207  // async io transport interface
208 
209  //private:
210  public:
211  SessionPtr createSession();
212  void onReadCompleted(SessionPtr sessionPtr);
213  void onWriteCompleted(SessionPtr sessionPtr);
214 
215 
216  //*************************************
217  // transports, queues and threads
218 
219  private:
220 
221  void startImpl();
222 
223  public:
224 
225 
226  template<typename Iter>
227  void enumerateSessions(const Iter & iter)
228  {
229 
230  for (std::size_t i=0; i<mServerTransports.size(); ++i)
231  {
232  mServerTransports[i]->enumerateSessions(iter);
233  }
234 
235  }
236 
237  //*************************************
238  // stub management
239 
240  private:
241  ReadWriteMutex mStubMapMutex;
242  typedef std::map<std::string, StubEntryPtr> StubMap;
243  StubMap mStubMap;
244 
245  typedef boost::function<void(const JsonRpcRequest &, JsonRpcResponse &)> JsonRpcMethod;
246  typedef std::map<std::string, JsonRpcMethod> JsonRpcMethods;
247  JsonRpcMethods mJsonRpcMethods;
248 
249  friend class RcfSession;
250 
251 
252 
253  //*************************************
254  // service management
255 
256  private:
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;
269 
270  void startService(ServicePtr servicePtr) const;
271  void stopService(ServicePtr servicePtr) const;
272  void resolveServiceThreadPools(ServicePtr servicePtr) const;
273 
274  friend class AsioNetworkSession;
275  FilterPtr createFilter(int filterId);
276 
277  private:
278  // start/stop functionality
279  StartCallback mStartCallback;
280  Condition mStartEvent;
281  Condition mStopEvent;
282 
283  Mutex mStartStopMutex;
284  bool mStarted;
285 
286 
287  public:
288  void setThreadPool(ThreadPoolPtr threadPoolPtr);
289  ThreadPoolPtr getThreadPool();
290  ServerTransport & addEndpoint(const Endpoint & endpoint);
291 
292  private:
293  ThreadPoolPtr mThreadPoolPtr;
294 
295  public:
296 
297  void waitForStopEvent();
298  void waitForStartEvent();
299 
300  bool isStarted();
301 
302  // TODO: get rid of this hack
303  private:
304  friend class MethodInvocationRequest;
305 
306  public:
307  boost::uint32_t getRuntimeVersion();
308  void setRuntimeVersion(boost::uint32_t version);
309 
310  boost::uint32_t getArchiveVersion();
311  void setArchiveVersion(boost::uint32_t version);
312 
313  private:
314  boost::uint32_t mRuntimeVersion;
315  boost::uint32_t mArchiveVersion;
316 
317 
318  public:
319 
320  template<typename I1, typename ImplementationT>
321  ServerBindingPtr bind(ImplementationT & t, const std::string &name = "")
322  {
323  boost::shared_ptr< I_Deref<ImplementationT> > derefPtr(
324  new DerefObj<ImplementationT>(t) );
325 
326  RcfClientPtr rcfClientPtr = createServerStub(
327  (I1 *) 0,
328  (ImplementationT *) 0, derefPtr);
329 
330  return bindImpl(
331  name.empty() ?
332  I1::getInterfaceName() :
333  name ,
334  rcfClientPtr);
335  }
336 
337  template<typename InterfaceT>
338  bool unbind(const std::string &name_ = "")
339  {
340  const std::string &name = (name_ == "") ?
341  getInterfaceName((InterfaceT *) NULL) :
342  name_;
343 
344  WriteLock writeLock(mStubMapMutex);
345  mStubMap.erase(name);
346  return true;
347  }
348 
349 #if RCF_FEATURE_JSON==1
350  void bindJsonRpc(JsonRpcMethod jsonRpcMethod, const std::string & jsonRpcName);
351  void unbindJsonRpc(const std::string & jsonRpcName);
352 #endif
353 
354  void setSupportedTransportProtocols(
355  const std::vector<TransportProtocol> & protocols);
356 
357  const std::vector<TransportProtocol> &
358  getSupportedTransportProtocols() const;
359 
360  void setCertificate(CertificatePtr certificatePtr);
361  CertificatePtr getCertificate();
362 
363  void setOpenSslCipherSuite(const std::string & cipherSuite);
364  std::string getOpenSslCipherSuite() const;
365 
366  void setCaCertificate(CertificatePtr certificatePtr);
367  CertificatePtr getCaCertificate();
368 
369 
370  typedef boost::function<bool(Certificate *)> CertificateValidationCb;
371  void setCertificateValidationCallback(CertificateValidationCb certificateValidationCb);
372  const CertificateValidationCb & getCertificateValidationCallback() const;
373 
374  void setEnableSchannelCertificateValidation(const tstring & peerName);
375  tstring getEnableSchannelCertificateValidation() const;
376 
377  void setSslImplementation(SslImplementation sslImplementation);
378  SslImplementation getSslImplementation() const;
379 
380  void setSessionTimeoutMs(boost::uint32_t sessionTimeoutMs);
381  boost::uint32_t getSessionTimeoutMs();
382 
383  void setSessionHarvestingIntervalMs(boost::uint32_t sessionHarvestingIntervalMs);
384  boost::uint32_t getSessionHarvestingIntervalMs();
385 
386  void setHttpSessionTimeoutMs(boost::uint32_t httpSessionTimeoutMs);
387  boost::uint32_t getHttpSessionTimeoutMs();
388 
389  void setOnCallbackConnectionCreated(OnCallbackConnectionCreated onCallbackConnectionCreated);
390  OnCallbackConnectionCreated getOnCallbackConnectionCreated();
391 
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);
396 
397  boost::uint32_t getOfsMaxNumberOfObjects() const;
398  boost::uint32_t getOfsObjectTimeoutS() const;
399  boost::uint32_t getOfsCleanupIntervalS() const;
400  float getOfsCleanupThreshold() const;
401 
402 #if RCF_FEATURE_FILETRANSFER==1
403 
404  typedef FileTransferService::OnFileDownloadProgress OnFileDownloadProgress;
405  typedef FileTransferService::OnFileUploadProgress OnFileUploadProgress;
406 
407  void setOnFileDownloadProgress(OnFileDownloadProgress onFileDownloadProgress);
408  void setOnFileUploadProgress(OnFileUploadProgress onFileUploadProgress);
409 
410  void setFileUploadDirectory(const std::string & uploadDir);
411  std::string getFileUploadDirectory() const;
412 
413  typedef boost::function1<BandwidthQuotaPtr, RcfSession &> BandwidthQuotaCallback;
414  typedef BandwidthQuotaCallback FileUploadQuotaCallback;
415  typedef BandwidthQuotaCallback FileDownloadQuotaCallback;
416 
417  void setFileUploadBandwidthLimit(boost::uint32_t uploadQuotaBps);
418  boost::uint32_t getFileUploadBandwidthLimit() const;
419 
420  void setFileUploadCustomBandwidthLimit(FileUploadQuotaCallback uploadQuotaCb);
421 
422  void setFileDownloadBandwidthLimit(boost::uint32_t downloadQuotaBps);
423  boost::uint32_t getFileDownloadBandwidthLimit() const;
424 
425  void setFileDownloadCustomBandwidthLimit(FileDownloadQuotaCallback downloadQuotaCb);
426 
427  private:
428 
429  OnFileDownloadProgress mOnFileDownloadProgress;
430  OnFileUploadProgress mOnFileUploadProgress;
431 
432  std::string mFileUploadDirectory;
433 
434  boost::uint32_t mFileUploadQuota;
435  FileUploadQuotaCallback mFileUploadQuotaCb;
436 
437  boost::uint32_t mFileDownloadQuota;
438  FileDownloadQuotaCallback mFileDownloadQuotaCb;
439 
440  friend class FileTransferService;
441 
442 #endif
443 
444  private:
445 
446  mutable ReadWriteMutex mPropertiesMutex;
447 
448  std::vector<TransportProtocol> mSupportedProtocols;
449  CertificatePtr mCertificatePtr;
450  std::string mOpenSslCipherSuite;
451 
452  CertificatePtr mCaCertificatePtr;
453  CertificateValidationCb mCertificateValidationCb;
454  tstring mSchannelCertificateValidation;
455 
456  SslImplementation mSslImplementation;
457 
458  boost::uint32_t mSessionTimeoutMs;
459  boost::uint32_t mSessionHarvestingIntervalMs;
460 
461  boost::uint32_t mHttpSessionTimeoutMs;
462 
463  OnCallbackConnectionCreated mOnCallbackConnectionCreated;
464 
465  boost::uint32_t mOfsMaxNumberOfObjects;
466  boost::uint32_t mOfsObjectTimeoutS;
467  boost::uint32_t mOfsCleanupIntervalS;
468  float mOfsCleanupThreshold;
469 
470  public:
471 
472 #if RCF_FEATURE_PUBSUB==1
473 
474  template<typename Interface>
475  boost::shared_ptr< Publisher<Interface> > createPublisher()
476  {
477  PublisherParms parms;
478  return mPublishingServicePtr->createPublisher<Interface>(parms);
479  }
480 
481  template<typename Interface>
482  boost::shared_ptr< Publisher<Interface> > createPublisher(
483  const PublisherParms & parms)
484  {
485  return mPublishingServicePtr->createPublisher<Interface>(parms);
486  }
487 
488  template<typename Interface, typename T>
489  boost::shared_ptr< Subscription > createSubscription(
490  T & t,
491  const RCF::Endpoint & publisherEp)
492  {
493  RCF_ASSERT(mStarted);
494  SubscriptionParms parms;
495  parms.setPublisherEndpoint(publisherEp);
496  return mSubscriptionServicePtr->createSubscription<Interface>(t, publisherEp);
497  }
498 
499  template<typename Interface, typename T>
500  boost::shared_ptr< Subscription > createSubscription(
501  T & t,
502  const SubscriptionParms & parms)
503  {
504  RCF_ASSERT(mStarted);
505  return mSubscriptionServicePtr->createSubscription<Interface>(t, parms);
506  }
507 
508 #endif
509 
510  private:
511 
512  boost::uint32_t mServerObjectHarvestingIntervalS;
513 
514  Mutex mHttpSessionMapMutex;
515  std::map<std::string, HttpSessionPtr> mHttpSessionMap;
516 
517  friend class HttpSessionFilter;
518 
519  HttpSessionPtr attachHttpSession(const std::string & httpSessionId, bool allowCreate, ExceptionPtr & ePtr);
520  void detachHttpSession(HttpSessionPtr httpSessionPtr);
521 
522  friend class ServerObjectService;
523  void harvestHttpSessions();
524 
525  public:
526 
527  boost::uint32_t getServerObjectHarvestingIntervalS() const;
528  void setServerObjectHarvestingIntervalS(boost::uint32_t harvestingIntervalS);
529 
530  template<typename T>
531  boost::shared_ptr<T> queryServerObject(
532  const std::string & objectKey)
533  {
534  return mServerObjectServicePtr->queryServerObject<T>(objectKey);
535  }
536 
537  template<typename T>
538  boost::shared_ptr<T> getServerObject(
539  const std::string & objectKey,
540  boost::uint32_t timeoutMs)
541  {
542  return mServerObjectServicePtr->getServerObject<T>(objectKey, timeoutMs);
543  }
544 
545  void deleteServerObject(const std::string & objectKey);
546 
547  StubEntryPtr findStubEntryForToken(const Token & token);
548  };
549 
550 } // namespace RCF
551 
552 #endif // ! INCLUDE_RCF_RCFSERVER_HPP
553