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

📄 irlapcontext.java

📁 JRed is a 100% Java implementation of the IrDA infrared communications protocols. JRed lets you beam
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
**************************************************************************
** $Header: /cvsroot/jred/jred/src/com/synchrona/jred/irlap/IrLAPContext.java,v 1.4 2000/07/30 20:18:12 mpatters Exp $
**
** Copyright (C) 2000 Synchrona, Inc. All rights reserved.
**
** This file is part of JRed, a 100% Java implementation of the IrDA
** infrared communications protocols.
**
** This file may be distributed under the terms of the Synchrona Public
** License as defined by Synchrona, Inc. and appearing in the file
** LICENSE included in the packaging of this file. The Synchrona Public
** License is based on the Q Public License as defined by Troll Tech AS
** of Norway; it differs only in its use of the courts of Florida, USA
** rather than those of Oslo, Norway.
**************************************************************************
*/
package com.synchrona.jred.irlap;

import com.synchrona.util.Log;
import com.synchrona.util.Utilities;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.PrintWriter;
import java.util.Random;
import javax.swing.Timer;

/**
 * IrLAPContext offers services for the IrLAP states.
 * Log messages mimic directives in the IrLAP specification, which
 * helps in debuggering.
 */
public class IrLAPContext implements iIrLAPFramerListener {
	public static final int DEFAULT_F_TIMEOUT_MSEC        = 500;
	public static final int DEFAULT_MIN_TURNAROUND_MSEC   = 5; // was 10
	public static final int DEFAULT_QUERY_TIMEOUT_MSEC    = 500;
	public static final int DEFAULT_SLOT_TIMEOUT_MSEC     = 600;
	public static final int DEFAULT_WATCHDOG_TIMEOUT_MSEC = 5000;
	public static final int DEFAULT_WINDOW_SIZE           = 1;

	// TBD: This should really be provided by the service user (IrOBEX) via
	//      IrLMP.
	private static final byte [] DISCOVERY_INFO = {
		(byte) 0x82, // means "I am a PDA, and this byte is followed by another"
		(byte) 0x20, // means "I am a file server and this is the last service hint"
		(byte) 0x00, // means "ASCII data follows"
		(byte) 'J',  // what follows is a device hint that identifies this station
		(byte) 'R',
		(byte) 'e',
		(byte) 'd',
		(byte) '0',
		(byte) '.',
		(byte) '1'
	};

	private byte []        m_ayCommParameters     = null;
	private boolean        m_bAckRequired         = false;
	private boolean        m_bConnectionConfirmed = false;
	private boolean        m_bFrameSent           = false;
	private boolean        m_bMediaBusy           = false;
	private boolean        m_bRemoteBusy          = false;
	private ConnectionLog  m_connectionLog        = null;
	private DiscoveryLog   m_discoveryLog         = null;
	private IrLAPFramer    m_framer               = null;
	private Timer          m_fTimer               = null;
	private iIrLAPListener m_listener             = null;
	private Log            m_log                  = null;
	private int            m_nFTimeout            = DEFAULT_F_TIMEOUT_MSEC;
	private int            m_nMyAddress           = 12345678;
	private int            m_nQueryTimeout        = DEFAULT_QUERY_TIMEOUT_MSEC;
	private int            m_nRemoteAddress       = 0;
	private int            m_nRetryCount          = 0;
	private int            m_nWatchdogTimeout     = DEFAULT_WATCHDOG_TIMEOUT_MSEC;
	private int            m_nVr                  = 0;
	private int            m_nVs                  = 0;
	private int            m_nWindowSize          = 1;
	private Timer          m_queryTimer           = null; 
	private Random         m_random               = new Random();
	private IrLAPSendQueue m_sendQueue            = new IrLAPSendQueue();

	private byte           _slotCount             = 0;
	private Timer          _slotTimer             = null;
	private int            _slotTimeout           = DEFAULT_SLOT_TIMEOUT_MSEC;

	private IrLAPState     m_state                = IrLAPStateNDM.getInstance();
	private IrLAPContext   m_this                 = null;
	private Timer          m_watchdogTimer        = null;
	private byte           m_yConnection          = 0;
	private byte           m_yResponseSlot        = 2;

	private IrLAPContext() {
		// No-arg constructor is private so we don't get a null
		// log device; that would cause NullPointerExceptions
		// all over the place.
	}

	public IrLAPContext(Log log) {
		m_log           = log;
		m_this          = this;
		m_yResponseSlot = (byte) 2;
		m_connectionLog = new ConnectionLog(m_log);
		m_discoveryLog  = new DiscoveryLog(m_log);

		generateAddress();

		m_fTimer = new Timer(m_nFTimeout, new ActionListener() {
			public void actionPerformed(ActionEvent action) {
				try {
					m_log.debug("IrLAPContext", "F-timer-expired");
					m_state.handleFTimerExpired(m_this);
				} catch ( Exception e ) {
					System.err.println(e);
				}
			}
		});

		m_queryTimer = new Timer(m_nQueryTimeout, new ActionListener() {
			public void actionPerformed(ActionEvent action) {
				try {
					m_log.debug("IrLAPContext", "query-timer-expired");
					m_state.handleQueryTimerExpired(m_this);
				} catch ( Exception e ) {
					System.err.println(e);
				}
			}
		});

		_slotTimer = new Timer(_slotTimeout, new ActionListener() {
			public void actionPerformed(ActionEvent action) {
				try {
					m_log.debug("IrLAPContext", "slot-timer-expired");
					m_state.handleSlotTimerExpired(m_this);
				} catch ( Exception e ) {
					System.err.println(e);
				}
			}
		});

		m_watchdogTimer = new Timer(m_nWatchdogTimeout, new ActionListener() {
			public void actionPerformed(ActionEvent action) {
				try {
					m_log.debug("IrLAPContext", "WD-timer-expired");
					m_state.handleWatchdogTimerExpired(m_this);
				} catch ( Exception e ) {
					System.err.println(e);
				}
			}
		});
	}

	//------------------------------------------------------------------
	// Users of IrLAPContext instances use these
	//------------------------------------------------------------------

	public void addIrLAPListener(iIrLAPListener listener) throws Exception {	
		if ( null != m_listener ) {
			throw new Exception("This IrLAPContext already has a listener.");
		} else {
			m_listener = listener;
		}
	}

	/**
	** Discover other IrDA hosts.
	*/
	public void startDiscovery() throws Exception {
		if ( m_bMediaBusy ) {
			m_log.debug("IrLAPContext", "media is busy now, ignoring");
		} else {
			m_log.debug("IrLAPContext", "\tSend-Discovery-Request:" + m_nMyAddress);

			_slotCount = 0;
			m_framer.sendXID(m_nMyAddress, 0xFFFFFFFF, true, _slotCount, DISCOVERY_INFO);

			startSlotTimer();

			setMediaBusy(true);
			nextState(IrLAPStateQuery.getInstance());
		}
	}

	/**
	 * @deprecated
	 */
	public void setLog(PrintWriter log) {
	}

	/**
	 * @deprecated
	 */
	public void setLogging(boolean bDoLogging) {
	}

	//------------------------------------------------------------------
	// Implement iIrLAPFramerListener
	//------------------------------------------------------------------

	public void handleData(byte yConnection, boolean bCommand, int nNs, int nNr, boolean bPoll, byte [] ayData) throws Exception {
		m_log.debug("IrLAPContext", "Recv i:" + ( bCommand ? "cmd" : "rsp" ) + ":" + (0x00000007 & nNs) + ":" + (0x00000007 & nNr) + ":" + ( bPoll ? "P" : "~P"));
		m_state.handleData(this, yConnection, nNr, bCommand, bPoll, ayData);
	}

	public void handleDisconnect(byte yConnection, boolean bCommand, boolean bPoll) throws Exception {
		m_log.debug("IrLAPContext", "u:disc:" + (bCommand ? "cmd" : "rsp") + ":" + (bPoll ? "P" : "~P"));
		m_state.handleDisconnect(this, m_nRemoteAddress, yConnection);
	}

	public void handleRR(byte yConnection, int nNr, boolean bCommand, boolean bPoll) throws Exception {
		m_log.debug("IrLAPContext", "Recv s:rr:" + ( bCommand ? "cmd" : "rsp" ) + ":" + (0x00000007 & nNr) + ":" + ( bPoll ? "P" : "~P"));
		m_state.handleRR(this, yConnection, nNr, bCommand, bPoll);
	}

	public void handleSNRM(int nSource, int nDestination, byte yConnection, byte [] ayParameters) throws Exception {
		m_log.debug("IrLAPContext", "Recv u:snrm:cmd:P:" + Utilities.byteToString(yConnection) + ":" + nSource);
		m_yConnection = yConnection;
		m_state.handleSNRM(this, nSource, nDestination, yConnection, ayParameters);
	} 

	public void handleXID(int nSource, int nDestination, byte yNumSlots, byte ySlot, boolean bCommand, byte [] ayHints) throws Exception {
		if ( (byte) 0xFF == ySlot ) {
			m_log.debug("IrLAPContext", "Recv End-Discovery-XID-Cmd");
		} else {
			m_log.debug("IrLAPContext", "Recv Discovery-XID-Cmd:" + yNumSlots + "," + ySlot);
		}
		m_state.handleXID(this, nSource, nDestination, ySlot, bCommand, ayHints);
	}

	//------------------------------------------------------------------
	// End implementation of iIrLAPFramerListener, start methods to
	// support IrLAPState
	//------------------------------------------------------------------

	public void applyConnectionParameters() throws Exception {
		m_log.debug("IrLAPContext", "\tApply-Connection-Parameters");
	}
/*
	public void connectionConfirm() {
		m_log.debug("connection-confirm");
	}

	public void connectRequest(int nDestination) throws Exception {
		m_log.debug("Connect-Request(" + nDestination + ")");
		m_state.connectionRequest(this, nDestination);
	}
*/
	public void connectIndication(int nRemote, byte yConnection, byte [] ayParameters) throws Exception {
		m_log.debug("IrLAPContext", "\tConnect-Indication");

		ConnectionInformation conn = new ConnectionInformation(yConnection, ayParameters, m_discoveryLog.get(nRemote));
		m_connectionLog.add(conn);

		if ( null != m_listener ) {
			m_nRemoteAddress = nRemote;
			//m_listener.connectIndication(this, nRemote, yConnection, ayParameters);
			m_listener.connectIndication(this, conn);
		}
	}

	public void connectResponse(int nDestination, byte yConnection, byte [] ayParams) throws Exception {
		m_state.connectResponse(this, nDestination, yConnection, ayParams);
	}


	public void dataIndication(byte yConnection, byte [] ayData) throws Exception {
		m_log.debug("IrLAPContext", "\tData-Indication");
		if ( null != m_listener ) {
			ConnectionInformation connInfo = m_connectionLog.get(yConnection);
			m_listener.dataIndication(this, connInfo, ayData);
		}
	}

	public void discoveryIndication(DiscoveryInformation discoveryInfo) throws Exception {
		m_log.debug("IrLAPContext", "\tDiscovery-Indication(" + discoveryInfo + ")");

⌨️ 快捷键说明

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