1 /*
2 * Copyright (C) 2011,2013 Tommi Maekitalo
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * As a special exception, you may use this file as part of a free
10 * software library without restriction. Specifically, if other files
11 * instantiate templates or use macros or inline functions from this
12 * file, or you compile this file and link it with other files to
13 * produce an executable, this file does not by itself cause the
14 * resulting executable to be covered by the GNU General Public
15 * License. This exception does not however invalidate any other
16 * reasons why the executable file might be covered by the GNU Library
17 * General Public License.
18 *
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
23 *
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 */
28
29 /*
30
31 This demo program shows how to run multiple remote rpc calls asncronously so
32 that they run in parallel.
33
34 */
35
36 #include <iostream>
37 #include <cxxtools/arg.h>
38 #include <cxxtools/log.h>
39 #include <cxxtools/xmlrpc/httpclient.h>
40 #include <cxxtools/bin/rpcclient.h>
41 #include <cxxtools/json/rpcclient.h>
42 #include <cxxtools/json/httpclient.h>
43 #include <cxxtools/remoteprocedure.h>
44 #include <cxxtools/selector.h>
45
main(int argc,char * argv[])46 int main(int argc, char* argv[])
47 {
48 try
49 {
50 // initialize logging - this reads the file log.xml from the current directory
51 log_init();
52
53 // read the command line options
54 cxxtools::Arg<std::string> ip(argc, argv, 'i'); // option -i <ip-addres> defines the address where to find the server
55 cxxtools::Arg<bool> binary(argc, argv, 'b');
56 cxxtools::Arg<bool> json(argc, argv, 'j');
57 cxxtools::Arg<bool> jsonhttp(argc, argv, 'J');
58 cxxtools::Arg<std::size_t> timeout(argc, argv, 't', cxxtools::RemoteClient::WaitInfinite);
59 cxxtools::Arg<unsigned short> port(argc, argv, 'p', binary ? 7003 : json ? 7004 : 7002);
60
61 // we need a selector, which controls the network activity
62 cxxtools::Selector selector;
63
64 // Normally we would define just one rpc client for the protocol we use but
65 // here we want to demonstrate, that it is just up to the client, which protocol
66 // is used for the remote call.
67
68 // One client can run just one request at a time. To run parallel requests
69 // we need 2 clients. So we define 2 clients for each protocol.
70
71 // define a xlmrpc client
72 cxxtools::xmlrpc::HttpClient xmlrpcClient1(selector, ip, port, "/xmlrpc");
73 cxxtools::xmlrpc::HttpClient xmlrpcClient2(selector, ip, port, "/xmlrpc");
74
75 // and a binary rpc client
76 cxxtools::bin::RpcClient binaryClient1(selector, ip, port);
77 cxxtools::bin::RpcClient binaryClient2(selector, ip, port);
78
79 // and a tcp json rpc client
80 cxxtools::json::RpcClient jsonClient1(selector, ip, port);
81 cxxtools::json::RpcClient jsonClient2(selector, ip, port);
82
83 // and a http json rpc client
84 cxxtools::json::HttpClient httpJsonClient1(selector, ip, port, "/jsonrpc");
85 cxxtools::json::HttpClient httpJsonClient2(selector, ip, port, "/jsonrpc");
86
87 // now se select the client depending on the command line flags
88
89 cxxtools::RemoteClient& client1(
90 binary ? static_cast<cxxtools::RemoteClient&>(binaryClient1) :
91 json ? static_cast<cxxtools::RemoteClient&>(jsonClient1) :
92 jsonhttp ? static_cast<cxxtools::RemoteClient&>(httpJsonClient1) :
93 static_cast<cxxtools::RemoteClient&>(xmlrpcClient1));
94
95 cxxtools::RemoteClient& client2(
96 binary ? static_cast<cxxtools::RemoteClient&>(binaryClient2) :
97 json ? static_cast<cxxtools::RemoteClient&>(jsonClient2) :
98 jsonhttp ? static_cast<cxxtools::RemoteClient&>(httpJsonClient2) :
99 static_cast<cxxtools::RemoteClient&>(xmlrpcClient2));
100
101 // define remote procedure with dobule return value and two double parameters
102 cxxtools::RemoteProcedure<double, double, double> add1(client1, "add");
103 cxxtools::RemoteProcedure<double, double, double> add2(client2, "add");
104
105 // initiate the execution of our method
106 add1.begin(5, 6);
107 add2.begin(1, 2);
108
109 // Calling RemoteProcedure::end will run the underlying event loop until
110 // the remote procedure is finished and return the result.
111 // In case of a error, an exception is thrown.
112
113 // Note that waiting for the end of one remote procedure will also start
114 // and maybe finish the second remote procedure.
115
116 double result1 = add1.end(timeout);
117
118 std::cout << "result1=" << result1 << std::endl;
119
120 // Here we run the loop again until the second procedure is finished. It
121 // may well be, that the procedure is already finished and we get the
122 // result immediately.
123
124 double result2 = add2.end(timeout);
125
126 std::cout << "result2=" << result2 << std::endl;
127
128 }
129 catch (const std::exception& e)
130 {
131 std::cerr << "error: " << e.what() << std::endl;
132 }
133 }
134
135