📄 calllistener.java
字号:
/* * CallListener.java * * Created on May 21, 2003, 3:52 PM */package gov.nist.examples.pcc;import java.util.*;import java.net.*;import javax.sip.*;import javax.sip.message.*;import javax.sip.header.*;import javax.sip.address.*;import tools.rtpgateway.*;import gov.nist.examples.pcc.registrar.*;/** * * @author deruelle */public class CallListener implements SipListener { protected Vector sipStacksList; protected CallLauncher callLauncher; protected MessageFactory messageFactory; protected HeaderFactory headerFactory; protected AddressFactory addressFactory; protected RTPHole firstRTPHole; protected RTPHole secondRTPHole; protected CallManager callManager; /** Creates a new instance of CallListener */ public CallListener(CallLauncher callLauncher,SipFactory sipFactory) { try{ this.callLauncher=callLauncher; headerFactory = sipFactory.createHeaderFactory(); addressFactory = sipFactory.createAddressFactory(); messageFactory = sipFactory.createMessageFactory(); callManager=new CallManager(this); sipStacksList=new Vector(); } catch(Exception e) { if (CallLauncherDebug.debug) { CallLauncherDebug.println("CallListener, internal error, "+ "exception raised:"); CallLauncherDebug.logException(e); } } } public AddressFactory getAddressFactory() { return addressFactory; } public MessageFactory getMessageFactory() { return messageFactory; } public HeaderFactory getHeaderFactory() { return headerFactory; } public CallLauncher getCallLauncher() { return callLauncher; } public void initCalls(Agent firstAgent, Agent secondAgent) { callManager.initCalls(firstAgent,secondAgent); } /** This is a listener method. */ public void processRequest(RequestEvent requestEvent) { Request request = requestEvent.getRequest(); SipProvider sipProvider = (SipProvider) requestEvent.getSource(); ServerTransaction serverTransaction=requestEvent.getServerTransaction(); try { if (CallLauncherDebug.debug) CallLauncherDebug.println ("\n***************************************************************"+ "\n***************************************************************"+ "\nRequest " + request.getMethod() +" received:\n"+request.toString()); // Let's check for the REGISTER: if (request.getMethod().equals("REGISTER")) { //address translation if(request.getHeader(FromHeader.NAME).toString().indexOf(callLauncher.configuration.serviceGuy)!=-1){ if(!callLauncher.registrar.hasExpiresZero(request)) callLauncher.configuration.isRegistered=true; else callLauncher.configuration.isRegistered=false; System.out.println("isRegistered : " +callLauncher.configuration.isRegistered); } // we call the RegisterProcessing: callLauncher.registrar.processRegister(request,sipProvider,serverTransaction); if(callLauncher.configuration.isRegistered){ Agent serviceGuy=new Agent(callLauncher.configuration.serviceGuy); RegistrationsTable registrationsTable=callLauncher.registrar.getRegistrationsTable(); Hashtable registrations=registrationsTable.getRegistrations(); String calleeAddress=null; Enumeration e=registrations.keys(); while(calleeAddress==null && e.hasMoreElements()){ calleeAddress=(String)e.nextElement(); if(calleeAddress.equals(callLauncher.configuration.serviceGuy)) calleeAddress=null; } if(calleeAddress!=null){ Agent callee=new Agent(calleeAddress); initCalls(serviceGuy,callee); } } return; } // Let's check for the OPTIONS: if (request.getMethod().equals("OPTIONS")) { // Let's reply OK: Response response=messageFactory.createResponse (Response.OK,request); if (serverTransaction!=null) serverTransaction.sendResponse(response); else sipProvider.sendResponse(response); CallLauncherDebug.println("CallListener, OK replied to OPTIONS:\n" + response.toString()); return; } // Let's check for the NOTIFY: if (request.getMethod().equals("NOTIFY")) { // Let's reply OK: Response response=messageFactory.createResponse (Response.OK,request); if (serverTransaction!=null) serverTransaction.sendResponse(response); else sipProvider.sendResponse(response); CallLauncherDebug.println("CallListener, OK replied to NOTIFY:\n" + response.toString()); // Let's update the GUI: FromHeader fromHeader=(FromHeader)request.getHeader(FromHeader.NAME); Address fromAddress=fromHeader.getAddress(); javax.sip.address.URI fromURI=fromAddress.getURI(); String fromURIString=fromURI.toString().toLowerCase(); CallLauncherDebug.println("CallListener, from sip url to update:"+fromURIString ); Object content=request.getContent(); String locationInformation=null; if (content instanceof String) locationInformation=(String)content; else if (content instanceof byte[] ) { locationInformation=new String( (byte[])content ); } else { } CallLauncherDebug.println("CallListener, loc info:"+locationInformation); //MyTable myTable=callLauncher.controllerGUI.getTable(); //myTable.setLocationInformation(fromURIString,locationInformation); return; } // Let's check for the BYE: if (request.getMethod().equals("BYE")) { // First, let's reply OK: try{ Response response=messageFactory.createResponse (Response.OK,request); if (serverTransaction!=null) serverTransaction.sendResponse(response); else sipProvider.sendResponse(response); CallLauncherDebug.println("CallListener, OK replied to BYE:\n" + response.toString()); } catch(Exception e) { e.printStackTrace(); } // Let's send a BYE to the other side: CallIdHeader callIdHeader=(CallIdHeader)request.getHeader(CallIdHeader.NAME); CallAssociation callAssociation=callManager.getCallAssociation(callIdHeader.getCallId() ); if (callAssociation ==null ){ CallLauncherDebug.println("CallListener, ERROR, the call Association"+ " is null!!!"); return; } if (callAssociation.firstCall.isDialog(serverTransaction.getDialog()) ) { // Let's terminate the second Call if it is established if (callAssociation.secondCall.isOKReceived()) { CallLauncherDebug.println("CallLauncher, we have to terminate"+ " the second call"); callAssociation.secondCall.sendBye(); } else { // We have to cancel it... CallLauncherDebug.println("CallListener, we have to cancel"+ " the second call"); callAssociation.secondCall.sendCancel(); } } else { // Let's terminate the first Call if it is established if (callAssociation.firstCall.isOKReceived()) { CallLauncherDebug.println("CallListener, we have to terminate"+ " the first call"); callAssociation.firstCall.sendBye(); } else { // We have to cancel it... CallLauncherDebug.println("CallListener, we have to cancel"+ " the first call"); callAssociation.firstCall.sendCancel(); } } String key=callAssociation.firstCall.toAgent.getSipURI(); CallLauncherDebug.println("CallListener, BYE key:"+key); //callLauncher.controllerGUI.updateRow(key,"Disconnected"); if (callLauncher.rtpHolesManager!=null) callLauncher.rtpHolesManager.closeRTPHoles( callIdHeader.getCallId() ); return; } // First, let's reply METHOD_NOT_ALLOWED: try{ Response response=messageFactory.createResponse (Response.METHOD_NOT_ALLOWED,request); if (serverTransaction!=null) serverTransaction.sendResponse(response); else sipProvider.sendResponse(response); CallLauncherDebug.println("CallListener, METHOD_NOT_ALLOWED replied:\n" + response.toString()); } catch(Exception e) { e.printStackTrace(); } } catch (Exception ex){ try{ if (CallLauncherDebug.debug) { CallLauncherDebug.println("CallListener, processRequest(), internal error, "+ "exception raised:"); CallLauncherDebug.logException(ex); } // This is an internal error: // Let's return a 500 SERVER_INTERNAL_ERROR Response response=messageFactory.createResponse (Response.SERVER_INTERNAL_ERROR,request); if (serverTransaction!=null) serverTransaction.sendResponse(response); else sipProvider.sendResponse(response); if (CallLauncherDebug.debug) CallLauncherDebug.println("CallListener, processRequest(),"+ " 500 SERVER_INTERNAL_ERROR replied:\n"+ response.toString()); } catch (Exception e){ e.printStackTrace(); } } } /** This is a listener method. */ public void processResponse(ResponseEvent responseEvent) { try{ Response response = responseEvent.getResponse(); SipProvider sipProvider = (SipProvider) responseEvent.getSource(); SipStack sipStack=sipProvider.getSipStack(); String holeIPAddress=sipStack.getIPAddress(); ClientTransaction clientTransaction=responseEvent.getClientTransaction(); CallLauncherDebug.println ("\n***************************************************************"+ "\n***************************************************************"+ "\nResponse "+response.getStatusCode() + " "+response.getReasonPhrase() +" received:\n"+response.toString() ); CallLauncherDebug.println("Processing Response in progress"); CSeqHeader cseqHeader=(CSeqHeader)response.getHeader(CSeqHeader.NAME); CallIdHeader callIdHeader=(CallIdHeader)response.getHeader(CallIdHeader.NAME); CallAssociation callAssociation=callManager.getCallAssociation(callIdHeader.getCallId() ); if (callAssociation ==null ){ CallLauncherDebug.println("CallListener, ERROR, the call Association"+ " is null!!!"); return; } if (clientTransaction==null ) { CallLauncherDebug.println("CallListener, The client transaction"+ " associated to the response is null: RETRANSMISSION, we discard this response"); return;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -