コード例 #1
0
ファイル: main.cpp プロジェクト: balashovartem/QtRpc2
int main(int argc, char *argv[])
{
	QCoreApplication app(argc,argv);

    //Create an instance of the service object
    BasicService service;

    //Connect to the server using an incorrect password
    ReturnValue ret = service.connect("tcp://*****:*****@localhost:10123/MyService");
    //If the connection failes, ret.isError() will be true
    if(ret.isError())
    {
        qDebug() << "Failed to connect:" << ret;
        qDebug() << "This was expected.";
    }

    //Connect to the server using the correct username and password
    ret = service.connect("tcp://*****:*****@localhost:10123/MyService");
    //If the connection failes, ret.isError() will be true
    if(ret.isError())
    {
        //You can use ReturnValues in qDebug() statements to quickly print error messages.
        qCritical() << "Failed to connect:" << ret;
        return(1);
    }

    //Run the remove function. This will block untill the function returns,
    //Or untill the default time-out is reached
    ret = service.addNumbers(3,5);
    if(ret.isError())
    {
        qCritical() << "Failed to call addNumbers():" << ret;
        return(1);
    }

    //You can use ReturnValue just like a QVariant
    qDebug() << "Call to add() succeeded.";
    qDebug() << "Result:" << ret.toInt();
    qDebug();

    //Calling this function will return an error.
    ret = service.returnError();
    if(ret.isError())
    {
        //The ReturnValue class can be used to inspect the error.
        qDebug() << "Failed to call returnError():" << ret;
        qDebug() << "Error Number:" << ret.errNumber();
        qDebug() << "Error String:" << ret.errString();
        qDebug() << "This was expected";
    }

    return(0);
}
コード例 #2
0
ファイル: main.cpp プロジェクト: wmanth/QtRpc2
int main(int argc, char *argv[])
{
	QApplication app(argc,argv);

    //Create an instance of the service object
    BasicService service;

    //Connect to the server, specifying the port, and the remove service to connect to
    ReturnValue ret = service.connect("tcp://localhost:10123/MyService");
    //If the connection failes, ret.isError() will be true
    if(ret.isError())
    {
        //You can use ReturnValues in qDebug() statements to quickly print error messages.
        qCritical() << "Failed to connect:" << ret;
        return(1);
    }

    TestObject object;

    //Run the asynchronous pause function on the server. This will call the
    //pauseAsnc slot on object when it returns.
    ret = service.pauseAsync(&object, SLOT(pauseAsync(uint,ReturnValue)));
    if(ret.isError())
    {
        qCritical() << "Failed to call pauseAsync():" << ret;
        return(1);
    }
    //Calling an asynchronous function returns a numeric ID. This ID can be
    //Used to identify the function when it returns.ret = service.pauseAsync(&object, SLOT(pauseAsync(uint,ReturnValue)));
    if(ret.isError())
    {
        qCritical() << "Failed to call pauseAsync():" << ret;
        return(1);
    }
    qDebug() << "pauseAsync() ID:" << ret;
    qDebug() << "pauseAsync() ID:" << ret;

    //Run the synchronous pause function. This will block both the client
    //and the server untill it returns
    qDebug() << "Starting Blocking Pause";
    ret = service.pause();
    if(ret.isError())
    {
        qCritical() << "Failed to call pause():" << ret;
        return(1);
    }
    qDebug() << "Blocking Pause Returned";

    //Run the pause() function asynchronously. This will block on the server,
    //but not on the client. When the function returns, the pause slot on object
    //will be called
    ret = service.pause(&object, SLOT(pause(uint,ReturnValue)));
    if(ret.isError())
    {
        qCritical() << "Failed to call pause():" << ret;
        return(1);
    }
    qDebug() << "pause() ID:" << ret;

    //An event loop is needed for asynchronous calls.
    return(app.exec());
}
コード例 #3
0
ファイル: main.cpp プロジェクト: balashovartem/QtRpc2
int main(int argc, char *argv[])
{
	QCoreApplication app(argc,argv);

    //Create an instance of the service object
    DataService service;

    //Connect to the server, specifying the port, and the remove service to connect to
    ReturnValue ret = service.connect("tcp://localhost:10123/Data");
    //If the connection failes, ret.isError() will be true
    if(ret.isError())
    {
        //You can use ReturnValues in qDebug() statements to quickly print error messages.
        qCritical() << "Failed to connect:" << ret;
        return(1);
    }

    //Retrive the custom data.
    ret = service.getData();
    if(ret.isError())
    {
        qCritical() << "Failed to call getData():" << ret;
        return(1);
    }

    //Pull the custom data out of ReturnValue.
    DataService::CustomData data = ret.value<DataService::CustomData>();
    qDebug() << "Custom Data: " << data.x << data.y << data.z;

    //Retrieve an instance to a service
    ret = service.getBasicService();
    if(ret.isError())
    {
        qCritical() << "Failed to call getBasicService():" << ret;
        return(1);
    }

    //Simply assign an instance of the service class to the returned ReturnValue.
    BasicService basic = ret;

    //Then use the service as you normally would.
    ret = basic.addNumbers(3,5);
    if(ret.isError())
    {
        qCritical() << "Failed to call addNumbers():" << ret;
        return(1);
    }

    //You can use ReturnValue just like a QVariant
    qDebug() << "Call to add() succeeded.";
    qDebug() << "Result:" << ret.toInt();
    qDebug();

    //Calling this function will return an error.
    ret = basic.returnError();
    if(ret.isError())
    {
        //The ReturnValue class can be used to inspect the error.
        qDebug() << "Failed to call returnError():" << ret;
        qDebug() << "Error Number:" << ret.errNumber();
        qDebug() << "Error String:" << ret.errString();
        qDebug() << "This was expected";
    }

    return(0);
}