Allow duplex connections' dispose() methods to throw IOExceptions.

This commit is contained in:
akwizgran
2012-10-19 21:52:53 +01:00
parent 21f177d695
commit cc6e9d53ad
9 changed files with 47 additions and 51 deletions

View File

@@ -28,5 +28,5 @@ public interface DuplexTransportConnection {
* of an exception and the second argument indicates whether the connection * of an exception and the second argument indicates whether the connection
* was recognised, which may affect how resources are disposed of. * was recognised, which may affect how resources are disposed of.
*/ */
void dispose(boolean exception, boolean recognised); void dispose(boolean exception, boolean recognised) throws IOException;
} }

View File

@@ -18,7 +18,6 @@ public interface SimplexTransportReader {
* argument indicates whether the reader is being closed because of an * argument indicates whether the reader is being closed because of an
* exception and the second argument indicates whether the connection was * exception and the second argument indicates whether the connection was
* recognised, which may affect how resources are disposed of. * recognised, which may affect how resources are disposed of.
* @throws IOException
*/ */
void dispose(boolean exception, boolean recognised) throws IOException; void dispose(boolean exception, boolean recognised) throws IOException;
} }

View File

@@ -25,7 +25,6 @@ public interface SimplexTransportWriter {
* Closes the writer and disposes of any associated resources. The * Closes the writer and disposes of any associated resources. The
* argument indicates whether the writer is being closed because of an * argument indicates whether the writer is being closed because of an
* exception, which may affect how resources are disposed of. * exception, which may affect how resources are disposed of.
* @throws IOException
*/ */
void dispose(boolean exception) throws IOException; void dispose(boolean exception) throws IOException;
} }

View File

@@ -3,8 +3,6 @@ package net.sf.briar.plugins.bluetooth;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.microedition.io.StreamConnection; import javax.microedition.io.StreamConnection;
@@ -12,9 +10,6 @@ import net.sf.briar.api.plugins.duplex.DuplexTransportConnection;
class BluetoothTransportConnection implements DuplexTransportConnection { class BluetoothTransportConnection implements DuplexTransportConnection {
private static final Logger LOG =
Logger.getLogger(BluetoothTransportConnection.class.getName());
private final StreamConnection stream; private final StreamConnection stream;
BluetoothTransportConnection(StreamConnection stream) { BluetoothTransportConnection(StreamConnection stream) {
@@ -33,11 +28,8 @@ class BluetoothTransportConnection implements DuplexTransportConnection {
return true; return true;
} }
public void dispose(boolean exception, boolean recognised) { public void dispose(boolean exception, boolean recognised)
try { throws IOException {
stream.close(); stream.close();
} catch(IOException e) {
if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.toString());
}
} }
} }

View File

@@ -4,16 +4,11 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.net.Socket; import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.sf.briar.api.plugins.duplex.DuplexTransportConnection; import net.sf.briar.api.plugins.duplex.DuplexTransportConnection;
class SocketTransportConnection implements DuplexTransportConnection { class SocketTransportConnection implements DuplexTransportConnection {
private static final Logger LOG =
Logger.getLogger(SocketTransportConnection.class.getName());
private final Socket socket; private final Socket socket;
SocketTransportConnection(Socket socket) { SocketTransportConnection(Socket socket) {
@@ -32,11 +27,8 @@ class SocketTransportConnection implements DuplexTransportConnection {
return true; return true;
} }
public void dispose(boolean exception, boolean recognised) { public void dispose(boolean exception, boolean recognised)
try { throws IOException {
socket.close(); socket.close();
} catch(IOException e) {
if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.toString());
}
} }
} }

View File

@@ -3,8 +3,6 @@ package net.sf.briar.plugins.tor;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.sf.briar.api.plugins.duplex.DuplexTransportConnection; import net.sf.briar.api.plugins.duplex.DuplexTransportConnection;
@@ -12,9 +10,6 @@ import org.silvertunnel.netlib.api.NetSocket;
class TorTransportConnection implements DuplexTransportConnection { class TorTransportConnection implements DuplexTransportConnection {
private static final Logger LOG =
Logger.getLogger(TorTransportConnection.class.getName());
private final NetSocket socket; private final NetSocket socket;
TorTransportConnection(NetSocket socket) { TorTransportConnection(NetSocket socket) {
@@ -33,11 +28,8 @@ class TorTransportConnection implements DuplexTransportConnection {
return true; return true;
} }
public void dispose(boolean exception, boolean recognised) { public void dispose(boolean exception, boolean recognised)
try { throws IOException {
socket.close(); socket.close();
} catch(IOException e) {
if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.toString());
}
} }
} }

View File

@@ -232,7 +232,11 @@ abstract class DuplexConnection implements DatabaseListener {
private void dispose(boolean exception, boolean recognised) { private void dispose(boolean exception, boolean recognised) {
if(disposed.getAndSet(true)) return; if(disposed.getAndSet(true)) return;
ByteUtils.erase(ctx.getSecret()); ByteUtils.erase(ctx.getSecret());
transport.dispose(exception, recognised); try {
transport.dispose(exception, recognised);
} catch(IOException e) {
if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.toString());
}
} }
// This task runs on a database thread // This task runs on a database thread

View File

@@ -127,21 +127,31 @@ class ConnectionDispatcherImpl implements ConnectionDispatcher {
} }
public void run() { public void run() {
byte[] tag;
try { try {
byte[] tag = readTag(transport.getInputStream()); tag = readTag(transport.getInputStream());
ConnectionContext ctx = recogniser.acceptConnection(transportId, } catch(IOException e) {
tag); if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.toString());
if(ctx == null) { dispose(true, false);
transport.dispose(false, false); return;
} else { }
duplexConnFactory.createIncomingConnection(ctx, transport); ConnectionContext ctx = null;
} try {
} catch(DbException e) { ctx = recogniser.acceptConnection(transportId, tag);
if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.toString()); } catch(DbException e) {
transport.dispose(true, false); if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.toString());
dispose(true, false);
return;
}
if(ctx == null) dispose(false, false);
else duplexConnFactory.createIncomingConnection(ctx, transport);
}
private void dispose(boolean exception, boolean recognised) {
try {
transport.dispose(exception, recognised);
} catch(IOException e) { } catch(IOException e) {
if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.toString()); if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.toString());
transport.dispose(true, false);
} }
} }
} }

View File

@@ -41,7 +41,11 @@ abstract class DuplexTest {
d.dispose(false, true); d.dispose(false, true);
} catch(IOException e) { } catch(IOException e) {
e.printStackTrace(); e.printStackTrace();
d.dispose(true, true); try {
d.dispose(true, true);
} catch(IOException e1) {
e1.printStackTrace();
}
} }
} }
@@ -65,7 +69,11 @@ abstract class DuplexTest {
d.dispose(false, true); d.dispose(false, true);
} catch(IOException e) { } catch(IOException e) {
e.printStackTrace(); e.printStackTrace();
d.dispose(true, true); try {
d.dispose(true, true);
} catch(IOException e1) {
e1.printStackTrace();
}
} }
} }