Transfer Ease
Loading...
Searching...
No Matches
tease::TransferManager Class Reference

Use to perform download/upload of ressources easily. More...

Public Types

enum  IdError {
  ERR_NO_ERROR = 0 , ERR_INTERNAL , ERR_INVALID_LOGIN , ERR_INVALID_REQUEST ,
  ERR_INVALID_SSL , ERR_BUSY , ERR_USER_ABORT , ERR_MAX_TRIALS ,
  ERR_MEMORY_FULL_HOST , ERR_MEMORY_FULL_REMOTE , ERR_HOST_NOT_FOUND , ERR_HOST_REFUSED ,
  ERR_CONTENT_NOT_FOUND
}
 List of errors identifiers. More...
 
enum  FlagOption : std::uint32_t { OPT_NONE = 0 , OPT_VERBOSE = 1 << 0 , OPT_FTP_CREATE_DIRS = 1 << 1 }
 List of available options. More...
 
using CbStarted = std::function<void(Request::TypeTransfer typeTransfer)>
 Callback called when transfer has been started.
 
using CbProgress = std::function<void(Request::TypeTransfer typeTransfer, size_t transferTotal, size_t transferNow)>
 Callback called during transfer.
 
using CbCompleted = std::function<void(Request::TypeTransfer typeTransfer)>
 Callback called when transfer finished and succeed.
 
using CbFailed = std::function<void(Request::TypeTransfer typeTransfer, IdError idErr)>
 Callback called when transfer finished due to an error.
 

Public Member Functions

IdError startDownload (const Request::List &listReqs)
 Use to start downloading list of requests.
 
IdError startUpload (const Request::List &listReqs)
 Use to start upload list of requests.
 
void abortTransfer ()
 Use to abort current transfer.
 
bool transferIsInProgress () const
 Verify is a tranfer is in progress or not.
 
const std::string & getUserLogin () const
 Retrieve login information currently used.
 
const std::string & getUserPasswd () const
 Retrieve password information currently used.
 
int getNbMaxTrials () const
 Retrieve maximum number of trials currently set.
 
long getTimeoutConnection () const
 Retrieve connection timeout.
 
long getTimeoutTransfer () const
 Retrieve transfer timeout.
 
FlagOption getOptions () const
 Retrieve transfer options.
 
void setUserInfos (const std::string &username, const std::string &passwd)
 Use to set user informations.
 
void setNbMaxTrials (int nbTrials)
 Use to set maximum number of trials.
 
void setTimeoutConnection (long timeout)
 Use to set the maximum time in seconds that allow connection phase to take.
 
void setTimeoutTransfer (long timeout)
 Use to set the maximum time in seconds to wait when no data is received before considering a timeout.
 
void setOptions (FlagOption options)
 Use to set options of the transfer manager.
 
void setCbStarted (CbStarted fct)
 Use to set started transfer callback.
 
void setCbProgress (CbProgress fct)
 Use to set progress transfer callback.
 
void setCbCompleted (CbCompleted fct)
 Use to set completed transfer callback.
 
void setCbFailed (CbFailed fct)
 Use to set failed transfer callback.
 

Static Public Member Functions

static double transferProgressToPercent (size_t transferTotal, size_t transferNow)
 Use to convert progress data to a percentage.
 
static std::string flagOptionToStr (FlagOption options, char separator='|')
 Use to convert flags option to a string.
 
static const std::string & idErrorToStr (IdError idErr)
 

Detailed Description

Use to perform download/upload of ressources easily.

This class will allow to easily perform download/upload ressources from/to a remote.
A simple example used to download/upload a list of request:

1#include <iostream>
2#include <map>
3#include <thread>
4
5#include <transferease/transfermanager.h>
6
7/*****************************/
8/* Example details */
9/*****************************/
10
11/*
12 * Simple example to download/upload a list of requests.
13 * Note that default callback behaviour are used
14 * here to reduce example size.
15 */
16
17/*****************************/
18/* Macro definitions */
19/*****************************/
20
21#define CFG_HOST "0.0.0.0"
22#define CFG_USERNAME "myusername"
23#define CFG_PASSWD "mypasswd"
24#define CFG_MAX_TRIALS 1
25
26#define RUN_DL_ENABLE true // Only here to simplify example : set to "false" for upload
27
28/*****************************/
29/* Class aliases */
30/*****************************/
31
33using Manager = tease::TransferManager;
35using Url = tease::Url;
36
37/*****************************/
38/* Functions helpers */
39/*****************************/
40
41static void prepareRequestsDl(Request::List &listReqs)
42{
43 /* Create server ressources path */
44 const std::vector<std::string> listPaths =
45 {
46 "mypath/res/entity1.zip",
47 "mypath/res/entity2.zip",
48 "mypath/res/entity3.zip"
49 };
50
51 /* Prepare requests */
52 for(auto it = listPaths.cbegin(); it != listPaths.cend(); ++it){
53 Url url;
54 url.setIdScheme(Url::SCHEME_FTP);
55 url.setHost(CFG_HOST);
56 url.setPath(*it);
57
58 Request::PtrShared req = std::make_shared<Request>();
59 req->configureDownload(url);
60
61 listReqs.push_back(req);
62 }
63}
64
65static void prepareRequestsUp(Request::List &listReqs)
66{
67 /* Create ressources path */
68 const std::map<std::string, std::string> mapRrcs =
69 {
70 {"entity1.zip", "path/server/entity1.zip"},
71 {"entity2.zip", "path/server/entity2.zip"},
72 };
73
74 /* Prepare requests */
75 for(auto it = mapRrcs.cbegin(); it != mapRrcs.cend(); ++it){
76 // Load ressources to upload
77 BytesArray data;
78 data.setFromFile(it->first);
79
80 // Set destination URL
81 Url url;
82 url.setIdScheme(Url::SCHEME_FTP);
83 url.setHost(CFG_HOST);
84 url.setPath(it->second);
85
86 // Create associated request
87 Request::PtrShared req = std::make_shared<Request>();
88 req->configureUpload(url, std::move(data));
89
90 listReqs.push_back(req);
91 }
92}
93
94/*****************************/
95/* Main method */
96/*****************************/
97
98int main(int argc, char *argv[])
99{
100 Manager manager;
101 Request::List listReqs;
102
103 /* Configure transfer manager */
104 manager.setUserInfos(CFG_USERNAME, CFG_PASSWD);
105 manager.setNbMaxTrials(CFG_MAX_TRIALS);
106
107 /* Create requests and start transfer */
108#if RUN_DL_ENABLE
109 prepareRequestsDl(listReqs);
110 Manager::IdError idErr = manager.startDownload(listReqs);
111#else
112 prepareRequestsUp(listReqs);
113 Manager::IdError idErr = manager.startUpload(listReqs);
114#endif
115
116 if(idErr != Manager::ERR_NO_ERROR){
117 std::cerr << "Failed to start transfer [id-err: " << idErr << "]" << std::endl;
118 return 1;
119 }
120
121 /*
122 * Placeholder loop used to wait for the transfer to finish before exiting application.
123 * In a real application, prefer to use callbacks methods !
124 */
125 while(manager.transferIsInProgress()) {
126 std::this_thread::sleep_for(std::chrono::milliseconds(100));
127 }
128
129 std::cout << "Done" << std::endl;
130
131 return 0;
132}
Allow to store an array of bytes.
Definition bytesarray.h:15
Manage a ressource request informations.
Definition request.h:11
std::vector< PtrShared > List
Definition request.h:28
std::shared_ptr< Request > PtrShared
Definition request.h:27
Use to perform download/upload of ressources easily.
Definition transfermanager.h:20
Use to manage URLs.
Definition url.h:13
Note
For a more real-world example, we can refer to application using this library at:
https://github.com/legerch/TransferEaseApp

This class allow to register custom callbacks using std::function type. We have multiple ways to register a callback function:

/* Using lambda */
transferManager.setCbFailed([](tease::Request::TypeTransfer typeTransfer, tease::TransferManager::IdError idErr){
std::cerr << "Failed to perform transfer [type: " << typeTransfer << ", id-err: " << idErr << "]" << std::endl;
});
/* Using class methods : static or member */
class CustomClass
{
public:
CustomClass()
{
m_transferManager.setCbFailed(myMethodStaticForFailure); // Register static method
m_transferManager.setCbFailed(std::bind(&CustomClass::myMethodMemberForFailure, this, std::placeholders::_1, std::placeholders::_2)); // Register member method
}
private:
void myMethodMemberForFailure(tease::Request::TypeTransfer typeTransfer, tease::TransferManager::IdError idErr){...}
private:
static void myMethodStaticForFailure(tease::Request::TypeTransfer typeTransfer, tease::TransferManager::IdError idErr){...}
private:
tease::TransferManager m_transferManager;
}
TypeTransfer
List of types of transfers.
Definition request.h:19
IdError
List of errors identifiers.
Definition transfermanager.h:28
Note
Some developer useful docs:
See also
startDownload()
tease::Url

Member Typedef Documentation

◆ CbCompleted

using tease::TransferManager::CbCompleted = std::function<void(Request::TypeTransfer typeTransfer)>

Callback called when transfer finished and succeed.

Parameters
[in]typeTransferType of transfer which succeeded to complete.
See also
setCbCompleted()
startDownload()

◆ CbFailed

using tease::TransferManager::CbFailed = std::function<void(Request::TypeTransfer typeTransfer, IdError idErr)>

Callback called when transfer finished due to an error.

Parameters
[in]typeTransferType of transfer which failed to complete.
See also
setCbFailed()
startDownload()

◆ CbProgress

using tease::TransferManager::CbProgress = std::function<void(Request::TypeTransfer typeTransfer, size_t transferTotal, size_t transferNow)>

Callback called during transfer.

Parameters
[in]typeTransferType of transfer being started.
[in]transferTotalTotal size of the transfer in bytes
[in]transferNowCurrent values of transfered data in bytes
See also
setCbProgress()
startDownload()

◆ CbStarted

using tease::TransferManager::CbStarted = std::function<void(Request::TypeTransfer typeTransfer)>

Callback called when transfer has been started.

Parameters
[in]typeTransferType of transfer being started.
See also
setCbStarted()
startDownload()

Member Enumeration Documentation

◆ FlagOption

enum tease::TransferManager::FlagOption : std::uint32_t

List of available options.

See also
setOptions()
flagOptionToStr()
Enumerator
OPT_NONE 

No options defined, use this value to reset flags

OPT_VERBOSE 1 << 0 

Enable to provide a lot of verbose informations, you hardly ever want this enabled in production use, you almost always want this used when you debug/report problems.

OPT_FTP_CREATE_DIRS 1 << 1 

When uploading ressource via FTP protocol, missing directories will be automatically created.
Note that this option will be ignored for any other protocol.

◆ IdError

List of errors identifiers.

Enumerator
ERR_NO_ERROR 

Success return code, no error detected

ERR_INTERNAL  

Internal error mainly due to underlying library, please refer to logs if this error is triggered

ERR_INVALID_LOGIN  

Login informations used were wrong

ERR_INVALID_REQUEST  

Receive an invalid request : can be an unsupported protocol and a misformatted request

ERR_INVALID_SSL  

Provided SSL informations are invalid

ERR_BUSY  

Manager is already performing requests transfers

ERR_USER_ABORT  

User abort current transfer

ERR_MAX_TRIALS  

Maximum number of trials were reached

ERR_MEMORY_FULL_HOST  

Trying to download a ressource to host which have his memory full

ERR_MEMORY_FULL_REMOTE  

Trying to upload a ressource to remote which have his memory full

ERR_HOST_NOT_FOUND  

Host server informations are either invalid or unreachable

ERR_HOST_REFUSED  

Host server refused connection

ERR_CONTENT_NOT_FOUND  

Ressource could not be found

Member Function Documentation

◆ abortTransfer()

void tease::TransferManager::abortTransfer ( )

Use to abort current transfer.

This method will return directly, that don't mean that transfer has been aborted yet.
Once transfer aborted, callback TransferManager::CbFailed will be called with error code TransferManager::ERR_USER_ABORT.
Nothing will be perform if no transfer is currently running

Note
This method is thread-safe
This method is asynchronous, so please use dedicated callbacks to manage transfer status.
See also
startDownload(), startUpload()

◆ flagOptionToStr()

std::string tease::TransferManager::flagOptionToStr ( FlagOption options,
char separator = '|' )
static

Use to convert flags option to a string.

Parameters
[in]optionsOptions to convert to string
[in]separatorCharacter separator to use between each options
Returns
Returns string of options.
Example:
std::cout << TransferManager::flagOptionToStr(options, '|'); // Will display: "OPT_FTP_CREATE_DIRS|OPT_VERBOSE"
FlagOption
List of available options.
Definition transfermanager.h:52
@ OPT_FTP_CREATE_DIRS
Definition transfermanager.h:56
@ OPT_VERBOSE
Definition transfermanager.h:55
static std::string flagOptionToStr(FlagOption options, char separator='|')
Use to convert flags option to a string.
Definition transfermanager.cpp:1176

◆ getNbMaxTrials()

int tease::TransferManager::getNbMaxTrials ( ) const

Retrieve maximum number of trials currently set.

Note
This method is thread-safe
Returns
Returns maximum number of trials currently set.
See also
setNbMaxTrials()

◆ getOptions()

TransferManager::FlagOption tease::TransferManager::getOptions ( ) const

Retrieve transfer options.

Note
This method is thread-safe
Returns
Returns transfer options.
See also
setOptions()

◆ getTimeoutConnection()

long tease::TransferManager::getTimeoutConnection ( ) const

Retrieve connection timeout.

Note
This method is thread-safe
Returns
Returns connection timeout in seconds currently set.
See also
setTimeoutConnection()

◆ getTimeoutTransfer()

long tease::TransferManager::getTimeoutTransfer ( ) const

Retrieve transfer timeout.

Note
This method is thread-safe
Returns
Returns transfer timeout in seconds currently set.
See also
setTimeoutTransfer()

◆ getUserLogin()

const std::string & tease::TransferManager::getUserLogin ( ) const

Retrieve login information currently used.

Note
This method is thread-safe
Returns
Returns user information login.
See also
setUserInfos()

◆ getUserPasswd()

const std::string & tease::TransferManager::getUserPasswd ( ) const

Retrieve password information currently used.

Note
This method is thread-safe
Returns
Returns user information password.
See also
setUserInfos()

◆ setCbCompleted()

void tease::TransferManager::setCbCompleted ( CbCompleted fct)

Use to set completed transfer callback.

Default callback will simply log a message.
See TransferManager documentation for more details on how to set the callback.

Parameters
[in]fctCallback function to use when transfer is completed
Note
This method is thread-safe

◆ setCbFailed()

void tease::TransferManager::setCbFailed ( CbFailed fct)

Use to set failed transfer callback.

Default callback will simply log a message.
See TransferManager documentation for more details on how to set the callback.

Parameters
[in]fctCallback function to use when transfer has failed
Note
This method is thread-safe

◆ setCbProgress()

void tease::TransferManager::setCbProgress ( CbProgress fct)

Use to set progress transfer callback.

Default callback will simply log a message.
See TransferManager documentation for more details on how to set the callback.

Parameters
[in]fctCallback function to use for transfer progress
Note
This method is thread-safe

◆ setCbStarted()

void tease::TransferManager::setCbStarted ( CbStarted fct)

Use to set started transfer callback.

Default callback will simply log a message.
See TransferManager documentation for more details on how to set the callback.

Parameters
[in]fctCallback function to use when transfer is started
Note
This method is thread-safe

◆ setNbMaxTrials()

void tease::TransferManager::setNbMaxTrials ( int nbTrials)

Use to set maximum number of trials.

If a request fail, it will be restarted until max number of trials is reached.
Default value is: 1

Parameters
[in]nbTrialsMaximum number of trials allowed
Note
This method is thread-safe
See also
getNbMaxTrials()

◆ setOptions()

void tease::TransferManager::setOptions ( FlagOption options)

Use to set options of the transfer manager.

Parameters
[in]optionsOption flag(s) to use.
Default value is: TransferManager::OPT_NONE
Note
This method is thread-safe
See also
getOptions()

◆ setTimeoutConnection()

void tease::TransferManager::setTimeoutConnection ( long timeout)

Use to set the maximum time in seconds that allow connection phase to take.

This timeout only limits the connection phase, it has no impact once connection has been done.
The connection phase includes the name resolve (DNS) and all protocol handshakes and negotiations until there is an established connection with the remote side.

Parameters
[in]timeoutTimeout in seconds.
To disable it, use value 0. Default value is: 10
Note
This method is thread-safe
See also
getTimeoutConnection()

◆ setTimeoutTransfer()

void tease::TransferManager::setTimeoutTransfer ( long timeout)

Use to set the maximum time in seconds to wait when no data is received before considering a timeout.

This timeout is used when connection to host has been made, it will check for average speed transfer.
If transfer speed is below 20 bytes/sec for timeout time, request is aborted (and retried if available).

Parameters
[in]timeoutTimeout in seconds.
To disable it, use value 0. Default value is: 10
Note
This method is thread-safe
See also
getTimeoutTransfer()

◆ setUserInfos()

void tease::TransferManager::setUserInfos ( const std::string & username,
const std::string & passwd )

Use to set user informations.

Can be useful if server require authentication.

Parameters
[in]usernameLogin username to use.
[in]passwdLogin password to use.
Note
This method is thread-safe
If credentials are invalid, transfer will failed with error TransferManager::ERR_INVALID_LOGIN
See also
getUserLogin(), getUserPasswd()

◆ startDownload()

TransferManager::IdError tease::TransferManager::startDownload ( const Request::List & listReqs)

Use to start downloading list of requests.

Parameters
[in,out]listReqsList of requests to download.
This argument is a list of request pointers, those will be directly filled with downloaded datas, so pointers must remains valid.
Once transfer is finished, user can read request content directly
Note
This method is thread-safe
This method is asynchronous, so please use dedicated callbacks to manage transfer status.
Returns
Returns TransferManager::ERR_NO_ERROR if download succeed to be prepared.
This method will return TransferManager::ERR_BUSY error if a transfer is already running or if called from a callback.
See also
startUpload()
abortTransfer()

◆ startUpload()

TransferManager::IdError tease::TransferManager::startUpload ( const Request::List & listReqs)

Use to start upload list of requests.

Parameters
[in,out]listReqsList of requests to upload.
This argument is a list of request pointers, those will be directly read in order to upload datas, so pointers must remains valid.
Once transfer is finished, user can still use request content.
Note
This method is thread-safe
This method is asynchronous, so please use dedicated callbacks to manage transfer status.
Returns
Returns TransferManager::ERR_NO_ERROR if upload succeed to be prepared.
This method will return TransferManager::ERR_BUSY error if a transfer is already running or if called from a callback.
See also
startDownload()
abortTransfer()

◆ transferIsInProgress()

bool tease::TransferManager::transferIsInProgress ( ) const

Verify is a tranfer is in progress or not.

Note
This method is thread-safe
Returns
Returns true if a transfer is currently running

◆ transferProgressToPercent()

double tease::TransferManager::transferProgressToPercent ( size_t transferTotal,
size_t transferNow )
static

Use to convert progress data to a percentage.

Parameters
[in]transferTotalTotal size of the transfer
[in]transferNowCurrent size of transferred datas
Returns
Return progress percentage.

The documentation for this class was generated from the following files: