Asterisk as Sip User Agent emulator

Most common use case of Asterisk is that of a Sip Server. User Agents register to it and can call each other. But have anyone though of using it as a sip user agent emulator? This is exactly what we are doing ! Our main product is written in java which needs to register to a Sip Service Provider, receive calls, play automated answers/forward calls and all kind of play around with the Call. We looked into various open source sip stack written in C / java. We kind of overlooked Asterisk as its known as a sip server! We were just exploring asterisk sip stack to see if we can somehow reuse it, but we were surprised to find out we can use Asterisk as it is without any modifications! Asterisk is written as a B2B UA (Back to Back User Agent) model (not so good for sip servers from performance point of view) but more than fine for us! You can register to your sip service provider as a user agent , answer calls, play messages, collect dtmf, make outgoing call and many other cool stuff! Asterisk real-time even takes this in another level where you can do the all these things dynamically! The best point is, you can write your logic in any language (in our case java) with AGI and AMI which is just TCP message based programming ! Wait, if you are using java, life is even better for you! Asterisk-java is a wonderful abstraction written on AGI/AMI so you don’t even need to bother about TCP programming. Hats off to open source community, It would take us a Year to accomplish what we have accomplished in 3 months with asterisk.

The free book “Asterisk: The Future of Telephony” is a great resource but I am surprised to see how little information is there on net about using Asterisk as a Sip User Agent emulator! Are we the only one who is using asterisk this way !!!???

Asterisk-java + Spring

Those who don’t know about Asterisk Java, its a wonderful java library to talk to a asterisk server through AGI & AMI. We are using it heavily to control our Asterisk box from our “main application”. Our main application is a full fledged spring application running on tomcat. At first we were running the asterisk java components (AGIServer , AgiScripts etc) as a normal java application but soon we wanted our AgiScripts to talk to our spring beans Or even better, we want our agi scripts to be Spring beans.

The beauty of the spring and asterisk-java library is that they were made for each other! Asterisk java library was written in wonderful object-oriented way with clear separation on dependencies. So it was very easy to define the AGIServer and all its dependencies as Spring Bean. So We can inject the MappingStrategy with our own implementation of “BeanNameAwareAGIMappingStrategy” which implements ApplicationContextAware looks like this

 protected AgiScript createAgiScriptInstance(String beanName) {
        Object bean = applicationContext.getBean(beanName)
        if(bean == null) {
           throw new IllegalArgumentException(\"No bean with name: [\" + beanName +\"] found. Make sure that you have all beans defined which are there in your\");

        if(!(bean instanceof AgiScript)) {
          throw new IllegalArgumentException(\"spring bean : \" + beanName + \" must implement org.asteriskjava.fastagi.AgiScript interface\");

        return (AgiScript) bean;

So now, instead of defining fully qualified classnames in your “”, you only give the bean name and our mapping strategy looks it up from the application context. So your plain agi scripts can talk to your dao, services and do all sort of fancy things! Isn’t that wonderful!