Remote Call Framework 3.1
FileTransferService.hpp
1 
2 //******************************************************************************
3 // RCF - Remote Call Framework
4 //
5 // Copyright (c) 2005 - 2019, 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.1
15 // Contact: support <at> deltavsoft.com
16 //
17 //******************************************************************************
18 
19 #ifndef INCLUDE_RCF_FILETRANSFERSERVICE_HPP
20 #define INCLUDE_RCF_FILETRANSFERSERVICE_HPP
21 
22 #include <map>
23 #include <set>
24 
25 #include <RCF/FileStream.hpp>
26 #include <RCF/RcfFwd.hpp>
27 #include <RCF/Service.hpp>
28 
29 #include <SF/vector.hpp>
30 #include <SF/map.hpp>
31 
32 #include <RCF/FileSystem.hpp>
33 
34 #if RCF_FEATURE_SF==1
35 
36 namespace SF {
37 
38  RCF_EXPORT void serialize(SF::Archive &ar, RCF::Path &p);
39 
40 } // namespace SF
41 
42 #endif
43 
44 namespace RCF {
45 
46  class FileTransferService;
47 
48  class FileUploadInfo;
49  class FileDownloadInfo;
50 
51  class RcfSession;
52 
53  class FileUploadInfo;
54  class FileDownloadInfo;
55 
56  typedef std::shared_ptr<FileUploadInfo> FileUploadInfoPtr;
57  typedef std::shared_ptr<FileDownloadInfo> FileDownloadInfoPtr;
58 
59  typedef std::function<bool(const FileUploadInfo &)> UploadAccessCallback;
60  typedef std::function<bool(const FileDownloadInfo &)> DownloadAccessCallback;
61 
62  class FileIoRequest;
63  typedef std::shared_ptr<FileIoRequest> FileIoRequestPtr;
64 
66  class FileUploadInfo : Noncopyable
67  {
68  public:
70  ~FileUploadInfo();
71 
72  std::string getUploadId() const;
73 
74  FileManifest mManifest;
75  std::uint32_t mCurrentFile;
76  std::uint64_t mCurrentPos;
77  Path mUploadDir;
78 
79  private:
80 
81  friend class FileTransferService;
82 
83  FileHandlePtr mFileHandle;
84  Path mFinalFilePath;
85  FileIoRequestPtr mWriteOp;
86 
87  bool mCompleted;
88  bool mResume;
89  std::uint32_t mTimeStampMs;
90 
91  std::uint32_t mSessionLocalId;
92  std::string mUploadId;
93 
94  BandwidthQuotaPtr mQuotaPtr;
95  };
96 
98  class FileDownloadInfo : Noncopyable
99  {
100  public:
102  ~FileDownloadInfo();
103 
104  Path mDownloadPath;
105  FileManifest mManifest;
106  std::uint32_t mCurrentFile;
107  std::uint64_t mCurrentPos;
108  bool mCancel;
109 
110  private:
111 
112  friend class FileTransferService;
113 
114  FileHandlePtr mFileHandle;
115  Path mFileHandlePath;
116  FileIoRequestPtr mReadOp;
117  ByteBuffer mReadBuffer;
118  ByteBuffer mSendBuffer;
119  ByteBuffer mSendBufferRemaining;
120 
121  bool mResume;
122 
123  Timer mTransferWindowTimer;
124  std::uint32_t mTransferWindowBytesSoFar;
125  std::uint32_t mTransferWindowBytesTotal;
126 
127  std::uint32_t mSessionLocalId;
128  std::string mServerDownloadId;
129 
130  BandwidthQuotaPtr mQuotaPtr;
131  };
132 
133  typedef std::shared_ptr<FileUploadInfo> FileUploadInfoPtr;
134  typedef std::shared_ptr<FileDownloadInfo> FileDownloadInfoPtr;
135 
136  class FileChunk;
137  class FileTransferRequest;
138 
139  class TransferInfo
140  {
141  public:
142  Path mPath;
143  BandwidthQuotaPtr mBandwidthQuotaPtr;
144  };
145 
146  class RCF_EXPORT FileTransferService : public I_Service
147  {
148  public:
149  FileTransferService();
150 
151  typedef std::function<BandwidthQuotaPtr(RcfSession &)> BandwidthQuotaCallback;
152  typedef BandwidthQuotaCallback UploadQuotaCallback;
153  typedef BandwidthQuotaCallback DownloadQuotaCallback;
154 
155  // For testing.
156  void setTransferWindowS(std::uint32_t transferWindowS);
157  std::uint32_t getTransferWindowS();
158 
159  //----------------------------------------------------------------------
160  // Remotely accessible.
161 
162  void BeginUpload(
163  const FileManifest & manifest,
164  const std::vector<FileChunk> & chunks,
165  FileChunk & startPos,
166  std::uint32_t & maxMessageLength,
167  std::string & uploadId,
168  std::uint32_t & bps,
169  std::uint32_t sessionLocalId);
170 
171  void UploadChunks(
172  const std::vector<FileChunk> & chunks,
173  std::uint32_t & bps);
174 
175  void BeginDownload(
176  FileManifest & manifest,
177  const FileTransferRequest & request,
178  std::vector<FileChunk> & chunks,
179  std::uint32_t & maxMessageLength,
180  std::uint32_t & bps,
181  std::uint32_t sessionLocalId,
182  const std::string & serverDownloadId);
183 
184  void TrimDownload(
185  const FileChunk & startPos);
186 
187  void DownloadChunks(
188  const FileTransferRequest & request,
189  std::vector<FileChunk> & chunks,
190  std::uint32_t & adviseWaitMs,
191  std::uint32_t & bps);
192 
193  //----------------------------------------------------------------------
194 
195  private:
196 
197  friend class RcfServer;
198  friend class RcfSession;
199 
200  void addFileTransfer(const std::string & transferId, const TransferInfo & transferInfo);
201  void removeFileTransfer(const std::string & transferId);
202  TransferInfo findFileTransfer(const std::string & transferId);
203 
204  Mutex mFileTransfersInProgressMutex;
205  std::map<std::string, TransferInfo> mFileTransfersInProgress;
206 
207  void checkForUploadCompletion(FileUploadInfoPtr uploadInfoPtr);
208 
209 
210 
211  void onServerStart(RcfServer & server);
212  void onServerStop(RcfServer & server);
213 
214  Path mUploadDirectory;
215 
216  DownloadProgressCallback mDownloadProgressCb;
217  UploadProgressCallback mUploadProgressCb;
218 
219  std::uint32_t mTransferWindowS;
220 
221  BandwidthQuotaPtr mUploadQuota;
222  BandwidthQuotaPtr mDownloadQuota;
223 
224  BandwidthQuotaCallback mUploadQuotaCallback;
225  BandwidthQuotaCallback mDownloadQuotaCallback;
226  };
227 
228  typedef std::shared_ptr<FileTransferService> FileTransferServicePtr;
229 
230 } // namespace RCF
231 
232 #endif // ! INCLUDE_RCF_FILETRANSFERSERVICE_HPP
Represents an archive, in which serialized objects are stored.
Definition: Archive.hpp:32
RCF_FILESYSTEM_NS::path Path
Typedef for standard C++ path type.
Definition: FileSystem.hpp:35
std::function< BandwidthQuotaPtr(RcfSession &)> BandwidthQuotaCallback
Describes user-provided callback functions for assigning custom bandwidth quotas to a RcfSession...
Definition: RcfFwd.hpp:140
std::function< void(RcfSession &, FileDownloadInfo &)> DownloadProgressCallback
Describes user-provided callback functions for server-side monitoring of a file download.
Definition: RcfFwd.hpp:134
Represents a server side session, associated with a client connection.
Definition: RcfSession.hpp:67
std::shared_ptr< BandwidthQuota > BandwidthQuotaPtr
Reference counted wrapper for RCF::BandwidthQuota.
Definition: RcfFwd.hpp:127
Server-side information about a file download taking place from a RcfServer.
Definition: FileTransferService.hpp:98
Definition: ByteBuffer.hpp:189
Provides RCF server-side functionality.
Definition: RcfServer.hpp:54
std::function< void(RcfSession &, FileUploadInfo &)> UploadProgressCallback
Describes user-provided callback functions for server-side monitoring of a file upload.
Definition: RcfFwd.hpp:137
Definition: ByteBuffer.hpp:40
Definition: AmiIoHandler.hpp:24
Server-side information about a file upload taking place to a RcfServer.
Definition: FileTransferService.hpp:66