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

📄 cwspsession.java~3~

📁 jwap 协议 udp 可以用于手机通讯
💻 JAVA~3~
📖 第 1 页 / 共 3 页
字号:
/** * JWAP - A Java Implementation of the WAP Protocols * Copyright (C) 2001-2004 Niko Bender * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */package net.sourceforge.jwap.wsp;import java.net.InetAddress;import java.net.SocketException;import java.util.Vector;import net.sourceforge.jwap.util.Logger;import net.sourceforge.jwap.wsp.pdu.CWSPConnect;import net.sourceforge.jwap.wsp.pdu.CWSPConnectReply;import net.sourceforge.jwap.wsp.pdu.CWSPDisconnect;import net.sourceforge.jwap.wsp.pdu.CWSPGet;import net.sourceforge.jwap.wsp.pdu.CWSPHeaders;import net.sourceforge.jwap.wsp.pdu.CWSPPDU;import net.sourceforge.jwap.wsp.pdu.CWSPPost;import net.sourceforge.jwap.wsp.pdu.CWSPRedirect;import net.sourceforge.jwap.wsp.pdu.CWSPReply;import net.sourceforge.jwap.wsp.pdu.CWSPResume;import net.sourceforge.jwap.wsp.pdu.CWSPSuspend;import net.sourceforge.jwap.wsp.pdu.EWSPCorruptPDUException;import net.sourceforge.jwap.wtp.CWTPEvent;import net.sourceforge.jwap.wtp.CWTPSocket;import net.sourceforge.jwap.wtp.EWTPAbortedException;import net.sourceforge.jwap.wtp.IWTPTransaction;import net.sourceforge.jwap.wtp.IWTPUpperLayer;/** * This class implements the WSP state machine named "management entity" * in the Wireless Session Protocol Specification by the <a href="http://www.wapforum.org">WAP-forum</a>. * <br> * Using this class the programmer can use all methods of the WSP layer. * To use the WAP-Stack with a class corresponding to HttpURLConnection * please use WapURLConnection by calling<br> * <code><br> * URL example = new URL("wap://wap.nokia.com");<br> * URLConnection con = example.getConnection();<br> * </code> * @author Niko Bender */public class CWSPSession    implements IWTPUpperLayer {  // SESSION states  public static final short STATE_NULL = 0;  public static final short STATE_CONNECTING = 1;  public static final short STATE_CONNECTED = 2;  public static final short STATE_SUSPENDED = 3;  public static final short STATE_RESUMING = 4;  // Abort reason code assignments  // Table 35 in spec.  public static final short ABORT_PROTOERR = 0xE0;  public static final short ABORT_DISCONNECT = 0xE1;  public static final short ABORT_SUSPEND = 0xE2;  public static final short ABORT_RESUME = 0xE3;  public static final short ABORT_CONGESTION = 0xE4;  public static final short ABORT_CONNECTERR = 0xE5;  public static final short ABORT_MRUEXCEEDED = 0xE6;  public static final short ABORT_MOREXCEEDED = 0xE7;  public static final short ABORT_PEERREQ = 0xE8;  public static final short ABORT_NETERR = 0xE9;  public static final short ABORT_USERREQ = 0xEA;  public static final short ABORT_USERRFS = 0xEB;  public static final short ABORT_USERPND = 0xEC;  public static final short ABORT_USERDCR = 0xED;  public static final short ABORT_USERDCU = 0xEE;  static Logger logger = Logger.getLogger(CWSPSession.class);  public String[] states = {      "STATE_NULL", "STATE_CONNECTING", "STATE_CONNECTED", "STATE_SUSPENDED",      "STATE_RESUMING"  };  private boolean isSuspended = false;  private short suspendCode;  private boolean isDisconnected = false;  private short disconnectCode;  /**   * the actual session state   */  private short state = STATE_NULL;  /**   * the acual transaction concering the session management   */  private IWTPTransaction wtp;  /**   * the Layer below   */  private CWTPSocket socket;  /**   * Holds all pending CWSPMethodManagers of this session   */  private Vector methods = new Vector();  /**   * Holds all pending CWSPPushManagers of this session   */  private Vector pushes = new Vector();  private IWSPUpperLayer upperlayer;  //////////////////////////////////////////////////////////////////////////////  ////////////////////////////// Protocol parameters and variables - sect. 7.1.3  /**   * Maximum Receive Unit (sect. 7.1.3.1)   */  private int MRU = 1024;  /**   * Maximum Outstanding Method Requests (sect. 7.1.3.2)   */  private int MOM = 1;  /**   * Maximum Outstanding Push Requests (sect. 7.1.3.3)   */  private int MOP = 1;  /**   * keeps track of the number of push transactions in process in the client   * (sect. 7.1.4.2)   */  // pushs.size();  /**   * saves the session identifier (sect. 7.1.4.3)   * We will get this from the ConnectReply by the Server   */  private long session_id = 0;  /**   * do we use IWSPUpperLayer2 or   */  private byte version = 0;  public int linkgate=1;  /**   * Construct a new WSP Session.   * @param toAddress address of the WAP gateway   * @param toPort WAP gateway port   * @param upperLayer WSP Upper Layer   * @param verbose verbose logging   * @throws SocketException if the underlying WTP socket cannot be created   *   */  public CWSPSession(InetAddress toAddress, int toPort,                     IWSPUpperLayer upperLayer, boolean verbose) throws      SocketException {    this(toAddress, toPort, null, CWTPSocket.DEFAULT_PORT, upperLayer, verbose);  }  /**   * Construct a new WSP Session.   * @param toAddress address of the WAP gateway   * @param toPort WAP gateway port   * @param localAddress local address to bind to (null to let the OS decide)   * @param localPort local port to bind to (use 0 to let the OS pick a free port)   * @param upperLayer WSP Upper Layer   * @param verbose verbose logging   * @throws SocketException if the underlying WTP socket cannot be created   */  public CWSPSession(InetAddress toAddress, int toPort,                     InetAddress localAddress, int localPort,                     IWSPUpperLayer upperLayer, boolean verbose) throws      SocketException {    if ( (upperLayer != null) && upperLayer instanceof IWSPUpperLayer2) {      this.version = 2;    }    else {      this.version = 1;    }    // Make sure that the logging system is initialized    Logger.initLogSystem(verbose);    this.upperlayer = upperLayer;    socket = new CWTPSocket(toAddress, toPort, localAddress, localPort, this);    // there can not be any session with the same peer address quadruplet!  }  /**   * Construct a new WSP session.   * @param toAddress the address of the WAP gateway   * @param toPort WAP gateway port   * @param upperLayer WSP Upper Layer   * @throws SocketException if the underlying WTP socket cannot be created   */  public CWSPSession(InetAddress toAddress, int toPort,                     IWSPUpperLayer upperLayer) throws SocketException {    this(toAddress, toPort, upperLayer, false);  }  /**   * Construct a new WSP session.   * @param toAddress the address and port of the WAP gateway   * @param upperLayer WSP Upper Layer   * @param verbose verbose logging   * @throws SocketException if the underlying WTP socket cannot be created   */  public CWSPSession(CWSPSocketAddress address, IWSPUpperLayer upperLayer,                     boolean verbose) throws SocketException {    this(address, null, upperLayer, verbose);  }  /**   * Construct a new WSP session.   * @param address the address and port of the WAP gateway   * @param localAddress the local address and port or null   * @param upperLayer WSP Upper Layer   * @param verbose verbose logging   * @throws SocketException if the underlying WTP socket cannot be created   */  public CWSPSession(CWSPSocketAddress address, CWSPSocketAddress localAddress,                     IWSPUpperLayer upperLayer, boolean verbose) throws      SocketException {    this(address.getAddress(), address.getPort(),         localAddress == null ? null : localAddress.getAddress(),         localAddress == null ? CWTPSocket.DEFAULT_PORT : localAddress.getPort(),         upperLayer, verbose);  }  //////////////////////////////////////////////////////////////////////////////  /////////////////////////////////////////////// WSP service primitives - S-*.*  /**   * Establish a WSP connection.   */  public synchronized void s_connect() {    s_connect(null);  }  /**   * Establish a WSP connection using WSP headers   * @param headers the WSP headers to set or null   */  public synchronized void s_connect(CWSPHeaders headers) {    if (state == STATE_NULL) {      abortAllMethods(ABORT_DISCONNECT);      abortAllPushes(ABORT_DISCONNECT);      // prepare WSP Connect PDU      CWSPConnect pdu = new CWSPConnect();      //set the header      CWSPHeaders tempHeader=new CWSPHeaders();      tempHeader.setHeader("Accept","*/*");      tempHeader.addHeader("Accept","application/vnd.wap.wmlc");      tempHeader.addHeader("Accept","application/vnd.wap.multipart.mixed");      tempHeader.addHeader("Accept","application/vnd.wap.wmlcscriptc");      tempHeader.addHeader("Accept","text/css");      tempHeader.setHeader("Profile","URIx");      tempHeader.addHeader("Profile-Diff","0x01 0x05 0x04 0xAA");      tempHeader.addHeader("Profile","URIy");      tempHeader.setHeader("user-agent","Nokio");      //tempHeader.setHeader("Encoding-version", "1.2");      if(headers==null)      {        pdu.setHeaders(tempHeader);      }      else      {        pdu.setHeaders(headers);      }      // prepare WTP Service Primitive      CWTPEvent initPacket = new CWTPEvent(pdu.toByteArray(),                                           CWTPEvent.TR_INVOKE_REQ);      // construct transaction with initPacket      wtp = socket.tr_invoke(this, initPacket, false,                             IWTPTransaction.CLASS_TYPE_2);      setState(STATE_CONNECTING);    }  }  /**     * Establish a WSP connection using WSP headers     * @param headers the WSP headers to set or null     */    public synchronized void s_connect(CWSPHeaders headers,String agent) {      if (state == STATE_NULL) {        abortAllMethods(ABORT_DISCONNECT);        abortAllPushes(ABORT_DISCONNECT);        // prepare WSP Connect PDU        CWSPConnect pdu = new CWSPConnect();        //set the header        CWSPHeaders tempHeader=new CWSPHeaders();        tempHeader.setHeader("Encoding-Version", "2.0");        tempHeader.setHeader("Accept","*/*");        tempHeader.addHeader("Accept","application/vnd.wap.wmlc");        tempHeader.addHeader("Accept","application/vnd.wap.multipart.mixed");        tempHeader.addHeader("Accept","application/vnd.wap.wmlcscriptc");        tempHeader.addHeader("Accept","application/vnd.wap.multipart.related");        tempHeader.addHeader("Accept","application/octet-stream");        tempHeader.addHeader("Accept","text/plain");        tempHeader.addHeader("Accept","text/css");        tempHeader.addHeader("Accept","image/bmp");        tempHeader.addHeader("Accept","image/gif");        tempHeader.addHeader("Accept","image/jpg");        tempHeader.addHeader("Accept","image/png");        tempHeader.addHeader("Accept","image/vnd.wap.wbmp");        tempHeader.addHeader("Accept","application/smil");        tempHeader.addHeader("Accept","application/vnd.wap.mms-message");        tempHeader.addHeader("Accept","text/vnd.sun.j2me.app-descriptor");        tempHeader.addHeader("Accept","application/java-archive");        tempHeader.setHeader("Profile","URIx");        tempHeader.addHeader("Profile","URIy");

⌨️ 快捷键说明

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