RCFProto
 All Classes Functions Typedefs
ClientStub.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_CLIENTSTUB_HPP
20 #define INCLUDE_RCF_CLIENTSTUB_HPP
21 
22 #include <string>
23 #include <vector>
24 #include <memory>
25 
26 #include <boost/scoped_ptr.hpp>
27 #include <boost/any.hpp>
28 #include <boost/shared_ptr.hpp>
29 #include <boost/enable_shared_from_this.hpp>
30 
31 #include <RCF/Certificate.hpp>
32 #include <RCF/ClientProgress.hpp>
33 #include <RCF/Filter.hpp>
34 #include <RCF/ClientTransport.hpp>
35 #include <RCF/Endpoint.hpp>
36 #include <RCF/Enums.hpp>
37 #include <RCF/Export.hpp>
38 #include <RCF/GetInterfaceName.hpp>
39 #include <RCF/MethodInvocation.hpp>
40 #include <RCF/SerializationProtocol_Base.hpp>
41 #include <RCF/RecursionLimiter.hpp>
42 #include <RCF/SerializationProtocol.hpp>
43 #include <RCF/Token.hpp>
44 
45 #if RCF_FEATURE_FILETRANSFER==1
46 #include <RCF/FileStream.hpp>
47 #include <RCF/FileDownload.hpp>
48 #include <RCF/FileUpload.hpp>
49 #endif
50 
51 namespace RCF {
52 
53  template<typename T>
54  class FutureImpl;
55 
56  struct Void;
57 
58  class CallOptions;
59 
60  class ConnectionResetGuard;
61 
62  class I_Parameters;
63 
65  RCF_EXPORT void setDefaultSslImplementation(SslImplementation sslImplementation);
66 
68  RCF_EXPORT SslImplementation getDefaultSslImplementation();
69 
70  RCF_EXPORT void setDefaultConnectTimeoutMs(unsigned int connectTimeoutMs);
71  RCF_EXPORT unsigned int getDefaultConnectTimeoutMs();
72 
73  RCF_EXPORT void setDefaultRemoteCallTimeoutMs(unsigned int remoteCallTimeoutMs);
74  RCF_EXPORT unsigned int getDefaultRemoteCallTimeoutMs();
75 
76  RCF_EXPORT void setDefaultNativeWstringSerialization(bool enable);
77  RCF_EXPORT bool getDefaultNativeWstringSerialization();
78 
79  class ClientStub;
80 
81  typedef boost::shared_ptr<ClientStub> ClientStubPtr;
82 
83  typedef Token FileTransferToken;
84 
85  class ClientProgress;
86  typedef boost::shared_ptr<ClientProgress> ClientProgressPtr;
87 
88  class ClientTransport;
89  typedef std::auto_ptr<ClientTransport> ClientTransportAutoPtr;
90 
91  class I_RcfClient;
92  typedef boost::shared_ptr<I_RcfClient> RcfClientPtr;
93 
94  class I_Future;
95  class IpClientTransport;
96 
97  template<typename T>
98  class FutureImpl;
99 
100  class FileTransferProgress;
101 
102  class OpenSslEncryptionFilter;
103 
104  class SspiFilter;
105 
106  typedef boost::function1<void, const FileTransferProgress &> FileProgressCb;
107 
108  template<
109  typename R,
110  typename A1,
111  typename A2,
112  typename A3,
113  typename A4,
114  typename A5,
115  typename A6,
116  typename A7,
117  typename A8,
118  typename A9,
119  typename A10,
120  typename A11,
121  typename A12,
122  typename A13,
123  typename A14,
124  typename A15>
125  class AllocateClientParameters;
126 
127  template<
128  typename R,
129  typename A1,
130  typename A2,
131  typename A3,
132  typename A4,
133  typename A5,
134  typename A6,
135  typename A7,
136  typename A8,
137  typename A9,
138  typename A10,
139  typename A11,
140  typename A12,
141  typename A13,
142  typename A14,
143  typename A15>
144  class ClientParameters;
145 
146  class OverlappedAmi;
147  typedef boost::shared_ptr<OverlappedAmi> OverlappedAmiPtr;
148 
149  class RCF_EXPORT CurrentClientStubSentry
150  {
151  public:
152  CurrentClientStubSentry(ClientStub & clientStub);
153  CurrentClientStubSentry(ClientStub * pClientStub);
154  ~CurrentClientStubSentry();
155 
156  private:
157  bool mEnabled;
158  };
159 
160  class RCF_EXPORT ClientStub :
161  public ClientTransportCallback,
162  public boost::enable_shared_from_this<ClientStub>
163  {
164  public:
165  ClientStub(const std::string & interfaceName);
166  ClientStub(const std::string & interfaceName, const std::string &objectName);
167  ClientStub(const ClientStub & rhs);
168  ~ClientStub();
169 
170  ClientStub &operator=(const ClientStub & rhs);
171 
172  private:
173  void init(const std::string & interfaceName, const std::string & objectName);
174  void assign(const ClientStub & rhs);
175 
176  public:
177 
178  void setEndpoint(const Endpoint &endpoint);
179  void setEndpoint(EndpointPtr endpointPtr);
180  EndpointPtr getEndpoint() const;
181  Token getTargetToken() const;
182  void setTargetToken(Token token);
183  const std::string & getTargetName() const;
184  void setTargetName(const std::string &targetName);
185  void setInterfaceName(const std::string & interfaceName);
186  const std::string & getInterfaceName();
187  RemoteCallSemantics getRemoteCallSemantics() const;
188  void setRemoteCallSemantics(RemoteCallSemantics defaultCallingSemantics);
189 
190  // Deprecated - use getRemoteCallSemantics()/setRemoteCallSemantics instead.
191  RemoteCallSemantics getDefaultCallingSemantics() const;
192  void setDefaultCallingSemantics(RemoteCallSemantics defaultCallingSemantics);
193 
194  void setSerializationProtocol(SerializationProtocol protocol);
195  SerializationProtocol getSerializationProtocol() const;
196 
197  void setMarshalingProtocol(MarshalingProtocol protocol);
198  MarshalingProtocol getMarshalingProtocol() const;
199 
200  bool getNativeWstringSerialization();
201  void setNativeWstringSerialization(bool enable);
202 
203  void setEnableSfPointerTracking(bool enable);
204  bool getEnableSfPointerTracking() const;
205 
206  void setTransport(ClientTransportAutoPtr transport);
207 
208  ClientTransport& getTransport();
209  IpClientTransport & getIpTransport();
210 
211  ClientTransportAutoPtr releaseTransport();
212 
213  void instantiateTransport();
214  void connect();
215  void connectAsync(boost::function0<void> onCompletion);
216  void wait(boost::function0<void> onCompletion, boost::uint32_t timeoutMs);
217  void disconnect();
218  bool isConnected();
219  void setConnected(bool connected);
220 
221  void setMessageFilters(const std::vector<FilterPtr> &filters);
222  void setMessageFilters();
223  void setMessageFilters(FilterPtr filterPtr);
224 
225  const std::vector<FilterPtr> &
226  getMessageFilters();
227 
228  virtual bool isClientStub() const;
229 
230 
231  // Synchronous transport filter requests.
232  void requestTransportFilters_Legacy(const std::vector<FilterPtr> &filters);
233  void requestTransportFilters(const std::vector<FilterPtr> &filters);
234  void requestTransportFilters(FilterPtr filterPtr);
235  void requestTransportFilters();
236 
237  void clearTransportFilters();
238 
239  // Asynchronous transport filter requests.
240  void requestTransportFiltersAsync_Legacy(
241  const std::vector<FilterPtr> &filters,
242  boost::function0<void> onCompletion);
243 
244  void requestTransportFiltersAsync(
245  const std::vector<FilterPtr> &filters,
246  boost::function0<void> onCompletion);
247 
248  void requestTransportFiltersAsync(
249  FilterPtr filterPtr,
250  boost::function0<void> onCompletion);
251 
252 
253  void setRemoteCallTimeoutMs(unsigned int remoteCallTimeoutMs);
254  unsigned int getRemoteCallTimeoutMs() const;
255 
256  void setConnectTimeoutMs(unsigned int connectTimeoutMs);
257  unsigned int getConnectTimeoutMs() const;
258 
259  void setAutoReconnect(bool autoReconnect);
260  bool getAutoReconnect() const;
261 
262  void setAutoVersioning(bool autoVersioning);
263  bool getAutoVersioning() const;
264 
265  void setRuntimeVersion(boost::uint32_t version);
266  boost::uint32_t getRuntimeVersion() const;
267 
268  void setArchiveVersion(boost::uint32_t version);
269  boost::uint32_t getArchiveVersion() const;
270 
271  void setClientProgressPtr(ClientProgressPtr clientProgressPtr);
272 
273  ClientProgressPtr getClientProgressPtr() const;
274 
275  typedef ClientProgress::ProgressCallback RemoteCallProgressCallback;
276 
277  void setRemoteCallProgressCallback(
278  RemoteCallProgressCallback cb,
279  boost::uint32_t callbackIntervalMs);
280 
281  void setTries(std::size_t tries);
282  std::size_t getTries() const;
283 
284  void setUserData(boost::any userData);
285  boost::any getUserData();
286 
287  //**********************************************************************
288  // These functions involve network calls.
289 
290  // Synchronous versions.
291 
292 #if RCF_FEATURE_LEGACY==1
293 
294  void createRemoteObject(const std::string &objectName = "");
295  void deleteRemoteObject();
296 
297  void createRemoteSessionObject(const std::string &objectName = "");
298  void deleteRemoteSessionObject();
299 
300 #endif
301 
302 #if RCF_FEATURE_FILETRANSFER==1
303  void setFileProgressCallback(FileProgressCb fileProgressCb);
304  void setFileProgressCallback() { setFileProgressCallback( FileProgressCb() ); }
305 
306  void uploadFiles(
307  const std::string & whichFile,
308  std::string & uploadId,
309  boost::uint32_t chunkSize,
310  boost::uint32_t transferRateBps,
311  boost::uint32_t sessionLocalId);
312 
313  void uploadFiles(
314  const FileManifest & whichFile,
315  std::string & uploadId,
316  boost::uint32_t chunkSize,
317  boost::uint32_t transferRateBps,
318  boost::uint32_t sessionLocalId);
319 
320  void downloadFiles(
321  const std::string & downloadLocation,
322  FileManifest & manifest,
323  boost::uint32_t chunkSize,
324  boost::uint32_t transferRateBps,
325  boost::uint32_t sessionLocalId);
326 
327  boost::uint32_t addUploadStream(FileUpload fileStream);
328  void processUploadStreams();
329 
330  boost::uint32_t addDownloadStream(FileDownload fileStream);
331 
332  // For testing.
333  void setTransferWindowS(boost::uint32_t transferWindowS);
334  boost::uint32_t getTransferWindowS();
335 #endif
336 
337  FutureImpl<Void> ping();
338  FutureImpl<Void> ping(const CallOptions & callOptions);
339 
340  ByteBuffer getOutOfBandRequest();
341  void setOutofBandRequest(ByteBuffer requestBuffer);
342 
343  ByteBuffer getOutOfBandResponse();
344  void setOutofBandResponse(ByteBuffer responseBuffer);
345 
346  FutureImpl<Void> doControlMessage(
347  const CallOptions & callOptions,
348  ByteBuffer controlRequest);
349 
350  //**********************************************************************
351 
352  void setPingBackIntervalMs(int pingBackIntervalMs);
353  int getPingBackIntervalMs();
354 
355  std::size_t getPingBackCount();
356  boost::uint32_t getPingBackTimeStamp();
357 
358  void clearParameters();
359 
360  SerializationProtocolIn & getSpIn();
361  SerializationProtocolOut & getSpOut();
362 
363  std::auto_ptr<Exception> getAsyncException();
364  void setAsyncException(std::auto_ptr<Exception>);
365  bool hasAsyncException();
366 
367  boost::uint32_t generatePollingTimeout(boost::uint32_t timeoutMs);
368  void onPollingTimeout();
369  void onUiMessage();
370 
371  friend class CallOptions;
372 
373  private:
374 
375  friend class FutureImplBase;
376 
377  template<
378  typename R,
379  typename A1,
380  typename A2,
381  typename A3,
382  typename A4,
383  typename A5,
384  typename A6,
385  typename A7,
386  typename A8,
387  typename A9,
388  typename A10,
389  typename A11,
390  typename A12,
391  typename A13,
392  typename A14,
393  typename A15>
394  friend class AllocateClientParameters;
395 
396  template<
397  typename R,
398  typename A1,
399  typename A2,
400  typename A3,
401  typename A4,
402  typename A5,
403  typename A6,
404  typename A7,
405  typename A8,
406  typename A9,
407  typename A10,
408  typename A11,
409  typename A12,
410  typename A13,
411  typename A14,
412  typename A15>
413  friend class ClientParameters;
414 
415  Token mToken;
416  RemoteCallSemantics mDefaultCallingSemantics;
417  SerializationProtocol mProtocol;
418  MarshalingProtocol mMarshalingProtocol;
419  std::string mEndpointName;
420  std::string mObjectName;
421  std::string mInterfaceName;
422 
423  unsigned int mRemoteCallTimeoutMs;
424  unsigned int mConnectTimeoutMs;
425  bool mAutoReconnect;
426  bool mConnected;
427  std::size_t mTries;
428 
429  EndpointPtr mEndpoint;
430  ClientTransportAutoPtr mTransport;
431 
432  VectorFilter mMessageFilters;
433 
434  ClientProgressPtr mClientProgressPtr;
435 
436  bool mAutoVersioning;
437  boost::uint32_t mRuntimeVersion;
438  boost::uint32_t mArchiveVersion;
439 
440  bool mUseNativeWstringSerialization;
441  bool mEnableSfPointerTracking;
442 
443  std::vector<I_Future *> mFutures;
444  boost::any mUserData;
445 
446  MethodInvocationRequest mRequest;
447  SerializationProtocolIn mIn;
448  SerializationProtocolOut mOut;
449 
450  bool mAsync;
451  AsyncOpType mAsyncOpType;
452  boost::function0<void> mAsyncCallback;
453  std::auto_ptr<Exception> mAsyncException;
454  unsigned int mEndTimeMs;
455  bool mRetry;
456  RemoteCallSemantics mRcs;
457  ByteBuffer mEncodedByteBuffer;
458  std::vector<ByteBuffer> mEncodedByteBuffers;
459 
460  std::vector<char> mRetValVec;
461  std::vector<char> mParametersVec;
462  I_Parameters * mpParameters;
463 
464  boost::uint32_t mPingBackIntervalMs;
465  boost::uint32_t mPingBackTimeStamp;
466  std::size_t mPingBackCount;
467 
468  boost::uint32_t mNextTimerCallbackMs;
469  boost::uint32_t mNextPingBackCheckMs;
470  boost::uint32_t mPingBackCheckIntervalMs;
471  boost::uint32_t mTimerIntervalMs;
472 
473  MutexPtr mSignalledMutexPtr;
474  ConditionPtr mSignalledConditionPtr;
475  LockPtr mSignalledLockPtr;
476 
477  bool mSignalled;
478 
479  Mutex mSubRcfClientMutex;
480  RcfClientPtr mSubRcfClientPtr;
481 
482  bool mBatchMode;
483  ReallocBufferPtr mBatchBufferPtr;
484  ReallocBuffer mBatchBufferTemp;
485  boost::uint32_t mBatchMaxMessageLength;
486  boost::uint32_t mBatchCount;
487  boost::uint32_t mBatchMessageCount;
488 
489  // Set to true if we need to set the transport protocol without disconnecting.
490  bool mSetTransportProtocol;
491 
492  void enrol(
493  I_Future *pFuture);
494 
495  void init(
496  const std::string & interfaceName,
497  int fnId,
498  RCF::RemoteCallSemantics rcs);
499 
500  void beginCall();
501 
502  void beginSend();
503 
504  void beginReceive();
505 
506  void call(
507  RCF::RemoteCallSemantics rcs);
508 
509  void onConnectCompleted(
510  bool alreadyConnected = false);
511 
512  void onRequestTransportFiltersCompleted();
513 
514  void doBatching();
515 
516  void onSendCompleted();
517 
518  void onReceiveCompleted();
519 
520  void onTimerExpired();
521 
522  void onError(
523  const std::exception &e);
524 
525  void setAsyncCallback(
526  boost::function0<void> callback);
527 
528  void onException(
529  const Exception & e);
530 
531  void scheduleAmiNotification();
532 
533  void createFilterSequence(std::vector<FilterPtr> & filters);
534 
535 
536  public:
537 
538  std::vector<char> & getRetValVec() { return mRetValVec; }
539 
540  // Batching
541 
542  void enableBatching();
543  void disableBatching(bool flush = true);
544  void flushBatch(unsigned int timeoutMs = 0);
545 
546  void setMaxBatchMessageLength(boost::uint32_t maxBatchMessageLength);
547  boost::uint32_t getMaxBatchMessageLength();
548 
549  boost::uint32_t getBatchesSent();
550  boost::uint32_t getMessagesInCurrentBatch();
551 
552  // Async
553 
554  void setAsync(bool async);
555  bool getAsync();
556 
557  bool ready();
558  void waitForReady(boost::uint32_t timeoutMs = 0);
559  void cancel();
560 
561  void setSubRcfClientPtr(RcfClientPtr clientStubPtr);
562  RcfClientPtr getSubRcfClientPtr();
563 
564  // User data
565 
566  void setRequestUserData(const std::string & userData);
567  std::string getRequestUserData();
568 
569  void setResponseUserData(const std::string & userData);
570  std::string getResponseUserData();
571 
572 #if RCF_FEATURE_FILETRANSFER==1
573  FileProgressCb mFileProgressCb;
574 
575  std::vector<FileUpload> mUploadStreams;
576  std::vector<FileDownload> mDownloadStreams;
577 #endif
578 
579  boost::uint32_t mTransferWindowS;
580 
581  RecursionState<int, int> mRecursionState;
582 
583  std::string mCurrentCallDesc;
584  bool mCallInProgress;
585 
586  public:
587 
588  void setHttpProxy(const std::string & httpProxy);
589  std::string getHttpProxy() const;
590 
591  void setHttpProxyPort(int httpProxyPort);
592  int getHttpProxyPort() const;
593 
594  void setTransportProtocol(TransportProtocol protocol);
595  TransportProtocol getTransportProtocol() const;
596 
597  TransportType getTransportType();
598 
599  void setUsername(const tstring & username);
600  tstring getUsername() const;
601 
602  void setPassword(const tstring & password);
603  tstring getPassword() const;
604 
605  void setHttpProxyUsername(const tstring & proxyUsername);
606  tstring getHttpProxyUsername() const;
607 
608  void setHttpProxyPassword(const tstring & proxyPassword);
609  tstring getHttpProxyPassword() const;
610 
611  void setHttpProxyRealm(const tstring & proxyRealm);
612  tstring getHttpProxyRealm() const;
613 
614  void setKerberosSpn(const tstring & kerberosSpn);
615  tstring getKerberosSpn() const;
616 
617  void setEnableCompression(bool enableCompression);
618  bool getEnableCompression() const;
619 
620  void setCertificate(CertificatePtr certificatePtr);
621  CertificatePtr getCertificate() const;
622 
623  void setCaCertificate(CertificatePtr certificatePtr);
624  CertificatePtr getCaCertificate() const;
625 
626  void setOpenSslCipherSuite(const std::string & cipherSuite);
627  std::string getOpenSslCipherSuite() const;
628 
629  void setEnableSchannelCertificateValidation(const tstring & peerName);
630  tstring getEnableSchannelCertificateValidation() const;
631 
632  typedef boost::function<bool(Certificate *)> CertificateValidationCb;
633  void setCertificateValidationCallback(CertificateValidationCb certificateValidationCb);
634  const CertificateValidationCb & getCertificateValidationCallback() const;
635 
636  void setSslImplementation(SslImplementation sslImplementation);
637  SslImplementation getSslImplementation() const;
638 
639 #ifdef BOOST_WINDOWS
640  void setWindowsImpersonationToken(HANDLE hToken);
641  HANDLE getWindowsImpersonationToken() const;
642 
643  private:
644  std::auto_ptr<HANDLE> mWindowsImpersonationToken;
645 #endif
646 
647  private:
648  std::string mHttpProxy;
649  int mHttpProxyPort;
650  tstring mHttpProxyRealm;
651  TransportProtocol mTransportProtocol;
652  tstring mUsername;
653  tstring mPassword;
654  tstring mHttpProxyUsername;
655  tstring mHttpProxyPassword;
656  tstring mKerberosSpn;
657  bool mEnableCompression;
658 
659  CertificatePtr mCertificatePtr;
660  CertificatePtr mCaCertificatePtr;
661  CertificateValidationCb mCertificateValidationCb;
662  tstring mSchannelCertificateValidation;
663 
664  std::string mOpenSslCipherSuite;
665 
666  SslImplementation mSslImplementation;
667  };
668 
669  class CallOptions
670  {
671  public:
672  CallOptions() :
673  mAsync(false),
674  mRcsSpecified(false),
675  mRcs(Twoway),
676  mCallback()
677  {}
678 
679  CallOptions(RemoteCallSemantics rcs) :
680  mAsync(false),
681  mRcsSpecified(true),
682  mRcs(rcs),
683  mCallback()
684  {}
685 
686  CallOptions(RemoteCallSemantics rcs, const boost::function0<void> & callback) :
687  mAsync(true),
688  mRcsSpecified(true),
689  mRcs(rcs),
690  mCallback(callback)
691  {}
692 
693  CallOptions(boost::function0<void> callback) :
694  mAsync(true),
695  mRcsSpecified(false),
696  mRcs(Twoway),
697  mCallback(callback)
698  {}
699 
700  RemoteCallSemantics apply(ClientStub &clientStub) const
701  {
702  clientStub.setAsync(mAsync);
703  clientStub.setAsyncCallback(mCallback);
704  return mRcsSpecified ? mRcs : clientStub.getRemoteCallSemantics();
705  }
706 
707  private:
708  bool mAsync;
709  bool mRcsSpecified;
710  RemoteCallSemantics mRcs;
711  boost::function0<void> mCallback;
712  };
713 
714  class RCF_EXPORT AsyncTwoway : public CallOptions
715  {
716  public:
717  AsyncTwoway(const boost::function0<void> & callback) :
718  CallOptions(RCF::Twoway, callback)
719  {}
720  };
721 
722  class RCF_EXPORT AsyncOneway : public CallOptions
723  {
724  public:
725  AsyncOneway(const boost::function0<void> & callback) :
726  CallOptions(RCF::Oneway, callback)
727  {}
728  };
729 
730  class RestoreClientTransportGuard
731  {
732  public:
733 
734  RestoreClientTransportGuard(ClientStub &client, ClientStub &clientTemp);
735  ~RestoreClientTransportGuard();
736 
737  private:
738  ClientStub &mClient;
739  ClientStub &mClientTemp;
740  };
741 
742 } // namespace RCF
743 
744 #endif // ! INCLUDE_RCF_CLIENTSTUB_HPP