Close transport connection if tag isn't recognised. #281

This commit is contained in:
akwizgran
2016-04-05 11:37:38 +01:00
parent bbd14f1af4
commit 2b19e4c8db
6 changed files with 142 additions and 265 deletions

View File

@@ -3,74 +3,33 @@ package org.briarproject.plugins.droidtooth;
import android.bluetooth.BluetoothSocket;
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;
import org.briarproject.api.plugins.duplex.AbstractDuplexTransportConnection;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.concurrent.atomic.AtomicBoolean;
class DroidtoothTransportConnection implements DuplexTransportConnection {
class DroidtoothTransportConnection extends AbstractDuplexTransportConnection {
private final Plugin plugin;
private final BluetoothSocket socket;
private final Reader reader;
private final Writer writer;
private final AtomicBoolean halfClosed, closed;
DroidtoothTransportConnection(Plugin plugin, BluetoothSocket socket) {
this.plugin = plugin;
super(plugin);
this.socket = socket;
reader = new Reader();
writer = new Writer();
halfClosed = new AtomicBoolean(false);
closed = new AtomicBoolean(false);
}
public TransportConnectionReader getReader() {
return reader;
@Override
protected InputStream getInputStream() throws IOException {
return socket.getInputStream();
}
public TransportConnectionWriter getWriter() {
return writer;
@Override
protected OutputStream getOutputStream() throws IOException {
return socket.getOutputStream();
}
private class Reader implements TransportConnectionReader {
public InputStream getInputStream() throws IOException {
return socket.getInputStream();
}
public void dispose(boolean exception, boolean recognised)
throws IOException {
if (halfClosed.getAndSet(true) || exception)
if (!closed.getAndSet(true)) socket.close();
}
}
private class Writer implements TransportConnectionWriter {
public int getMaxLatency() {
return plugin.getMaxLatency();
}
public int getMaxIdleTime() {
return plugin.getMaxIdleTime();
}
public long getCapacity() {
return Long.MAX_VALUE;
}
public OutputStream getOutputStream() throws IOException {
return socket.getOutputStream();
}
public void dispose(boolean exception) throws IOException {
if (halfClosed.getAndSet(true) || exception)
if (!closed.getAndSet(true)) socket.close();
}
@Override
protected void closeConnection(boolean exception) throws IOException {
socket.close();
}
}

View File

@@ -1,75 +1,34 @@
package org.briarproject.plugins.tor;
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;
import org.briarproject.api.plugins.duplex.AbstractDuplexTransportConnection;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.concurrent.atomic.AtomicBoolean;
class TorTransportConnection implements DuplexTransportConnection {
class TorTransportConnection extends AbstractDuplexTransportConnection {
private final Plugin plugin;
private final Socket socket;
private final Reader reader;
private final Writer writer;
private final AtomicBoolean halfClosed, closed;
TorTransportConnection(Plugin plugin, Socket socket) {
this.plugin = plugin;
super(plugin);
this.socket = socket;
reader = new Reader();
writer = new Writer();
halfClosed = new AtomicBoolean(false);
closed = new AtomicBoolean(false);
}
public TransportConnectionReader getReader() {
return reader;
@Override
protected InputStream getInputStream() throws IOException {
return socket.getInputStream();
}
public TransportConnectionWriter getWriter() {
return writer;
@Override
protected OutputStream getOutputStream() throws IOException {
return socket.getOutputStream();
}
private class Reader implements TransportConnectionReader {
public InputStream getInputStream() throws IOException {
return socket.getInputStream();
}
public void dispose(boolean exception, boolean recognised)
throws IOException {
if (halfClosed.getAndSet(true) || exception)
if (!closed.getAndSet(true)) socket.close();
}
}
private class Writer implements TransportConnectionWriter {
public int getMaxLatency() {
return plugin.getMaxLatency();
}
public int getMaxIdleTime() {
return plugin.getMaxIdleTime();
}
public long getCapacity() {
return Long.MAX_VALUE;
}
public OutputStream getOutputStream() throws IOException {
return socket.getOutputStream();
}
public void dispose(boolean exception) throws IOException {
if (halfClosed.getAndSet(true) || exception)
if (!closed.getAndSet(true)) socket.close();
}
@Override
protected void closeConnection(boolean exception) throws IOException {
socket.close();
}
}