clientconnection.java

来自「jsr-180 (SIP) 实现源码。可以在真实手机上使用」· Java 代码 · 共 705 行 · 第 1/2 页

JAVA
705
字号
          (byte) 5);
    }
    if (m_sipResponse == null) {
      throw new SipException("Original request missing", (byte) 6);
    }
    String s1 = ((SipRequest) m_sipResponse).getMethod();
    if (!s1.equals("INVITE")) {
      throw new SipException("ACK can not be applied to non-INVITE request",
          (byte) 6);
    }
    else {
      SipRequest u1 = createSipRequest("ACK", ((Notifier) (null)));
      String s2 = m_sipResponse.getHeaderValue("CSeq");
      int i = Integer.parseInt(s2.substring(0, s2.indexOf(' ')));
      u1.setHeader("CSeq", i + " " + "ACK", true);
      m_sipRequest = ((SipMessage) (u1));
      m_state = 1;
    }
  }

  /**
   * initCancel
   * 
   * @return SipClientConnection
   * @throws SipException
   */
  public SipClientConnection initCancel() throws SipException {
    if (m_state != 3) {
      throw new SipException("CANCEL can not be initialized in this state",
          (byte) 5);
    }
    if (m_sipResponse == null) {
      throw new SipException("Original request missing", (byte) 6);
    }
    String s1 = ((SipRequest) m_sipResponse).getMethod();
    if (!s1.equals("INVITE")) {
      throw new SipException("CANCEL can not be applied to non-INVITE request",
          (byte) 6);
    }
    ClientConnection s2 = new ClientConnection();
    s2.m_sipResponse = m_sipResponse;
    s2.m_sipAddress = m_sipAddress;
    SipHeader sipheader = new SipHeader("Via", m_sipResponse
        .getHeaderValue("Via"));
    s2.m_transaction = ((BaseTransaction) (m_txHandler.getNewClientTransaction(
        s2, "CANCEL", sipheader.getParameter("branch"))));
    Vector vector = new Vector(1);
    try {
      vector.addElement(((Object) (new PairStructure("From", m_sipResponse
          .getHeaderValue("From")))));
      vector.addElement(((Object) (new PairStructure("To", m_sipResponse
          .getHeaderValue("To")))));
      vector.addElement(((Object) (new PairStructure("Call-ID", m_sipResponse
          .getHeaderValue("Call-ID")))));
      vector.addElement(((Object) (new PairStructure("Max-Forwards", "70"))));
      vector.addElement(((Object) (new PairStructure("Via", m_sipResponse
          .getHeaderValue("Via")))));
      String s3 = m_sipResponse.getHeaderValue("Route");
      if (s3 != null)
        vector.addElement(((Object) (new PairStructure("Route", s3))));
      s2.m_sipRequest = ((SipMessage) (new SipRequest("CANCEL", m_sipAddress,
          vector)));
    }
    catch (Exception exception) {
      throw new SipException(exception.getMessage(), (byte) 6);
    }
    String s4 = m_sipResponse.getHeaderValue("CSeq");
    int i = Integer.parseInt(s4.substring(0, s4.indexOf(' ')));
    s2.m_sipRequest.setHeader("CSeq", i + " " + "CANCEL", true);
    m_state = 3;
    s2.m_state = 1;
    return ((SipClientConnection) (s2));
  }

  /**
   * receive
   * 
   * @param timeout
   * @return timeout
   * @throws SipException
   * @throws IOException
   */
  public boolean receive(long timeout) throws SipException, IOException {
    // ClientConnection s1 = this;
    if (m_state != 3 && m_state != 4) {
      throw new SipException("Can not receive message in this state", (byte) 5);
    }
    if (m_startNotifier) {
      m_startNotifier = false;
      throw new IOException("Client transaction timeout");
    }

    try {
      if (timeout <= 0) {
        if (!m_sipResponses.isEmpty()) {
          m_sipMessage = (SipMessage) m_sipResponses.firstElement();
          m_sipResponses.removeElement(((Object) (m_sipMessage)));
          m_sipRequest = m_sipMessage;
          int i = getStatusCode();
          updateDialogInfo(i, (SipResponse) m_sipRequest);
          if (i < 200) {
            m_state = 3;
          }
          else if (i >= 200 && i < 300) {
            m_state = 4;
          }
          else if (i >= 300) {
            m_state = 4;
          }
          return true;
        }
        else {
          return false;
        }
      }
      else {
        do {
          if (!m_sipResponses.isEmpty()) {
            m_sipMessage = (SipMessage) m_sipResponses.firstElement();
            m_sipResponses.removeElement(((Object) (m_sipMessage)));
            m_sipRequest = m_sipMessage;
            int i = getStatusCode();
            updateDialogInfo(i, (SipResponse) m_sipRequest);
            if (i < 200) {
              m_state = 3;
            }
            else if (i >= 200 && i < 300) {
              m_state = 4;
            }
            else if (i >= 300) {
              m_state = 4;
            }
            return true;
          }
          else {
            timeout = timeout(timeout);
          }
        }
        while (timeout > 0L || !m_sipResponses.isEmpty());
        return false;
      }
    }
    catch (EmptyStackException e) {
      e.printStackTrace();
      return false;
    }
  }
  
  /**
   * updateDialogInfo
   * 
   * @param statusCode
   * @param sipResponse
   */
  private void updateDialogInfo(int statusCode, SipResponse sipResponse) {
    if (m_sipDialog == null) {
      m_sipDialog = m_txHandler.getSipClientDialog(this);
    }
    else if (statusCode >= 200 && statusCode < 300) {
      if (getMethod().equals("BYE")) {
        m_sipDialog.setState((byte) 0);
      }
      else if (getMethod().equals("NOTIFY")) {
        SipHeader sipheader = new SipHeader("Subscription-State", m_sipResponse
            .getHeaderValue("Subscription-State"));
        if (sipheader.getValue().equals("terminated")) {
          m_sipDialog.setState((byte) 0);
        }
      }
      else {
        m_sipDialog.setState((byte) 2);
      }
    }
    else if (statusCode >= 300) {
      m_sipDialog.setState((byte) 0);
    }
    if (getMethod().equals("SUBSCRIBE")) {
      try {
        m_sipDialog.updateOnSubscribe(statusCode, sipResponse, m_sipResponse);
      }
      catch (Exception exception) {
        Protocol.echo("ClientConnection: Error updating the dialog info; "
            + exception.getMessage());
        Protocol.echo("ClientConnection: Terminate the dialog state");
        m_sipDialog.setState((byte) 0);
      }
    }
    if (m_id > 0 && m_sipClientConnectionListenerImpl != null) {
      m_sipClientConnectionListenerImpl.refreshResponse(m_id, sipResponse,
          m_sipDialog);
    }
  }

  /**
   * timeout
   * 
   * @param timeout
   * @return Expires time
   * @throws IOException
   */
  private int timeout(long timeout) throws IOException {
    try {
      // ClientConnection s1 = this;
      Protocol.echo("ClientConnection: " + m_transaction.m_clientTransactionID
          + " waiting for message...");
      long l1 = System.currentTimeMillis();
      try {
        synchronized (this) {
          this.wait(timeout);
        }
        timeout = getRemainTime(l1, timeout);
        if (m_startNotifier) {
          m_startNotifier = false;
          throw new IOException("Client transaction timeout");
        }
        Protocol.echo("ClientConnection: "
            + m_transaction.m_clientTransactionID + " running");
        return (int) timeout;
      }
      catch (Exception exception) {
        exception.printStackTrace();
        timeout = getRemainTime(l1, timeout);
        return (int) timeout;
      }
    }
    catch (Exception exception1) {
      throw new IOException(exception1.getMessage());
    }
  }

  /**
   * getRemainTime
   * 
   * @param lastaccess
   * @param timeout
   * @return remain time
   */
  private long getRemainTime(long lastaccess, long timeout) {
    long l2 = timeout - (System.currentTimeMillis() - lastaccess);
    if (l2 < 0L) {
      return 0L;
    }
    else {
      return l2;
    }
  }

  /**
   * addNotifyResponse
   * 
   * @param sipResponse
   */
  protected void addNotifyResponse(SipResponse sipResponse) {
    synchronized (this) {
      m_sipResponses.addElement(((Object) (sipResponse)));
      ((Object) this).notify();
    }
    try {
      if (m_sipClientConnectionListener != null)
        m_sipClientConnectionListener
            .notifyResponse(((SipClientConnection) (this)));
    }
    catch (Exception exception) {
    }
  }

  /**
   * notifyResponse
   */
  protected void notifyResponse() {
    synchronized (this) {
      m_startNotifier = true;
      ((Object) this).notify();
    }
    try {
      if (m_sipClientConnectionListener != null)
        m_sipClientConnectionListener
            .notifyResponse(((SipClientConnection) (this)));
    }
    catch (Exception exception) {
    }
  }

  /**
   * setListener
   */
  public void setListener(
      SipClientConnectionListener sipclientconnectionlistener)
      throws IOException {
    checkConnection();
    m_sipClientConnectionListener = sipclientconnectionlistener;
  }

  /**
   * enableRefresh
   * 
   * @param sipRefreshListener
   * @return id
   */
  public int enableRefresh(SipRefreshListener sipRefreshListener)
      throws SipException {
    if (sipRefreshListener == null)
      return 0;
    if (m_state != 1) {
      throw new SipException("Can not set refresh in this state", (byte) 5);
    }
    else {
      m_sipClientConnectionListenerImpl = RefreshHelper.getInstance();
      m_id = m_sipClientConnectionListenerImpl.getRefreshID(sipRefreshListener,
          this, m_id);
      return m_id;
    }
  }

  /**
   * setCredentials
   * 
   * @param username
   * @param password
   * @param realm
   * @throws SipException
   */
  public void setCredentials(String username, String password, String realm)
      throws SipException {
    if (m_state != 1) {
      throw new SipException("Can not set credentials in this state", (byte) 5);
    }
    if (username == null) {
      throw new NullPointerException("User name is null");
    }
    if (password == null) {
      throw new NullPointerException("Password is null");
    }
    if (realm == null) {
      throw new NullPointerException("Realm is null");
    }
    else {
      Protocol.echo("setCredentials: not implemented yet!");
    }
  }

  /**
   * close
   */
  public void close() {
    if (m_transaction != null) {
      m_txHandler.removeTransaction(m_transaction);
    }
    m_transaction = null;
    super.close();
  }
}

⌨️ 快捷键说明

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