1 /*
2  * Copyright (C) 2009 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 #include <iostream>
30 #include <cxxtools/arg.h>
31 #include <cxxtools/log.h>
32 #include <cxxtools/remoteprocedure.h>
33 #include <cxxtools/xmlrpc/httpclient.h>
34 #include <cxxtools/bin/rpcclient.h>
35 #include <cxxtools/json/rpcclient.h>
36 #include <cxxtools/json/httpclient.h>
37 
38 ////////////////////////////////////////////////////////////////////////
39 // main
40 //
main(int argc,char * argv[])41 int main(int argc, char* argv[])
42 {
43   try
44   {
45     log_init();
46 
47     cxxtools::Arg<std::string> ip(argc, argv, 'i');
48     cxxtools::Arg<bool> binary(argc, argv, 'b');
49     cxxtools::Arg<bool> json(argc, argv, 'j');
50     cxxtools::Arg<bool> jsonhttp(argc, argv, 'J');
51     cxxtools::Arg<unsigned short> port(argc, argv, 'p', binary ? 7003 : json ? 7004 : 7002);
52 
53     // Normally we would define just one rpc client for the protocol we use but
54     // here we want to demonstrate, that it is just up to the client, which protocol
55     // is used for the remote call.
56 
57     // define a xlmrpc client
58     cxxtools::xmlrpc::HttpClient xmlrpcClient(ip, port, "/xmlrpc");
59     // and a binary rpc client
60     cxxtools::bin::RpcClient binaryClient(ip, port);
61     // and a tcp json rpc client
62     cxxtools::json::RpcClient jsonClient(ip, port);
63 
64     // and a http json rpc client
65     cxxtools::json::HttpClient httpJsonClient(ip, port,"/jsonrpc");
66 
67     // now se welect the client depending on the command line flags
68     cxxtools::RemoteClient& theClient =
69         binary   ? static_cast<cxxtools::RemoteClient&>(binaryClient) :
70         json     ? static_cast<cxxtools::RemoteClient&>(jsonClient) :
71         jsonhttp ? static_cast<cxxtools::RemoteClient&>(httpJsonClient) :
72                    static_cast<cxxtools::RemoteClient&>(xmlrpcClient);
73 
74     // define remote procedure with dobule return value and a double and a std::string parameter:
75     // Note: We send the second parameter as a string since it is converted by the server anyway.
76     cxxtools::RemoteProcedure<double, double, std::string> add(theClient, "add");
77 
78     double sum = 0;
79     for (int a = 1; a < argc; ++a)
80       sum = add(sum, argv[a]);
81 
82     std::cout << "sum=" << sum << std::endl;
83   }
84   catch (const std::exception& e)
85   {
86     std::cerr << e.what() << std::endl;
87   }
88 }
89 
90