31

XML-RPC

 5 years ago
source link: https://www.tuicool.com/articles/hit/6VnyUbn
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

XML-RPC is yet another method of implementing remote procedure calls. It used XML over HTTP to transmit data. In my past live working at TLO I used XML-RPC-C library to implement communication between cluster nodes and a cluster management system. I thought the library was well designed and easy to use so I wanted to introduce you to it.

Below is a simple client and server implementation using the XML-RPC-C library. The server implements one RPC that accepts one string parameter and returns one string. The client makes the call to the server saying hello and prints the reply. The code is easy to read and does not need any further explanation

The client. xmlrpc_c.cpp :

#include <iostream>
#include <string>
#include <xmlrpc-c/girerr.hpp>
#include <xmlrpc-c/base.hpp>
#include <xmlrpc-c/client_simple.hpp>
 
using namespace std;
 
int main(int argc, char** argv)
{
	string serverUrl("http://localhost:8080/RPC2");
	string methodName("hello");
 
	xmlrpc_c::clientSimple client;
	xmlrpc_c::value result;
 
	client.call(serverUrl, methodName, "s", &result, "XMLRPC client says hellp!");
 
	auto reply = xmlrpc_c::value_string(result);
 
	cout << static_cast<string>(reply) << endl;
 
	return 1;
}

The server. xmlrpc_s.cpp :

#include <iostream>
#include <string>
#include <stdexcept>
#include <xmlrpc-c/base.hpp>
#include <xmlrpc-c/registry.hpp>
#include <xmlrpc-c/server_abyss.hpp>
 
using namespace std;
 
class hello : public xmlrpc_c::method
{
public:
	void execute(const xmlrpc_c::paramList& params, xmlrpc_c::value* retval)
	{
		string msg(params.getString(0));
		params.verifyEnd(1);
 
		cout << msg << endl;
 
		*retval = xmlrpc_c::value_string("XMLRPC server says hello!");
	}
};
 
int main(int argc, char** argv)
{
	xmlrpc_c::registry registry;
	registry.addMethod("hello", new hello);
 
	xmlrpc_c::serverAbyss server(xmlrpc_c::serverAbyss::constrOpt().registryP(&registry).portNumber(8080));
	server.run();
 
	return 1;
}

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK