registerprocessing.java

来自「java 开发的sip软电话 源码 jain sip」· Java 代码 · 共 657 行 · 第 1/2 页

JAVA
657
字号
/* ====================================================================
 * 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/>.
 *
 * Portions of this software are based upon public domain software
 * originally written at the National Center for Supercomputing Applications,
 * University of Illinois, Urbana-Champaign.
 */

package net.java.mais.sip;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

import javax.sip.ClientTransaction;
import javax.sip.InvalidArgumentException;
import javax.sip.SipException;
import javax.sip.Transaction;
import javax.sip.TransactionUnavailableException;
import javax.sip.address.Address;
import javax.sip.address.SipURI;
import javax.sip.header.CSeqHeader;
import javax.sip.header.CallIdHeader;
import javax.sip.header.ContactHeader;
import javax.sip.header.ExpiresHeader;
import javax.sip.header.FromHeader;
import javax.sip.header.MaxForwardsHeader;
import javax.sip.header.ToHeader;
import javax.sip.header.UserAgentHeader;
import javax.sip.message.Request;
import javax.sip.message.Response;

import net.java.mais.common.Utils;
import net.java.mais.sip.security.SipSecurityException;
import softPhone.GuiManager;
import softPhone.SoftPhonePlugin;

/**
 * <p>Title: Netsite TudoMais</p>
 * <p>Description:JAIN-SIP Audio/Video phone application</p>
 * <p>Copyright: Copyright (c) 2006</p>
 * <p>Organisation: CTBC Telecom / Netsite </p>
 * @author Thiago Rocha Camargo (barata7@yahoo.com)
 */

/**
 * <p>Title: SIP COMMUNICATOR-1.1</p>
 * <p>Description: JAIN-SIP-1.1 Audio/Video Phone Application</p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Organisation: LSIIT Laboratory (http://lsiit.u-strasbg.fr)</p>
 * <p>Network Research Team (http://www-r2.u-strasbg.fr))</p>
 * <p>Louis Pasteur University - Strasbourg - France</p>
 * @author Emil Ivov
 * @version 1.1
 */

class RegisterProcessing{
	    
    private SipManager sipManCallback = null;
    private Request registerRequest = null;
    private boolean isRegistered = false;
    private boolean isUnregistering = false;

    private Timer reRegisterTimer = null;
    private int keepAlivePort = 0;
    
    // Thiago Rocha Camargo
    // Keep-Alive
    private Timer keepAliveTimer = null;

    RegisterProcessing(SipManager sipManCallback)
    {
        this.sipManCallback = sipManCallback;
    }

    void setSipManagerCallBack(SipManager sipManCallback)
    {
        this.sipManCallback = sipManCallback;
    }

    void processOK(ClientTransaction clientTransatcion, Response response)
    {
        try {
            
            isRegistered = true;
            FromHeader fromHeader =
                ( (FromHeader) response.getHeader(FromHeader.NAME));
            Address address = fromHeader.getAddress();
            
            int expires = 0;
            if(!isUnregistering){
            	ContactHeader contactHeader = (ContactHeader) response.getHeader(ContactHeader.NAME);
            	//TODO check if the registrar created the contact address
            	if (contactHeader != null) {
            		expires = contactHeader.getExpires();
        		}
            	else {
	            	ExpiresHeader expiresHeader = response.getExpires();
	            	if (expiresHeader != null) {
	            		expires = expiresHeader.getExpires();
	            	}
            	}
            }
            //expires may be null
            //fix by Luca Bincoletto <Luca.Bincoletto@tilab.com>
            if (expires == 0) {
            	isUnregistering=false;
        		sipManCallback.fireUnregistered(address.toString());
        	}
        	else {
        		System.out.println("SIP: "+ response.toString());
        		
        		if(reRegisterTimer!=null)
        			reRegisterTimer.cancel();
        		if(keepAliveTimer!=null)
        			keepAliveTimer.cancel();
        		
        		reRegisterTimer = new Timer();
        		keepAliveTimer = new Timer();
//        		if (expires > 0 && expires < 60) {
        			//[issue 2] Schedule re registrations
                    //bug reported by LynlvL@netscape.com
        			//use the value returned by the server to reschedule registration
        			SipURI uri = (SipURI) address.getURI();
        			scheduleReRegistration(uri.getHost(), uri.getPort(), uri.getTransportParam(), expires);
            		scheduleKeepAlive(Integer.parseInt(Utils.getProperty("net.java.mais.sip.KEEP_ALIVE_DELAY")));        		
//        		}
            /*else{
        			SipURI uri = (SipURI) address.getURI();
        			scheduleReRegistration(uri.getHost(), uri.getPort(), uri.getTransportParam(), expires);
        		}*/
        		sipManCallback.fireRegistered(address.toString());

        	}
        }
        finally {
            
        }
    }

    void processTimeout(Transaction transatcion, Request request)
    {
        try {
            
            isRegistered = true;
            FromHeader fromHeader =
                ( (FromHeader) request.getHeader(FromHeader.NAME));
            Address address = fromHeader.getAddress();
            sipManCallback.fireUnregistered("Request timeouted for: " +
                                            address.toString());
        }
        finally {
            
        }

    }

    void processNotImplemented(ClientTransaction transatcion, Response response)
    {
        try {
            
            isRegistered = true;
            FromHeader fromHeader =
                ( (FromHeader) response.getHeader(FromHeader.NAME));
            Address address = fromHeader.getAddress();
            sipManCallback.fireUnregistered("Server returned NOT_IMPLEMENTED. "
                                            + address.toString());
        }
        finally {
            
        }

    }

    /**
     * Attempts to re-ogenerate the corresponding request with the proper
     * credentials and terminates the call if it fails.
     *
     * @param clientTransaction the corresponding transaction
     * @param response the challenge
     */
    void processAuthenticationChallenge(ClientTransaction clientTransaction,
                                        Response response)
    {
        try {            

            Request register = clientTransaction.getRequest();

            Request reoriginatedRequest = null;

            ClientTransaction retryTran = sipManCallback.sipSecurityManager.
                handleChallenge(response,
                                clientTransaction);

            //Dialog dialog = clientTransaction.getDialog();

            retryTran.sendRequest();
            return;
        }
        catch (SipSecurityException exc) {
            //tell the others we couldn't register
            sipManCallback.
                fireUnregistered(
                    ( (FromHeader) clientTransaction.getRequest().getHeader(FromHeader.NAME)).
                        getAddress().toString());
            sipManCallback.fireCommunicationsError(
                new CommunicationsException("Authorization failed!", exc));
        }
        catch (Exception exc) {
            //tell the others we couldn't register
            sipManCallback.
                fireUnregistered(
                    ( (FromHeader) clientTransaction.getRequest().getHeader(FromHeader.NAME)).
                        getAddress().toString());
            sipManCallback.fireCommunicationsError(
                new CommunicationsException("Failed to resend a request "
                                            + "after a security challenge!",
                                            exc)
                );
        }
        finally {
            
        }
    }


    synchronized void register(String registrarAddress, int registrarPort,
                  String registrarTransport, int expires) throws
         CommunicationsException
    {
        try
        {
        	isUnregistering=false;
            //From
            FromHeader fromHeader = sipManCallback.getFromHeader(true);
            Address fromAddress = fromHeader.getAddress();
            sipManCallback.fireRegistering(fromAddress.toString());
            //Request URI
            SipURI requestURI = null;
            try {
                requestURI = sipManCallback.addressFactory.createSipURI(null,
                    registrarAddress);
            }
            catch (ParseException ex) {
                
                throw new CommunicationsException(
                    "Bad registrar address:"
                    + registrarAddress,
                    ex);
            }
            catch (NullPointerException ex) {
            //Do not throw an exc, we should rather silently notify the user
            //throw new CommunicationsException("A registrar address was not specified!", ex);
                sipManCallback.fireUnregistered(fromAddress.getURI().toString() + " (registrar not specified)");
                return;
            }
            requestURI.setPort(registrarPort);
            try {
                requestURI.setTransportParam(registrarTransport);
            }
            catch (ParseException ex) {
                throw new CommunicationsException(
                    registrarTransport + " is not a valid transport!", ex);
            }
            //Call ID Header
            CallIdHeader callIdHeader = sipManCallback.sipProvider.getNewCallId();
            //CSeq Header
            CSeqHeader cSeqHeader = null;
            try {
                cSeqHeader = sipManCallback.headerFactory.createCSeqHeader(1,
                    Request.REGISTER);
            }
            catch (ParseException ex) {
                //Should never happen
                
                
            }
            catch (InvalidArgumentException ex) {

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?