Plugins may dispose of resources differently depending on whether a

connection was recognised.
This commit is contained in:
akwizgran
2011-12-08 18:56:53 +00:00
parent 0fdc69ff00
commit 844ae8f0a7
18 changed files with 97 additions and 74 deletions

View File

@@ -12,8 +12,10 @@ public interface BatchTransportReader {
InputStream getInputStream(); InputStream getInputStream();
/** /**
* Closes the reader and disposes of any associated state. The argument * Closes the reader and disposes of any associated resources. The first
* should be false if an exception was thrown while using the reader. * argument indicates whether the reader is being closed because of an
* exception and the second argument indicates whether the connection was
* recognised, which may affect how resources are disposed of.
*/ */
void dispose(boolean success); void dispose(boolean exception, boolean recognised);
} }

View File

@@ -15,8 +15,9 @@ public interface BatchTransportWriter {
OutputStream getOutputStream(); OutputStream getOutputStream();
/** /**
* Closes the writer and disposes of any associated state. The argument * Closes the writer and disposes of any associated resources. The
* should be false if an exception was thrown while using the writer. * argument indicates whether the writer is being closed because of an
* exception, which may affect how resources are disposed of.
*/ */
void dispose(boolean success); void dispose(boolean exception);
} }

View File

@@ -18,8 +18,10 @@ public interface StreamTransportConnection {
OutputStream getOutputStream() throws IOException; OutputStream getOutputStream() throws IOException;
/** /**
* Closes the connection and disposes of any associated state. The argument * Closes the connection and disposes of any associated resources. The
* should be false if an exception was thrown while using the connection. * first argument indicates whether the connection is being closed because
* of an exception and the second argument indicates whether the connection
* was recognised, which may affect how resources are disposed of.
*/ */
void dispose(boolean success); void dispose(boolean exception, boolean recognised);
} }

View File

@@ -29,7 +29,7 @@ class BluetoothTransportConnection implements StreamTransportConnection {
return stream.openOutputStream(); return stream.openOutputStream();
} }
public void dispose(boolean success) { public void dispose(boolean exception, boolean recognised) {
try { try {
stream.close(); stream.close();
} catch(IOException e) { } catch(IOException e) {

View File

@@ -27,13 +27,13 @@ class FileTransportReader implements BatchTransportReader {
return in; return in;
} }
public void dispose(boolean success) { public void dispose(boolean exception, boolean recognised) {
try { try {
in.close(); in.close();
} catch(IOException e) { } catch(IOException e) {
if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.toString()); if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.toString());
} }
if(success) { if(recognised) {
file.delete(); file.delete();
plugin.readerFinished(file); plugin.readerFinished(file);
} }

View File

@@ -34,13 +34,13 @@ class FileTransportWriter implements BatchTransportWriter {
return out; return out;
} }
public void dispose(boolean success) { public void dispose(boolean exception) {
try { try {
out.close(); out.close();
} catch(IOException e) { } catch(IOException e) {
if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.toString()); if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.toString());
} }
if(success) plugin.writerFinished(file); if(exception) file.delete();
else file.delete(); else plugin.writerFinished(file);
} }
} }

View File

@@ -28,7 +28,7 @@ class SocketTransportConnection implements StreamTransportConnection {
return socket.getOutputStream(); return socket.getOutputStream();
} }
public void dispose(boolean success) { public void dispose(boolean exception, boolean recognised) {
try { try {
socket.close(); socket.close();
} catch(IOException e) { } catch(IOException e) {

View File

@@ -79,10 +79,10 @@ class IncomingBatchConnection {
throw new FormatException(); throw new FormatException();
} }
} }
transport.dispose(true); transport.dispose(false, true);
} 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(false); transport.dispose(true, true);
} }
} }

View File

@@ -90,13 +90,13 @@ class OutgoingBatchConnection {
} }
// Flush the output stream // Flush the output stream
out.flush(); out.flush();
transport.dispose(true); transport.dispose(false);
} catch(DbException e) { } catch(DbException e) {
if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.toString()); if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.toString());
transport.dispose(false); transport.dispose(true);
} 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(false); transport.dispose(true);
} }
} }
} }

View File

@@ -166,13 +166,13 @@ abstract class StreamConnection implements DatabaseListener {
} }
} }
writerTasks.add(CLOSE); writerTasks.add(CLOSE);
if(!disposed.getAndSet(true)) transport.dispose(true); if(!disposed.getAndSet(true)) transport.dispose(false, true);
} catch(DbException e) { } catch(DbException e) {
if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.toString()); if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.toString());
if(!disposed.getAndSet(true)) transport.dispose(false); if(!disposed.getAndSet(true)) transport.dispose(true, true);
} catch(IOException e) { } catch(IOException e) {
if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.toString()); if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.toString());
if(!disposed.getAndSet(true)) transport.dispose(false); if(!disposed.getAndSet(true)) transport.dispose(true, true);
} }
} }
@@ -203,17 +203,17 @@ abstract class StreamConnection implements DatabaseListener {
if(task == CLOSE) break; if(task == CLOSE) break;
task.run(); task.run();
} }
if(!disposed.getAndSet(true)) transport.dispose(true); if(!disposed.getAndSet(true)) transport.dispose(false, true);
} catch(DbException e) { } catch(DbException e) {
if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.toString()); if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.toString());
if(!disposed.getAndSet(true)) transport.dispose(false); if(!disposed.getAndSet(true)) transport.dispose(true, true);
} catch(InterruptedException e) { } catch(InterruptedException e) {
if(LOG.isLoggable(Level.INFO)) if(LOG.isLoggable(Level.INFO))
LOG.info("Interrupted while waiting for task"); LOG.info("Interrupted while waiting for task");
if(!disposed.getAndSet(true)) transport.dispose(false); if(!disposed.getAndSet(true)) transport.dispose(true, true);
} catch(IOException e) { } catch(IOException e) {
if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.toString()); if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.toString());
if(!disposed.getAndSet(true)) transport.dispose(false); if(!disposed.getAndSet(true)) transport.dispose(true, true);
} finally { } finally {
db.removeListener(this); db.removeListener(this);
} }
@@ -308,7 +308,7 @@ abstract class StreamConnection implements DatabaseListener {
writer.writeRequest(request); writer.writeRequest(request);
} 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(false); transport.dispose(true, true);
} }
} }
} }
@@ -398,7 +398,7 @@ abstract class StreamConnection implements DatabaseListener {
dbExecutor.execute(new GenerateAcks()); dbExecutor.execute(new GenerateAcks());
} 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(false); transport.dispose(true, true);
} }
} }
} }
@@ -444,7 +444,7 @@ abstract class StreamConnection implements DatabaseListener {
else dbExecutor.execute(new GenerateBatches(requested)); else dbExecutor.execute(new GenerateBatches(requested));
} 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(false); transport.dispose(true, true);
} }
} }
} }
@@ -487,7 +487,7 @@ abstract class StreamConnection implements DatabaseListener {
writer.writeOffer(offer); writer.writeOffer(offer);
} 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(false); transport.dispose(true, true);
} }
} }
} }
@@ -520,7 +520,7 @@ abstract class StreamConnection implements DatabaseListener {
writer.writeSubscriptionUpdate(update); writer.writeSubscriptionUpdate(update);
} 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(false); transport.dispose(true, true);
} }
} }
} }
@@ -553,7 +553,7 @@ abstract class StreamConnection implements DatabaseListener {
writer.writeTransportUpdate(update); writer.writeTransportUpdate(update);
} 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(false); transport.dispose(true, true);
} }
} }
} }

View File

@@ -89,14 +89,14 @@ class ConnectionDispatcherImpl implements ConnectionDispatcher {
try { try {
byte[] tag = readTag(r.getInputStream()); byte[] tag = readTag(r.getInputStream());
ConnectionContext ctx = recogniser.acceptConnection(t, tag); ConnectionContext ctx = recogniser.acceptConnection(t, tag);
if(ctx == null) r.dispose(true); if(ctx == null) r.dispose(false, false);
else batchConnFactory.createIncomingConnection(ctx, r, tag); else batchConnFactory.createIncomingConnection(ctx, r, tag);
} catch(DbException e) { } catch(DbException e) {
if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.toString()); if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.toString());
r.dispose(false); r.dispose(true, false);
} catch(IOException e) { } catch(IOException e) {
if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.toString()); if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.toString());
r.dispose(false); r.dispose(true, false);
} }
} }
} }
@@ -116,14 +116,14 @@ class ConnectionDispatcherImpl implements ConnectionDispatcher {
try { try {
byte[] tag = readTag(s.getInputStream()); byte[] tag = readTag(s.getInputStream());
ConnectionContext ctx = recogniser.acceptConnection(t, tag); ConnectionContext ctx = recogniser.acceptConnection(t, tag);
if(ctx == null) s.dispose(true); if(ctx == null) s.dispose(false, false);
else streamConnFactory.createIncomingConnection(ctx, s, tag); else streamConnFactory.createIncomingConnection(ctx, s, tag);
} catch(DbException e) { } catch(DbException e) {
if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.toString()); if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.toString());
s.dispose(false); s.dispose(true, false);
} catch(IOException e) { } catch(IOException e) {
if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.toString()); if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.toString());
s.dispose(false); s.dispose(true, false);
} }
} }
} }

View File

@@ -38,10 +38,10 @@ abstract class StreamTest {
} else { } else {
System.out.println("No response"); System.out.println("No response");
} }
s.dispose(true); s.dispose(false, true);
} catch(IOException e) { } catch(IOException e) {
e.printStackTrace(); e.printStackTrace();
s.dispose(false); s.dispose(true, true);
} }
} }
@@ -62,10 +62,10 @@ abstract class StreamTest {
} else { } else {
System.out.println("No challenge"); System.out.println("No challenge");
} }
s.dispose(true); s.dispose(false, true);
} catch(IOException e) { } catch(IOException e) {
e.printStackTrace(); e.printStackTrace();
s.dispose(false); s.dispose(true, true);
} }
} }
} }

View File

@@ -283,11 +283,9 @@ public class RemovableDrivePluginTest extends TestCase {
out.write(new byte[123]); out.write(new byte[123]);
out.flush(); out.flush();
out.close(); out.close();
assertEquals(123L, files[0].length()); // Disposing of the writer should not delete the file
// Successfully disposing of the writer should not delete the file writer.dispose(false);
writer.dispose(true); assertTrue(files[0].exists());
files = drive1.listFiles();
assertEquals(1, files.length);
assertEquals(123L, files[0].length()); assertEquals(123L, files[0].length());
context.assertIsSatisfied(); context.assertIsSatisfied();

View File

@@ -95,7 +95,7 @@ public class SimpleSocketPluginTest extends TestCase {
assertTrue(latch.await(1, TimeUnit.SECONDS)); assertTrue(latch.await(1, TimeUnit.SECONDS));
assertFalse(error.get()); assertFalse(error.get());
// Clean up // Clean up
conn.dispose(true); conn.dispose(false, true);
ss.close(); ss.close();
plugin.stop(); plugin.stop();
} }

View File

@@ -118,7 +118,8 @@ public class BatchConnectionReadWriteTest extends TestCase {
transport); transport);
// Write whatever needs to be written // Write whatever needs to be written
batchOut.write(); batchOut.write();
assertTrue(transport.getSuccess()); assertTrue(transport.getDisposed());
assertFalse(transport.getException());
// Close Alice's database // Close Alice's database
db.close(); db.close();
// Return the contents of the batch connection // Return the contents of the batch connection
@@ -172,7 +173,9 @@ public class BatchConnectionReadWriteTest extends TestCase {
assertFalse(listener.messagesAdded); assertFalse(listener.messagesAdded);
// Read whatever needs to be read // Read whatever needs to be read
batchIn.read(); batchIn.read();
assertTrue(transport.getSuccess()); assertTrue(transport.getDisposed());
assertFalse(transport.getException());
assertTrue(transport.getRecognised());
// The private message from Alice should have been added // The private message from Alice should have been added
assertTrue(listener.messagesAdded); assertTrue(listener.messagesAdded);
// Close Bob's database // Close Bob's database

View File

@@ -87,8 +87,9 @@ public class OutgoingBatchConnectionTest extends TestCase {
connection.write(); connection.write();
// Nothing should have been written // Nothing should have been written
assertEquals(0, out.size()); assertEquals(0, out.size());
// The transport should have been disposed with success == false // The transport should have been disposed with exception == true
assertFalse(transport.getSuccess()); assertTrue(transport.getDisposed());
assertTrue(transport.getException());
context.assertIsSatisfied(); context.assertIsSatisfied();
} }
@@ -122,8 +123,9 @@ public class OutgoingBatchConnectionTest extends TestCase {
connection.write(); connection.write();
// Nothing should have been written // Nothing should have been written
assertEquals(0, out.size()); assertEquals(0, out.size());
// The transport should have been disposed with success == true // The transport should have been disposed with exception == false
assertTrue(transport.getSuccess()); assertTrue(transport.getDisposed());
assertFalse(transport.getException());
context.assertIsSatisfied(); context.assertIsSatisfied();
} }
@@ -171,8 +173,9 @@ public class OutgoingBatchConnectionTest extends TestCase {
connection.write(); connection.write();
// Something should have been written // Something should have been written
assertTrue(out.size() > UniqueId.LENGTH + message.length); assertTrue(out.size() > UniqueId.LENGTH + message.length);
// The transport should have been disposed with success == true // The transport should have been disposed with exception == false
assertTrue(transport.getSuccess()); assertTrue(transport.getDisposed());
assertFalse(transport.getException());
context.assertIsSatisfied(); context.assertIsSatisfied();
} }
} }

View File

@@ -4,12 +4,11 @@ import java.io.InputStream;
import net.sf.briar.api.transport.BatchTransportReader; import net.sf.briar.api.transport.BatchTransportReader;
class TestBatchTransportReader class TestBatchTransportReader implements BatchTransportReader {
implements BatchTransportReader {
private final InputStream in; private final InputStream in;
private boolean success = false; private boolean disposed = false, exception = false, recognised = false;
TestBatchTransportReader(InputStream in) { TestBatchTransportReader(InputStream in) {
this.in = in; this.in = in;
@@ -19,11 +18,22 @@ implements BatchTransportReader {
return in; return in;
} }
public void dispose(boolean success) { public void dispose(boolean exception, boolean recognised) {
this.success = success; assert !disposed;
disposed = true;
this.exception = exception;
this.recognised = recognised;
} }
boolean getSuccess() { boolean getDisposed() {
return success; return disposed;
}
boolean getException() {
return exception;
}
boolean getRecognised() {
return recognised;
} }
} }

View File

@@ -5,16 +5,14 @@ import java.io.OutputStream;
import net.sf.briar.api.transport.BatchTransportWriter; import net.sf.briar.api.transport.BatchTransportWriter;
class TestBatchTransportWriter class TestBatchTransportWriter implements BatchTransportWriter {
implements BatchTransportWriter {
private final ByteArrayOutputStream out; private final ByteArrayOutputStream out;
private final long capacity; private final long capacity;
private boolean success = false; private boolean disposed = false, exception = false;
TestBatchTransportWriter(ByteArrayOutputStream out, TestBatchTransportWriter(ByteArrayOutputStream out, long capacity) {
long capacity) {
this.out = out; this.out = out;
this.capacity = capacity; this.capacity = capacity;
} }
@@ -27,11 +25,17 @@ implements BatchTransportWriter {
return out; return out;
} }
public void dispose(boolean success) { public void dispose(boolean exception) {
this.success = success; assert !disposed;
disposed = true;
this.exception = exception;
} }
boolean getSuccess() { boolean getDisposed() {
return success; return disposed;
}
boolean getException() {
return exception;
} }
} }