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 {
void readerCreated(ContactId contactId, byte[] encryptedIv,
BatchTransportReader r);
void readerCreated(BatchTransportReader r);
void writerCreated(ContactId contactId, TransportId t, long connection,
BatchTransportWriter w);

View File

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

View File

@@ -9,8 +9,6 @@ import java.util.Map;
import java.util.concurrent.Executor;
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.InvalidTransportException;
import net.sf.briar.api.transport.TransportConstants;
@@ -23,7 +21,6 @@ import org.apache.commons.io.FileSystemUtils;
abstract class FilePlugin implements BatchTransportPlugin {
private final ConnectionRecogniser recogniser;
private final Executor executor;
protected Map<String, String> localProperties = null;
@@ -36,8 +33,7 @@ abstract class FilePlugin implements BatchTransportPlugin {
protected abstract File chooseOutputDirectory();
protected abstract void writerFinished(File f);
FilePlugin(ConnectionRecogniser recogniser, Executor executor) {
this.recogniser = recogniser;
FilePlugin(Executor executor) {
this.executor = executor;
}
@@ -142,31 +138,8 @@ abstract class FilePlugin implements BatchTransportPlugin {
if(f.length() < TransportConstants.MIN_CONNECTION_LENGTH) return;
try {
FileInputStream in = new FileInputStream(f);
byte[] iv = new byte[TransportConstants.IV_LENGTH];
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;
callback.readerCreated(new FileTransportReader(f, in));
} catch(IOException e) {
// FIXME: At least log it
return;
}
}

View File

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

View File

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