mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-14 03:39:05 +01:00
Read the tag on a connection recogniser thread, don't block the
plugin.
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
package net.sf.briar.transport;
|
||||
|
||||
import java.io.EOFException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
@@ -16,7 +18,7 @@ import net.sf.briar.api.transport.BatchTransportWriter;
|
||||
import net.sf.briar.api.transport.ConnectionContext;
|
||||
import net.sf.briar.api.transport.ConnectionDispatcher;
|
||||
import net.sf.briar.api.transport.ConnectionRecogniser;
|
||||
import net.sf.briar.api.transport.ConnectionRecogniser.Callback;
|
||||
import net.sf.briar.api.transport.ConnectionRecogniserExecutor;
|
||||
import net.sf.briar.api.transport.StreamTransportConnection;
|
||||
import net.sf.briar.api.transport.TransportConstants;
|
||||
|
||||
@@ -27,56 +29,24 @@ class ConnectionDispatcherImpl implements ConnectionDispatcher {
|
||||
private static final Logger LOG =
|
||||
Logger.getLogger(ConnectionDispatcherImpl.class.getName());
|
||||
|
||||
private final Executor executor;
|
||||
private final ConnectionRecogniser recogniser;
|
||||
private final BatchConnectionFactory batchConnFactory;
|
||||
private final StreamConnectionFactory streamConnFactory;
|
||||
|
||||
@Inject
|
||||
ConnectionDispatcherImpl(ConnectionRecogniser recogniser,
|
||||
ConnectionDispatcherImpl(@ConnectionRecogniserExecutor Executor executor,
|
||||
ConnectionRecogniser recogniser,
|
||||
BatchConnectionFactory batchConnFactory,
|
||||
StreamConnectionFactory streamConnFactory) {
|
||||
this.executor = executor;
|
||||
this.recogniser = recogniser;
|
||||
this.batchConnFactory = batchConnFactory;
|
||||
this.streamConnFactory = streamConnFactory;
|
||||
}
|
||||
|
||||
public void dispatchReader(TransportId t, final BatchTransportReader r) {
|
||||
// Read the tag
|
||||
final byte[] tag;
|
||||
try {
|
||||
tag = readTag(r.getInputStream());
|
||||
} catch(IOException e) {
|
||||
if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.getMessage());
|
||||
r.dispose(false);
|
||||
return;
|
||||
}
|
||||
// Get the connection context asynchronously
|
||||
recogniser.acceptConnection(t, tag, new Callback() {
|
||||
|
||||
public void connectionAccepted(ConnectionContext ctx) {
|
||||
batchConnFactory.createIncomingConnection(ctx, r, tag);
|
||||
}
|
||||
|
||||
public void connectionRejected() {
|
||||
r.dispose(true);
|
||||
}
|
||||
|
||||
public void handleException(DbException e) {
|
||||
if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.getMessage());
|
||||
r.dispose(false);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private byte[] readTag(InputStream in) throws IOException {
|
||||
byte[] b = new byte[TransportConstants.TAG_LENGTH];
|
||||
int offset = 0;
|
||||
while(offset < b.length) {
|
||||
int read = in.read(b, offset, b.length - offset);
|
||||
if(read == -1) throw new IOException();
|
||||
offset += read;
|
||||
}
|
||||
return b;
|
||||
public void dispatchReader(TransportId t, BatchTransportReader r) {
|
||||
executor.execute(new DispatchBatchConnection(t, r));
|
||||
}
|
||||
|
||||
public void dispatchWriter(ContactId c, TransportIndex i,
|
||||
@@ -85,36 +55,76 @@ class ConnectionDispatcherImpl implements ConnectionDispatcher {
|
||||
}
|
||||
|
||||
public void dispatchIncomingConnection(TransportId t,
|
||||
final StreamTransportConnection s) {
|
||||
// Read the tag
|
||||
final byte[] tag;
|
||||
try {
|
||||
tag = readTag(s.getInputStream());
|
||||
} catch(IOException e) {
|
||||
if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.getMessage());
|
||||
s.dispose(false);
|
||||
return;
|
||||
}
|
||||
// Get the connection context asynchronously
|
||||
recogniser.acceptConnection(t, tag, new Callback() {
|
||||
|
||||
public void connectionAccepted(ConnectionContext ctx) {
|
||||
streamConnFactory.createIncomingConnection(ctx, s, tag);
|
||||
}
|
||||
|
||||
public void connectionRejected() {
|
||||
s.dispose(true);
|
||||
}
|
||||
|
||||
public void handleException(DbException e) {
|
||||
if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.getMessage());
|
||||
s.dispose(false);
|
||||
}
|
||||
});
|
||||
StreamTransportConnection s) {
|
||||
executor.execute(new DispatchStreamConnection(t, s));
|
||||
}
|
||||
|
||||
public void dispatchOutgoingConnection(ContactId c, TransportIndex i,
|
||||
StreamTransportConnection s) {
|
||||
streamConnFactory.createOutgoingConnection(c, i, s);
|
||||
}
|
||||
}
|
||||
|
||||
private byte[] readTag(InputStream in) throws IOException {
|
||||
byte[] b = new byte[TransportConstants.TAG_LENGTH];
|
||||
int offset = 0;
|
||||
while(offset < b.length) {
|
||||
int read = in.read(b, offset, b.length - offset);
|
||||
if(read == -1) throw new EOFException();
|
||||
offset += read;
|
||||
}
|
||||
return b;
|
||||
}
|
||||
|
||||
private class DispatchBatchConnection implements Runnable {
|
||||
|
||||
private final TransportId t;
|
||||
private final BatchTransportReader r;
|
||||
|
||||
private DispatchBatchConnection(TransportId t, BatchTransportReader r) {
|
||||
this.t = t;
|
||||
this.r = r;
|
||||
}
|
||||
|
||||
public void run() {
|
||||
try {
|
||||
byte[] tag = readTag(r.getInputStream());
|
||||
ConnectionContext ctx = recogniser.acceptConnection(t, tag);
|
||||
if(ctx == null) r.dispose(true);
|
||||
else batchConnFactory.createIncomingConnection(ctx, r, tag);
|
||||
} catch(DbException e) {
|
||||
if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.getMessage());
|
||||
r.dispose(false);
|
||||
} catch(IOException e) {
|
||||
if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.getMessage());
|
||||
r.dispose(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private class DispatchStreamConnection implements Runnable {
|
||||
|
||||
private final TransportId t;
|
||||
private final StreamTransportConnection s;
|
||||
|
||||
private DispatchStreamConnection(TransportId t,
|
||||
StreamTransportConnection s) {
|
||||
this.t = t;
|
||||
this.s = s;
|
||||
}
|
||||
|
||||
public void run() {
|
||||
try {
|
||||
byte[] tag = readTag(s.getInputStream());
|
||||
ConnectionContext ctx = recogniser.acceptConnection(t, tag);
|
||||
if(ctx == null) s.dispose(true);
|
||||
else streamConnFactory.createIncomingConnection(ctx, s, tag);
|
||||
} catch(DbException e) {
|
||||
if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.getMessage());
|
||||
s.dispose(false);
|
||||
} catch(IOException e) {
|
||||
if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.getMessage());
|
||||
s.dispose(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -107,23 +107,7 @@ DatabaseListener {
|
||||
return new Bytes(tag);
|
||||
}
|
||||
|
||||
public void acceptConnection(final TransportId t, final byte[] tag,
|
||||
final Callback callback) {
|
||||
executor.execute(new Runnable() {
|
||||
public void run() {
|
||||
try {
|
||||
ConnectionContext ctx = acceptConnection(t, tag);
|
||||
if(ctx == null) callback.connectionRejected();
|
||||
else callback.connectionAccepted(ctx);
|
||||
} catch(DbException e) {
|
||||
callback.handleException(e);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Package access for testing
|
||||
ConnectionContext acceptConnection(TransportId t, byte[] tag)
|
||||
public ConnectionContext acceptConnection(TransportId t, byte[] tag)
|
||||
throws DbException {
|
||||
if(tag.length != TAG_LENGTH)
|
||||
throw new IllegalArgumentException();
|
||||
|
||||
@@ -12,7 +12,6 @@ import net.sf.briar.api.transport.ConnectionWindowFactory;
|
||||
import net.sf.briar.api.transport.ConnectionWriterFactory;
|
||||
|
||||
import com.google.inject.AbstractModule;
|
||||
import com.google.inject.Singleton;
|
||||
|
||||
public class TransportModule extends AbstractModule {
|
||||
|
||||
@@ -23,8 +22,7 @@ public class TransportModule extends AbstractModule {
|
||||
bind(ConnectionDispatcher.class).to(ConnectionDispatcherImpl.class);
|
||||
bind(ConnectionReaderFactory.class).to(
|
||||
ConnectionReaderFactoryImpl.class);
|
||||
bind(ConnectionRecogniser.class).to(ConnectionRecogniserImpl.class).in(
|
||||
Singleton.class);
|
||||
bind(ConnectionRecogniser.class).to(ConnectionRecogniserImpl.class);
|
||||
bind(ConnectionWindowFactory.class).to(
|
||||
ConnectionWindowFactoryImpl.class);
|
||||
bind(ConnectionWriterFactory.class).to(
|
||||
|
||||
Reference in New Issue
Block a user