Class: XMLRPC::Server

Inherits:
WEBrickServlet show all
Defined in:
lib/xmlrpc/server.rb

Overview

Implements a standalone XML-RPC server. The method XMLRPC::Server#serve is left if a SIGHUP is sent to the program.

 require "xmlrpc/server"

s = XMLRPC::Server.new(8080)

s.add_handler("michael.add") do |a,b|
  a + b
end

s.add_handler("michael.div") do |a,b|
  if b == 0
    raise XMLRPC::FaultException.new(1, "division by zero")
  else
    a / b
  end
end

s.set_default_handler do |name, *args|
  raise XMLRPC::FaultException.new(-99, "Method #{name} missing" +
                                   " or wrong number of parameters!")
end

s.serve

Constant Summary

Constants inherited from BasicServer

BasicServer::ERR_MC_EXPECTED_STRUCT, BasicServer::ERR_MC_MISSING_METHNAME, BasicServer::ERR_MC_MISSING_PARAMS, BasicServer::ERR_MC_RECURSIVE_CALL, BasicServer::ERR_MC_WRONG_PARAM, BasicServer::ERR_MC_WRONG_PARAM_PARAMS, BasicServer::ERR_METHOD_MISSING, BasicServer::ERR_UNCAUGHT_EXCEPTION

Instance Method Summary collapse

Methods inherited from WEBrickServlet

#get_instance, #get_valid_ip, #require_path_info?, #service, #set_valid_ip

Methods inherited from BasicServer

#add_handler, #add_introspection, #add_multicall, #get_default_handler, #get_service_hook, #process, #set_default_handler, #set_service_hook

Methods included from ParseContentType

#parse_content_type

Methods included from ParserWriterChooseMixin

#set_parser, #set_writer

Constructor Details

#initialize(port = 8080, host = "127.0.0.1", maxConnections = 4, stdlog = $stdout, audit = true, debug = true, *a) ⇒ Server

Creates a new XMLRPC::Server instance, which is a XML-RPC server listening on the given port and accepts requests for the given host, which is localhost by default.

The server is not started, to start it you have to call XMLRPC::Server#serve.

The optional audit and debug parameters are obsolete!

All additionally provided parameters in *a are by-passed to XMLRPC::BasicServer.new.



575
576
577
578
579
580
581
# File 'lib/xmlrpc/server.rb', line 575

def initialize(port=8080, host="127.0.0.1", maxConnections=4, stdlog=$stdout, audit=true, debug=true, *a)
  super(*a)
  require 'webrick'
  @server = WEBrick::HTTPServer.new(:Port => port, :BindAddress => host, :MaxClients => maxConnections,
                                    :Logger => WEBrick::Log.new(stdlog))
  @server.mount("/", self)
end

Instance Method Details

#portObject

Get the port of the server, useful when started with port=0



598
599
600
# File 'lib/xmlrpc/server.rb', line 598

def port
  @server.config[:Port]
end

#serveObject

Call this after you have added all you handlers to the server. This method starts the server to listen for XML-RPC requests and answer them.



585
586
587
588
589
590
# File 'lib/xmlrpc/server.rb', line 585

def serve
  signals = %w[INT TERM HUP] & Signal.list.keys
  signals.each { |signal| trap(signal) { @server.shutdown } }

  @server.start
end

#shutdownObject

Stops and shuts the server down.



593
594
595
# File 'lib/xmlrpc/server.rb', line 593

def shutdown
  @server.shutdown
end