MANAGERAPI-JAVA-AGI

// file name HelloEvents.java full flashed demo of Asterisk-JAVA package APIs
// by imran ( Pakistan ) you can use this to handle every thing in java that asterisk supports.
// it is a complex script but gives u a clear vision how you will use java api for any task.
// Please feed back >>>>>>>>> imran_accout@yahoo.com


import java.io.IOException;
import net.sf.asterisk.manager.AuthenticationFailedException;
import net.sf.asterisk.manager.ManagerConnection;
import net.sf.asterisk.manager.ManagerConnectionFactory;
import net.sf.asterisk.manager.ManagerEventHandler;
import net.sf.asterisk.manager.TimeoutException;
import net.sf.asterisk.manager.action.StatusAction;
import net.sf.asterisk.manager.event.ManagerEvent;
import net.sf.asterisk.manager.event.*;
import net.sf.asterisk.manager.action.OriginateAction;
import net.sf.asterisk.manager.response.ManagerResponse;

import net.sf.asterisk.fastagi.command.*;


public class HelloEvents implements ManagerEventHandler
{
private ManagerConnection managerConnection;

public HelloEvents() throws IOException
{
ManagerConnectionFactory factory = new ManagerConnectionFactory();

this.managerConnection = factory.getManagerConnection("localhost", "manager", "pa55w0rd");
}

public void run() throws IOException, AuthenticationFailedException,
TimeoutException, InterruptedException
{
// register for events
managerConnection.addEventHandler(this);
OriginateAction originateAction;
ManagerResponse originateResponse;
originateAction = new OriginateAction();


ChannelStatusCommand OBJ_CHANNELSTATUS = new ChannelStatusCommand();

String channelstatus = OBJ_CHANNELSTATUS.getChannel();


AnswerCommand OBJ_ANSWER = new AnswerCommand();

String DialStr ="SIP/100|40|tr";
ExecCommand OBJ_EXEC = new ExecCommand("DIAL",DialStr);


GetVariableCommand OBJ_DIALSTATUS= new GetVariableCommand("DIALSTATUS");

String dialstatus = OBJ_DIALSTATUS.getVariable();

if(dialstatus.equals("ANSWER"))
{
}

HangupCommand OBJ_HANGUP = new HangupCommand();


originateAction.setChannel("SIP/John");
originateAction.setContext("default");
originateAction.setExten("1300");
originateAction.setPriority(new Integer(1));
originateAction.setTimeout(new Integer(30000));

// connect to Asterisk and log in
managerConnection.login();

// send the originate action and wait for a maximum of 30 seconds for Asterisk
// to send a reply
originateResponse = managerConnection.sendAction(originateAction, 30000);

// print out whether the originate succeeded or not
System.out.println(originateResponse.getResponse());

// request channel state
managerConnection.sendAction(new StatusAction());

// wait 10 seconds for events to come in
Thread.sleep(10000);

// and finally log off and disconnect
managerConnection.logoff();
}

public void handleEvent(ManagerEvent event)
{
//DialEvent DialObject = (DialEvent)event;
//ChannelEvent ChannelObject = (ChannelEvent)event;
DialEvent DialObject = null;
ChannelEvent ChannelObject = null;

Object obj = event.getSource();
// just print received events
if( obj == DialObject/*DialEvent*/ )
{
System.out.println("
DialEvent
i'm excuted");
DialObject = (DialEvent)event;

DialObject.getCallerId();
DialObject.getCallerIdName();
DialObject.getDestination();
DialObject.getDestUniqueId();
DialObject.getSrc();
DialObject.getSrcUniqueId();
}
else if( obj == ChannelObject/*ChannelEvent*/ )
{
System.out.println("
(ChannelEvent)
i'm excuted");
ChannelObject = (ChannelEvent)event;
ChannelObject.getCallerId();
ChannelObject.getCallerIdName();
ChannelObject.getChannel();
ChannelObject.getState();
}

System.out.println(event);
}

public static void main(String[] args) throws Exception
{
HelloEvents helloEvents;
helloEvents = new HelloEvents();
helloEvents.run();
}
}

// file name HelloEvents.java full flashed demo of Asterisk-JAVA package APIs
// by imran ( Pakistan ) you can use this to handle every thing in java that asterisk supports.
// it is a complex script but gives u a clear vision how you will use java api for any task.
// Please feed back >>>>>>>>> imran_accout@yahoo.com


import java.io.IOException;
import net.sf.asterisk.manager.AuthenticationFailedException;
import net.sf.asterisk.manager.ManagerConnection;
import net.sf.asterisk.manager.ManagerConnectionFactory;
import net.sf.asterisk.manager.ManagerEventHandler;
import net.sf.asterisk.manager.TimeoutException;
import net.sf.asterisk.manager.action.StatusAction;
import net.sf.asterisk.manager.event.ManagerEvent;
import net.sf.asterisk.manager.event.*;
import net.sf.asterisk.manager.action.OriginateAction;
import net.sf.asterisk.manager.response.ManagerResponse;

import net.sf.asterisk.fastagi.command.*;


public class HelloEvents implements ManagerEventHandler
{
private ManagerConnection managerConnection;

public HelloEvents() throws IOException
{
ManagerConnectionFactory factory = new ManagerConnectionFactory();

this.managerConnection = factory.getManagerConnection("localhost", "manager", "pa55w0rd");
}

public void run() throws IOException, AuthenticationFailedException,
TimeoutException, InterruptedException
{
// register for events
managerConnection.addEventHandler(this);
OriginateAction originateAction;
ManagerResponse originateResponse;
originateAction = new OriginateAction();


ChannelStatusCommand OBJ_CHANNELSTATUS = new ChannelStatusCommand();

String channelstatus = OBJ_CHANNELSTATUS.getChannel();


AnswerCommand OBJ_ANSWER = new AnswerCommand();

String DialStr ="SIP/100|40|tr";
ExecCommand OBJ_EXEC = new ExecCommand("DIAL",DialStr);


GetVariableCommand OBJ_DIALSTATUS= new GetVariableCommand("DIALSTATUS");

String dialstatus = OBJ_DIALSTATUS.getVariable();

if(dialstatus.equals("ANSWER"))
{
}

HangupCommand OBJ_HANGUP = new HangupCommand();


originateAction.setChannel("SIP/John");
originateAction.setContext("default");
originateAction.setExten("1300");
originateAction.setPriority(new Integer(1));
originateAction.setTimeout(new Integer(30000));

// connect to Asterisk and log in
managerConnection.login();

// send the originate action and wait for a maximum of 30 seconds for Asterisk
// to send a reply
originateResponse = managerConnection.sendAction(originateAction, 30000);

// print out whether the originate succeeded or not
System.out.println(originateResponse.getResponse());

// request channel state
managerConnection.sendAction(new StatusAction());

// wait 10 seconds for events to come in
Thread.sleep(10000);

// and finally log off and disconnect
managerConnection.logoff();
}

public void handleEvent(ManagerEvent event)
{
//DialEvent DialObject = (DialEvent)event;
//ChannelEvent ChannelObject = (ChannelEvent)event;
DialEvent DialObject = null;
ChannelEvent ChannelObject = null;

Object obj = event.getSource();
// just print received events
if( obj == DialObject/*DialEvent*/ )
{
System.out.println("
DialEvent
i'm excuted");
DialObject = (DialEvent)event;

DialObject.getCallerId();
DialObject.getCallerIdName();
DialObject.getDestination();
DialObject.getDestUniqueId();
DialObject.getSrc();
DialObject.getSrcUniqueId();
}
else if( obj == ChannelObject/*ChannelEvent*/ )
{
System.out.println("
(ChannelEvent)
i'm excuted");
ChannelObject = (ChannelEvent)event;
ChannelObject.getCallerId();
ChannelObject.getCallerIdName();
ChannelObject.getChannel();
ChannelObject.getState();
}

System.out.println(event);
}

public static void main(String[] args) throws Exception
{
HelloEvents helloEvents;
helloEvents = new HelloEvents();
helloEvents.run();
}
}

Created by: ddx, Last modification: Fri 26 of Aug, 2005 (07:57 UTC)
Please update this page with new information, just login and click on the "Edit" or "Discussion" tab. Get a free login here: Register Thanks! - Find us on Google+