nx_server_plugin_sdk 1.0 Server Plugin SDK

ftp_library.h
Go to the documentation of this file.
1 // Copyright 2018-present Network Optix, Inc. Licensed under MPL 2.0: www.mozilla.org/MPL/2.0/
2
3 #pragma once
4
41 #include <atomic>
42 #include <memory>
43 #include <mutex>
44 #include <stdexcept>
45 #include <string>
46 #include <vector>
47
48 #include <stdint.h>
49
50 #include "ftplibpp/ftplib.h"
51 #include "storage/third_party_storage.h"
52
53 namespace nx_spl
54 {
55 namespace aux
56 { // Generic reference counter Mix-In. Private inherit it.
57 template <typename P>
58 class PluginRefCounter
59 {
60 public:
61 PluginRefCounter()
62 : m_count(1)
63 {}
64
65 int p_addRef() const { return ++m_count; }
66
67 int p_releaseRef() const
68 {
69 int new_count = --m_count;
70 if (new_count <= 0) {
71 delete static_cast<const P*>(this);
72 }
73 return new_count;
74 }
75 private:
76 mutable std::atomic<int> m_count;
77 }; // class PluginRefCounter
78
79 class NonCopyable
80 {
81 public:
82 NonCopyable() {}
83 NonCopyable(const NonCopyable&);
84 NonCopyable& operator =(const NonCopyable&);
85
86 NonCopyable(NonCopyable&&);
87 NonCopyable& operator =(NonCopyable&&);
88 }; // class NonCopyable
89
95 struct FileNameAndPath
96 {
97 std::string name;
98 std::string fullPath;
99 };
100
101 class FtpLibWrapper;
102
103 } //namespace aux
104
105 using FtpImplPtr = std::shared_ptr<aux::FtpLibWrapper>;
106 class FtpStorage;
107 // At construction phase we synchronize remote file with local one.
108 // During destruction synchronization attempt is repeated.
109 // All intermediate actions (read/write/seek) are made with the local copy.
110 class FtpIODevice
111 : public IODevice,
112 private aux::NonCopyable,
113 private aux::PluginRefCounter<FtpIODevice>
114 {
115 friend class aux::PluginRefCounter<FtpIODevice>;
116 public:
117 FtpIODevice(FtpImplPtr ftpImplPtr, const char *uri, int mode);
118
119 virtual uint32_t STORAGE_METHOD_CALL write(
120 const void* src,
121 const uint32_t size,
122 int* ecode
123 ) override;
124
125 virtual uint32_t STORAGE_METHOD_CALL read(
126 void* dst,
127 const uint32_t size,
128 int* ecode
129 ) const override;
130
131 virtual int STORAGE_METHOD_CALL seek(
132 uint64_t pos,
133 int* ecode
134 ) override;
135
136 virtual int STORAGE_METHOD_CALL getMode() const override;
137 virtual uint32_t STORAGE_METHOD_CALL size(int* ecode) const override;
138
139 public: // plugin interface implementation
140 virtual void* queryInterface(const nxpl::NX_GUID& interfaceID) override;
141
142 virtual int addRef() const override;
143 virtual int releaseRef() const override;
144
145 private:
146 // synchronize local file with remote one
147 void flush();
148 // delete only via releaseRef()
149 ~FtpIODevice();
150
151 private:
152 FtpImplPtr m_impl;
153 int m_mode;
154 mutable int64_t m_pos;
155 std::string m_uri; //file URI
156 aux::FileNameAndPath m_localfile;
157 bool m_altered;
158 long long m_localsize;
159 mutable std::mutex m_mutex;
160 std::string m_implurl;
161 std::string m_user;
162 std::string m_passwd;
163 };
164
165 // Fileinfo list is obtained from the server at construction phase.
166 // After this phase there are no real interactions with FTP server.
167 class FtpFileInfoIterator
168 : public FileInfoIterator,
169 private aux::NonCopyable,
170 private aux::PluginRefCounter<FtpFileInfoIterator>
171 {
172 friend class aux::PluginRefCounter<FtpFileInfoIterator>;
173
174 typedef std::vector<std::string> FileListType;
175 typedef FileListType::const_iterator FileListIteratorType;
176 public:
177 FtpFileInfoIterator(FtpImplPtr impl, FileListType&& fileList);
178 virtual FileInfo* STORAGE_METHOD_CALL next(int* ecode) const override;
179
180 public: // plugin interface implementation
181 virtual void* queryInterface(const nxpl::NX_GUID& interfaceID) override;
182
183 virtual int addRef() const override;
184 virtual int releaseRef() const override;
185
186 private:
187 ~FtpFileInfoIterator();
188
189 private:
190 FtpImplPtr m_impl;
191 mutable FileInfo m_fileInfo;
192 FileListType m_fileList;
193 mutable FileListIteratorType m_curFile;
194 }; // class FtpFileListIterator
195
196 class FtpStorage
197 : public Storage,
198 private aux::NonCopyable,
199 private aux::PluginRefCounter<FtpStorage>
200 {
201 friend class aux::PluginRefCounter<FtpStorage>;
202
203 public:
204 FtpStorage(const std::string& url);
205
206 public: // Storage interface implementation
207 virtual int STORAGE_METHOD_CALL isAvailable() const override;
208
209 virtual IODevice* STORAGE_METHOD_CALL open(
210 const char* uri,
211 int flags,
212 int* ecode
213 ) const override;
214
215 virtual uint64_t STORAGE_METHOD_CALL getFreeSpace(int* ecode) const override;
216 virtual uint64_t STORAGE_METHOD_CALL getTotalSpace(int* ecode) const override;
217 virtual int STORAGE_METHOD_CALL getCapabilities() const override;
218
219 virtual void STORAGE_METHOD_CALL removeFile(
220 const char* url,
221 int* ecode
222 ) override;
223
224 virtual void STORAGE_METHOD_CALL removeDir(
225 const char* url,
226 int* ecode
227 ) override;
228
229 virtual void STORAGE_METHOD_CALL renameFile(
230 const char* oldUrl,
231 const char* newUrl,
232 int* ecode
233 ) override;
234
235 virtual FileInfoIterator* STORAGE_METHOD_CALL getFileIterator(
236 const char* dirUrl,
237 int* ecode
238 ) const override;
239
240 virtual int STORAGE_METHOD_CALL fileExists(
241 const char* url,
242 int* ecode
243 ) const override;
244
245 virtual int STORAGE_METHOD_CALL dirExists(
246 const char* url,
247 int* ecode
248 ) const override;
249
250 virtual uint64_t STORAGE_METHOD_CALL fileSize(
251 const char* url,
252 int* ecode
253 ) const override;
254
255 public: // plugin interface implementation
256 virtual void* queryInterface(const nxpl::NX_GUID& interfaceID) override;
257
258 virtual int addRef() const override;
259 virtual int releaseRef() const override;
260
261 private:
262 // destroy only via releaseRef()
263 ~FtpStorage();
264
265 private:
266 FtpImplPtr m_impl;
267 mutable std::mutex m_mutex;
268 mutable int m_capabilities = 0;
269 }; // class Ftpstorage
270
271 class FtpStorageFactory
272 : public StorageFactory,
273 private aux::NonCopyable,
274 private aux::PluginRefCounter<FtpStorageFactory>
275 {
276 friend class aux::PluginRefCounter<FtpStorageFactory>;
277 public:
278 FtpStorageFactory();
279 // currently unimplemented
280 virtual const char** STORAGE_METHOD_CALL findAvailable() const override;
281
282 virtual Storage* STORAGE_METHOD_CALL createStorage(
283 const char* url,
284 int* ecode
285 ) override;
286
287 virtual const char* STORAGE_METHOD_CALL storageType() const override;
288 virtual const char* lastErrorMessage(int ecode) const override;
289
290 public: // plugin interface implementation
291 virtual void* queryInterface(const nxpl::NX_GUID& interfaceID) override;
292
293 virtual int addRef() const override;
294 virtual int releaseRef() const override;
295 }; // class FtpStorageFactory
296 } // namespace Qn
nx_spl::FtpStorage::removeDir
virtual void STORAGE_METHOD_CALL removeDir(const char *url, int *ecode) override
Definition: ftp_library.cpp:807
nx_spl::FtpIODevice::releaseRef
virtual int releaseRef() const override
Decrement reference counter.
Definition: ftp_library.cpp:1198
nx_spl::FtpStorage::getFileIterator
virtual FileInfoIterator *STORAGE_METHOD_CALL getFileIterator(const char *dirUrl, int *ecode) const override
Definition: ftp_library.cpp:819
nx_spl::aux::PluginRefCounter
Definition: ftp_library.h:58
nx_spl::FtpStorage::removeFile
virtual void STORAGE_METHOD_CALL removeFile(const char *url, int *ecode) override
Definition: ftp_library.cpp:801
nx_spl::FtpFileInfoIterator
Definition: ftp_library.h:167
nx_spl::FtpStorage::addRef
virtual int addRef() const override
Increment reference counter.
Definition: ftp_library.cpp:902
nx_spl::FtpStorage::dirExists
virtual int STORAGE_METHOD_CALL dirExists(const char *url, int *ecode) const override
Definition: ftp_library.cpp:846
nx_spl::FileInfoIterator
File information iterator abstraction.
Definition: third_party_storage.h:138
nx_spl::FtpStorageFactory
Definition: ftp_library.h:271
nx_spl::FtpIODevice
Definition: ftp_library.h:110
nx_spl::StorageFactory
Storage factory abstraction.
Definition: third_party_storage.h:273
nx_spl::FtpStorageFactory::createStorage
virtual Storage *STORAGE_METHOD_CALL createStorage(const char *url, int *ecode) override
Definition: ftp_library.cpp:953
nx_spl::FtpStorageFactory::lastErrorMessage
virtual const char * lastErrorMessage(int ecode) const override
Definition: ftp_library.cpp:988
nx_spl::FtpIODevice::seek
virtual int STORAGE_METHOD_CALL seek(uint64_t pos, int *ecode) override
Definition: ftp_library.cpp:1134
nx_spl::aux::FtpLibWrapper
Definition: ftp_library.cpp:394
nx_spl::FileInfo
Common file information.
Definition: third_party_storage.h:126
nx_spl
Storage plugin namespace.
Definition: ftp_library.cpp:29
nx_spl::FtpFileInfoIterator::addRef
virtual int addRef() const override
Increment reference counter.
Definition: ftp_library.cpp:726
nx_spl::FtpStorage::fileExists
virtual int STORAGE_METHOD_CALL fileExists(const char *url, int *ecode) const override
Definition: ftp_library.cpp:835
nx_spl::FtpStorage::getFreeSpace
virtual uint64_t STORAGE_METHOD_CALL getFreeSpace(int *ecode) const override
Definition: ftp_library.cpp:751
nx_spl::FtpStorage::isAvailable
virtual int STORAGE_METHOD_CALL isAvailable() const override
Definition: ftp_library.cpp:746
nx_spl::FtpStorageFactory::releaseRef
virtual int releaseRef() const override
Decrement reference counter.
Definition: ftp_library.cpp:942
nx_spl::FtpStorage
Definition: ftp_library.h:196
nx_spl::FtpStorage::getTotalSpace
virtual uint64_t STORAGE_METHOD_CALL getTotalSpace(int *ecode) const override
Definition: ftp_library.cpp:759
nxpl::NX_GUID
GUID of plugin interface.
Definition: plugin_api.h:26
nx_spl::aux::FileNameAndPath
Definition: ftp_library.h:95
nx_spl::FtpStorageFactory::queryInterface
virtual void * queryInterface(const nxpl::NX_GUID &interfaceID) override
Cast to type, specified by interfaceID.
Definition: ftp_library.cpp:918
nx_spl::Storage
Storage abstraction.
Definition: third_party_storage.h:155
nx_spl::FtpIODevice::write
virtual uint32_t STORAGE_METHOD_CALL write(const void *src, const uint32_t size, int *ecode) override
Definition: ftp_library.cpp:1063
nx_spl::FtpStorage::getCapabilities
virtual int STORAGE_METHOD_CALL getCapabilities() const override
Definition: ftp_library.cpp:767
nx_spl::FtpIODevice::queryInterface
virtual void * queryInterface(const nxpl::NX_GUID &interfaceID) override
Cast to type, specified by interfaceID.
Definition: ftp_library.cpp:1174
nx_spl::FtpFileInfoIterator::next
virtual FileInfo *STORAGE_METHOD_CALL next(int *ecode) const override
Definition: ftp_library.cpp:689
nx_spl::FtpStorageFactory::storageType
virtual const char *STORAGE_METHOD_CALL storageType() const override
Definition: ftp_library.cpp:970
nx_spl::FtpStorage::open
virtual IODevice *STORAGE_METHOD_CALL open(const char *uri, int flags, int *ecode) const override
Definition: ftp_library.cpp:870
nx_spl::FtpIODevice::size
virtual uint32_t STORAGE_METHOD_CALL size(int *ecode) const override
Definition: ftp_library.cpp:1158
nx_spl::aux::NonCopyable
Definition: ftp_library.h:79
nx_spl::FtpStorageFactory::addRef
virtual int addRef() const override
Increment reference counter.
Definition: ftp_library.cpp:937
nx_spl::FtpStorage::queryInterface
virtual void * queryInterface(const nxpl::NX_GUID &interfaceID) override
Cast to type, specified by interfaceID.
Definition: ftp_library.cpp:884
nx_spl::FtpStorage::renameFile
virtual void STORAGE_METHOD_CALL renameFile(const char *oldUrl, const char *newUrl, int *ecode) override
Definition: ftp_library.cpp:813
nx_spl::FtpStorage::releaseRef
virtual int releaseRef() const override
Decrement reference counter.
Definition: ftp_library.cpp:907
nx_spl::FtpStorage::fileSize
virtual uint64_t STORAGE_METHOD_CALL fileSize(const char *url, int *ecode) const override
Definition: ftp_library.cpp:857
nx_spl::FtpIODevice::getMode
virtual int STORAGE_METHOD_CALL getMode() const override
Definition: ftp_library.cpp:1153
nx_spl::FtpIODevice::read
virtual uint32_t STORAGE_METHOD_CALL read(void *dst, const uint32_t size, int *ecode) const override
Definition: ftp_library.cpp:1096
nx_spl::FtpFileInfoIterator::releaseRef
virtual int releaseRef() const override
Decrement reference counter.
Definition: ftp_library.cpp:731
nx_spl::FtpFileInfoIterator::queryInterface
virtual void * queryInterface(const nxpl::NX_GUID &interfaceID) override
Cast to type, specified by interfaceID.
Definition: ftp_library.cpp:707
nx_spl::FtpIODevice::addRef
virtual int addRef() const override
Increment reference counter.
Definition: ftp_library.cpp:1193
nx_spl::IODevice
IO device abstraction.
Definition: third_party_storage.h:73
nx_spl::FtpStorageFactory::findAvailable
virtual const char **STORAGE_METHOD_CALL findAvailable() const override
Definition: ftp_library.cpp:947