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); }
void TestCaller::callMegaString() { QMutexLocker locker(&mutex); qDebug() << "Iteration!" << this << thread() << QThread::currentThread(); ReturnValue ret = largeValues(megaString); QString value = ret.isError() ? ret.errString() : ret.toString(); if (value != megaString) qFatal(qPrintable(QString("ERROR: Malformed megastring: %1 (%2)").arg(value).arg(ret.errString()))); }
void TestObject::pause(uint id, ReturnValue ret) { if(ret.isError()) { qDebug() << "Async Pause returned with an error:" << ret; } else { qDebug() << "Async Pause returned."; } }
TestCaller::TestCaller(QObject* parent): ClientProxy(parent) { // ServiceFinder* finder = new ServiceFinder("TestService", this); // QObject::connect(finder, SIGNAL(serviceEvent(ServiceFinder::Service)), this, SLOT(serviceEvent(ServiceFinder::Service))); // finder->scan(); ReturnValue ret = connect("tcp://localhost:18777/TestService"); if(ret.isError()) qDebug() << ret.errString(); else qDebug() << ret; ret = somethingElse("this is some text that will become binary data"); if(ret.isError()) qDebug() << ret.errString(); else qDebug() << ret; megaString = "zxcvbnmasdfghjklqwertyuiop"; for (int i = 0 ; i < 2 ; i++) { megaString += megaString; } QObject::connect(this, SIGNAL(testEvent(QString)), this, SLOT(receiveEvent(QString))); }
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()); }
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); }