Since we can serialize an object or a set of objects into a form suitable for out-of-process storage, we can use this capability for the transmission of objects from one process to another. Couple this capability with the power of networking, and voila: you have a distributed object system. To save you the trouble of having to write the code, we suggest downloading Masatoshi Seki's Distributed Ruby library (drb) from the RAA.
Using drb, a Ruby process may act as a server, as a client, or as both. A drb server acts as a source of objects, while a client is a user of those objects. To the client, it appears that the objects are local, but in reality the code is still being executed remotely.
A server starts a service by associating an object with a given port. Threads are created internally to handle incoming requests on that port, so remember to join the drb thread before exiting your program.
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 simple drb client simply creates a local drb object and associates it with the object on the remote server; the local object is a proxy.
require 'drb' DRb.start_service() obj = DRbObject?.new(nil, 'druby://localhost:9000') # Now use obj p obj.doit
The client connects to the server and calls the method doit, which returns a string that the client prints out:
"Hello, Distributed World"
The initial nil argument to DRbObject? indicates that we want to attach to a new distributed object. We could also use an existing object.
Ho hum, you say. This sounds like Java's RMI, or CORBA, or whatever. Yes, it is a functional distributed object mechanism---but it is written in just 200 lines of Ruby code. No C, nothing fancy, just plain old Ruby code. Of course, there's no naming service or trader service, or anything like you'd see in CORBA, but it is simple and reasonably fast. On the 233MHz test system, this sample code runs at about 50 remote message calls per second.
And, if you like the look of Sun's JavaSpaces?, the basis of their JINI architecture, you'll be interested to know that drb is distributed with a short module that does the same kind of thing. JavaSpaces? is based on a technology called Linda. To prove that its Japanese author has a sense of humor, Ruby's version of Linda is known as ``rinda.''