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

📄 protocol.java

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

import com.micromethod.sipstack.i.*;
import com.sun.cldc.io.ConnectionBaseInterface;
import java.io.*;
import java.util.Vector;
import javax.microedition.io.Connection;

/**
 * Protocol
 */
public class Protocol implements SipConnection, ConnectionBaseInterface {
  /**
   * Reader
   */
  protected class Reader extends InputStream {
    /**
     * m_buf
     */
    private byte m_buf[] = null;

    /**
     * m_pos
     */
    private int m_pos = 0;

    /**
     * m_errorCode
     */
    private int m_errorCode = 0;

    public int read() throws IOException {
      if (m_pos >= m_buf.length) {
        return m_errorCode;
      }
      else {
        return ((int) (m_buf[m_pos++]));
      }
    }

    /**
     * Reader Constructor
     * 
     * @param data
     */
    public Reader(byte data[]) {
      m_pos = 0;
      m_errorCode = -1;
      m_buf = data;
    }
  }

  /**
   * Writer
   */
  protected class Writer extends OutputStream {
    /**
     * m_buf
     */
    private byte m_buf[] = null;

    /**
     * m_pos
     */
    private int m_pos = 0;

    /**
     * m_isOpen
     */
    private boolean m_isOpen = false;

    public void write(int i1) throws IOException {
      if (!m_isOpen)
        throw new IOException("stream closed");
      if (m_pos >= m_buf.length) {
        throw new IOException("exceeding content output buffer");
      }
      else {
        m_buf[m_pos] = (byte) i1;
        m_pos++;
        return;
      }
    }

    /**
     * close
     * 
     * @throws IOException
     */
    public void close() throws IOException {
      if (!m_isOpen) {
        return;
      }
      m_isOpen = false;
      if (m_state != 10) {
        send();
      }
    }

    /**
     * isOpen
     * 
     * @return isOpen
     */
    public boolean a() {
      return m_isOpen;
    }

    /**
     * Writer Constructor
     * 
     * @param data
     */
    public Writer(byte data[]) {
      m_pos = 0;
      m_isOpen = true;
      m_buf = data;
    }
  }

  public static final String _fldbyte = "REGISTER";

  public static final String a = "INVITE";

  public static final String p = "ACK";

  public static final String k = "CANCEL";

  public static final String g = "BYE";

  public static final String t = "OPTIONS";

  public static final String m = "MESSAGE";

  public static final String h = "SUBSCRIBE";

  public static final String r = "NOTIFY";

  public static final String s = "PUBLISH";

  public static final String f = "PRACK";

  public static final String _fldnull = "REFER";

  protected static final byte j = -1;

  protected static final byte _fldnew = 0;

  protected static final byte _fldgoto = 1;

  protected static final byte _fldfor = 2;

  protected static final byte _flddo = 3;

  protected static final byte n = 4;

  protected static final byte _fldchar = 5;

  protected static final byte _fldvoid = 10;

  protected static final byte _fldtry = 10;

  protected static final byte v = 11;

  /**
   * m_connectionType
   */
  protected byte m_connectionType = 0;

  /**
   * m_state
   */
  protected byte m_state = 0;

  /**
   * m_startNotifier
   */
  protected boolean m_startNotifier = false;

  /**
   * m_txHandler
   */
  protected TxHandler m_txHandler = null;

  /**
   * m_sipAddress
   */
  protected SipAddress m_sipAddress = null;

  /**
   * m_transaction
   */
  protected BaseTransaction m_transaction = null;

  /**
   * m_sipDialog
   */
  protected Dialog m_sipDialog = null;

  /**
   * m_sipResponse
   */
  protected SipMessage m_sipResponse = null;

  /**
   * m_sipRequest
   */
  protected SipMessage m_sipRequest = null;

  /**
   * m_sipMessage
   */
  protected SipMessage m_sipMessage = null;

  /**
   * m_outData
   */
  protected byte m_outData[] = null;

  /**
   * m_inData
   */
  protected byte m_inData[] = null;

  /**
   * m_writer
   */
  protected Writer m_writer = null;

  /**
   * m_reader
   */
  protected Reader m_reader = null;

  /**
   * DEBUG
   */
  private static String DEBUG = null;

  /**
   * Protocol Constructor
   */
  public Protocol() {
    m_connectionType = 0;
    m_state = 0;
    m_startNotifier = false;
    m_sipResponse = null;
    m_sipRequest = null;
    m_sipMessage = null;
    // XXX defined DEBUG
    DEBUG = "true";
    m_txHandler = TxHandler.getInstance();
  }

  /**
   * openPrim
   * 
   * @param name
   * @param mode
   * @param timeouts
   * @return Connection
   * @throws IOException
   */
  public Connection openPrim(String name, int mode, boolean timeouts)
      throws IOException {
    TxHandler.getInstance();
    try {
      if (name.trim().length() != 0) {
        int j1 = -1;
        if (!name.startsWith(";")) {
          SipHeader sipheader1 = new SipHeader("Dummy", name);
          String s2 = sipheader1.getParameter("type");
          String s3 = sipheader1.getValue();
          if (s3 == null || !s3.equals("*")) {
            int k1 = name.indexOf(';');
            String s4;
            if (name.indexOf(';') >= 0) {
              SipHeader sipheader2 = new SipHeader("Dummy", name);
              s2 = sipheader2.getParameter("type");
              s4 = name.substring(0, k1);
            }
            else {
              s4 = name;
            }
            try {
              j1 = Integer.parseInt(s4);
            }
            catch (NumberFormatException numberformatexception) {
            }
            if (j1 == -1) {
              echo("Protocol: Initialize ClientConnection with URI '" + name
                  + "'");
              return new ClientConnection(name, mode, timeouts);
            }
            else {
              echo("Protocol: Initialize dedicated Notifier with port: " + j1
                  + " application type: " + s2);
              return new Notifier(j1, mode, timeouts, s2);
            }
          }
          else {
            if (s2 == null) {
              throw new IllegalArgumentException(
                  "Wrong server URI, parameter 'type' missing from sip:* server connection");
            }
            else {
              Notifier m1;
              echo("Protocol: Initialize Notifier with application type: " + s2
                  + " and share system SIP port");
              m1 = new Notifier(j1, mode, timeouts, s2);
              m1.a(true);
              if (!m_txHandler.register(m1)) {
                throw new IOException("Registration for "
                    + m_txHandler.getUserAddress() + " failed!");
              }
              return m1;
            }
          }
        }
        else {
          String s2;
          name = new String("0" + name);
          SipHeader sipheader = new SipHeader("Dummy", name);
          s2 = sipheader.getParameter("type");
          echo("Protocol: Initialize dedicated Notifier with arbitrary port and application type: "
              + s2);
          return new Notifier(0, mode, timeouts, s2);
        }
      }
      else {
        echo("Protocol: Initialize dedicated Notifier with arbitrary port");
        return ((Connection) (new Notifier(0, mode, timeouts, ((String) (null)))));
      }
    }
    catch (IOException e) {
      throw e;
    }
  }

  /**
   * close
   */
  public void close() {
    m_writer = null;
    m_reader = null;
    m_sipDialog = null;
    m_state = -1;
  }

  /**
   * checkConnection
   * 
   * @throws IOException
   */
  protected void checkConnection() throws IOException {
    if (m_state == -1) {
      throw new IOException("SIP connection closed");
    }
  }

  /**
   * checkEditable
   * 
   * @param objName
   * @throws SipException
   */
  private void checkEditable(String objName) throws SipException {

⌨️ 快捷键说明

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