import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import org.asteriskjava.manager.AbstractManagerEventListener;
import org.asteriskjava.manager.AuthenticationFailedException;
import org.asteriskjava.manager.ManagerConnection;
import org.asteriskjava.manager.ManagerConnectionFactory;
import org.asteriskjava.manager.ManagerEventListener;
import org.asteriskjava.manager.TimeoutException;
import org.asteriskjava.manager.action.StatusAction;
import org.asteriskjava.manager.event.AgentCalledEvent;
import org.asteriskjava.manager.event.ManagerEvent;
import org.asteriskjava.manager.event.NewCallerIdEvent;
//public class freshEvent implements ManagerEventListener {
public class freshEvent extends AbstractManagerEventListener{
private ManagerConnection managerConnection;
private int port=9527;
private String callerIdNum;
private ServerSocket serverSocket;
private Socket clientSocket = null;
//private PrintWriter out;
public freshEvent() throws IOException, IllegalStateException,
AuthenticationFailedException, TimeoutException, InterruptedException
{
serverSocket = new ServerSocket(port);
ManagerConnectionFactory factory = new ManagerConnectionFactory(
"localhost", "admin", "amp111");
this.managerConnection = factory.createManagerConnection();
managerConnection.login();
managerConnection.addEventListener(this);
}
public void writeToSocket() throws IOException, IllegalStateException, AuthenticationFailedException, TimeoutException, InterruptedException
{
while(true)
{
try {
System.out.println("ppp");
clientSocket = serverSocket.accept();
PrintWriter out= new PrintWriter(clientSocket.getOutputStream());
out.println("Jsjsjsjsjjs"+"/0");
out.flush();
System.out.println("ffff");
} catch (IOException e) {
System.err.println("Accept failed.");
System.exit(1);
}
}
}
/*public void onManagerEvent(ManagerEvent event) {
try{
callerIdNum=((NewCallerIdEvent)event).getCallerId();
PrintWriter out= new PrintWriter(clientSocket.getOutputStream());
out.println(callerIdNum+"/0");
out.flush();
}catch(Exception e){e.printStackTrace();}
}*/
protected void handleEvent(AgentCalledEvent event) {
callerIdNum=event.getCallerId();
try{
PrintWriter out = new PrintWriter(clientSocket.getOutputStream());
out.print(callerIdNum + "/0");
out.flush();
System.out.println("send ok");
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String args[]) throws IOException, IllegalStateException,
AuthenticationFailedException, TimeoutException, InterruptedException
{
freshEvent he=new freshEvent();
he.writeToSocket();
}
}
注释掉的是不能用的类(不能获得稳定实值)
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import org.asteriskjava.manager.AbstractManagerEventListener;
import org.asteriskjava.manager.AuthenticationFailedException;
import org.asteriskjava.manager.ManagerConnection;
import org.asteriskjava.manager.ManagerConnectionFactory;
import org.asteriskjava.manager.ManagerEventListener;
import org.asteriskjava.manager.TimeoutException;
import org.asteriskjava.manager.action.StatusAction;
import org.asteriskjava.manager.event.AgentCalledEvent;
import org.asteriskjava.manager.event.ManagerEvent;
import org.asteriskjava.manager.event.NewCallerIdEvent;
//public class freshEvent implements ManagerEventListener {
public class freshEvent extends AbstractManagerEventListener{
private ManagerConnection managerConnection;
private int port=9527;
private String callerIdNum;
private ServerSocket serverSocket;
private Socket clientSocket = null;
//private PrintWriter out;
public freshEvent() throws IOException, IllegalStateException,
AuthenticationFailedException, TimeoutException, InterruptedException
{
serverSocket = new ServerSocket(port);
ManagerConnectionFactory factory = new ManagerConnectionFactory(
"localhost", "admin", "amp111");
this.managerConnection = factory.createManagerConnection();
managerConnection.login();
managerConnection.addEventListener(this);
}
public void writeToSocket() throws IOException, IllegalStateException, AuthenticationFailedException, TimeoutException, InterruptedException
{
while(true)
{
try {
System.out.println("ppp");
clientSocket = serverSocket.accept();
PrintWriter out= new PrintWriter(clientSocket.getOutputStream());
out.println("Jsjsjsjsjjs"+"/0");
out.flush();
System.out.println("ffff");
} catch (IOException e) {
System.err.println("Accept failed.");
System.exit(1);
}
}
}
/*public void onManagerEvent(ManagerEvent event) {
try{
callerIdNum=((NewCallerIdEvent)event).getCallerId();
PrintWriter out= new PrintWriter(clientSocket.getOutputStream());
out.println(callerIdNum+"/0");
out.flush();
}catch(Exception e){e.printStackTrace();}
}*/
protected void handleEvent(AgentCalledEvent event) {
callerIdNum=event.getCallerId();
try{
PrintWriter out = new PrintWriter(clientSocket.getOutputStream());
out.print(callerIdNum + "/0");
out.flush();
System.out.println("send ok");
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String args[]) throws IOException, IllegalStateException,
AuthenticationFailedException, TimeoutException, InterruptedException
{
freshEvent he=new freshEvent();
he.writeToSocket();
}
}
注释掉的是不能用的类(不能获得稳定实值)