mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-14 11:49:04 +01:00
Massive refactoring to merge handling of simplex and duplex connections.
This commit is contained in:
@@ -27,6 +27,8 @@ import org.briarproject.api.lifecycle.IoExecutor;
|
||||
import org.briarproject.api.plugins.Plugin;
|
||||
import org.briarproject.api.plugins.PluginCallback;
|
||||
import org.briarproject.api.plugins.PluginManager;
|
||||
import org.briarproject.api.plugins.TransportConnectionReader;
|
||||
import org.briarproject.api.plugins.TransportConnectionWriter;
|
||||
import org.briarproject.api.plugins.duplex.DuplexPlugin;
|
||||
import org.briarproject.api.plugins.duplex.DuplexPluginCallback;
|
||||
import org.briarproject.api.plugins.duplex.DuplexPluginConfig;
|
||||
@@ -36,8 +38,6 @@ import org.briarproject.api.plugins.simplex.SimplexPlugin;
|
||||
import org.briarproject.api.plugins.simplex.SimplexPluginCallback;
|
||||
import org.briarproject.api.plugins.simplex.SimplexPluginConfig;
|
||||
import org.briarproject.api.plugins.simplex.SimplexPluginFactory;
|
||||
import org.briarproject.api.plugins.simplex.SimplexTransportReader;
|
||||
import org.briarproject.api.plugins.simplex.SimplexTransportWriter;
|
||||
import org.briarproject.api.system.Clock;
|
||||
import org.briarproject.api.transport.ConnectionDispatcher;
|
||||
import org.briarproject.api.ui.UiCallback;
|
||||
@@ -377,11 +377,11 @@ class PluginManagerImpl implements PluginManager {
|
||||
super(id);
|
||||
}
|
||||
|
||||
public void readerCreated(SimplexTransportReader r) {
|
||||
public void readerCreated(TransportConnectionReader r) {
|
||||
dispatcher.dispatchIncomingConnection(id, r);
|
||||
}
|
||||
|
||||
public void writerCreated(ContactId c, SimplexTransportWriter w) {
|
||||
public void writerCreated(ContactId c, TransportConnectionWriter w) {
|
||||
dispatcher.dispatchOutgoingConnection(c, id, w);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,14 +20,14 @@ class PollerImpl implements Poller {
|
||||
Logger.getLogger(PollerImpl.class.getName());
|
||||
|
||||
private final Executor ioExecutor;
|
||||
private final ConnectionRegistry connRegistry;
|
||||
private final ConnectionRegistry connectionRegistry;
|
||||
private final Timer timer;
|
||||
|
||||
@Inject
|
||||
PollerImpl(@IoExecutor Executor ioExecutor, ConnectionRegistry connRegistry,
|
||||
Timer timer) {
|
||||
PollerImpl(@IoExecutor Executor ioExecutor,
|
||||
ConnectionRegistry connectionRegistry, Timer timer) {
|
||||
this.ioExecutor = ioExecutor;
|
||||
this.connRegistry = connRegistry;
|
||||
this.connectionRegistry = connectionRegistry;
|
||||
this.timer = timer;
|
||||
}
|
||||
|
||||
@@ -53,7 +53,7 @@ class PollerImpl implements Poller {
|
||||
public void run() {
|
||||
if(LOG.isLoggable(INFO))
|
||||
LOG.info("Polling " + p.getClass().getSimpleName());
|
||||
p.poll(connRegistry.getConnectedContacts(p.getId()));
|
||||
p.poll(connectionRegistry.getConnectedContacts(p.getId()));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -14,10 +14,10 @@ import java.util.concurrent.Executor;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import org.briarproject.api.ContactId;
|
||||
import org.briarproject.api.plugins.TransportConnectionReader;
|
||||
import org.briarproject.api.plugins.TransportConnectionWriter;
|
||||
import org.briarproject.api.plugins.simplex.SimplexPlugin;
|
||||
import org.briarproject.api.plugins.simplex.SimplexPluginCallback;
|
||||
import org.briarproject.api.plugins.simplex.SimplexTransportReader;
|
||||
import org.briarproject.api.plugins.simplex.SimplexTransportWriter;
|
||||
import org.briarproject.api.system.FileUtils;
|
||||
|
||||
public abstract class FilePlugin implements SimplexPlugin {
|
||||
@@ -60,11 +60,11 @@ public abstract class FilePlugin implements SimplexPlugin {
|
||||
return running;
|
||||
}
|
||||
|
||||
public SimplexTransportReader createReader(ContactId c) {
|
||||
public TransportConnectionReader createReader(ContactId c) {
|
||||
return null;
|
||||
}
|
||||
|
||||
public SimplexTransportWriter createWriter(ContactId c) {
|
||||
public TransportConnectionWriter createWriter(ContactId c) {
|
||||
if(!running) return null;
|
||||
return createWriter(createConnectionFilename());
|
||||
}
|
||||
@@ -81,7 +81,7 @@ public abstract class FilePlugin implements SimplexPlugin {
|
||||
return filename.toLowerCase(Locale.US).matches("[a-z]{8}\\.dat");
|
||||
}
|
||||
|
||||
private SimplexTransportWriter createWriter(String filename) {
|
||||
private TransportConnectionWriter createWriter(String filename) {
|
||||
if(!running) return null;
|
||||
File dir = chooseOutputDirectory();
|
||||
if(dir == null || !dir.exists() || !dir.isDirectory()) return null;
|
||||
|
||||
@@ -7,9 +7,9 @@ import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import org.briarproject.api.plugins.simplex.SimplexTransportReader;
|
||||
import org.briarproject.api.plugins.TransportConnectionReader;
|
||||
|
||||
class FileTransportReader implements SimplexTransportReader {
|
||||
class FileTransportReader implements TransportConnectionReader {
|
||||
|
||||
private static final Logger LOG =
|
||||
Logger.getLogger(FileTransportReader.class.getName());
|
||||
@@ -28,6 +28,10 @@ class FileTransportReader implements SimplexTransportReader {
|
||||
return plugin.getMaxFrameLength();
|
||||
}
|
||||
|
||||
public long getMaxLatency() {
|
||||
return plugin.getMaxLatency();
|
||||
}
|
||||
|
||||
public InputStream getInputStream() {
|
||||
return in;
|
||||
}
|
||||
|
||||
@@ -7,9 +7,9 @@ import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import org.briarproject.api.plugins.simplex.SimplexTransportWriter;
|
||||
import org.briarproject.api.plugins.TransportConnectionWriter;
|
||||
|
||||
class FileTransportWriter implements SimplexTransportWriter {
|
||||
class FileTransportWriter implements TransportConnectionWriter {
|
||||
|
||||
private static final Logger LOG =
|
||||
Logger.getLogger(FileTransportWriter.class.getName());
|
||||
@@ -27,10 +27,6 @@ class FileTransportWriter implements SimplexTransportWriter {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
public long getCapacity() {
|
||||
return capacity;
|
||||
}
|
||||
|
||||
public int getMaxFrameLength() {
|
||||
return plugin.getMaxFrameLength();
|
||||
}
|
||||
@@ -39,6 +35,10 @@ class FileTransportWriter implements SimplexTransportWriter {
|
||||
return plugin.getMaxLatency();
|
||||
}
|
||||
|
||||
public long getCapacity() {
|
||||
return capacity;
|
||||
}
|
||||
|
||||
public OutputStream getOutputStream() {
|
||||
return out;
|
||||
}
|
||||
|
||||
@@ -4,38 +4,80 @@ import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.net.Socket;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
import org.briarproject.api.plugins.Plugin;
|
||||
import org.briarproject.api.plugins.TransportConnectionReader;
|
||||
import org.briarproject.api.plugins.TransportConnectionWriter;
|
||||
import org.briarproject.api.plugins.duplex.DuplexTransportConnection;
|
||||
|
||||
class TcpTransportConnection implements DuplexTransportConnection {
|
||||
|
||||
private final Plugin plugin;
|
||||
private final Socket socket;
|
||||
private final Reader reader;
|
||||
private final Writer writer;
|
||||
private final AtomicBoolean halfClosed, closed;
|
||||
|
||||
TcpTransportConnection(Plugin plugin, Socket socket) {
|
||||
this.plugin = plugin;
|
||||
this.socket = socket;
|
||||
reader = new Reader();
|
||||
writer = new Writer();
|
||||
halfClosed = new AtomicBoolean(false);
|
||||
closed = new AtomicBoolean(false);
|
||||
}
|
||||
|
||||
public int getMaxFrameLength() {
|
||||
return plugin.getMaxFrameLength();
|
||||
public TransportConnectionReader getReader() {
|
||||
return reader;
|
||||
}
|
||||
|
||||
public long getMaxLatency() {
|
||||
return plugin.getMaxLatency();
|
||||
public TransportConnectionWriter getWriter() {
|
||||
return writer;
|
||||
}
|
||||
|
||||
public InputStream getInputStream() throws IOException {
|
||||
return socket.getInputStream();
|
||||
private class Reader implements TransportConnectionReader {
|
||||
|
||||
public int getMaxFrameLength() {
|
||||
return plugin.getMaxFrameLength();
|
||||
}
|
||||
|
||||
public long getMaxLatency() {
|
||||
return plugin.getMaxLatency();
|
||||
}
|
||||
|
||||
public InputStream getInputStream() throws IOException {
|
||||
return socket.getInputStream();
|
||||
}
|
||||
|
||||
public void dispose(boolean exception, boolean recognised)
|
||||
throws IOException {
|
||||
if(halfClosed.getAndSet(true) || exception)
|
||||
if(!closed.getAndSet(true)) socket.close();
|
||||
}
|
||||
}
|
||||
|
||||
public OutputStream getOutputStream() throws IOException {
|
||||
return socket.getOutputStream();
|
||||
}
|
||||
private class Writer implements TransportConnectionWriter {
|
||||
|
||||
public void dispose(boolean exception, boolean recognised)
|
||||
throws IOException {
|
||||
socket.close();
|
||||
public int getMaxFrameLength() {
|
||||
return plugin.getMaxFrameLength();
|
||||
}
|
||||
|
||||
public long getMaxLatency() {
|
||||
return plugin.getMaxLatency();
|
||||
}
|
||||
|
||||
public long getCapacity() {
|
||||
return Long.MAX_VALUE;
|
||||
}
|
||||
|
||||
public OutputStream getOutputStream() throws IOException {
|
||||
return socket.getOutputStream();
|
||||
}
|
||||
|
||||
public void dispose(boolean exception) throws IOException {
|
||||
if(halfClosed.getAndSet(true) || exception)
|
||||
if(!closed.getAndSet(true)) socket.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user