David Pollak has posted details of a method to run AGI commands through the manager interface:
Folks,
7 months ago, I made some modifications to Asterisk to allow AGI commands to be passed via the Manager API. The advantage over AGI and FastAGI is the reduced number of sockets to monitor on the controlling machine. If there are 200 channels open on 10 machines that are all being managed from a single control machine, that's 2,000 open sockets (a lot for a kernel to handle) and lots of threads listening to those open sockets. By sending all the commands (AGI and Manager) through a single socket, there are fewer open sockets and a single place to manage all the interactions with Asterisk.
I've had systems in production for the last 7 months running the MAGI (Manager AGI) code. There hasn't been a single failure or problem. I've got Java code running on my application server managing the Asterisk instances. All works like a charm.
I've had a couple of requests for the code. I figure I'll make it available to everyone. Maybe if enough people use it, we'll be able to get the mods into the main Asterisk distribution.
You can download a tarball of Asterisk (it's the source from about a week ago with the MAGI mods) plus Java code that allows you to control Asterisk. In addition, I'm enclosing a sample Java class that answers the call and plays "12345" to the caller.
You can download the tarball from:
http://www.projectsinmotion.com/ast_with_magi.tgz
To access, MAGI, put the following in your dialplan:
exten => 4157380662,1,MAGI(DOSTUFF|${EXTEN})
The code that follows is example Java code.
I hope folks out there find the code valuable. If you've got any questions, please let me know. If you've got enhancements or suggestions for improvement, I'd love to see them.
Thanks,
David
Place this method someplace so that the connection to Asterisk gets initialized and the right factories vend the right instances. I have this code executed as part of a class static {} in a class that's loaded when my app server starts.
private static void startAsterisk() {
// fire off the Asterisk manager
AstManager.setChannelFactory(new AstManager.ICreateChannel() {
public AstChannel create(String chanNum, String chanId,
AstConnection conn) {
return new MyAstChannel(chanNum, chanId, (MyAstConnection) conn);
}
});
AstManager.setConnectionFactory(new AstManager.ICreateConnection() {
public AstConnection create(String host, int port,
AstConnection.IUserNamePassword pwd) throws IOException {
return new MyAstConnection(host, port, pwd);
}
});
Thread t = new Thread(new Runnable() {
public void run() {
AstManager man = new AstManager();
man.start(new AstManager.IConnected() {
public void connectedToAsterisk(AstConnection conn) {
System.out.println("Dude... we're connected");
}
public void failedToConnect() {
run();
}
}, "localhost", 5038, new AstConnection.IUserNamePassword() {
public String getUsername() {
return "foo";
}
public String getPassword() {
return "bar";
}
});
}
});
t.start();
}
MyAstChannel: This class handles the current channel. The key thing is it subclasses runNAExec which is called when the MAGI command is called in Asterisk on the given channel. It answers the line, waits 2 seconds, and then runs "SAY DIGITS" with 12345.
/*
* Created on Aug 11, 2004
*
* (c) 2004-2005 David Pollak
*/
package net.maxvox.mvasterisk;
import java.net.URLDecoder;
import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import net.maxvox.asterisk.AstChannel;
import net.maxvox.asterisk.INameValuePair;
import net.maxvox.asterisk.IServerCommand;
import net.maxvox.asterisk.IVRUtils;
import net.maxvox.asterisk.NameValuePairImpl;
import net.maxvox.asterisk.ServerCommandImpl;
/**
* @author dpp
*
* Represents a channel in the Asterisk
*/
public class MyAstChannel extends AstChannel {
public MyAstChannel(String name, String uniqueId, MyAstConnection conn) {
super(name, uniqueId, conn);
}
protected void runNAExec(HashMap params,
HashMap results) {
super.runNAExec(params, results);
this.answer(new IServerCommand.ICommandStringCallback() {
public void complete(int code, IServerCommand command, String result) {
try {
Thread.sleep(2000);
} catch (InterruptedException ie) {
// ignore
}
sayDigits("12345");
}
});
}
}
MyAstConnection: There's 1 instance of this class per Asterisk server connection. No real reason to subclass it, but I did anyway... :-)
/*
* Created on Aug 9, 2004
*
* (c) 2004-2005 David Pollak
*/
package net.maxvox.mvasterisk;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.SelectableChannel;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Set;
import org.jendo.base.SmartBaseObject;
import org.jendo.base.SmartException;
import org.jendo.util.Diag;
import net.maxvox.asterisk.AstChannel;
import net.maxvox.asterisk.AstConnection;
import net.maxvox.asterisk.INameValuePair;
import net.maxvox.asterisk.IServerCommand;
import net.maxvox.asterisk.NameValuePairImpl;
import net.maxvox.asterisk.SelectChannelMonitor;
import net.maxvox.asterisk.ServerCommandImpl;
import net.maxvox.asterisk.AstConnection.IUserNamePassword;
/**
* @author dpp
*
* Connection to an Asterisk Server's Manager API
*/
public class MYAstConnection extends AstConnection {
public MyAstConnection(String host, int port, IUserNamePassword pwd)
throws IOException {
super(host, port, pwd);
}
public static MyAstConnection instance() {
AstConnection conn = AstConnection.instance();
if (conn != null && conn instanceof MyAstConnection) {
return (MyAstConnection) conn;
}
return null;
}
}
Current Rating: 0/10 (0 votes) Similar Articles (Based on Title)*-dev Developers meeting at von - September 10, 2006 Olle has posted details of the developers meeting at VON.
*-dev Open Source Pavilion at AstriCon: Your project wanted - July 31, 2009 John Todd has posted a note to let people know that Digium will give you a free booth and passes to Astricon for an Open Source project.
CallerID Via WinPopUp and smbclient - November 21, 2004 If you have Windows machines and would like to send people messages when a call is coming in for them, you can use the following macro.
*-Dev: Show incoming number via jabber - March 14, 2005 -mat- filid brandy has posted his PHP script for Jabber/Asterisk integration
Subscribe to RSS Feed Via Email - August 29, 2006 With the help of feedburner we have added the ability to receive updates on the Daily Asterisk News via email. We also have a new URL for the RSS feed.
chan_celliax, for managing cellphones via Asterisk - October 25, 2006 Giovanni Maruzzelli has posted details of the first release of chan_celliax.
New Tutorial: Asterisk on EPIA VIA C3 - March 31, 2008 Lenz has posted details of a new tutorial he has written.
Support for distributing device state and MWI via XMPP PubSub - October 12, 2009 Leif Madsen has posted a note asking for testers of his PubSub branch.
CVS Update: Fast AGI - September 23, 2004 Implement Fast AGI (agi://) over TCP socket (Astricon talk idea)
Asterisk-Users: AGI File for unpatched Festival - October 14, 2004 Donny Kavanagh has released an AGI file to allow text to speech from Festival to be used with Asterisk without patching Festival. He also asks for help with Sphinx (speech to text).
VoIP-info.org: MacinTalk AGI for Mac Asterisk users - July 10, 2005 The wiki has a link to an agi by jcovert to call MacinTalk
AGI and DeadAGI - July 30, 2007 Some information on the changes in the way AGI and DeadAGI work.
Asterisk AGI 2.0 - October 25, 2007 Zoltan Gaspar has posted some ideas about improvements and a change of style for Asterisk Gateway Interfaces.
Added ability to perform SRV lookups for AGI URIs - September 30, 2009 Brent Thomson has posted some cool code to allow fast agi to be used with multiple failover servers.
Original Content (C) 2004-2010
Matt Riddell

Icons by: FastIcon.com
|
AstriDevCon: October 29th, Washington DC August 23, 2010 Average Vote: 10
John Todd has posted a note about the AstriDevCon conference which occurs within the Astricon conference.
Code Review: SRTP support for Asterisk March 12, 2009 Average Vote: 10
Terry Wilson has moved his SRTP branch onto the Digium review board.
The Everything Asterisk Video Collection August 5, 2010 Average Vote: 10
Steven Sokol has posted a blog entry on Asterisk Video Resources.
Voip-Forum: Lots of new articles March 12, 2005 Average Vote: 10
Oej's Voip-Forum.com site has posted lots of new news articles while I've been away. Hopefully you found them via the asterisk-docs site. If not I've bookmarked them for you.
Interview with Mark Spencer November 26, 2004 Average Vote: 9.9
We have managed to get an interview with Mark Spencer AKA Markster. Mark Spencer is the creator of Asterisk and by far the most active developer.
Asterisk and Kamailio realtime integration tutorial May 24, 2010 Average Vote: 9.9
Daniel-Constantin Mierla has posted a link to a tutorial on integrating Asterisk and Kamailio using realtime.
Asterisk and Kamailio (openser) realtime integration August 5, 2010 Average Vote: 9.8
Daniel-Constantin Mierla posted a writeup on combining Asterisk and Kamailio.
Asterisk IPv6 update February 1, 2010 Average Vote: 9.8
Olle has posted an update on IPV6 in Asterisk and a link to a blog post of his.
Proposal for T.38 transparent gateway design in Asterisk April 29, 2010 Average Vote: 9.8
Kevin Fleming has posted a proposed design for a transparent T.38 gateway for Asterisk:
Back to life July 21, 2010 Average Vote: 9.8
Hey all - I am back online after some pretty big projects which have taken all my time. Will be updating the Asterisk news over the next few days.
Announcing Adhearsion 0.8.5 August 25, 2010 Average Vote: 9.8
Ben Klang has posted a note about the latest release of Adhearsion - a framework for developing Asterisk based solutions using Ruby.
app_swift v2.0 released July 21, 2010 Average Vote: 9.8
Like a few of these news stories that I will be posting over the next couple of days this is a little old - hope it is not something you have already seen. This one is for a new version of the app_swift text-to-speech module for Asterisk 1.2, 1.4, and 1.6.
Monitoring Asterisk with Munin January 7, 2010 Average Vote: 9.7
I had a few requests for these munin plugins after some discussion on one of the Asterisk lists and thought people might like them.
GUI changes from Trixbox, FreePBX, 2600hz, BlueBox September 1, 2010 Average Vote: 9.7
Ok, bear with me on this one. If you understand all the ramifications, FreePBX has split to a new project called BlueBox contained within the 2600hz project. This obviously has implications for Trixbox that uses FreePBX to provide quite a bit of functionality.
Nerd Vittles: Finally... Installing Asterisk at Home on Your Windows PC February 9, 2006 Average Vote: 9.7
Ward Mundy has posted details of a how to for installing Asterisk at Home on a windows machine without removing windows.
Asterisk 1.8.0-beta5 Now Available September 9, 2010 The Asterisk Development Team has announced the release of Asterisk 1.8.0-beta5.
libpri 1.4.11.4 Now Available September 3, 2010 The Asterisk Development Team has announced the release of libpri 1.4.11.4.
New CDR Stats Package September 1, 2010 This one has been a long time coming. A new CDR stats package from Star2Billing to replace the 7 year old stalwart for viewing Asterisk call detail records.
GUI changes from Trixbox, FreePBX, 2600hz, BlueBox September 1, 2010 Ok, bear with me on this one. If you understand all the ramifications, FreePBX has split to a new project called BlueBox contained within the 2600hz project. This obviously has implications for Trixbox that uses FreePBX to provide quite a bit of functionality.
RazorQuotePBP Asterisk Payment Module August 31, 2010 RazorQuote has sent us a press release about the launch of RazorQuotePBP, a native Asterisk module that allows any Asterisk connected device to accept credit card payments.
CloudVox: Install an open source Asterisk phone app and get 250 dollars August 30, 2010 CloudVox is running a competition for people to receive 250 dollars for writing up some documentation for Open Source applications on CloudVox - first in first served.
AstriCon approaches August 25, 2010 John Todd has posted a note about the upcoming AstriCon conference in Washington, DC, and the innovation awards.
Announcing Adhearsion 0.8.5 August 25, 2010 Ben Klang has posted a note about the latest release of Adhearsion - a framework for developing Asterisk based solutions using Ruby.
Asterisk 1.8.0-beta4 Now Available August 25, 2010 The Asterisk Development Team has announced the release of Asterisk 1.8.0-beta4.
AstriDevCon: October 29th, Washington DC August 23, 2010 John Todd has posted a note about the AstriDevCon conference which occurs within the Astricon conference.
|