Page 268 - Asterisk™: The Future of Telephony
P. 268

application. In fact, open sourcing individual Adhearsion applications is greatly en-
               couraged.
               Additionally, on a more localized level, users can reuse Adhearsion framework exten-
               sions, called helpers, or roll their own. Helpers range from entire sub-frameworks like
               the Micromenus framework for integrating with on-phone micro-browsers to adding
               a trivial new dialplan method that returns a random quote by Oscar Wilde.
               Below is a simple Adhearsion helper written in Ruby. It creates a new method that will
               exist across the entire framework, including the dialplan. For simplicity’s sake, the
               method downloads an XML document at a specified HTTP URL and converts it to a
               Ruby Hash object (Ruby’s associative array type):
                   def remote_parse url
                     Hash.from_xml open(url).read
                   end
               Note that these three lines can work as the entire contents of a helper file. When Ad-
               hearsion boots, it executes the script in a way that makes any methods or classes defined
               available anywhere in the system.

               For some issues, particularly ones of scaling Adhearsion, it may be necessary to profile
               out bottlenecks to the king of efficiency: C. Below is a sample Adhearsion helper that
               returns the factorial of a number given:
                   int fast_factorial(int input) {
                     int fact = 1, count = 1;
                     while(count <= input) {
                       fact *= count++;
                     }
                     return fact;
                   }
               Again, the code here can exist as the entire contents of a helper file. In this case, because
               it is written in C, it should have the name factorial.alien.c. This tells Adhearsion to
               invoke its algorithm to read the file, add in the standard C and Ruby language devel-
               opment headers, compile it, cache the shared object, load it into the interpreter, and
               then wrap the C method in a Ruby equivalent. Below is a sample dialplan that simply
               speaks back the factorial of six using this C helper:
                   fast_test {
                     num = fast_factorial 6
                     play num
                   }
               Note that the C method becomes a first-class Ruby method. Ruby number objects
               passed to the method are converted to C’s “int” primitive, and the return value is con-
               verted back to a Ruby number object.
               Helpers promise robust albeit simple extensibility to a VoIP engineer’s toolbox, but,
               best of all, useful helpers can be traded and benefit the entire community.




               240 | Chapter 10: Asterisk Manager Interface (AMI) and Adhearsion
   263   264   265   266   267   268   269   270   271   272   273