SFTP Progress Monitoring in C++

This C++ example demonstrates how to derive a class from the CkSFtpProgress base class to implement progress monitoring and abort for SFTP:

// To monitor SFTP progress, derive a class from CkSFtpProgress and
// provide implementations for AbortCheck and PercentDone:
class MySFtpProgress : public CkSFtpProgress  
{
    public:
	MySFtpProgress() { }
	virtual ~MySFtpProgress() { }

	// Called periodically during any SFTP method that communicates with
	// the server.  The HeartbeatMs property controls the frequency
	// of callbacks.  The default HeartbeatMs value = 0, which disables
	// AbortCheck callbacks.
	void AbortCheck(bool *abort) 
	    { 
	    printf("SFTP abort check...\n");

	    // To abort any operation while in progress, set the "abort" argument
	    // equal to true, like this:    (uncomment the line below to abort)
	    //*abort = true;
	    }

	// The PercentDone callbacks is called for any method where it it is possible
	// to monitor a percentage completion, such as uploading and downloading files.
	void PercentDone(int pctDone, bool *abort)
	    {
	    printf("SFTP percent done: %d\n", pctDone);

	    // To abort an operation while in progress, set the "abort" argument
	    // equal to true, like this:    (uncomment the line below to abort)
	    //*abort = true;
	    }
    };


void TestSftp(void)
    {
    CkSFtp sftp;

    bool success = sftp.UnlockComponent("Anything for 30-day trial");
    if (success != true) {
        printf("%s\n",sftp.lastErrorText());
        return;
    }

    // Set the event callback object:
    MySFtpProgress myProgress;
    sftp.put_EventCallbackObject(&myProgress);

    // Set HeartbeatMs so that AbortCheck events are called once every 100 milliseconds.
    sftp.put_HeartbeatMs(100);

    const char *hostname = "192.168.1.107"; 
    long port = 22;

    sftp.put_ConnectTimeoutMs(8000);
    sftp.put_IdleTimeoutMs(8000);

    printf("calling Connect...\n");
    success = sftp.Connect(hostname,port);
    if (success != true) {
        printf("%s\n",sftp.lastErrorText());
        return;
    }

    //  Authenticate using login/password:
    printf("calling AuthenticatePw...\n");
    success = sftp.AuthenticatePw("myLogin","myPassword");
    if (success != true) {
        printf("%s\n",sftp.lastErrorText());
        return;
    }

    printf("calling InitializeSftp...\n");
    success = sftp.InitializeSftp();
    if (success != true) {
        printf("%s\n",sftp.lastErrorText());
        return;
    }

    printf("calling DownloadFileByName...\n");
    success = sftp.DownloadFileByName("hamlet.xml","out.txt");
    if (success != true) {
        printf("%s\n",sftp.lastErrorText());
        return;
    }
    printf("Download successful!\n");

Tags :