section id="druby" xreflabel="dRuby"
dRuby, nebo taky DRb je označení pro Distributed Ruby. Jedná se o knihovnu která umožňuje zasílat a přijímat zprávy od vzdálených Ruby objektů přes TCP/IP.
Aplikace používající knihovnu DRb sestává ze dvou částí, serveru a klienta.
Jednoduchý server vypadá takto:
#!/usr/bin/env ruby
# File: example/net/drb/server.rb
require 'drb'
class TestServer
def doit
"Hello, Distributed World"
end
end
aServerObject = TestServer.new
DRb.start_service('druby://localhost:9000', aServerObject)
DRb.thread.join # Don't exit just yet!
A klient k tomuto serveru pak
#!/usr/bin/env ruby # File: example/net/drb/client.rb require 'drb' DRb.start_service() obj = DRbObject.new(nil, 'druby://localhost:9000') # Now use obj p obj.doit
Jiný příklad. Server:
#!/usr/bin/ruby
# File: example/net/drb/srv2.rb
require 'drb/drb'
class DRbEx
def initialize
@hello = 'hello'
end
def hello
@hello
end
def sample(a, b, c)
a-to_i + b.to_i + c.to_i
end
end
DRb.start_service(nil, DRbEx.new)
puts DRb.uri
DRb.thread.join
a klient:
#!/usr/bin/ruby
# File: example/net/drb/cli2.rb
require 'drb/drb'
class DRbEx2
include DRbUndumped
def initialize(n)
@n = n
end
def to_i
@n.to_i
end
end
there = ARGV.shift
DRb.start_service()
ro = DRbObject.new(nil, there)
p ro.hello
p ro.sample(DRbEx2.new(1), 2, 3)
ro.sample(1, ro.sample(DRbEx2.new(1), 2, 3), DRbEx2.new(3))
