Menu

[r1368]: / advanced / examples / cpp / asynch_client.cpp  Maximize  Restore  History

Download this file

76 lines (53 with data), 2.3 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*=============================================================================
asynch_client.cpp
===============================================================================
This is an example of an XML-RPC client that uses XML-RPC for C/C++
(Xmlrpc-c).
In particular, it does multiple RPCs asynchronously, running
simultaneously.
=============================================================================*/
#include <cassert>
#include <cstdlib>
#include <string>
#include <iostream>
#include <xmlrpc-c/girerr.hpp>
#include <xmlrpc-c/base.hpp>
#include <xmlrpc-c/client.hpp>
using namespace std;
int
main(int argc, char **) {
if (argc-1 > 0) {
cerr << "This program has no arguments" << endl;
exit(1);
}
try {
xmlrpc_c::clientXmlTransport_curl myTransport;
xmlrpc_c::client_xml myClient(&myTransport);
string const methodName("sample.add");
xmlrpc_c::paramList sampleAddParms1;
sampleAddParms1.add(xmlrpc_c::value_int(3));
sampleAddParms1.add(xmlrpc_c::value_int(1));
xmlrpc_c::rpcPtr rpc1P(methodName, sampleAddParms1);
xmlrpc_c::paramList sampleAddParms2;
sampleAddParms2.add(xmlrpc_c::value_int(5));
sampleAddParms2.add(xmlrpc_c::value_int(7));
xmlrpc_c::rpcPtr rpc2P(methodName, sampleAddParms2);
string const serverUrl("http://localhost:8080/RPC2");
xmlrpc_c::carriageParm_curl0 myCarriageParm(serverUrl);
rpc1P->start(&myClient, &myCarriageParm);
rpc2P->start(&myClient, &myCarriageParm);
cout << "Two RPCs started. Waiting for them to finish." << endl;
myClient.finishAsync(xmlrpc_c::timeout()); // infinite timeout
assert(rpc1P->isFinished());
assert(rpc2P->isFinished());
int const sum1(xmlrpc_c::value_int(rpc1P->getResult()));
int const sum2(xmlrpc_c::value_int(rpc2P->getResult()));
cout << "Result of RPC 1 (sum of 3 and 1): " << sum1 << endl;
cout << "Result of RPC 2 (sum of 5 and 7): " << sum2 << endl;
} catch (exception const& e) {
cerr << "Client threw error: " << e.what() << endl;
} catch (...) {
cerr << "Client threw unexpected error." << endl;
}
return 0;
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.