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

📄 packetscheduler.java

📁 Bluetooth chat Server and Client in j2me
💻 JAVA
字号:
/* The Bluetooth Library for client-server communication Copyright (C) 2006 Martin Vysny This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program 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 General Public License for more details. */package net.sf.btw.btlib;import java.util.Enumeration;import java.util.Hashtable;import javax.bluetooth.L2CAPConnection;/** * The packet scheduler. *  * @author Martin Vysny *  */final class PacketScheduler {	/**	 * Creates new scheduler.	 * 	 * @param listener	 *            the error listener.	 */	PacketScheduler(final IErrorListener listener) {		super();		this.listener = listener;	}	final IErrorListener listener;	/**	 * Maps ID to the {@link PacketQueueSender} instance.	 */	private final Hashtable table = new Hashtable();	/**	 * Adds route for the client.	 * 	 * @param id	 *            the client id.	 * @param connection	 *            the connection to the client	 */	void addRoute(final Byte id, final L2CAPConnection connection) {		synchronized (table) {			final PacketQueueSender thread = new PacketQueueSender(this, id					.byteValue(), connection);			final PacketQueueSender oldThread = (PacketQueueSender) table.put(					id, thread);			if (oldThread != null)				oldThread.interrupt();			thread.start();		}	}	/**	 * Removes route for the client.	 * 	 * @param id	 *            the client id.	 */	void removeRoute(final Byte id) {		synchronized (table) {			final PacketQueueSender oldThread = (PacketQueueSender) table					.remove(id);			if (oldThread != null)				oldThread.interrupt();		}	}	/**	 * Sends given packet to the destination.	 * 	 * @param targetId	 *            target id to send the packet to.	 * @param packet	 *            packet to send.	 */	void sendPacket(final Byte targetId, byte[] packet) {		synchronized (table) {			final PacketQueueSender oldThread = (PacketQueueSender) table					.get(targetId);			oldThread.addPacket(packet);		}	}	/**	 * Stops the scheduler, kills all threads etc.	 */	void stop() {		synchronized (table) {			for (final Enumeration e = table.elements(); e.hasMoreElements();) {				final PacketQueueSender oldThread = (PacketQueueSender) e						.nextElement();				oldThread.interrupt();			}			table.clear();		}	}}

⌨️ 快捷键说明

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