📄 sipmanager.java
字号:
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2000 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" must
* not be used to endorse or promote products derived from this
* software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* nor may "Apache" appear in their name, without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
* Large portions of this software are based upon public domain software
* https://sip-communicator.dev.java.net/
*
*/
package net.sourceforge.gjtapi.raw.sipprovider.sip;
import net.sourceforge.gjtapi.raw.sipprovider.sip.security.SecurityAuthority;
import net.sourceforge.gjtapi.raw.sipprovider.sip.security.SipSecurityManager;
import net.sourceforge.gjtapi.raw.sipprovider.sip.security.UserCredentials;
import net.sourceforge.gjtapi.raw.sipprovider.sip.event.CallEvent;
import net.sourceforge.gjtapi.raw.sipprovider.sip.event.CallRejectedEvent;
import net.sourceforge.gjtapi.raw.sipprovider.sip.event.CommunicationsErrorEvent;
import net.sourceforge.gjtapi.raw.sipprovider.sip.event.CommunicationsListener;
import net.sourceforge.gjtapi.raw.sipprovider.sip.event.MessageEvent;
import net.sourceforge.gjtapi.raw.sipprovider.sip.event.RegistrationEvent;
import net.sourceforge.gjtapi.raw.sipprovider.sip.event.UnknownMessageEvent;
import net.sourceforge.gjtapi.raw.sipprovider.common.Console;
import net.sourceforge.gjtapi.raw.sipprovider.common.NetworkAddressManager;
import net.sourceforge.gjtapi.raw.sipprovider.common.Utils;
import java.net.*;
import java.text.*;
import java.util.*;
import javax.sip.*;
import javax.sip.address.*;
import javax.sip.header.*;
import javax.sip.message.*;
import java.net.InetSocketAddress;
import java.util.Properties;
/**
* The SipManager provides wrapping of the underlying stack's functionalities.
* It also implements the SipListener interface and handles incoming
* SIP messages.
*
* @author Emil Ivov
* @version 1.0
*/
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;
/**
* Specifies the time to wait before retrying delete of a sipProvider.
*/
protected static final long RETRY_OBJECT_DELETES_AFTER = 500;
protected static final Console console = Console.getConsole(SipManager.class);
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 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 = null;
public String currentlyUsedURI = 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;
//mandatory stack properties
protected String stackAddress = null;
protected String stackName = "sip-communicator";
//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 ArrayList listeners = new ArrayList();
//XxxProcessing 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 subscriptions.
*/
//public Watcher watcher = null;
/**
* The instance that informs others of our avaibility.
*/
// public PresenceAgent presenceAgent = null;
/**
* The instance that handles status management and notifications.
*/
// public PresenceUserAgent presenceUserAgent = null;
/**
* The instance that handles incoming/outgoing REFER requests.
*/
TransferProcessing transferProcessing = null;
/**
* Authentication manager.
*/
public SipSecurityManager sipSecurityManager = null;
protected boolean isStarted = false;
private Properties sipProp;
/**
* Constructor. It only creates a SipManager instance without initializing
* the stack itself.
*/
public SipManager(Properties sipProp)
{
this.sipProp = new Properties() ;
this.sipProp.putAll(sipProp);
registerProcessing = new RegisterProcessing(this);
callProcessing = new CallProcessing(this);
// watcher = new Watcher(this);
sipSecurityManager = new SipSecurityManager(this.sipProp);
}
/**
* 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
{
console.logEntry();
initProperties();
this.sipFactory = SipFactory.getInstance();
sipFactory.setPathName("gov.nist");
try
{
addressFactory = sipFactory.createAddressFactory();
headerFactory = sipFactory.createHeaderFactory();
messageFactory = sipFactory.createMessageFactory();
}
catch (PeerUnavailableException ex)
{
console.error("Could not could not create factories!", ex);
throw new CommunicationsException(
"Could not could not create factories!",
ex
);
}
try
{
sipStack = sipFactory.createSipStack(sipProp);
}
catch (PeerUnavailableException ex)
{
console.error("Could not could not create SipStack!", ex);
throw new CommunicationsException(
"Could not could not create SipStack!\n"
+
"A possible reason is an incorrect OUTBOUND_PROXY property\n"
+ "(Syntax:<proxy_address:port/transport>)",
ex
);
}
try
{
boolean successfullyBound = false;
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(localPort, transport);
}
catch (InvalidArgumentException ex)
{
//choose another port between 1024 and 65000
console.error("error binging stack to port " + localPort +
". Will try another port", ex);
localPort = (int) ( (65000 - 1024) * Math.random()) +
1024;
continue;
}
successfullyBound = true;
}
}
catch (TransportNotSupportedException ex)
{
console.error(
"Transport " + transport
+
" is not suppported by the stack!\n Try specifying another"
+ " transport in SipCommunicator property files.\n",
ex);
throw new CommunicationsException(
"Transport " + transport
+
" is not suppported by the stack!\n Try specifying another"
+ " transport in SipCommunicator property files.\n",
ex);
}
try
{
sipProvider = sipStack.createSipProvider(listeningPoint);
}
catch (ObjectInUseException ex)
{
console.error("Could not could not create factories!\n", ex);
throw new CommunicationsException(
"Could not could not create factories!\n", ex);
}
try
{
sipProvider.addSipListener(this);
}
catch (TooManyListenersException exc)
{
console.error(
"Could not register SipManager as a sip listener!", exc);
throw new CommunicationsException(
"Could not register SipManager as a sip listener!", exc);
}
// we should have a security authority to be able to handle
// authentication
if(sipSecurityManager.getSecurityAuthority() == null)
{
throw new CommunicationsException(
"No SecurityAuthority was provided to SipManager!");
}
sipSecurityManager.setHeaderFactory(headerFactory);
sipSecurityManager.setTransactionCreator(sipProvider);
sipSecurityManager.setSipManCallback(this);
//Make sure prebuilt headers are nulled so that they get reinited
//if this is a restart
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -