Factored ConnectionRecogniser out of transport plugins.

This commit is contained in:
akwizgran
2011-10-05 18:48:10 +01:00
parent af41e42c6c
commit 3ffb56bf12
5 changed files with 30 additions and 121 deletions

View File

@@ -10,8 +10,7 @@ import net.sf.briar.api.transport.TransportCallback;
*/ */
public interface BatchTransportCallback extends TransportCallback { public interface BatchTransportCallback extends TransportCallback {
void readerCreated(ContactId contactId, byte[] encryptedIv, void readerCreated(BatchTransportReader r);
BatchTransportReader r);
void writerCreated(ContactId contactId, TransportId t, long connection, void writerCreated(ContactId contactId, TransportId t, long connection,
BatchTransportWriter w); BatchTransportWriter w);

View File

@@ -10,8 +10,7 @@ import net.sf.briar.api.transport.TransportCallback;
*/ */
public interface StreamTransportCallback extends TransportCallback { public interface StreamTransportCallback extends TransportCallback {
void incomingConnectionCreated(ContactId contactId, byte[] encryptedIv, void incomingConnectionCreated(StreamTransportConnection c);
StreamTransportConnection c);
void outgoingConnectionCreated(ContactId contactId, TransportId t, void outgoingConnectionCreated(ContactId contactId, TransportId t,
long connection, StreamTransportConnection c); long connection, StreamTransportConnection c);

View File

@@ -9,8 +9,6 @@ import java.util.Map;
import java.util.concurrent.Executor; import java.util.concurrent.Executor;
import net.sf.briar.api.ContactId; import net.sf.briar.api.ContactId;
import net.sf.briar.api.db.DbException;
import net.sf.briar.api.transport.ConnectionRecogniser;
import net.sf.briar.api.transport.InvalidConfigException; import net.sf.briar.api.transport.InvalidConfigException;
import net.sf.briar.api.transport.InvalidTransportException; import net.sf.briar.api.transport.InvalidTransportException;
import net.sf.briar.api.transport.TransportConstants; import net.sf.briar.api.transport.TransportConstants;
@@ -23,7 +21,6 @@ import org.apache.commons.io.FileSystemUtils;
abstract class FilePlugin implements BatchTransportPlugin { abstract class FilePlugin implements BatchTransportPlugin {
private final ConnectionRecogniser recogniser;
private final Executor executor; private final Executor executor;
protected Map<String, String> localProperties = null; protected Map<String, String> localProperties = null;
@@ -36,8 +33,7 @@ abstract class FilePlugin implements BatchTransportPlugin {
protected abstract File chooseOutputDirectory(); protected abstract File chooseOutputDirectory();
protected abstract void writerFinished(File f); protected abstract void writerFinished(File f);
FilePlugin(ConnectionRecogniser recogniser, Executor executor) { FilePlugin(Executor executor) {
this.recogniser = recogniser;
this.executor = executor; this.executor = executor;
} }
@@ -142,31 +138,8 @@ abstract class FilePlugin implements BatchTransportPlugin {
if(f.length() < TransportConstants.MIN_CONNECTION_LENGTH) return; if(f.length() < TransportConstants.MIN_CONNECTION_LENGTH) return;
try { try {
FileInputStream in = new FileInputStream(f); FileInputStream in = new FileInputStream(f);
byte[] iv = new byte[TransportConstants.IV_LENGTH]; callback.readerCreated(new FileTransportReader(f, in));
int offset = 0;
while(offset < iv.length) {
int read = in.read(iv, offset, iv.length - offset);
if(read == -1) break;
offset += read;
}
if(offset < iv.length) {
// The file was truncated
in.close();
return;
}
ContactId c = recogniser.acceptConnection(iv);
if(c == null) {
// Nobody there
in.close();
return;
}
FileTransportReader reader = new FileTransportReader(f, in);
callback.readerCreated(c, iv, reader);
} catch(DbException e) {
// FIXME: At least log it
return;
} catch(IOException e) { } catch(IOException e) {
// FIXME: At least log it
return; return;
} }
} }

View File

@@ -8,7 +8,6 @@ import java.util.concurrent.Executor;
import net.sf.briar.api.ContactId; import net.sf.briar.api.ContactId;
import net.sf.briar.api.TransportId; import net.sf.briar.api.TransportId;
import net.sf.briar.api.transport.ConnectionRecogniser;
import net.sf.briar.api.transport.InvalidConfigException; import net.sf.briar.api.transport.InvalidConfigException;
import net.sf.briar.api.transport.InvalidTransportException; import net.sf.briar.api.transport.InvalidTransportException;
import net.sf.briar.api.transport.batch.BatchTransportCallback; import net.sf.briar.api.transport.batch.BatchTransportCallback;
@@ -23,9 +22,9 @@ implements RemovableDriveMonitor.Callback {
private final RemovableDriveFinder finder; private final RemovableDriveFinder finder;
private final RemovableDriveMonitor monitor; private final RemovableDriveMonitor monitor;
RemovableDrivePlugin(ConnectionRecogniser recogniser, Executor executor, RemovableDrivePlugin(Executor executor, RemovableDriveFinder finder,
RemovableDriveFinder finder, RemovableDriveMonitor monitor) { RemovableDriveMonitor monitor) {
super(recogniser, executor); super(executor);
this.finder = finder; this.finder = finder;
this.monitor = monitor; this.monitor = monitor;
} }

View File

@@ -11,10 +11,8 @@ import java.util.concurrent.Executor;
import junit.framework.TestCase; import junit.framework.TestCase;
import net.sf.briar.TestUtils; import net.sf.briar.TestUtils;
import net.sf.briar.api.ContactId; import net.sf.briar.api.ContactId;
import net.sf.briar.api.transport.ConnectionRecogniser;
import net.sf.briar.api.transport.TransportConstants; import net.sf.briar.api.transport.TransportConstants;
import net.sf.briar.api.transport.batch.BatchTransportCallback; import net.sf.briar.api.transport.batch.BatchTransportCallback;
import net.sf.briar.api.transport.batch.BatchTransportReader;
import net.sf.briar.api.transport.batch.BatchTransportWriter; import net.sf.briar.api.transport.batch.BatchTransportWriter;
import net.sf.briar.plugins.file.RemovableDriveMonitor.Callback; import net.sf.briar.plugins.file.RemovableDriveMonitor.Callback;
@@ -38,15 +36,13 @@ public class RemovableDrivePluginTest extends TestCase {
public void testGetId() { public void testGetId() {
Mockery context = new Mockery(); Mockery context = new Mockery();
final Executor executor = context.mock(Executor.class); final Executor executor = context.mock(Executor.class);
final ConnectionRecogniser recogniser =
context.mock(ConnectionRecogniser.class);
final RemovableDriveFinder finder = final RemovableDriveFinder finder =
context.mock(RemovableDriveFinder.class); context.mock(RemovableDriveFinder.class);
final RemovableDriveMonitor monitor = final RemovableDriveMonitor monitor =
context.mock(RemovableDriveMonitor.class); context.mock(RemovableDriveMonitor.class);
RemovableDrivePlugin plugin = new RemovableDrivePlugin(recogniser, RemovableDrivePlugin plugin = new RemovableDrivePlugin(executor,
executor, finder, monitor); finder, monitor);
assertEquals(RemovableDrivePlugin.TRANSPORT_ID, assertEquals(RemovableDrivePlugin.TRANSPORT_ID,
plugin.getId().getInt()); plugin.getId().getInt());
@@ -60,8 +56,6 @@ public class RemovableDrivePluginTest extends TestCase {
Mockery context = new Mockery(); Mockery context = new Mockery();
final Executor executor = context.mock(Executor.class); final Executor executor = context.mock(Executor.class);
final ConnectionRecogniser recogniser =
context.mock(ConnectionRecogniser.class);
final RemovableDriveFinder finder = final RemovableDriveFinder finder =
context.mock(RemovableDriveFinder.class); context.mock(RemovableDriveFinder.class);
final RemovableDriveMonitor monitor = final RemovableDriveMonitor monitor =
@@ -75,8 +69,8 @@ public class RemovableDrivePluginTest extends TestCase {
will(returnValue(drives)); will(returnValue(drives));
}}); }});
RemovableDrivePlugin plugin = new RemovableDrivePlugin(recogniser, RemovableDrivePlugin plugin = new RemovableDrivePlugin(executor,
executor, finder, monitor); finder, monitor);
plugin.start(null, null, null, callback); plugin.start(null, null, null, callback);
assertNull(plugin.createWriter(contactId)); assertNull(plugin.createWriter(contactId));
@@ -94,8 +88,6 @@ public class RemovableDrivePluginTest extends TestCase {
Mockery context = new Mockery(); Mockery context = new Mockery();
final Executor executor = context.mock(Executor.class); final Executor executor = context.mock(Executor.class);
final ConnectionRecogniser recogniser =
context.mock(ConnectionRecogniser.class);
final RemovableDriveFinder finder = final RemovableDriveFinder finder =
context.mock(RemovableDriveFinder.class); context.mock(RemovableDriveFinder.class);
final RemovableDriveMonitor monitor = final RemovableDriveMonitor monitor =
@@ -112,8 +104,8 @@ public class RemovableDrivePluginTest extends TestCase {
will(returnValue(-1)); // The user cancelled the choice will(returnValue(-1)); // The user cancelled the choice
}}); }});
RemovableDrivePlugin plugin = new RemovableDrivePlugin(recogniser, RemovableDrivePlugin plugin = new RemovableDrivePlugin(executor,
executor, finder, monitor); finder, monitor);
plugin.start(null, null, null, callback); plugin.start(null, null, null, callback);
assertNull(plugin.createWriter(contactId)); assertNull(plugin.createWriter(contactId));
@@ -133,8 +125,6 @@ public class RemovableDrivePluginTest extends TestCase {
Mockery context = new Mockery(); Mockery context = new Mockery();
final Executor executor = context.mock(Executor.class); final Executor executor = context.mock(Executor.class);
final ConnectionRecogniser recogniser =
context.mock(ConnectionRecogniser.class);
final RemovableDriveFinder finder = final RemovableDriveFinder finder =
context.mock(RemovableDriveFinder.class); context.mock(RemovableDriveFinder.class);
final RemovableDriveMonitor monitor = final RemovableDriveMonitor monitor =
@@ -151,8 +141,8 @@ public class RemovableDrivePluginTest extends TestCase {
will(returnValue(0)); // The user chose drive1 but it doesn't exist will(returnValue(0)); // The user chose drive1 but it doesn't exist
}}); }});
RemovableDrivePlugin plugin = new RemovableDrivePlugin(recogniser, RemovableDrivePlugin plugin = new RemovableDrivePlugin(executor,
executor, finder, monitor); finder, monitor);
plugin.start(null, null, null, callback); plugin.start(null, null, null, callback);
assertNull(plugin.createWriter(contactId)); assertNull(plugin.createWriter(contactId));
@@ -174,8 +164,6 @@ public class RemovableDrivePluginTest extends TestCase {
Mockery context = new Mockery(); Mockery context = new Mockery();
final Executor executor = context.mock(Executor.class); final Executor executor = context.mock(Executor.class);
final ConnectionRecogniser recogniser =
context.mock(ConnectionRecogniser.class);
final RemovableDriveFinder finder = final RemovableDriveFinder finder =
context.mock(RemovableDriveFinder.class); context.mock(RemovableDriveFinder.class);
final RemovableDriveMonitor monitor = final RemovableDriveMonitor monitor =
@@ -192,8 +180,8 @@ public class RemovableDrivePluginTest extends TestCase {
will(returnValue(0)); // The user chose drive1 but it's not a dir will(returnValue(0)); // The user chose drive1 but it's not a dir
}}); }});
RemovableDrivePlugin plugin = new RemovableDrivePlugin(recogniser, RemovableDrivePlugin plugin = new RemovableDrivePlugin(executor,
executor, finder, monitor); finder, monitor);
plugin.start(null, null, null, callback); plugin.start(null, null, null, callback);
assertNull(plugin.createWriter(contactId)); assertNull(plugin.createWriter(contactId));
@@ -215,8 +203,6 @@ public class RemovableDrivePluginTest extends TestCase {
Mockery context = new Mockery(); Mockery context = new Mockery();
final Executor executor = context.mock(Executor.class); final Executor executor = context.mock(Executor.class);
final ConnectionRecogniser recogniser =
context.mock(ConnectionRecogniser.class);
final RemovableDriveFinder finder = final RemovableDriveFinder finder =
context.mock(RemovableDriveFinder.class); context.mock(RemovableDriveFinder.class);
final RemovableDriveMonitor monitor = final RemovableDriveMonitor monitor =
@@ -233,8 +219,8 @@ public class RemovableDrivePluginTest extends TestCase {
will(returnValue(0)); // The user chose drive1 will(returnValue(0)); // The user chose drive1
}}); }});
RemovableDrivePlugin plugin = new RemovableDrivePlugin(recogniser, RemovableDrivePlugin plugin = new RemovableDrivePlugin(executor,
executor, finder, monitor); finder, monitor);
plugin.start(null, null, null, callback); plugin.start(null, null, null, callback);
assertNotNull(plugin.createWriter(contactId)); assertNotNull(plugin.createWriter(contactId));
@@ -259,8 +245,6 @@ public class RemovableDrivePluginTest extends TestCase {
Mockery context = new Mockery(); Mockery context = new Mockery();
final Executor executor = context.mock(Executor.class); final Executor executor = context.mock(Executor.class);
final ConnectionRecogniser recogniser =
context.mock(ConnectionRecogniser.class);
final RemovableDriveFinder finder = final RemovableDriveFinder finder =
context.mock(RemovableDriveFinder.class); context.mock(RemovableDriveFinder.class);
final RemovableDriveMonitor monitor = final RemovableDriveMonitor monitor =
@@ -278,8 +262,8 @@ public class RemovableDrivePluginTest extends TestCase {
oneOf(callback).showMessage(with(any(String.class))); oneOf(callback).showMessage(with(any(String.class)));
}}); }});
RemovableDrivePlugin plugin = new RemovableDrivePlugin(recogniser, RemovableDrivePlugin plugin = new RemovableDrivePlugin(executor,
executor, finder, monitor); finder, monitor);
plugin.start(null, null, null, callback); plugin.start(null, null, null, callback);
BatchTransportWriter writer = plugin.createWriter(contactId); BatchTransportWriter writer = plugin.createWriter(contactId);
@@ -308,8 +292,6 @@ public class RemovableDrivePluginTest extends TestCase {
public void testEmptyDriveIsIgnored() throws Exception { public void testEmptyDriveIsIgnored() throws Exception {
Mockery context = new Mockery(); Mockery context = new Mockery();
final Executor executor = context.mock(Executor.class); final Executor executor = context.mock(Executor.class);
final ConnectionRecogniser recogniser =
context.mock(ConnectionRecogniser.class);
final RemovableDriveFinder finder = final RemovableDriveFinder finder =
context.mock(RemovableDriveFinder.class); context.mock(RemovableDriveFinder.class);
final RemovableDriveMonitor monitor = final RemovableDriveMonitor monitor =
@@ -321,8 +303,8 @@ public class RemovableDrivePluginTest extends TestCase {
oneOf(monitor).start(with(any(Callback.class))); oneOf(monitor).start(with(any(Callback.class)));
}}); }});
RemovableDrivePlugin plugin = new RemovableDrivePlugin(recogniser, RemovableDrivePlugin plugin = new RemovableDrivePlugin(executor,
executor, finder, monitor); finder, monitor);
plugin.start(null, null, null, callback); plugin.start(null, null, null, callback);
plugin.driveInserted(testDir); plugin.driveInserted(testDir);
@@ -334,15 +316,13 @@ public class RemovableDrivePluginTest extends TestCase {
public void testFilenames() { public void testFilenames() {
Mockery context = new Mockery(); Mockery context = new Mockery();
final Executor executor = context.mock(Executor.class); final Executor executor = context.mock(Executor.class);
final ConnectionRecogniser recogniser =
context.mock(ConnectionRecogniser.class);
final RemovableDriveFinder finder = final RemovableDriveFinder finder =
context.mock(RemovableDriveFinder.class); context.mock(RemovableDriveFinder.class);
final RemovableDriveMonitor monitor = final RemovableDriveMonitor monitor =
context.mock(RemovableDriveMonitor.class); context.mock(RemovableDriveMonitor.class);
RemovableDrivePlugin plugin = new RemovableDrivePlugin(recogniser, RemovableDrivePlugin plugin = new RemovableDrivePlugin(executor,
executor, finder, monitor); finder, monitor);
assertFalse(plugin.isPossibleConnectionFilename("abcdefg.dat")); assertFalse(plugin.isPossibleConnectionFilename("abcdefg.dat"));
assertFalse(plugin.isPossibleConnectionFilename("abcdefghi.dat")); assertFalse(plugin.isPossibleConnectionFilename("abcdefghi.dat"));
@@ -357,8 +337,6 @@ public class RemovableDrivePluginTest extends TestCase {
@Test @Test
public void testSmallFileIsIgnored() throws Exception { public void testSmallFileIsIgnored() throws Exception {
Mockery context = new Mockery(); Mockery context = new Mockery();
final ConnectionRecogniser recogniser =
context.mock(ConnectionRecogniser.class);
final RemovableDriveFinder finder = final RemovableDriveFinder finder =
context.mock(RemovableDriveFinder.class); context.mock(RemovableDriveFinder.class);
final RemovableDriveMonitor monitor = final RemovableDriveMonitor monitor =
@@ -370,8 +348,8 @@ public class RemovableDrivePluginTest extends TestCase {
oneOf(monitor).start(with(any(Callback.class))); oneOf(monitor).start(with(any(Callback.class)));
}}); }});
RemovableDrivePlugin plugin = new RemovableDrivePlugin(recogniser, RemovableDrivePlugin plugin = new RemovableDrivePlugin(new ImmediateExecutor(),
new ImmediateExecutor(), finder, monitor); finder, monitor);
plugin.start(null, null, null, callback); plugin.start(null, null, null, callback);
File f = new File(testDir, "abcdefgh.dat"); File f = new File(testDir, "abcdefgh.dat");
@@ -385,44 +363,9 @@ public class RemovableDrivePluginTest extends TestCase {
context.assertIsSatisfied(); context.assertIsSatisfied();
} }
@Test
public void testIvIsChecked() throws Exception {
Mockery context = new Mockery();
final ConnectionRecogniser recogniser =
context.mock(ConnectionRecogniser.class);
final RemovableDriveFinder finder =
context.mock(RemovableDriveFinder.class);
final RemovableDriveMonitor monitor =
context.mock(RemovableDriveMonitor.class);
final BatchTransportCallback callback =
context.mock(BatchTransportCallback.class);
context.checking(new Expectations() {{
oneOf(monitor).start(with(any(Callback.class)));
oneOf(recogniser).acceptConnection(with(any(byte[].class)));
will(returnValue(null));
}});
RemovableDrivePlugin plugin = new RemovableDrivePlugin(recogniser,
new ImmediateExecutor(), finder, monitor);
plugin.start(null, null, null, callback);
File f = new File(testDir, "abcdefgh.dat");
OutputStream out = new FileOutputStream(f);
out.write(new byte[TransportConstants.MIN_CONNECTION_LENGTH]);
out.flush();
out.close();
assertEquals(TransportConstants.MIN_CONNECTION_LENGTH, f.length());
plugin.driveInserted(testDir);
context.assertIsSatisfied();
}
@Test @Test
public void testReaderIsCreated() throws Exception { public void testReaderIsCreated() throws Exception {
Mockery context = new Mockery(); Mockery context = new Mockery();
final ConnectionRecogniser recogniser =
context.mock(ConnectionRecogniser.class);
final RemovableDriveFinder finder = final RemovableDriveFinder finder =
context.mock(RemovableDriveFinder.class); context.mock(RemovableDriveFinder.class);
final RemovableDriveMonitor monitor = final RemovableDriveMonitor monitor =
@@ -432,15 +375,11 @@ public class RemovableDrivePluginTest extends TestCase {
context.checking(new Expectations() {{ context.checking(new Expectations() {{
oneOf(monitor).start(with(any(Callback.class))); oneOf(monitor).start(with(any(Callback.class)));
oneOf(recogniser).acceptConnection(with(any(byte[].class))); oneOf(callback).readerCreated(with(any(FileTransportReader.class)));
will(returnValue(contactId));
oneOf(callback).readerCreated(with(contactId),
with(any(byte[].class)),
with(any(BatchTransportReader.class)));
}}); }});
RemovableDrivePlugin plugin = new RemovableDrivePlugin(recogniser, RemovableDrivePlugin plugin = new RemovableDrivePlugin(new ImmediateExecutor(),
new ImmediateExecutor(), finder, monitor); finder, monitor);
plugin.start(null, null, null, callback); plugin.start(null, null, null, callback);
File f = new File(testDir, "abcdefgh.dat"); File f = new File(testDir, "abcdefgh.dat");