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

📄 protocol.java

📁 jsr-180 (SIP) 实现源码。可以在真实手机上使用
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    if (m_state != 1) {
      throw new SipException("Can not edit " + objName + " in this sate",
          (byte) 5);
    }
  }

  /**
   * isSpecialState
   * 
   * @return yes:true, no:false
   */
  protected boolean isSpecialState() {
    return m_state == 1 || m_state == 5 || m_state == 3 || m_state == 4
        || m_state == 2;
  }

  /**
   * checkContact
   * 
   * @throws SipException
   */
  private void checkContact() throws SipException {
    try {
      SipHeader sipheader = new SipHeader("From", m_sipRequest
          .getHeaderValue("From"));
      if (sipheader != null) {
        String s1 = sipheader.getParameter("tag");
        if (s1 == null) {
          sipheader.setParameter("tag", ""
              + Math.abs(m_txHandler.getViaValue().hashCode()
                  + sipheader.getValue().hashCode()));
          m_sipRequest.setHeader("From", sipheader.getHeaderValue(), true);
        }
      }
    }
    catch (Exception exception) {
    }
    if (m_sipRequest.isRequest()) {
      String s2 = ((SipRequest) m_sipRequest).getMethod();
      if ((s2.equals("INVITE") || s2.equals("SUBSCRIBE") || s2.equals("REFER"))
          && m_sipRequest.getHeaderValue("Contact") == null)
        throw new SipException("Missing Contact header from " + s2
            + " request!", (byte) 8);
    }
  }

  /**
   * send
   * 
   * @throws IOException
   * @throws IllegalArgumentException
   * @throws InterruptedIOException
   * @throws SecurityException
   * @throws SipException
   */
  public void send() throws IOException, IllegalArgumentException,
      InterruptedIOException, SecurityException, SipException {
    if (m_connectionType == 10) {
      if (m_state != 1 && m_state != 2)
        throw new SipException("Can not send in this state", (byte) 5);
    }
    else {
      if (m_state != 1 && m_state != 2 && m_state != 4)
        throw new SipException("Can not send in this state", (byte) 5);
      if (m_state == 4) {
        if (m_sipRequest.isRequest())
          throw new SipException("Can not send "
              + ((SipRequest) m_sipRequest).getMethod()
              + " from SipServerConnection", (byte) 8);
        int i1 = ((SipResponse) m_sipRequest).getStatusCode();
        if (i1 >= 200 && i1 < 300) {
          m_txHandler.resendSipMessage(m_sipRequest, true);
          return;
        }
        else {
          throw new SipException("Can not resend " + i1 + " in this state",
              (byte) 5);
        }
      }
    }
    if (m_writer != null && m_state == 2) {
      m_state = 10;
      if (m_writer.a())
        m_writer.close();
      if (m_outData != null)
        m_sipRequest.setContent(m_outData);
      m_outData = null;
    }
    m_state = 10;
    if (m_connectionType == 10) {
      checkContact();
      m_txHandler.sendClientTx((ClientTransaction) m_transaction,
          (SipRequest) m_sipRequest);
      String s1 = ((SipRequest) m_sipRequest).getMethod();
      if (s1.equals("ACK"))
        m_state = 4;
      else
        m_state = 3;
    }
    else {
      m_txHandler.startTryingTask((ServerTransaction) m_transaction,
          (SipResponse) m_sipRequest);
      int j1 = ((SipResponse) m_sipRequest).getStatusCode();
      if (j1 < 200)
        m_state = 5;
      else
        m_state = 4;
      if (m_sipDialog == null)
        m_sipDialog = m_txHandler.getSipServerDialog((ServerConnection) this);
      else if (j1 < 200)
        m_sipDialog.setState((byte) 1);
      else if (j1 < 300)
        m_sipDialog.setState((byte) 2);
      else
        m_sipDialog.setState((byte) 0);
      String s2 = ((SipResponse) m_sipRequest).getMethod();
      if (m_sipDialog != null && s2.equals("BYE") && j1 >= 200 && j1 < 300)
        m_sipDialog.setState((byte) 0);
    }
  }

  /**
   * setHeader
   * 
   * @param name
   * @param value
   * @throws SipException
   */
  public void setHeader(String name, String value) throws SipException {
    checkEditable("headers");
    m_sipRequest.setHeader(name, value, true);
  }

  /**
   * addHeader
   * 
   * @param name
   * @param value
   * @throws SipException
   */
  public void addHeader(String name, String value) throws SipException {
    checkEditable("headers");
    m_sipRequest.addTopHeader(name, value);
  }

  /**
   * removeHeader
   * 
   * @param name
   * @throws SipException
   */
  public void removeHeader(String name) throws SipException {
    checkEditable("headers");
    m_sipRequest.removeHeader(name);
  }

  /**
   * getHeaders
   * 
   * @param name
   * @return Header String []
   */
  public String[] getHeaders(String name) {
    try {
      checkConnection();
    }
    catch (IOException ioexception) {
      return null;
    }
    if (!isSpecialState())
      return null;
    Vector vector = m_sipRequest.getHeaders(name);
    String as[] = null;
    if (vector != null) {
      as = new String[vector.size()];
      for (short word0 = 0; word0 < vector.size(); word0++) {
        PairStructure t1 = (PairStructure) vector.elementAt(((int) (word0)));
        as[word0] = t1.getValue();
      }

    }
    return as;
  }

  /**
   * getHeader
   * 
   * @param name
   * @return Header value
   */
  public String getHeader(String name) {
    try {
      checkConnection();
    }
    catch (IOException ioexception) {
      return null;
    }
    if (!isSpecialState())
      return null;
    else
      return m_sipRequest.getHeaderValue(name);
  }

  /**
   * getMethod
   * 
   * @return method
   */
  public String getMethod() {
    try {
      checkConnection();
    }
    catch (IOException ioexception) {
      return null;
    }
    if (!isSpecialState())
      return null;
    if (m_sipRequest.isRequest())
      return ((SipRequest) m_sipRequest).getMethod();
    else
      return ((SipResponse) m_sipRequest).getMethod();
  }

  /**
   * getRequestURI
   * 
   * @return requestURI
   */
  public String getRequestURI() {
    try {
      checkConnection();
    }
    catch (IOException ioexception) {
      return null;
    }
    if (!isSpecialState())
      return null;
    if (m_sipRequest != null && m_sipRequest.isRequest())
      return ((SipRequest) m_sipRequest).getSipAddress().toString();
    else
      return null;
  }

  /**
   * getStatusCode
   * 
   * @return statusCode
   */
  public int getStatusCode() {
    try {
      checkConnection();
    }
    catch (IOException ioexception) {
      return 0;
    }
    if (!isSpecialState())
      return 0;
    if (m_sipRequest.isResponse())
      return ((SipResponse) m_sipRequest).getStatusCode();
    else
      return 0;
  }

  /**
   * getReasonPhrase
   * 
   * @return reasonPhrase
   */
  public String getReasonPhrase() {
    try {
      checkConnection();
    }
    catch (IOException ioexception) {
      return null;
    }
    if (!isSpecialState())
      return null;
    if (m_sipRequest.isResponse())
      return ((SipResponse) m_sipRequest).getReasonPhrase();
    else
      return null;
  }

  /**
   * getDialog
   * 
   * @return SipDialog
   */
  public SipDialog getDialog() {
    try {
      checkConnection();
    }
    catch (IOException ioexception) {
      return null;
    }
    if (!isSpecialState())
      return null;
    else
      return ((SipDialog) (m_sipDialog));
  }

  /**
   * openContentInputStream
   * 
   * @return InputStream
   * @throws IOException
   * @throws SipException
   */
  public InputStream openContentInputStream() throws IOException, SipException {
    if (m_connectionType == 10) {
      if (m_state != 3 && m_state != 4)
        throw new SipException("Can not open InputStream in this state",
            (byte) 5);
    }
    else if (m_state != 5)
      throw new SipException("Can not open InputStream in this state", (byte) 5);
    m_inData = m_sipRequest.getContent();
    if (m_inData == null)
      throw new IOException("no input available");
    else
      return ((InputStream) (m_reader = new Reader(m_inData)));
  }

  /**
   * openContentOutputStream
   * 
   * @return OutputStream
   * @throws SipException
   */
  public OutputStream openContentOutputStream() throws SipException {
    if (m_state != 1)
      throw new SipException("Can not open OutputStream in this state",
          (byte) 5);
    int i1 = 0;
    String s1 = getHeader("Content-Length");
    if (s1 == null)
      throw new SipException("Content-Length not set", (byte) 4);
    if (s1 != null)
      try {
        i1 = Integer.parseInt(s1);
      }
      catch (Exception exception) {
        throw new SipException("Illegal Content-Length", (byte) 4);
      }
    if (getHeader("Content-Type") == null) {
      throw new SipException("Content-Type not set", (byte) 3);
    }
    else {
      m_outData = new byte[i1];
      m_state = 2;
      return ((OutputStream) (m_writer = new Writer(m_outData)));
    }
  }

  /**
   * echo
   * 
   * @param message
   */
  protected static void echo(String message) {
    if (DEBUG != null) {
      System.out.println(message);
    }
  }

}

⌨️ 快捷键说明

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