Ruby/Network/xmlrpc
Материал из Wiki.crossplatform.ru
Содержание |
Calling a method made available over XML-RPC
require "xmlrpc/client" server = XMLRPC::Client.new2("http://xmlrpc-c.sourceforge.net/api/sample.php") puts server.call("sample.sumAndDifference", 5, 3).inspect
Dumping and Loading XML-RPC Messages
require "xmlrpc/marshal" str = XMLRPC::Marshal.dump( { "Ruby" => "is cool" } ) puts str p XMLRPC::Marshal.load( str )
Making an XML-RPC-Enabled Program
require "xmlrpc/server" server = XMLRPC::Server.new(1234) server.add_handler("sample.sumAndDifference") do |a,b| { "sum" => a.to_i + b.to_i, "difference" => a.to_i - b.to_i } end trap("INT") { server.shutdown } server.serve # The following client could use the sample.sumAndDifference method made available by the preceding server: require "xmlrpc/client" server = XMLRPC::Client.new2("http://127.0.0.1:1234/") puts server.call("sample.sumAndDifference", 5, 3).inspect
process errors that come back from RPC calls with call2
require "xmlrpc/client" server = XMLRPC::Client.new2("http://xmlrpc-c.sourceforge.net/api/sample.php") ok, results = server.call2("sample.sumAndDifference", 5, 3) if ok puts results.inspect else puts results.faultCode puts results.faultString end
Writing XML-RPC Clients
require "xmlrpc/client" client = XMLRPC::Client.new2( "http://www.ruby-lang.org/~nahi/xmlrpc/raa/") p client.call("raa.getAllListings") p client.call("raa.getInfoFromName", "XML-RPC")
Writing XML-RPC Servers
require "xmlrpc/server" class Num INTERFACE = XMLRPC::interface("num") { meth "int add(int, int)", "Add two numbers", "add" meth "int div(int, int)", "Divide two numbers" } def add(a, b) a + b end def div(a, b) a / b end end server = XMLRPC::Server.new(8080, "0.0.0.0") server.add_handler(Num::INTERFACE, Num.new) server.serve
XMLRPC call
require "xmlrpc/client" client = XMLRPC::Client.new2("http://www.ruby-lang.org/~nahi/xmlrpc/raa/") list = info = nil t1 = Thread.new { list = client.call_async("raa.getAllListings") } t2 = Thread.new { ok, param = client.call2_asnyc("raa.getInfoFromName", "XML-RPC") info = param if ok } # wait for the threads to complete t1.join; t2.join p list, info
XMLRPC client
require "xmlrpc/client" uri = ARGV[0] || "http://localhost:8080" system = XMLRPC::Client.new2(uri).proxy("system") puts "Introspecting #{ uri }" for meth in system.listMethods.sort puts "=" * 70 for sig in system.methodSignature(meth) puts "- %s %s( %s )" % [ sig[0], meth, (sig[1..-1] || []).join(", ") ] end puts "", system.methodHelp(meth) end