De-uglified some code and moved two API classes.

This commit is contained in:
akwizgran
2011-10-14 21:49:58 +01:00
parent cb06ebc5d2
commit 0ef1fcb686
12 changed files with 72 additions and 77 deletions

View File

@@ -0,0 +1,13 @@
package net.sf.briar.api.transport;
import net.sf.briar.api.ContactId;
import net.sf.briar.api.TransportId;
public interface BatchConnectionFactory {
void createIncomingConnection(ContactId c, BatchTransportReader r,
byte[] encryptedIv);
void createOutgoingConnection(TransportId t, ContactId c,
BatchTransportWriter w);
}

View File

@@ -0,0 +1,13 @@
package net.sf.briar.api.transport;
import net.sf.briar.api.ContactId;
import net.sf.briar.api.TransportId;
public interface StreamConnectionFactory {
void createIncomingConnection(ContactId c, StreamTransportConnection s,
byte[] encryptedIv);
void createOutgoingConnection(TransportId t, ContactId c,
StreamTransportConnection s);
}

View File

@@ -1,15 +0,0 @@
package net.sf.briar.api.transport.batch;
import net.sf.briar.api.ContactId;
import net.sf.briar.api.TransportId;
import net.sf.briar.api.transport.BatchTransportReader;
import net.sf.briar.api.transport.BatchTransportWriter;
public interface BatchConnectionFactory {
Runnable createIncomingConnection(ContactId c, BatchTransportReader r,
byte[] encryptedIv);
Runnable createOutgoingConnection(TransportId t, ContactId c,
BatchTransportWriter w);
}

View File

@@ -1,14 +0,0 @@
package net.sf.briar.api.transport.stream;
import net.sf.briar.api.ContactId;
import net.sf.briar.api.TransportId;
import net.sf.briar.api.transport.StreamTransportConnection;
public interface StreamConnectionFactory {
Runnable[] createIncomingConnection(ContactId c,
StreamTransportConnection s, byte[] encryptedIv);
Runnable[] createOutgoingConnection(TransportId t, ContactId c,
StreamTransportConnection s);
}

View File

@@ -4,39 +4,35 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.concurrent.Executor;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
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.db.DbException; import net.sf.briar.api.db.DbException;
import net.sf.briar.api.transport.BatchConnectionFactory;
import net.sf.briar.api.transport.BatchTransportReader; import net.sf.briar.api.transport.BatchTransportReader;
import net.sf.briar.api.transport.BatchTransportWriter; import net.sf.briar.api.transport.BatchTransportWriter;
import net.sf.briar.api.transport.ConnectionDispatcher; import net.sf.briar.api.transport.ConnectionDispatcher;
import net.sf.briar.api.transport.ConnectionRecogniser; import net.sf.briar.api.transport.ConnectionRecogniser;
import net.sf.briar.api.transport.ConnectionRecogniserFactory; import net.sf.briar.api.transport.ConnectionRecogniserFactory;
import net.sf.briar.api.transport.StreamConnectionFactory;
import net.sf.briar.api.transport.StreamTransportConnection; import net.sf.briar.api.transport.StreamTransportConnection;
import net.sf.briar.api.transport.TransportConstants; import net.sf.briar.api.transport.TransportConstants;
import net.sf.briar.api.transport.batch.BatchConnectionFactory;
import net.sf.briar.api.transport.stream.StreamConnectionFactory;
public class ConnectionDispatcherImpl implements ConnectionDispatcher { public class ConnectionDispatcherImpl implements ConnectionDispatcher {
private static final Logger LOG = private static final Logger LOG =
Logger.getLogger(ConnectionDispatcherImpl.class.getName()); Logger.getLogger(ConnectionDispatcherImpl.class.getName());
private final Executor executor;
private final ConnectionRecogniserFactory recFactory; private final ConnectionRecogniserFactory recFactory;
private final BatchConnectionFactory batchConnFactory; private final BatchConnectionFactory batchConnFactory;
private final StreamConnectionFactory streamConnFactory; private final StreamConnectionFactory streamConnFactory;
private final Map<TransportId, ConnectionRecogniser> recognisers; private final Map<TransportId, ConnectionRecogniser> recognisers;
ConnectionDispatcherImpl(Executor executor, ConnectionDispatcherImpl(ConnectionRecogniserFactory recFactory,
ConnectionRecogniserFactory recFactory,
BatchConnectionFactory batchConnFactory, BatchConnectionFactory batchConnFactory,
StreamConnectionFactory streamConnFactory) { StreamConnectionFactory streamConnFactory) {
this.executor = executor;
this.recFactory = recFactory; this.recFactory = recFactory;
this.batchConnFactory = batchConnFactory; this.batchConnFactory = batchConnFactory;
this.streamConnFactory = streamConnFactory; this.streamConnFactory = streamConnFactory;
@@ -67,9 +63,7 @@ public class ConnectionDispatcherImpl implements ConnectionDispatcher {
r.dispose(false); r.dispose(false);
return; return;
} }
// Pass the connection to the executor and return batchConnFactory.createIncomingConnection(c, r, encryptedIv);
executor.execute(batchConnFactory.createIncomingConnection(c, r,
encryptedIv));
} }
private byte[] readIv(InputStream in) throws IOException { private byte[] readIv(InputStream in) throws IOException {
@@ -96,7 +90,7 @@ public class ConnectionDispatcherImpl implements ConnectionDispatcher {
public void dispatchWriter(TransportId t, ContactId c, public void dispatchWriter(TransportId t, ContactId c,
BatchTransportWriter w) { BatchTransportWriter w) {
executor.execute(batchConnFactory.createOutgoingConnection(t, c, w)); batchConnFactory.createOutgoingConnection(t, c, w);
} }
public void dispatchIncomingConnection(TransportId t, public void dispatchIncomingConnection(TransportId t,
@@ -124,19 +118,11 @@ public class ConnectionDispatcherImpl implements ConnectionDispatcher {
s.dispose(false); s.dispose(false);
return; return;
} }
// Pass the connection to the executor and return streamConnFactory.createIncomingConnection(c, s, encryptedIv);
Runnable[] r = streamConnFactory.createIncomingConnection(c, s,
encryptedIv);
assert r.length == 2;
executor.execute(r[0]); // Write
executor.execute(r[1]); // Read
} }
public void dispatchOutgoingConnection(TransportId t, ContactId c, public void dispatchOutgoingConnection(TransportId t, ContactId c,
StreamTransportConnection s) { StreamTransportConnection s) {
Runnable[] r = streamConnFactory.createOutgoingConnection(t, c, s); streamConnFactory.createOutgoingConnection(t, c, s);
assert r.length == 2;
executor.execute(r[0]); // Write
executor.execute(r[1]); // Read
} }
} }

View File

@@ -5,11 +5,11 @@ import net.sf.briar.api.TransportId;
import net.sf.briar.api.db.DatabaseComponent; import net.sf.briar.api.db.DatabaseComponent;
import net.sf.briar.api.protocol.ProtocolReaderFactory; import net.sf.briar.api.protocol.ProtocolReaderFactory;
import net.sf.briar.api.protocol.writers.ProtocolWriterFactory; import net.sf.briar.api.protocol.writers.ProtocolWriterFactory;
import net.sf.briar.api.transport.BatchConnectionFactory;
import net.sf.briar.api.transport.BatchTransportReader; import net.sf.briar.api.transport.BatchTransportReader;
import net.sf.briar.api.transport.BatchTransportWriter; import net.sf.briar.api.transport.BatchTransportWriter;
import net.sf.briar.api.transport.ConnectionReaderFactory; import net.sf.briar.api.transport.ConnectionReaderFactory;
import net.sf.briar.api.transport.ConnectionWriterFactory; import net.sf.briar.api.transport.ConnectionWriterFactory;
import net.sf.briar.api.transport.batch.BatchConnectionFactory;
import com.google.inject.Inject; import com.google.inject.Inject;
@@ -33,15 +33,27 @@ class BatchConnectionFactoryImpl implements BatchConnectionFactory {
this.protoWriterFactory = protoWriterFactory; this.protoWriterFactory = protoWriterFactory;
} }
public Runnable createIncomingConnection(ContactId c, public void createIncomingConnection(ContactId c,
BatchTransportReader r, byte[] encryptedIv) { BatchTransportReader r, byte[] encryptedIv) {
return new IncomingBatchConnection(connReaderFactory, db, final IncomingBatchConnection conn = new IncomingBatchConnection(
protoReaderFactory, c, r, encryptedIv); connReaderFactory, db, protoReaderFactory, c, r, encryptedIv);
Runnable read = new Runnable() {
public void run() {
conn.read();
}
};
new Thread(read).start();
} }
public Runnable createOutgoingConnection(TransportId t, ContactId c, public void createOutgoingConnection(TransportId t, ContactId c,
BatchTransportWriter w) { BatchTransportWriter w) {
return new OutgoingBatchConnection(connWriterFactory, db, final OutgoingBatchConnection conn = new OutgoingBatchConnection(
protoWriterFactory, t, c, w); connWriterFactory, db, protoWriterFactory, t, c, w);
Runnable write = new Runnable() {
public void run() {
conn.write();
}
};
new Thread(write).start();
} }
} }

View File

@@ -18,7 +18,7 @@ import net.sf.briar.api.transport.BatchTransportReader;
import net.sf.briar.api.transport.ConnectionReader; import net.sf.briar.api.transport.ConnectionReader;
import net.sf.briar.api.transport.ConnectionReaderFactory; import net.sf.briar.api.transport.ConnectionReaderFactory;
class IncomingBatchConnection implements Runnable { class IncomingBatchConnection {
private static final Logger LOG = private static final Logger LOG =
Logger.getLogger(IncomingBatchConnection.class.getName()); Logger.getLogger(IncomingBatchConnection.class.getName());
@@ -42,7 +42,7 @@ class IncomingBatchConnection implements Runnable {
this.encryptedIv = encryptedIv; this.encryptedIv = encryptedIv;
} }
public void run() { void read() {
try { try {
byte[] secret = db.getSharedSecret(contactId); byte[] secret = db.getSharedSecret(contactId);
ConnectionReader conn = connFactory.createConnectionReader( ConnectionReader conn = connFactory.createConnectionReader(

View File

@@ -20,7 +20,7 @@ import net.sf.briar.api.transport.BatchTransportWriter;
import net.sf.briar.api.transport.ConnectionWriter; import net.sf.briar.api.transport.ConnectionWriter;
import net.sf.briar.api.transport.ConnectionWriterFactory; import net.sf.briar.api.transport.ConnectionWriterFactory;
class OutgoingBatchConnection implements Runnable { class OutgoingBatchConnection {
private static final Logger LOG = private static final Logger LOG =
Logger.getLogger(OutgoingBatchConnection.class.getName()); Logger.getLogger(OutgoingBatchConnection.class.getName());
@@ -44,7 +44,7 @@ class OutgoingBatchConnection implements Runnable {
this.writer = writer; this.writer = writer;
} }
public void run() { void write() {
try { try {
byte[] secret = db.getSharedSecret(contactId); byte[] secret = db.getSharedSecret(contactId);
long connection = db.getConnectionNumber(contactId, transportId); long connection = db.getConnectionNumber(contactId, transportId);

View File

@@ -1,6 +1,6 @@
package net.sf.briar.transport.batch; package net.sf.briar.transport.batch;
import net.sf.briar.api.transport.batch.BatchConnectionFactory; import net.sf.briar.api.transport.BatchConnectionFactory;
import com.google.inject.AbstractModule; import com.google.inject.AbstractModule;
import com.google.inject.Singleton; import com.google.inject.Singleton;

View File

@@ -94,7 +94,7 @@ abstract class StreamConnection implements DatabaseListener {
} }
} }
public void read() { void read() {
try { try {
InputStream in = createConnectionReader().getInputStream(); InputStream in = createConnectionReader().getInputStream();
ProtocolReader proto = protoReaderFactory.createProtocolReader(in); ProtocolReader proto = protoReaderFactory.createProtocolReader(in);
@@ -160,7 +160,7 @@ abstract class StreamConnection implements DatabaseListener {
connection.dispose(true); connection.dispose(true);
} }
public void write() { void write() {
try { try {
OutputStream out = createConnectionWriter().getOutputStream(); OutputStream out = createConnectionWriter().getOutputStream();
// Create the packet writers // Create the packet writers

View File

@@ -7,8 +7,8 @@ import net.sf.briar.api.protocol.ProtocolReaderFactory;
import net.sf.briar.api.protocol.writers.ProtocolWriterFactory; import net.sf.briar.api.protocol.writers.ProtocolWriterFactory;
import net.sf.briar.api.transport.ConnectionReaderFactory; import net.sf.briar.api.transport.ConnectionReaderFactory;
import net.sf.briar.api.transport.ConnectionWriterFactory; import net.sf.briar.api.transport.ConnectionWriterFactory;
import net.sf.briar.api.transport.StreamConnectionFactory;
import net.sf.briar.api.transport.StreamTransportConnection; import net.sf.briar.api.transport.StreamTransportConnection;
import net.sf.briar.api.transport.stream.StreamConnectionFactory;
import com.google.inject.Inject; import com.google.inject.Inject;
@@ -32,42 +32,42 @@ public class StreamConnectionFactoryImpl implements StreamConnectionFactory {
this.protoWriterFactory = protoWriterFactory; this.protoWriterFactory = protoWriterFactory;
} }
public Runnable[] createIncomingConnection(ContactId c, public void createIncomingConnection(ContactId c,
StreamTransportConnection s, byte[] encryptedIv) { StreamTransportConnection s, byte[] encryptedIv) {
final StreamConnection conn = new IncomingStreamConnection( final StreamConnection conn = new IncomingStreamConnection(
connReaderFactory, connWriterFactory, db, protoReaderFactory, connReaderFactory, connWriterFactory, db, protoReaderFactory,
protoWriterFactory, c, s, encryptedIv); protoWriterFactory, c, s, encryptedIv);
Runnable[] runnables = new Runnable[2]; Runnable write = new Runnable() {
runnables[0] = new Runnable() {
public void run() { public void run() {
conn.write(); conn.write();
} }
}; };
runnables[1] = new Runnable() { new Thread(write).start();
Runnable read = new Runnable() {
public void run() { public void run() {
conn.read(); conn.read();
} }
}; };
return runnables; new Thread(read).start();
} }
public Runnable[] createOutgoingConnection(TransportId t, ContactId c, public void createOutgoingConnection(TransportId t, ContactId c,
StreamTransportConnection s) { StreamTransportConnection s) {
final StreamConnection conn = new OutgoingStreamConnection( final StreamConnection conn = new OutgoingStreamConnection(
connReaderFactory, connWriterFactory, db, protoReaderFactory, connReaderFactory, connWriterFactory, db, protoReaderFactory,
protoWriterFactory, c, s, t); protoWriterFactory, c, s, t);
Runnable[] runnables = new Runnable[2]; Runnable write = new Runnable() {
runnables[0] = new Runnable() {
public void run() { public void run() {
conn.write(); conn.write();
} }
}; };
runnables[1] = new Runnable() { new Thread(write).start();
Runnable read = new Runnable() {
public void run() { public void run() {
conn.read(); conn.read();
} }
}; };
return runnables; new Thread(read).start();
} }
} }

View File

@@ -109,7 +109,7 @@ public class BatchConnectionReadWriteTest extends TestCase {
OutgoingBatchConnection batchOut = new OutgoingBatchConnection( OutgoingBatchConnection batchOut = new OutgoingBatchConnection(
connFactory, db, protoFactory, transportId, contactId, writer); connFactory, db, protoFactory, transportId, contactId, writer);
// Write whatever needs to be written // Write whatever needs to be written
batchOut.run(); batchOut.write();
// 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
@@ -147,7 +147,7 @@ public class BatchConnectionReadWriteTest extends TestCase {
// No messages should have been added yet // No messages should have been added yet
assertFalse(listener.messagesAdded); assertFalse(listener.messagesAdded);
// Read whatever needs to be read // Read whatever needs to be read
batchIn.run(); batchIn.read();
// 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