📄 sipmanager.java.svn-base
字号:
/**
* $Revision: $
* $Date: $
*
* Copyright (C) 2007 Jive Software. All rights reserved.
*
* This software is published under the terms of the GNU Lesser Public License (LGPL),
* a copy of which is included in this distribution.
*/
package net.java.sipmack.sip;
import gov.nist.javax.sip.stack.SIPTransaction;
import net.java.sipmack.common.Log;
import net.java.sipmack.sip.event.CallEvent;
import net.java.sipmack.sip.event.CallRejectedEvent;
import net.java.sipmack.sip.event.CommunicationsErrorEvent;
import net.java.sipmack.sip.event.CommunicationsListener;
import net.java.sipmack.sip.event.MessageEvent;
import net.java.sipmack.sip.event.RegistrationEvent;
import net.java.sipmack.sip.event.UnknownMessageEvent;
import net.java.sipmack.sip.security.SecurityAuthority;
import net.java.sipmack.sip.security.SipSecurityManager;
import net.java.sipmack.sip.security.Credentials;
import net.java.sipmack.sip.simple.MessageProcessing;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Inet4Address;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.TooManyListenersException;
import javax.sip.ClientTransaction;
import javax.sip.Dialog;
import javax.sip.DialogTerminatedEvent;
import javax.sip.IOExceptionEvent;
import javax.sip.InvalidArgumentException;
import javax.sip.ListeningPoint;
import javax.sip.ObjectInUseException;
import javax.sip.PeerUnavailableException;
import javax.sip.RequestEvent;
import javax.sip.ResponseEvent;
import javax.sip.ServerTransaction;
import javax.sip.SipException;
import javax.sip.SipFactory;
import javax.sip.SipListener;
import javax.sip.SipProvider;
import javax.sip.SipStack;
import javax.sip.TimeoutEvent;
import javax.sip.Transaction;
import javax.sip.TransactionAlreadyExistsException;
import javax.sip.TransactionTerminatedEvent;
import javax.sip.TransactionUnavailableException;
import javax.sip.TransportNotSupportedException;
import javax.sip.address.Address;
import javax.sip.address.AddressFactory;
import javax.sip.address.SipURI;
import javax.sip.header.CSeqHeader;
import javax.sip.header.ContactHeader;
import javax.sip.header.FromHeader;
import javax.sip.header.HeaderFactory;
import javax.sip.header.MaxForwardsHeader;
import javax.sip.header.ToHeader;
import javax.sip.header.ViaHeader;
import javax.sip.message.Message;
import javax.sip.message.MessageFactory;
import javax.sip.message.Request;
import javax.sip.message.Response;
import javax.sdp.SessionDescription;
/**
* Title: SIPark
* Description: JAIN-SIP Audio/Video phone application
*
* @author Thiago Rocha Camargo (thiago@jivesoftware.com) The
* <code>SipManager</code> class that Manage SIP Stack
* @version 1.0, 20/07/2006
*/
public class SipManager implements SipListener {
/**
* Specifies the number of retries that should be attempted when deleting a
* sipProvider
*/
protected static final int RETRY_OBJECT_DELETES = 10;
protected static final long REGISTER_TIMEOUT = 20000;
/**
* Specifies the time to wait before retrying delete of a sipProvider.
*/
protected static final long RETRY_OBJECT_DELETES_AFTER = 500;
protected static final String DEFAULT_TRANSPORT = "udp";
// jain-sip objects - package accessibility as they should be
// available for XxxProcessing classes
/**
* The SipFactory instance used to create the SipStack and the Address
* Message and Header Factories.
*/
public SipFactory sipFactory;
/**
* The AddressFactory used to create URLs ans Address objects.
*/
public AddressFactory addressFactory;
/**
* The HeaderFactory used to create SIP message headers.
*/
public HeaderFactory headerFactory;
/**
* The Message Factory used to create SIP messages.
*/
public MessageFactory messageFactory;
/**
* The sipStack instance that handles SIP communications.
*/
SipStack sipStack;
/**
* The busy state of this SIP Manager
*/
public boolean isBusy = false;
/**
* The default (and currently the only) SIP listening point of the
* application.
*/
ListeningPoint listeningPoint;
/**
* The JAIN SIP SipProvider instance.
*/
public SipProvider sipProvider;
/**
* An instance used to provide user credentials
*/
private SecurityAuthority securityAuthority = null;
/**
* Used for the contact header to provide firewall support.
*/
private InetSocketAddress publicIpAddress = null;
// properties
protected String sipStackPath = "gov.nist";
/**
*/
protected String currentlyUsedURI = null;
protected String username = null;
protected String displayName = null;
protected String transport = null;
protected String registrarAddress = null;
protected int localPort = -1;
protected int registrarPort = -1;
protected int registrationsExpiration = -1;
protected String registrarTransport = null;
private int registerRetries = 0;
// mandatory stack properties
protected String stackAddress = null;
protected String stackName = "SIPark";
// Prebuilt Message headers
/**
*/
protected FromHeader fromHeader = null;
/**
*/
protected ContactHeader contactHeader = null;
protected ArrayList viaHeaders = null;
protected static final int MAX_FORWARDS = 70;
/**
*/
protected MaxForwardsHeader maxForwardsHeader = null;
protected long registrationTransaction = -1;
protected List<CommunicationsListener> listeners = new ArrayList<CommunicationsListener>();
// Processing managers
/**
* The instance that handles all registration associated activity such as
* registering, unregistering and keeping track of expirations.
*/
RegisterProcessing registerProcessing = null;
/**
* The instance that handles all call associated activity such as
* establishing, managing, and terminating calls.
*/
CallProcessing callProcessing = null;
/**
* The instance that handles incoming/outgoing MESSAGE requests.
*/
public MessageProcessing messageProcessing = null;
/**
* The instance that handles incoming/outgoing REFER requests.
*/
TransferProcessing transferProcessing = null;
/**
* Authentication manager.
*/
public SipSecurityManager sipSecurityManager = null;
/**
*/
protected boolean isStarted = false;
/**
* Constructor. It only creates a SipManager instance without initializing
* the stack itself.
*/
public SipManager() {
registerProcessing = new RegisterProcessing(this);
callProcessing = new CallProcessing(this);
messageProcessing = new MessageProcessing(this);
transferProcessing = new TransferProcessing(this, callProcessing);
sipSecurityManager = new SipSecurityManager();
registerRetries = 0;
}
/**
* Creates and initializes JAIN SIP objects (factories, stack, listening
* point and provider). Once this method is called the application is ready
* to handle (incoming and outgoing) sip messages.
*
* @throws CommunicationsException if an axception should occur during the initialization
* process
*/
public void start() throws CommunicationsException {
try {
initProperties();
SIPConfig.setSystemProperties();
this.sipFactory = SipFactory.getInstance();
sipFactory.setPathName(sipStackPath);
try {
addressFactory = sipFactory.createAddressFactory();
headerFactory = sipFactory.createHeaderFactory();
messageFactory = sipFactory.createMessageFactory();
}
catch (PeerUnavailableException ex) {
Log.error("start", ex);
throw new CommunicationsException(
"Could not create factories!", ex);
}
try {
sipStack = sipFactory.createSipStack(System.getProperties());
((SipCommRouter) sipStack.getRouter())
.setOutboundProxy(SIPConfig.getOutboundProxy());
}
catch (PeerUnavailableException ex) {
Log.error("start", ex);
throw new CommunicationsException(
"Cannot connect!\n"
+ "Please verify your connection.", ex);
}
try {
boolean successfullyBound = false;
int tries = 0;
while (!successfullyBound) {
try {
// try and capture the firewall mapping for this address
// just befre it gets occuppied by the stack
publicIpAddress = NetworkAddressManager
.getPublicAddressFor(localPort);
listeningPoint = sipStack.createListeningPoint(publicIpAddress.getAddress().getHostAddress(),
localPort, transport);
}
catch (InvalidArgumentException ex) {
Log.error("start", ex);
// choose another port between 1024 and 65000
if (tries > 3) {
if (!NetworkAddressManager.nextIndex()) throw new CommunicationsException(
"Cannot connect!\n"
+ "Please verify your connection.", ex);
tries = 0;
}
tries++;
localPort = (int) ((65000 - 1024) * Math.random()) + 1024;
try {
Thread.sleep(1000);
}
catch (Exception e) {
}
continue;
}
successfullyBound = true;
}
}
catch (TransportNotSupportedException ex) {
throw new CommunicationsException(
"Transport "
+ transport
+ " is not suppported by the stack!\n Try specifying another"
+ " transport in Mais property files.\n", ex);
}
try {
sipProvider = sipStack.createSipProvider(listeningPoint);
}
catch (ObjectInUseException ex) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -