1b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// HelloClient.cpp : A simple xmlrpc client. Usage: HelloClient serverHost serverPort
2b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// Link against xmlrpc lib and whatever socket libs your system needs (ws2_32.lib
3b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// on windows)
4b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat#include "XmlRpc.h"
5b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat#include <iostream>
6b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Eratusing namespace XmlRpc;
7b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
8b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Eratint main(int argc, char* argv[])
9b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat{
10b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  if (argc != 3) {
11b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat    std::cerr << "Usage: HelloClient serverHost serverPort\n";
12b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat    return -1;
13b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  }
14b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  int port = atoi(argv[2]);
15b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  //XmlRpc::setVerbosity(5);
16b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
17b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Use introspection API to look up the supported methods
18b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  XmlRpcClient c(argv[1], port);
19b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  XmlRpcValue noArgs, result;
20b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  if (c.execute("system.listMethods", noArgs, result))
21b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat    std::cout << "\nMethods:\n " << result << "\n\n";
22b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  else
23b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat    std::cout << "Error calling 'listMethods'\n\n";
24b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
25b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Use introspection API to get the help string for the Hello method
26b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  XmlRpcValue oneArg;
27b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  oneArg[0] = "Hello";
28b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  if (c.execute("system.methodHelp", oneArg, result))
29b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat    std::cout << "Help for 'Hello' method: " << result << "\n\n";
30b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  else
31b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat    std::cout << "Error calling 'methodHelp'\n\n";
32b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
33b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Call the Hello method
34b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  if (c.execute("Hello", noArgs, result))
35b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat    std::cout << result << "\n\n";
36b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  else
37b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat    std::cout << "Error calling 'Hello'\n\n";
38b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
39b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Call the HelloName method
40b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  oneArg[0] = "Chris";
41b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  if (c.execute("HelloName", oneArg, result))
42b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat    std::cout << result << "\n\n";
43b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  else
44b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat    std::cout << "Error calling 'HelloName'\n\n";
45b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
46b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Add up an array of numbers
47b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  XmlRpcValue numbers;
48b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  numbers[0] = 33.33;
49b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  numbers[1] = 112.57;
50b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  numbers[2] = 76.1;
51b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  std::cout << "numbers.size() is " << numbers.size() << std::endl;
52b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  if (c.execute("Sum", numbers, result))
53b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat    std::cout << "Sum = " << double(result) << "\n\n";
54b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  else
55b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat    std::cout << "Error calling 'Sum'\n\n";
56b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
57b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Test the "no such method" fault
58b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  if (c.execute("NoSuchMethod", numbers, result))
59b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat    std::cout << "NoSuchMethod call: fault: " << c.isFault() << ", result = " << result << std::endl;
60b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  else
61b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat    std::cout << "Error calling 'Sum'\n";
62b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
63b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Test the multicall method. It accepts one arg, an array of structs
64b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  XmlRpcValue multicall;
65b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  multicall[0][0]["methodName"] = "Sum";
66b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  multicall[0][0]["params"][0] = 5.0;
67b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  multicall[0][0]["params"][1] = 9.0;
68b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
69b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  multicall[0][1]["methodName"] = "NoSuchMethod";
70b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  multicall[0][1]["params"][0] = "";
71b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
72b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  multicall[0][2]["methodName"] = "Sum";
73b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Missing params
74b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
75b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  multicall[0][3]["methodName"] = "Sum";
76b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  multicall[0][3]["params"][0] = 10.5;
77b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  multicall[0][3]["params"][1] = 12.5;
78b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
79b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  if (c.execute("system.multicall", multicall, result))
80b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat    std::cout << "\nmulticall  result = " << result << std::endl;
81b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  else
82b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat    std::cout << "\nError calling 'system.multicall'\n";
83b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
84b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  return 0;
85b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat}
86b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat