Massive refactoring to merge handling of simplex and duplex connections.

This commit is contained in:
akwizgran
2014-11-04 16:51:25 +00:00
parent b24f153704
commit 7b8181e309
67 changed files with 1981 additions and 2288 deletions

View File

@@ -3,40 +3,82 @@ package org.briarproject.plugins.bluetooth;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.concurrent.atomic.AtomicBoolean;
import javax.microedition.io.StreamConnection;
import org.briarproject.api.plugins.Plugin;
import org.briarproject.api.plugins.TransportConnectionReader;
import org.briarproject.api.plugins.TransportConnectionWriter;
import org.briarproject.api.plugins.duplex.DuplexTransportConnection;
class BluetoothTransportConnection implements DuplexTransportConnection {
private final Plugin plugin;
private final StreamConnection stream;
private final Reader reader;
private final Writer writer;
private final AtomicBoolean halfClosed, closed;
BluetoothTransportConnection(Plugin plugin, StreamConnection stream) {
this.plugin = plugin;
this.stream = stream;
reader = new Reader();
writer = new Writer();
halfClosed = new AtomicBoolean(false);
closed = new AtomicBoolean(false);
}
public int getMaxFrameLength() {
return plugin.getMaxFrameLength();
public TransportConnectionReader getReader() {
return reader;
}
public long getMaxLatency() {
return plugin.getMaxLatency();
public TransportConnectionWriter getWriter() {
return writer;
}
public InputStream getInputStream() throws IOException {
return stream.openInputStream();
private class Reader implements TransportConnectionReader {
public int getMaxFrameLength() {
return plugin.getMaxFrameLength();
}
public long getMaxLatency() {
return plugin.getMaxLatency();
}
public InputStream getInputStream() throws IOException {
return stream.openInputStream();
}
public void dispose(boolean exception, boolean recognised)
throws IOException {
if(halfClosed.getAndSet(true) || exception)
if(!closed.getAndSet(true)) stream.close();
}
}
public OutputStream getOutputStream() throws IOException {
return stream.openOutputStream();
}
private class Writer implements TransportConnectionWriter {
public void dispose(boolean exception, boolean recognised)
throws IOException {
stream.close();
public int getMaxFrameLength() {
return plugin.getMaxFrameLength();
}
public long getMaxLatency() {
return plugin.getMaxLatency();
}
public long getCapacity() {
return Long.MAX_VALUE;
}
public OutputStream getOutputStream() throws IOException {
return stream.openOutputStream();
}
public void dispose(boolean exception) throws IOException {
if(halfClosed.getAndSet(true) || exception)
if(!closed.getAndSet(true)) stream.close();
}
}
}