Ruby XMLRPC sockets changes

[Continues: Ruby xml-talking to rtorrent]

I have rewritten how i changed the socket in XMLRPC are modified in a more Ruby style way.

A new class, in XMLRPC module, called ClientS (in a file called xmlrpcs.rb). It is a subclass of the Client class to ease the implementation of other kind of trasport system if the Net::HTTP based one doesn’t fit your needs.

require 'xmlrpc/client'

class XMLRPC::ClientS < XMLRPC::Client
        def initialize(info)
            @info = info
        # create new socket
        def new_socket(info,async)
            raise "Must be subclassed"

        # write xmlrpc request in the previously created socket
        def write_request(socket,request)
          if socket.write(request) != request.length then
              raise "Not all the data has been sent"

        # read response from the socket
        def read_response(socket)

        # do_rpc working with custom sockets
        def do_rpc( request, async )
          sock = new_socket(@info,async)
          return read_response(sock)

This makes it very simple to extend the class by just subclassing ClientS or just redefining the function new_socket.
To avoid to close the socket at each request, which may be not the right choice for every code, you can redefine the do_rpc
The info field is used to pass some arguments to create the socket, for instance

require 'xmlrpcs'
require 'socket'

class XMLClient < XMLRPC::ClientS
    def new_socket( info, async )

The code can be found in the code page

[Continued by: XMLRPCs gem published]


, , , , ,

  1. #1 by auguste on September 4, 2008 - 2:24 am

    Thanks, I asked me how to do that, and you do it :)

    Do you know if there are other XMLRPC libraries in other language (like C) wich can usr UNIX socket as transport layer ?

  2. #2 by Dario on September 4, 2008 - 9:19 am

    There are many library in C to do that.

    one of te most known is
    A good list is here

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: