Using DRb

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:

config.enable_drb
When you start your Adhearsion application next, 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 may do this:

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

Exposing features via DRb

We're going to create a new Adhearsion component named drb_rocks and expose a single method we can 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"
    Adhearsion.active_calls.size
  end
end
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 expose 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 is required to write a separate process into the Adhearsion process! When you run this script, it should print 0 and the Adhearsion process will 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

Example:

require 'drb'
Adhearsion = DRbObject.new_with_uri "druby://localhost:9050"
Adhearsion.call_into_context("Zap/g0/14155551212","my_context")

Browse Space

- Pages
- News
- Labels
- Attachments
- Bookmarks
- Mail
- Advanced
- Activity

Explore Confluence

- Popular Labels
- Notation Guide

Your Account

Log In

or Sign Up  

Other Features

Add Content