Using remote objects under dRuby

By: Jeya Emailed: 1698 times Printed: 2203 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

This example illustrates returning a reference to an object from a dRuby call. The Logger instances live in the server process. References to them are returned to the client process, where methods can be invoked upon them. These methods are executed in the server process.

Server code

  require 'drb/drb'

  URI="druby://localhost:8787"

  class Logger

      # Make dRuby send Logger instances as dRuby references,
      # not copies.
      include DRb::DRbUndumped

      def initialize(n, fname)
          @name = n
          @filename = fname
      end

      def log(message)
          File.open(@filename, "a") do |f|
              f.puts("#{Time.now}: #{@name}: #{message}")
          end
      end

  end

  # We have a central object for creating and retrieving loggers.
  # This retains a local reference to all loggers created.  This
  # is so an existing logger can be looked up by name, but also
  # to prevent loggers from being garbage collected.  A dRuby
  # reference to an object is not sufficient to prevent it being
  # garbage collected!
  class LoggerFactory

      def initialize(bdir)
          @basedir = bdir
          @loggers = {}
      end

      def get_logger(name)
          if !@loggers.has_key? name
              # make the filename safe, then declare it to be so
              fname = name.gsub(/[.\/]/, "_").untaint
              @loggers[name] = Logger.new(name, @basedir + "/" + fname)
          end
          return @loggers[name]
      end

  end

  FRONT_OBJECT=LoggerFactory.new("/tmp/dlog")

  $SAFE = 1   # disable eval() and friends

  DRb.start_service(URI, FRONT_OBJECT)
  DRb.thread.join

Client code

  require 'drb/drb'

  SERVER_URI="druby://localhost:8787"

  DRb.start_service

  log_service=DRbObject.new_with_uri(SERVER_URI)

  ["loga", "logb", "logc"].each do |logname|

      logger=log_service.get_logger(logname)

      logger.log("Hello, world!")
      logger.log("Goodbye, world!")
      logger.log("=== EOT ===")

  end

Ruby Home | All Ruby Tutorials | Latest Ruby Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(0)


Be the first one to add a comment

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Jeya
FTP using Ruby
Security in dRuby
Using remote objects under dRuby
bmbm(width = 0) {|job| ...} in Ruby
bm(label_width = 0, *labels) {|report| ...} in Ruby
benchmark(caption = "", label_width = nil, fmtstr = nil, *labels) {|report| ...} in Ruby

More Tutorials in Ruby
Standard Library Packages in Ruby
Module Abbrev in Ruby
encoding and decoding in Ruby using Base64 Module
Benchmark module in Ruby
benchmark(caption = "", label_width = nil, fmtstr = nil, *labels) {|report| ...} in Ruby
bm(label_width = 0, *labels) {|report| ...} in Ruby
bmbm(width = 0) {|job| ...} in Ruby
CGI class in Ruby
URL encode a string in Ruby
escapeElement() in Ruby
escapeHTML() in Ruby
new(type = "query") in Ruby
parse(query) in Ruby
pretty() in Ruby
Format a Time object as a String in Ruby

More Latest News
Most Viewed Articles (in Ruby )
Sending emails and Receiving emails using Ruby On rails
Methods in Ruby on rails
if . . . elsif . . . else in Ruby on rails
bm(label_width = 0, *labels) {|report| ...} in Ruby
URL encode a string in Ruby
URL decode a string in Ruby
Open and manipulate CSV files in Ruby
SimpleDelegator in Ruby
Using remote objects under dRuby
Reading emails using POP3 in Ruby
Using Observer in Ruby
Using Forwardable in Ruby
Getting started with Ruby on rails
Prompts, Command Lines, Prompts, and irb in Ruby on rails
Naming conventions in Ruby on rails
Most Emailed Articles (in Ruby)
parse(query) in Ruby
POST a form in Ruby
IMAP in Ruby
Sending emails using SMTP in Ruby
Defining tables and classes (using MySQL) in Ruby on Rails
Action Pack - Ruby On rails from request to response
Getting started with Ruby on rails
Prompts, Command Lines, Prompts, and irb in Ruby on rails
unless in Ruby on rails
escapeHTML() in Ruby
new(type = "query") in Ruby
date and time in Ruby
SimpleDelegator in Ruby
Reading URL content using Ruby (HTTP)
Naming conventions in Ruby on rails