⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 clientconnection.java

📁 jsr-180 (SIP) 实现源码。可以在真实手机上使用
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package com.micromethod.sipstack.ri;

import java.io.IOException;
import java.util.EmptyStackException;
import java.util.Vector;

import com.micromethod.sipstack.i.*;

/**
 * ClientConnection
 */
public class ClientConnection extends Protocol implements SipClientConnection {
  /**
   * m_user
   */
  private String m_user = null;

  /**
   * m_userAddress
   */
  private String m_userAddress = null;

  /**
   * m_displayName
   */
  private String m_displayName = null;

  /**
   * m_sipResponses
   */
  private Vector m_sipResponses = null;

  /**
   * m_localAddress
   */
  private String m_localAddress = null;

  /**
   * m_viaValue
   */
  private String m_viaValue = null;

  /**
   * m_sipClientConnectionListener
   */
  private SipClientConnectionListener m_sipClientConnectionListener = null;

  /**
   * m_id
   */
  protected int m_id = 0;

  /**
   * m_sipClientConnectionListenerImpl
   */
  protected RefreshHelper m_sipClientConnectionListenerImpl = null;

  /**
   * ClientConnection Constructor
   * 
   * @param host
   * @param port
   * @param flag
   * @throws IOException
   * @throws IllegalArgumentException
   */
  protected ClientConnection(String host, int port, boolean flag)
      throws IOException, IllegalArgumentException {
    m_localAddress = null;
    m_viaValue = null;
    m_sipClientConnectionListener = null;
    m_id = 0;
    m_sipClientConnectionListenerImpl = null;
    String s2 = "sip:" + host;
    SipAddress sipaddress = null;
    try {
      sipaddress = new SipAddress(s2);
    }
    catch (Exception exception) {
      throw new IllegalArgumentException(exception.getMessage());
    }
    String s3 = sipaddress.getParameter("transport");
    if (s3 != null) {
      s3 = s3.toLowerCase();
      String s4 = new String("UDP");
      s4 = s4.toLowerCase();
      if (!s3.equals(((Object) (s4))))
        throw new SipException("Transport " + s3 + " not supported", (byte) 1);
    }
    m_sipResponses = new Vector();
    if (!m_txHandler.waiting()) {
      m_txHandler.getListener(0, ((Notifier) (null)));
    }
    m_localAddress = m_txHandler.getLoaclAddress();
    m_user = m_txHandler.getUser();
    m_userAddress = m_txHandler.getUserAddress();
    m_displayName = m_txHandler.getDisplayName();
    if (sipaddress.getHost().equals("localhost")) {
      sipaddress.setHost(m_localAddress);
    }
    m_sipAddress = new SipAddress(sipaddress.toString());
    m_viaValue = m_txHandler.getViaValue();
    m_connectionType = 10;
    m_state = 0;
  }

  /**
   * SipClientConnectionImpl Constructor
   */
  private ClientConnection() {
    m_localAddress = null;
    m_viaValue = null;
    m_sipClientConnectionListener = null;
    m_id = 0;
    m_sipClientConnectionListenerImpl = null;
    m_sipResponses = new Vector();
    m_viaValue = m_txHandler.getViaValue();
    m_connectionType = 10;
    m_state = 0;
  }

  /**
   * SipClientConnectionImpl Constructor
   * 
   * @param sipDialog
   * @param method
   * @throws SipException
   * @throws IllegalArgumentException
   */
  protected ClientConnection(Dialog sipDialog, String method)
      throws SipException, IllegalArgumentException {
    this();
    m_sipDialog = sipDialog;
    initRequest(method, ((SipConnectionNotifier) (null)));
    Protocol.echo("ClientConnection created from Dialog");
  }

  /**
   * SipClientConnectionImpl Constructor
   * 
   * @param sipDialog
   */
  protected ClientConnection(Dialog sipDialog) {
    this();
    m_sipDialog = sipDialog;
  }

  /**
   * checkMethod
   * 
   * @param method
   * @throws IllegalArgumentException
   */
  private void checkMethod(String method) throws IllegalArgumentException {
    Vector vector = null;
    try {
      vector = SipHeaderParser.parseString(method);
    }
    catch (Exception exception) {
      throw new IllegalArgumentException("Illegal method '" + method + "'; "
          + exception.getMessage());
    }
    if (vector.size() != 2) {
      throw new IllegalArgumentException("Illegal method '" + method + "'");
    }
    Cache o1 = (Cache) vector.elementAt(0);
    if (o1.m_ctrlState != 1 && o1.m_ctrlState != 5) {
      throw new IllegalArgumentException("Illegal method '" + method + "'");
    }
  }

  /**
   * createSipRequest
   * 
   * @param method
   * @param sipConnectionNotifier
   * @return sipRequest
   */
  protected SipRequest createSipRequest(String method,
      Notifier sipConnectionNotifier) {
    SipHeader sipheader = null;
    SipAddress sipaddress = null;
    SipAddress sipaddress1 = null;
    String s2 = null;
    String s3 = null;
    String s4 = null;
    String as[] = null;
    boolean flag = false;
    if (method.equals("INVITE") || method.equals("REGISTER")
        || method.equals("SUBSCRIBE") || method.equals("REFER"))
      if (sipConnectionNotifier != null)
        s2 = sipConnectionNotifier.getSipAddress().toString();
      else if (m_sipDialog != null)
        s2 = new String(m_sipDialog.m_contactValue);
    String s5;
    if (method.equals("ACK"))
      s5 = m_viaValue + ";branch=" + "z9hG4bK" + m_transaction.m_newBranchTail;
    else
      s5 = m_viaValue + ";branch=" + "z9hG4bK" + m_transaction.m_oldBranchTail;
    if (m_sipDialog != null)
      try {
        SipAddress sipaddress2 = new SipAddress(m_sipDialog.m_toURI);
        sipheader = new SipHeader("From", sipaddress2.toString());
        sipheader.setParameter("tag", m_sipDialog.getToTag());
        sipaddress = new SipAddress(m_sipDialog.m_contactURI);
        sipaddress1 = new SipAddress(m_sipDialog.m_fromURI);
        if (m_sipDialog.getFromTag() != null)
          sipaddress1.setParameter("tag", m_sipDialog.getFromTag());
        if (m_sipDialog.m_recordRoutes != null) {
          SipAddress sipaddress4 = new SipAddress(m_sipDialog.m_recordRoutes[0]);
          if (sipaddress4.getParameter("lr") != null) {
            as = m_sipDialog.m_recordRoutes;
            flag = true;
          }
          else {
            sipaddress = new SipAddress(sipaddress4.getURI());
            as = new String[m_sipDialog.m_recordRoutes.length];
            if (m_sipDialog.m_recordRoutes.length > 1) {
              for (int i = 1; i < m_sipDialog.m_recordRoutes.length; i++)
                as[i - 1] = m_sipDialog.m_recordRoutes[i];

            }
            as[as.length - 1] = m_sipDialog.m_contactURI;
            flag = false;
          }
        }
        s3 = m_sipDialog.getCallIDValue();
        m_sipDialog.m_uacCSeqNum++;
        s4 = m_sipDialog.m_uacCSeqNum + " " + method;
      }
      catch (Exception exception) {
        throw new IllegalArgumentException(exception.getMessage());
      }
    else
      try {
        SipAddress sipaddress3 = new SipAddress(m_displayName + " "
            + m_userAddress);
        sipheader = new SipHeader("From", sipaddress3.toString());
        sipheader.setParameter("tag", ""
            + Math.abs(m_viaValue.hashCode() + m_userAddress.hashCode()));
        s3 = m_transaction.m_oldBranchTail + "@" + m_localAddress;
        s4 = "1 " + method;
        sipaddress = m_sipAddress;
        sipaddress1 = new SipAddress(sipaddress.getScheme() + ":"
            + sipaddress.getUser() + "@" + sipaddress.getHost());
        SipAddress sipaddress5 = m_txHandler.getSipAddress();
        if (sipaddress5 != null) {
          as = new String[1];
          if (sipaddress5.getParameter("lr") != null) {
            as[0] = new String(sipaddress5.toString());
            flag = true;
          }
          else {
            as[0] = sipaddress.toString();
            sipaddress = sipaddress5;
          }
        }
      }
      catch (Exception exception1) {
        throw new IllegalArgumentException(exception1.getMessage());
      }
    Vector vector = new Vector(4);
    SipRequest u1;
    try {
      vector.addElement(((Object) (new PairStructure("From", sipheader
          .getHeaderValue()))));
      vector.addElement(((Object) (new PairStructure("To", sipaddress1
          .toString()))));
      if (s2 != null)
        vector.addElement(((Object) (new PairStructure("Contact", s2))));
      u1 = new SipRequest(method, sipaddress, vector);
      u1.setHeader("Call-ID", s3, true);
      u1.setHeader("CSeq", s4, true);
      u1.setHeader("Max-Forwards", "70", true);
      u1.setHeader("Via", s5, true);
      if (as != null) {
        for (int j = as.length - 1; j >= 0; j--)
          u1.addTopHeader("Route", as[j]);

        u1.setHasRoute(flag);
      }
    }
    catch (Exception exception2) {
      throw new IllegalArgumentException(exception2.getMessage());
    }
    return u1;
  }

  /**
   * initRequest
   * 
   * @param method
   * @param sipConnectionNotifier
   * @throws
   */
  public void initRequest(String method,
      SipConnectionNotifier sipConnectionNotifier) throws SipException {
    if (m_state != 0) {
      throw new SipException("Can not initialize request in this state",
          (byte) 5);
    }
    if (method == null) {
      throw new NullPointerException("Method is null");
    }
    if (method.length() == 0) {
      throw new IllegalArgumentException("Method is empty");
    }
    checkMethod(method);
    m_transaction = ((BaseTransaction) (m_txHandler.getNewClientTransaction(
        this, method, ((String) (null)))));
    SipRequest u1 = null;
    if (sipConnectionNotifier != null
        && (sipConnectionNotifier instanceof Notifier)) {
      u1 = createSipRequest(method, (Notifier) sipConnectionNotifier);
    }
    else {
      u1 = createSipRequest(method, ((Notifier) (null)));
    }
    m_sipResponse = ((SipMessage) (u1));
    m_sipRequest = ((SipMessage) (u1));
    m_state = 1;
  }

  /**
   * setRequestURI
   * 
   * @param requestURI
   * @throws SipException
   */
  public void setRequestURI(String requestURI) throws SipException,
      IllegalArgumentException {
    if (m_state != 1) {
      throw new SipException("Request-URI can not be set in this state",
          (byte) 5);
    }
    SipAddress sipaddress = new SipAddress(requestURI);
    if (sipaddress.getDisplayName() != null) {
      throw new IllegalArgumentException(
          "Display name not allowed in Request-URI");
    }
    else {
      ((SipRequest) m_sipRequest).setSipAddress(sipaddress);
    }
  }

  /**
   * initAck
   * 
   * @throws SipException
   */
  public void initAck() throws SipException {
    if (m_state != 4) {
      throw new SipException("ACK can not be initialized in this state",

⌨️ 快捷键说明

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