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

📄 datagramcontroller.java

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

import java.io.*;
import javax.microedition.io.*;

import com.micromethod.sipstack.i.SipException;

/**
 * DatagramController
 */
public class DatagramController {
  /**
   * Receiver
   */
  private class Receiver extends Thread {

    public void run() {
      while (m_isOpen)
        try {
          Datagram datagram = m_udpDatagramConnection.newDatagram(1500);
          m_udpDatagramConnection.receive(datagram);
          String s = (new String(datagram.getData())).substring(0, datagram
              .getLength());
          Protocol.echo("\nDatagram received from: " + datagram.getAddress());
          if (datagram.getLength() > 0) {
            Protocol.echo("\n<-<-<-----------------------------------\n" + s);
            Protocol.echo("----------------------------------------");
            ByteArrayInputStream bytearrayinputstream = new ByteArrayInputStream(
                s.getBytes());
            SipMessage a1 = m_sipMessageParser
                .getSipMessage(((java.io.InputStream) (bytearrayinputstream)));
            if (a1 == null)
              throw new SipException("Unable to parse received message",
                  (byte) 0);
            a1.m_localPort = DatagramController.this.m_localPort;
            m_txHandler.dispatch(a1);
          }
          else {
            Protocol.echo("DatagramController: Datagram empty");
          }
          System.out.flush();
        }
        catch (InterruptedIOException interruptedioexception) {
        }
        catch (IOException ioexception) {
          Protocol.echo("\nDatagramController: receiver failure... "
              + ioexception);
        }
        catch (Exception exception) {
          Protocol.echo("\nDatagramController: Internal failure... "
              + exception);
        }
    }

    /**
     * Receiver Constructor
     */
    private Receiver() {
    }

  }

  private static final short _fldint = 1500;

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

  /**
   * m_sipMessageParser
   */
  private SipMessageParser m_sipMessageParser = null;

  /**
   * m_sipMessage
   */
  private Receiver m_sipMessage = null;

  /**
   * m_udpDatagramConnection
   */
  private UDPDatagramConnection m_udpDatagramConnection = null;

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

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

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

  /**
   * DatagramController Constructor
   * 
   * @param x1
   * @param i
   * @throws IOException
   */
  public DatagramController(TxHandler x1, int i) throws IOException {
    m_sipMessageParser = null;
    m_isOpen = true;
    m_txHandler = x1;
    m_sipMessageParser = new SipMessageParser();
    if (i == 0) {
      Protocol.echo("DatagramController: init with datagram://");
      m_udpDatagramConnection = (UDPDatagramConnection) Connector.open(
          "datagram://", 3);
    }
    else {
      Protocol.echo("DatagramController: init with datagram://:" + i);
      m_udpDatagramConnection = (UDPDatagramConnection) Connector.open(
          "datagram://:" + i, 3);
    }
    m_localAddress = m_udpDatagramConnection.getLocalAddress();
    if (m_localAddress != null && m_localAddress.indexOf(':') > -1)
      m_localAddress = new String("[" + m_localAddress + "]");
    m_localPort = m_udpDatagramConnection.getLocalPort();
    m_sipMessage = new Receiver();
    m_sipMessage.start();
    Protocol.echo("DatagramListener: started...on port: " + m_localPort);
  }

  /**
   * close
   * 
   */
  public void close() {
    m_isOpen = false;
    try {
      m_udpDatagramConnection.close();
    }
    catch (IOException ioexception) {
    }
  }

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

  /**
   * getLocalPort
   * 
   * @return localPort
   */
  public int getLocalPort() {
    return m_localPort;
  }

  /**
   * sendSipMessage, use Datagram
   * 
   * @param address
   * @param port
   * @param sipMessage
   * @throws IOException
   */
  public void sendSipMessage(String address, int port, String sipMessage)
      throws IOException {
    byte abyte0[] = sipMessage.getBytes();
    Datagram datagram = m_udpDatagramConnection.newDatagram(abyte0,
        abyte0.length);
    datagram.setAddress("datagram://" + address + ":" + port);
    m_udpDatagramConnection.send(datagram);
  }

}

⌨️ 快捷键说明

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