File: pstream_client.cpp

package info (click to toggle)
xmlrpc-c 1.59.03-10.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,132 kB
  • sloc: ansic: 55,248; cpp: 13,541; sh: 3,321; makefile: 2,553; perl: 593; xml: 134
file content (88 lines) | stat: -rw-r--r-- 2,601 bytes parent folder | download | duplicates (2)
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
76
77
78
79
80
81
82
83
84
85
86
87
88
/*=============================================================================
                        pstream_client.cpp
===============================================================================
  This is an example of a client that uses XML-RPC for C/C++
  (Xmlrpc-c).

  In particular, it uses the simple "packet stream" XML transport mechanism
  instead of HTTP as specified by XML-RPC (so this is not an XML-RPC
  client).

  You have to supply as Standard Input a stream (TCP) socket whose other
  end is hooked up to the RPC server.  The 'socketexec' program is a
  good way to arrange that.

  The sample program pstream_serial_server.cpp is compatible with this client.

  Example:

    $ socketexec -connect -remote_host=localhost -remote_port=8080 \
        ./pstream_client
=============================================================================*/

#include <cassert>
#include <cstdlib>
#include <string>
#include <iostream>
#include <unistd.h>
#include <signal.h>

using namespace std;

#include <xmlrpc-c/girerr.hpp>
#include <xmlrpc-c/base.hpp>
#include <xmlrpc-c/client.hpp>
#include <xmlrpc-c/client_transport.hpp>



int
main(int argc, char **) {

    if (argc-1 > 0) {
        cerr << "This program has no arguments" << endl;
        exit(1);
    }

#ifndef _WIN32
    // It's a good idea to disable SIGPIPE signals; if server closes his end
    // of the pipe/socket, we'd rather see a failure to send a call than
    // get killed by the OS.
    signal(SIGPIPE, SIG_IGN);
#endif

    try {
        xmlrpc_c::clientXmlTransport_pstream myTransport(
            xmlrpc_c::clientXmlTransport_pstream::constrOpt()
            .fd(STDIN_FILENO));

        xmlrpc_c::client_xml myClient(&myTransport);

        string const methodName("sample.add");

        xmlrpc_c::paramList sampleAddParms;
        sampleAddParms.add(xmlrpc_c::value_int(5));
        sampleAddParms.add(xmlrpc_c::value_int(7));

        xmlrpc_c::rpcPtr myRpcP(methodName, sampleAddParms);

        xmlrpc_c::carriageParm_pstream myCarriageParm;
            // Empty; transport doesn't need any information

        myRpcP->call(&myClient, &myCarriageParm);

        assert(myRpcP->isFinished());

        int const sum(xmlrpc_c::value_int(myRpcP->getResult()));
            // Assume the method returned an integer; throws error if not

        cout << "Result of RPC (sum of 5 and 7): " << sum << endl;

    } catch (exception const& e) {
        cerr << "Client threw error: " << e.what() << endl;
    } catch (...) {
        cerr << "Client threw unexpected error." << endl;
    }

    return 0;
}