Applying Adhearsion to Asterisk

Tackle your sticky VoIP projects with Ruby and Adhearsion.
Create Framework Extensions

As any IP telephony engineer can affirm, taking someone else's VoIP functionality and merging it with your own is not for the faint of heart. Nor can you find VoIP functionality in abundance on-line. The sheer difficulty of reusing this kind of code severely discourages trading. Standard PBX dial plan configurations are typically meaningless without the half-dozen other configuration files that put them into perspective.

Not only does Adhearsion allow others easily to integrate with their VoIP applications, it facilitates the integration. The Adhearsion Web site hosts a nice community where users can submit, tag, browse and rate extensions. All of these can be freely downloaded and copied to your helpers/ directory where Adhearsion will find it and automatically absorb its features into the framework. Extensions can vary from adding a new method to the dial plan DSL to entire Web servers that run in a separate thread.

Let's say in a fit of lunacy you decide to write a VoIP calculator application that speaks back an answer. Because you have many different mathematical operations from which to choose, you decide to implement a factorial method and expose it to the entire framework. This requires simply creating the file helpers/factorial.rb and adding the following code to it:

# helpers/factorial.rb
def your_factorial num
   (1..num).inject { |sum,n| n+sum }

When you start Adhearsion, you'll have the ability to use this in your dial plan, your Micromenus, Adhearsion's distributed computing servers, and any other nook or cranny of the framework. But, you say this doesn't cut it.

Like any dynamically typed language, this simply takes too long for very, very large numbers. Wouldn't it be nice if we could write this extension in C? Well, we can.

The terrific third-party library RubyInline takes a string of C/C++ code, read from a file or otherwise, and automatically compiles with the Ruby source headers, caches, and dynamically loads it into the Ruby interpreter. The library even finds any method signatures and creates a matching Ruby method. Static return types automatically convert to Ruby equivalents (int to Fixnum, double to Float) when the native code finishes its business. With this library, Adhearsion allows more efficient extensions to the framework with languages other than Ruby.

Because RubyInline requires the Ruby development headers and a configured compiling environment, it doesn't come in as an Adhearsion dependency. If you have GCC and its peripheral development requirements, do a gem install RubyInline, and throw this code in the file helpers/factorial.alien.c:

int fast_factorial(int num) {
    int sum = 0, counter = 1;
    while(counter <= num) {
        sum += counter++;
    return sum;

Like other extensions, Adhearsion automatically finds this file and hooks its functionality into the framework. If the C code requires special compile instructions or include statements, you easily can add these to the helper's config file, which all helpers can optionally have. These config files exist in the config/helpers directory with the same name as the helper to which they belong but with the YAML .yml extension.

Let your imagination run away with you. If you come up with a great new idea for a VoIP system, the Adhearsion extension architecture serves as a great launchpad to materialize your concepts easily.

Jay Phillips is a VoIP and Ruby enthusiast turned entrepreneur. As the creator and project manager of Adhearsion, Jay provides Adhearsion support and consulting through his newly created, Dallas-based company Codemecca, LLC.



Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Worth mentioning that 999 is

Ross's picture

Worth mentioning that 999 is a bad example to use for things like this as it's the emergency services number in the UK and a couple of other countries... Most Voip ATAs that have a PSTN fallback in these countries will route 999 over the PSTN and it will never reach Asterisk (not to mention you'll have a somewhat annoyed Emergency Operator on the line!)

Simply Amazing

Anonymous's picture

Even though this piece of software is beta, I have been amazed how easy it is do things that would be otherwise difficult to implement. Rails catapulted Ruby in the web dev arena, and I think adhearsion is about to do the same in the asterisk arena.
Go !