December 2nd, 2008, 11:15 AM
Injecting class definition in DRb server
I am writing a server/client pair using DRb and I want to be able to inject a foreign object FROM the client INTO the server and have it execute. The biggest problem seems to be letting the server know about the class of the foreign object. I want to be able to specify the class on the fly, basically allowing the server to be a very thin and its software can be specified from the outside. Here some of the code I've been playing with.
Firstly on the server side:
@remote = TestRemote.new
def remote_eval something
def put_something something
Next on the client side:
a = Something.new
c = DRbObject.new nil, 'druby://localhost:7000'
c.remote_eval "class Something\n\tdef print_something\n\t\tputs \'something\'\n\tend\nend"
Finally, when the client is run against the server:
snowdall$ ruby test_client.rb
(druby://localhost:7000) test_server.rb:21:in `put_something': undefined method `print_something' for #<DRb::DRbUnknown:0x1cba4c> (NoMethodError)
The client first gives the server the specification of the Something class. From there, and instance of Something is handed over to the server but the server doesn't recognize it as the Something class. Instead it casts it as DRbUnknown. It seems that the class definition that is eval'ed in the server ends up as TestServer::Something since it was specified INSIDE the server instance. I want to know if there is anyway to specify a class definition at the top level of the objectspace. I think that would allow me to inject any object I want into the server and have it run.
Any ideas anyone?