invitedialog.java

来自「结构非常清晰的SIP协议栈」· Java 代码 · 共 722 行 · 第 1/3 页

JAVA
722
字号
/*
 * Copyright (C) 2005 Luca Veltri - University of Parma - Italy
 * 
 * This file is part of MjSip (http://www.mjsip.org)
 * 
 * MjSip is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * MjSip is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with MjSip; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * 
 * Author(s):
 * Luca Veltri (luca.veltri@unipr.it)
 */

package org.zoolu.sip.dialog;


import org.zoolu.sip.address.*;
import org.zoolu.sip.transaction.*;
import org.zoolu.sip.message.*;
import org.zoolu.sip.header.*;
import org.zoolu.sip.provider.*;
import org.zoolu.tools.LogLevel;


/** Class InviteDialog can be used to manage invite dialogs.
  * An InviteDialog can be both client or server.
  * (i.e. generating an INVITE request or responding to an incoming INVITE request).
  * <p>
  * An InviteDialog can be in state inviting/waiting/invited, accepted/refused, call,
  * byed/byeing, and close.
  * <p>
  * InviteDialog supports the offer/answer model for the sip body, with the following rules:
  * <br> - both INVITE-offer/2xx-answer and 2xx-offer/ACK-answer modes for incoming calls
  * <br> - INVITE-offer/2xx-answer mode for outgoing calls.
  */
public class InviteDialog extends Dialog implements TransactionClientListener, InviteTransactionServerListener, AckTransactionServerListener, SipProviderListener
{  
   /** The last invite message */
   Message invite_req;
   /** The last ack message */
   Message ack_req;

   /** The InviteTransactionServer. */
   InviteTransactionServer invite_ts;
   /** The AckTransactionServer. */
   AckTransactionServer ack_ts;
   /** The BYE TransactionServer. */
   TransactionServer bye_ts;

   /** The InviteDialog listener */
   InviteDialogListener listener;
   

   /** Whether offer/answer are in INVITE/200_OK */
   boolean invite_offer;

   protected static final int D_INIT=0;   
   protected static final int D_WAITING=1;   
   protected static final int D_INVITING=2;
   protected static final int D_INVITED=3;
   protected static final int D_REFUSED=4;   
   protected static final int D_ACCEPTED=5;
   protected static final int D_CALL=6;

   protected static final int D_ReWAITING=11;   
   protected static final int D_ReINVITING=12;
   protected static final int D_ReINVITED=13;
   protected static final int D_ReREFUSED=14;   
   protected static final int D_ReACCEPTED=15;

   protected static final int D_BYEING=7;
   protected static final int D_BYED=8;
   protected static final int D_CLOSE=9;
      
   /** Gets the dialog state */
   protected String getStatus()
   {  switch (status)
      {  case D_INIT       : return "D_INIT";
         case D_WAITING    : return "D_WAITING";   
         case D_INVITING   : return "D_INVITING";
         case D_INVITED    : return "D_INVITED";
         case D_REFUSED    : return "D_REFUSED";   
         case D_ACCEPTED   : return "D_ACCEPTED";
         case D_CALL       : return "D_CALL";
         case D_ReWAITING  : return "D_ReWAITING";   
         case D_ReINVITING : return "D_ReINVITING";
         case D_ReINVITED  : return "D_ReINVITED";
         case D_ReREFUSED  : return "D_ReREFUSED";   
         case D_ReACCEPTED : return "D_ReACCEPTED";
         case D_BYEING     : return "D_BYEING";
         case D_BYED       : return "D_BYED";
         case D_CLOSE      : return "D_CLOSE";
         default : return null;
      }
   }

   // ************************** Public methods **************************

   /** Whether the dialog is in "early" state. */
   public boolean isEarly()
   {  return status<D_ACCEPTED;
   }

   /** Whether the dialog is in "confirmed" state. */
   public boolean isConfirmed()
   {  return status>=D_ACCEPTED && status<D_CLOSE;
   }

   /** Whether the dialog is in "terminated" state. */
   public boolean isTerminated()
   {  return status==D_CLOSE;
   }

   /** Whether the session is "active". */
   public boolean isSessionActive()
   {  return (status==D_CALL);
   }

   /** Gets the invite message */   
   public Message getInviteMessage()
   {  return invite_req;
   }

   /** Creates a new InviteDialog. */
   public InviteDialog(SipProvider sip_provider, InviteDialogListener listener)
   {  super(sip_provider);
      init(listener);
   }

   /** Creates a new InviteDialog for the already received INVITE request <i>invite</i>. */
   public InviteDialog(SipProvider sip_provider, Message invite, InviteDialogListener listener)
   {  super(sip_provider);
      init(listener);
      
      changeStatus(D_INVITED);
      invite_req=invite;
      invite_ts=new InviteTransactionServer(sip_provider,invite_req,this);
      update(Dialog.UAS,invite_req);
   }
   
   /** Inits the InviteDialog. */
   private void init(InviteDialogListener listener)
   {  log=sip_provider.getLog();
      this.listener=listener;
      this.invite_req=null;
      this.ack_req=null;
      this.invite_offer=true;
      changeStatus(D_INIT);
   }

   /** Starts a new InviteTransactionServer. */
   public void listen()
   {  if (!statusIs(D_INIT)) return;
      //else
      changeStatus(D_WAITING);
      invite_ts=new InviteTransactionServer(sip_provider,this);
      invite_ts.listen();
   }  

   /** Starts a new InviteTransactionClient
     * and initializes the dialog state information.
     * @param callee the callee url (and display name)
     * @param caller the caller url (and display name)
     * @param contact the contact url OR the contact username
     * @param session_descriptor SDP body
     */
   public void invite(String callee, String caller, String contact, String session_descriptor)
   {  printLog("inside invite(callee,caller,contact,sdp)",LogLevel.MEDIUM);
      if (!statusIs(D_INIT)) return;
      // else
      NameAddress to_url=new NameAddress(callee);
      NameAddress from_url=new NameAddress(caller);
      SipURL request_uri=to_url.getAddress();
      
      NameAddress contact_url=null;
      if (contact!=null)
      {  if (contact.indexOf("sip:")>=0) contact_url=new NameAddress(contact);
         else contact_url=new NameAddress(new SipURL(contact,sip_provider.getViaAddress(),sip_provider.getPort()));
      }
      else contact_url=from_url;
      
      Message invite=MessageFactory.createInviteRequest(sip_provider,request_uri,to_url,from_url,contact_url,session_descriptor);
      // do invite
      invite(invite);
   }

   /** Starts a new InviteTransactionClient
     * and initializes the dialog state information
     * @param invite the INVITE message
     */
   public void invite(Message invite)
   {  printLog("inside invite(invite)",LogLevel.MEDIUM);
      if (!statusIs(D_INIT)) return;
      // else
      changeStatus(D_INVITING);
      invite_req=invite;
      update(Dialog.UAC,invite_req);
      InviteTransactionClient invite_tc=new InviteTransactionClient(sip_provider,invite_req,this);      
      invite_tc.request();
   }
   

   /** Starts a new InviteTransactionClient with offer/answer in 2xx/ack
     * and initializes the dialog state information */
   public void inviteWithoutOffer(String callee, String caller, String contact)
   {  invite_offer=false;
      invite(callee,caller,contact,null);
   }

   /** Starts a new InviteTransactionClient with offer/answer in 2xx/ack
     * and initializes the dialog state information */
   public void inviteWithoutOffer(Message invite)
   {  invite_offer=false;
      invite(invite);
   }


   /** Re-invites the remote user.
     * <p>Starts a new InviteTransactionClient and changes the dialog state information
     * <p> Parameters:
     * <br>- contact : the contact url OR the contact username; if null, the previous contact is used
     * <br>- session_descriptor : the message body
     */
   public void reInvite(String contact, String session_descriptor)
   {  printLog("inside reInvite(contact,sdp)",LogLevel.MEDIUM);
      if (!statusIs(D_CALL)) return;
      // else
      Message invite=MessageFactory.createInviteRequest(this,session_descriptor);
      if (contact!=null)
      {  NameAddress contact_url;
         if (contact.indexOf("sip:")>=0) contact_url=new NameAddress(contact);

⌨️ 快捷键说明

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