Using DRb

Ben Langfeld edited this page Apr 11, 2012 · 3 revisions

DEPRECATION NOTICE: This is old documentation relevant to Adhearsion 1.x and will soon be removed. See the main documentation for up-to-date info.

DRb stands for "Distributed Ruby" and comes with the Ruby standard library. It is a synchronous TCP protocol, recognized only by Ruby, which allows one Ruby process to instantly execute a method in another, separate process.

You may want to use DRb so your Ruby on Rails (or Sinatra, or Merb, etc) applications can instantly access some state of the telephony system or instantly execute a method you've exposed.

##Enabling DRb##

In your config/startup.rb file, find the line that reads like the following and uncomment it:


When you next start your Adhearsion application, it will be listening on port 9050 for incoming DRb connections.

If you would like to start the DRb with an access control list, you can do this:

config.enable_drb :host => "", :deny => "", :allow => ["", ""]

##Exposing features via DRb##

You'll need to create a new Adhearsion component named drb_rocks and expose a single method to execute in a separate process.

With a terminal in the directory of your Adhearsion application, type the following:

ahn create component drb_rocks

Next, open the components/drb_rocks/drb_rocks.rb file in your favorite text editor. Place the following text in it:

methods_for :rpc do
  def active_call_count
    ahn_log.drb_rocks "Sending #{Adhearsion.active_calls.size} back via DRb"

RPC simply means "Remote Procedure Call" and, in this case, it's synonymous with DRb. There are other components, such as the restful_rpc component, which exposes these :rpc methods via REST.

After you've enabled DRb in startup.rb and added the component code, go ahead and start your Adhearsion application:

ahn start /path/to/your/app

##Connecting into Adhearsion##

Now let's create a separate .rb file with just the following code:

require 'drb'
Adhearsion = DRbObject.new_with_uri "druby://localhost:9050"
puts Adhearsion.active_call_count

That's all that's required to write a separate process into the Adhearsion process! When you run this script, it should print 0 and the Adhearsion process should show the log message we added in the component's method. Try executing the script while a call is in place; the counter should properly increment and decrement as calls come and go.

##Some Methods Available via DRb by Default##

Class: Adhearsion::VoIP::Asterisk::Manager::ManagerInterface


require 'drb'
Adhearsion = DRbObject.new_with_uri "druby://localhost:9050"