CallBack

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ramkumar Nagabhushanam

    #1

    CallBack

    Hello All,
    I am trying to integrate the ftpclient (in ftplib) with an application that
    has been written I C/C++.
    I am able to delete,send change working directories etc. When I receive data
    back from the server i.e. Retrieve files, get a directory listing) I want to
    pass a callback function to the ftpclient so that the output does not get
    displayed on stdout. The code I have written is as shown below. Can anyone
    out there give me advise on how to write "c_function " that is called in
    ftpTest::DoesFi leExist ?Any help will be most appreciated. The code runs
    under windows and is compiled with VC++ 6.0. THe code is as shown below.
    Regards,
    Ram
    [color=blue][color=green][color=darkred]
    >>>>>>>>>>>>>>> >>>> CODE START >>>>>>>>>>>>>>> >>>>[/color][/color][/color]
    // test.cpp : Defines the entry point for the console application.
    //
    #include <iostream>
    #include <string>
    #include <fstream>
    #include <sstream>
    #include <direct.h>
    #include "C:\Python24\in clude\Python.h"

    Class ftpTest
    {
    Private:
    PyObject *_FTP;
    PyObject *_callBack;

    Public:
    FtpTest (const char * ipAddress, const char * username, const char *
    password); // Open the FTP connection, Initialize python interpreter
    ~ftpTest (); // Close connection and finalize python interpreter
    Int Close ();
    Int GetFile (const char * fileName);
    Int PutFile (const char * fileName);
    Int ChangeDir (const char * dirName);
    Int DoesFileExist (const char * fileName);
    Int DeleteFile (const char * fileName);
    Int ChangePermissio ns (const char * fileName, const char * permissions);
    };

    FtpTest::ftpTes t (const char * ipAddress, const char * username, const char
    * password)
    {
    PyObject *modname, *mod, *func, *mdict, *pArgs, *pValue;

    // Initialize the python interpreter
    Py_Initialize() ;

    Modname = PyString_FromSt ring("ftplib");

    If (modname == NULL)
    {
    // Log an error
    }
    Else
    {
    Mod = PyImport_Import (modname);
    }

    If (mod)
    {
    Mdict = PyModule_GetDic t (mod);
    Func = PyDict_GetItemS tring (mdict, "FTP");
    If (func)
    {
    Printf ("I am here -- 1\n");
    }

    Const char *arguments[] = {ipAddress, username, password};

    PArgs = PyTuple_New (3);
    For (int I = 0; I < 3; ++I)
    {
    PValue = PyString_FromSt ring(arguments[I]);
    If (!pValue)
    {
    Py_DECREF(pArgs );
    Py_DECREF(mod);
    }
    /* pValue reference stolen here: */
    PyTuple_SetItem (pArgs, I, pValue);
    }

    _FTP = PyObject_CallOb ject (func, pArgs);

    If (_FTP != NULL)
    {
    Printf ("I am here -- 2\n");
    }
    }
    }

    FtpTest::~ftpTe st ()
    {
    //Py_Finalize();
    }

    Int ftpTest::Close ()
    {
    Return (1);
    }

    Int ftpTest::Change Dir (const char * dirName)
    {
    PyObject *t = PyObject_CallMe thod (_FTP, "cwd", "(S)", dirName);
    Return (1);
    }

    Int ftpTest::Delete File (const char * fileName)
    {
    PyObject *t = PyObject_CallMe thod (_FTP, "delete", "(S)", fileName);
    Return (1);
    }

    Int ftpTest::PutFil e (const char * fileName)
    {
    PyObject * py_file = PyFile_FromStri ng ((char *)fileName, "rb");
    Std::string S("STOR ");
    s += fileName;
    PyObject * t = PyObject_CallMe thod (_FTP, "storlines" , "(sO)", S.c_str(),
    py_file);
    Return (1);
    }

    Int ftpTest::Change Permissions (const char * fileName, const char *
    permissions)
    {
    Std::string S("SITE");
    s += " chmod ";
    s += permissions;
    s += " ";
    s += fileName;
    PyObject * t = PyObject_CallMe thod (_FTP, "sendcmd", "(S)", S.c_str());
    Return (1);
    }

    Int ftpTest::GetFil e (const char * fileName)
    {
    // TODO: Implement callback
    #if 0
    PyObject *t = PyObject_CallMe thod (_FTP, "retrlines" , "(S)", "RETR ram.txt
    );
    #endif
    Return (1);
    }

    PyObject * c_function(char * data)
    {
    // Process the data received in "data"
    Return (NULL);
    }

    Int ftpTest::DoesFi leExist (const char * fileName)
    {
    // Just get the list
    PyObject *t = PyObject_CallMe thod (_FTP, "retrlines" , "(sO)", "LIST",
    c_function);
    If (t == NULL)
    {
    Printf ("I am here -- 4\n");
    }
    Return (1);
    }

    Int main ()
    {
    FtpTest ftpClient("127. 0.0.1", "xxx", "yyyy");
    FtpClient.DoesF ileExist(NULL);
    Return (1);
    }
    [color=blue][color=green][color=darkred]
    >>>>>>>>>>>>>>> >>>> CODE END >>>>>>>>>>>>>>> >>>>>>[/color][/color][/color]
Working...