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

📄 txhandler.java

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

import com.micromethod.sipstack.i.*;
import java.io.IOException;
import java.util.*;

/**
 * TxHandler
 */
public class TxHandler {

  public static final String c = "SIP/2.0";

  public static final String _fldlong = "UDP";

  public static final String _fldif = "z9hG4bK";

  /**
   * m_txHandler
   */
  private static TxHandler m_txHandler = null;

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

  /**
   * m_useable
   */
  private boolean m_useable = false;

  /**
   * m_isBindListener
   */
  private boolean m_isBindListener = false;

  /**
   * m_clientTransactions
   */
  private Hashtable m_clientTransactions = null;

  /**
   * m_serverTransactions
   */
  private Hashtable m_serverTransactions = null;

  /**
   * m_sipDialogs
   */
  private Hashtable m_sipDialogs = null;

  /**
   * m_listeners
   */
  private Hashtable m_listeners = null;

  /**
   * m_listener
   */
  private Listener m_listener = null;

  /**
   * m_via
   */
  private String m_via = null;

  /**
   * m_user
   */
  private String m_user = null;

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

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

  /**
   * m_proxyAddress
   */
  private SipAddress m_proxyAddress = null;

  /**
   * m_localPort
   */
  private int m_localPort = 0;

  /**
   * m_autoTrying, send 100 Trying automatically
   */
  private boolean m_autoTrying = false;

  /**
   * m_registerAddress
   */
  private SipAddress m_registerAddress = null;

  /**
   * m_isRegistered
   */
  private boolean m_isRegistered = false;

  /**
   * m_random
   */
  private static Random m_random = new Random(System.currentTimeMillis());

  /**
   * TxHandler Constructor
   */
  private TxHandler() {
    m_localAddress = "127.0.0.1";
    m_useable = false;
    m_isBindListener = false;
    m_via = null;
    m_proxyAddress = null;
    m_localPort = 0;
    m_autoTrying = false;
    m_registerAddress = null;
    m_isRegistered = false;
    m_clientTransactions = new Hashtable();
    m_clientTransactions.put("name", "client");
    m_serverTransactions = new Hashtable();
    m_serverTransactions.put("name", "server");
    m_sipDialogs = new Hashtable();
    m_listeners = new Hashtable();
    // XXX defined autoTrying
    String s1 = "true";
    if (s1 != null && s1.equals("true")) {
      m_autoTrying = true;
      Protocol.echo("Configuration: send 100 Trying automatically");
    }
    // XXX defined registerAddress
    s1 = null;
    if (s1 != null) {
      m_registerAddress = new SipAddress(s1);
      Protocol.echo("Configuration Registrar: " + m_registerAddress.toString());
    }
    // XXX defined displayName & userAddress
    m_displayName = "micromethod";
    m_userAddress = "sip:" + m_displayName + "@127.0.0.1";
    if (m_userAddress == null || m_userAddress.trim().length() == 0) {
      throw new IllegalArgumentException("SIP user address not configured");
    }
    try {
      // XXX defined proxyAddress
      String s2 = null;
      if (s2 != null) {
        m_proxyAddress = new SipAddress(s2);
      }
      Protocol.echo("Configuration Outbound Proxy: "
          + m_proxyAddress.toString());
    }
    catch (Exception exception) {
      m_proxyAddress = null;
    }
    try {
      // XXX defined localPort
      String s3 = "5060";
      m_localPort = Integer.parseInt(s3);
      Protocol.echo("Configuration Local Port: " + m_localPort);
    }
    catch (Exception exception1) {
      m_localPort = 0;
    }
    Protocol.echo("Configuration Display Name: " + m_displayName);
    Protocol.echo("Configuration User Address: " + m_userAddress);
    SipAddress sipaddress = null;
    try {
      sipaddress = new SipAddress(m_userAddress.trim());
    }
    catch (Exception exception2) {
      throw new IllegalArgumentException("Invalid SIP user address; "
          + exception2.getMessage());
    }
    m_user = sipaddress.getUser();
    Protocol.echo("Configuration User Name: " + m_user);
  }

  /**
   * getInstance
   * 
   * @return Singleton TxHandler
   */
  public static synchronized TxHandler getInstance() {
    if (m_txHandler == null) {
      m_txHandler = new TxHandler();
    }
    return m_txHandler;
  }

  /**
   * getListener
   * 
   * @param localPort
   * @param sipConnectionNotifier
   * @return Listener
   * @throws IOException
   */
  public Listener getListener(int localPort, Notifier sipConnectionNotifier)
      throws IOException {
    Listener w1;
    m_isBindListener = true;
    m_useable = false;
    w1 = null;
    if (m_listener == null) {
      Protocol.echo("TxHandler: opening shared system point");
      m_listener = new Listener(this, m_localPort, true);
      m_localPort = m_listener.getPort();
      StringBuffer stringbuffer = new StringBuffer("SIP/2.0");
      stringbuffer.append("/");
      stringbuffer.append("UDP");
      stringbuffer.append(" ");
      stringbuffer.append(m_listener.getLocalAddress());
      stringbuffer.append(":");
      stringbuffer.append(m_listener.getPort());
      m_via = stringbuffer.toString();
      m_localAddress = m_listener.getLocalAddress();
      Protocol.echo("TxHandler: system point open, local Via is:\n\t"
          + stringbuffer);
      m_listener.setSipAddress(new SipAddress("sip:" + m_user + "@"
          + m_localAddress + ":" + m_localPort));
      m_listeners.put(((Object) (new Integer(m_listener.getPort()))),
          ((Object) (m_listener)));
    }
    if (localPort == -1) {
      localPort = m_localPort;
    }
    try {
      if (sipConnectionNotifier != null) {
        w1 = (Listener) m_listeners.get(((Object) (new Integer(localPort))));
        if (w1 != null) {
          Protocol.echo("TxHandler: found existing listening point");
          if (sipConnectionNotifier.getScheme() == null) {
            throw new Exception(
                "Could not bind new SipConnectionNotifier with no application identifier. Use 'type' to specify application identifier.");
          }
          if (!w1.addNotifier(sipConnectionNotifier)) {
            throw new Exception(
                "Could not bind new SipConnectionNotifier with application identifier type: '"
                    + sipConnectionNotifier.getScheme()
                    + "' to listening point at port " + localPort);
          }
          m_useable = true;
          m_isBindListener = false;
          callNotifyAll();
          return w1;
        }
        Protocol.echo("TxHandler: new listening point");
        w1 = new Listener(this, localPort, false);
        w1.setSipAddress(new SipAddress("sip:" + m_user + "@" + m_localAddress
            + ":" + w1.getPort()));
        if (!w1.addNotifier(sipConnectionNotifier)) {
          throw new Exception(
              "TxHandler: fatal internal error, could not add notifier to newly created listening point!");
        }
        m_listeners
            .put(((Object) (new Integer(w1.getPort()))), ((Object) (w1)));
      }
      m_useable = true;
      m_isBindListener = false;
      callNotifyAll();
      return w1;
    }
    catch (Exception e) {
      m_useable = false;
      m_isBindListener = false;
      throw new IOException(((Exception) (e)).getMessage());
    }
  }

  /**
   * waiting
   * 
   * @return useable
   */
  public boolean waiting() {
    synchronized (this) {
      try {
        if (m_isBindListener) {
          Protocol.echo("Waiting for TxHandler...");
          ((Object) this).wait();
        }
      }
      catch (Exception exception) {
        exception.printStackTrace();
      }
    }
    return m_useable;
  }

  /**
   * callNotifyAll
   */
  private void callNotifyAll() {
    synchronized (this) {
      ((Object) this).notifyAll();
    }
  }

  /**
   * register
   * 
   * @param sipConnectionNotifier
   * @return Registered
   */
  protected boolean register(Notifier sipConnectionNotifier) {
    m_isRegistered = false;
    if (m_registerAddress == null)
      return true;
    try {
      ClientConnection s1 = null;
      s1 = new ClientConnection(m_registerAddress.getHost() + ":"
          + m_registerAddress.getPort(), 0, false);
      ((SipClientConnection) (s1))
          .initRequest(
              "REGISTER",
              ((com.micromethod.sipstack.i.SipConnectionNotifier) (sipConnectionNotifier)));
      ((SipClientConnection) (s1)).setHeader("To", ((SipClientConnection) (s1))
          .getHeader("From"));
      ((SipClientConnection) (s1)).send();
      ((SipClientConnection) (s1)).receive(30000L);
      if (((SipClientConnection) (s1)).getStatusCode() == 200) {
        String as[] = ((SipClientConnection) (s1)).getHeaders("Contact");
        for (int j = 0; j < as.length; j++)
          Protocol.echo("TxHandler: Registered Contact: " + as[j] + "\n");

        m_isRegistered = true;
      }
    }
    catch (Exception exception) {
      Protocol.echo("TxHandler: Registration failed!");
    }
    return m_isRegistered;
  }

  /**
   * getUserAddress
   * 
   * @return userAddress
   */
  public String getUserAddress() {
    return m_userAddress;
  }

  /**
   * getUser
   * 
   * @return user
   */
  public String getUser() {
    return m_user;
  }

  /**
   * getDisplayName
   * 
   * @return displayName
   */
  public String getDisplayName() {
    return m_displayName;
  }

  /**
   * getSipAddress
   * 
   * @return sipAddress
   */
  public SipAddress getSipAddress() {
    return m_proxyAddress;
  }

  /**
   * getLoaclAddress
   * 
   * @return localAddress
   */
  public String getLoaclAddress() {
    return m_localAddress;
  }

  /**
   * getViaValue
   * 
   * @return Via value
   */
  public String getViaValue() {
    return m_via;
  }

  /**
   * removeTransaction
   * 
   * @param transaction
   */
  public synchronized void removeTransaction(BaseTransaction transaction) {
    BaseTransaction d2 = null;
    Protocol.echo("TxHandler: try to remove transaction "
        + transaction.m_clientTransactionID);
    Hashtable hashtable = m_clientTransactions;
    d2 = (BaseTransaction) hashtable
        .get(((Object) (transaction.m_clientTransactionID)));
    if (d2 == null) {
      hashtable = m_serverTransactions;
      d2 = (BaseTransaction) hashtable
          .get(((Object) (transaction.m_clientTransactionID)));
    }
    if (d2 != null) {
      Protocol.echo("TxHandler: removing transaction: "
          + transaction.m_clientTransactionID);
      hashtable.remove(((Object) (transaction.m_clientTransactionID)));
      transaction.cancel();
      Protocol.echo("TxHandler: " + (String) hashtable.get("name")
          + " table size = " + (hashtable.size() - 1));
    }

⌨️ 快捷键说明

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