📄 packetqueuesender.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.io.IOException;import java.io.InterruptedIOException;import javax.bluetooth.L2CAPConnection;import net.sf.btw.tools.Logger;import net.sf.btw.tools.Queue;/** * Contains all inner-class threads, to squeeze the package size. * * @author Martin Vysny */final class PacketQueueSender extends Thread { private volatile boolean interrupted = false; /** * Constructs new instance and executes {@link #packetSchedulerThread()}. * * @param scheduler * scheduler instance. * @param id * client id * @param connection * the connection to use. Sink. */ PacketQueueSender(final PacketScheduler scheduler, final byte id, final L2CAPConnection connection) { super(); this.scheduler = scheduler; this.connection = connection; this.id = id; } private final PacketScheduler scheduler; private final static int MAX_PACKETS = 100; /** * Thread locks onto this object whenever something needs to be done with * the buffer. */ private final Queue buffer = new Queue(MAX_PACKETS); /** * Client id. */ private final byte id; /** * Sink packets here. */ private L2CAPConnection connection; /** * Executes if {@link #scheduler} is not <code>null</code>. Schedules * given packet for sending. * * @throws InterruptedException * if the thread has been interrupted. */ private void packetSchedulerThread() throws InterruptedException { boolean firstRun = true; while (true) { synchronized (buffer) { if (interrupted) return; if (!firstRun) buffer.wait(); else firstRun = false; } while (true) { // grab packet to send. byte[] toSend; synchronized (buffer) { if (interrupted) return; toSend = (byte[]) buffer.poll(); } if (toSend == null) break; // send it. try { connection.send(toSend); } catch (InterruptedIOException e) { Logger.debug("PacketQueueSender" + id //$NON-NLS-1$ + ".packetSchedulerThread():sending", e); //$NON-NLS-1$ return; } catch (IOException e) { Logger.log(interrupted ? Logger.LEVEL_INFO : Logger.LEVEL_ERROR, "PacketQueueSender" + id //$NON-NLS-1$ + "packetSchedulerThread():sending", e); //$NON-NLS-1$ scheduler.listener.errorOccured(id, IErrorListener.ERROR_HINT_COMMUNICATING, false, e); } } } } /** * Adds packet to the buffer. * * @param packet * the packet to add. */ public void addPacket(byte[] packet) { synchronized (buffer) { if (!buffer.offer(packet)) { Logger.error("PacketQueueSender" + id //$NON-NLS-1$ + ".addPacket(): Buffer full", null); //$NON-NLS-1$ scheduler.listener.errorOccured(id, IErrorListener.ERROR_HINT_COMMUNICATING, false, null); } else { buffer.notify(); } } } /** * Terminates the queue sender thread asap. */ public void interrupt() { interrupted = true; synchronized (buffer) { buffer.notify(); } } /* * (non-Javadoc) * * @see java.lang.Thread#run() */ public void run() { Logger.debug("PacketQueueSender.run(): started", null); //$NON-NLS-1$ try { packetSchedulerThread(); } catch (Exception ex) { Logger.log(interrupted ? Logger.LEVEL_INFO : Logger.LEVEL_ERROR, "PacketQueueSender" + id + ".run()", ex); //$NON-NLS-1$ //$NON-NLS-2$ } Logger.debug("PacketQueueSender.run(): terminated", null); //$NON-NLS-1$ }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -