Add transports to DB during startup. #269

This commit is contained in:
akwizgran
2016-03-28 13:47:23 +01:00
parent 0417639410
commit 9714713d73
40 changed files with 276 additions and 402 deletions

View File

@@ -90,8 +90,8 @@ class ConnectionManagerImpl implements ConnectionManager {
TransportConnectionReader r) throws IOException {
InputStream streamReader = streamReaderFactory.createStreamReader(
r.getInputStream(), ctx);
return syncSessionFactory.createIncomingSession(
ctx.getContactId(), ctx.getTransportId(), streamReader);
return syncSessionFactory.createIncomingSession(ctx.getContactId(),
streamReader);
}
private SyncSession createSimplexOutgoingSession(StreamContext ctx,
@@ -99,8 +99,7 @@ class ConnectionManagerImpl implements ConnectionManager {
OutputStream streamWriter = streamWriterFactory.createStreamWriter(
w.getOutputStream(), ctx);
return syncSessionFactory.createSimplexOutgoingSession(
ctx.getContactId(), ctx.getTransportId(), w.getMaxLatency(),
streamWriter);
ctx.getContactId(), w.getMaxLatency(), streamWriter);
}
private SyncSession createDuplexOutgoingSession(StreamContext ctx,
@@ -108,8 +107,8 @@ class ConnectionManagerImpl implements ConnectionManager {
OutputStream streamWriter = streamWriterFactory.createStreamWriter(
w.getOutputStream(), ctx);
return syncSessionFactory.createDuplexOutgoingSession(
ctx.getContactId(), ctx.getTransportId(), w.getMaxLatency(),
w.getMaxIdleTime(), streamWriter);
ctx.getContactId(), w.getMaxLatency(), w.getMaxIdleTime(),
streamWriter);
}
private class ManageIncomingSimplexConnection implements Runnable {

View File

@@ -2,9 +2,7 @@ package org.briarproject.plugins;
import org.briarproject.api.TransportId;
import org.briarproject.api.contact.ContactId;
import org.briarproject.api.db.DatabaseComponent;
import org.briarproject.api.db.DbException;
import org.briarproject.api.db.Transaction;
import org.briarproject.api.event.EventBus;
import org.briarproject.api.event.TransportDisabledEvent;
import org.briarproject.api.event.TransportEnabledEvent;
@@ -28,7 +26,6 @@ import org.briarproject.api.properties.TransportProperties;
import org.briarproject.api.properties.TransportPropertyManager;
import org.briarproject.api.settings.Settings;
import org.briarproject.api.settings.SettingsManager;
import org.briarproject.api.system.Clock;
import org.briarproject.api.ui.UiCallback;
import java.io.IOException;
@@ -56,8 +53,6 @@ class PluginManagerImpl implements PluginManager, Service {
private final Executor ioExecutor;
private final EventBus eventBus;
private final PluginConfig pluginConfig;
private final Clock clock;
private final DatabaseComponent db;
private final Poller poller;
private final ConnectionManager connectionManager;
private final SettingsManager settingsManager;
@@ -69,8 +64,7 @@ class PluginManagerImpl implements PluginManager, Service {
@Inject
PluginManagerImpl(@IoExecutor Executor ioExecutor, EventBus eventBus,
PluginConfig pluginConfig, Clock clock,
DatabaseComponent db, Poller poller,
PluginConfig pluginConfig, Poller poller,
ConnectionManager connectionManager,
SettingsManager settingsManager,
TransportPropertyManager transportPropertyManager,
@@ -78,8 +72,6 @@ class PluginManagerImpl implements PluginManager, Service {
this.ioExecutor = ioExecutor;
this.eventBus = eventBus;
this.pluginConfig = pluginConfig;
this.clock = clock;
this.db = db;
this.poller = poller;
this.connectionManager = connectionManager;
this.settingsManager = settingsManager;
@@ -181,26 +173,9 @@ class PluginManagerImpl implements PluginManager, Service {
return;
}
try {
long start = clock.currentTimeMillis();
Transaction txn = db.startTransaction();
try {
db.addTransport(txn, id, plugin.getMaxLatency());
txn.setComplete();
} finally {
db.endTransaction(txn);
}
long duration = clock.currentTimeMillis() - start;
if (LOG.isLoggable(INFO))
LOG.info("Adding transport took " + duration + " ms");
} catch (DbException e) {
if (LOG.isLoggable(WARNING))
LOG.log(WARNING, e.toString(), e);
return;
}
try {
long start = clock.currentTimeMillis();
long start = System.currentTimeMillis();
boolean started = plugin.start();
long duration = clock.currentTimeMillis() - start;
long duration = System.currentTimeMillis() - start;
if (started) {
plugins.put(id, plugin);
simplexPlugins.add(plugin);
@@ -250,26 +225,9 @@ class PluginManagerImpl implements PluginManager, Service {
return;
}
try {
long start = clock.currentTimeMillis();
Transaction txn = db.startTransaction();
try {
db.addTransport(txn, id, plugin.getMaxLatency());
txn.setComplete();
} finally {
db.endTransaction(txn);
}
long duration = clock.currentTimeMillis() - start;
if (LOG.isLoggable(INFO))
LOG.info("Adding transport took " + duration + " ms");
} catch (DbException e) {
if (LOG.isLoggable(WARNING))
LOG.log(WARNING, e.toString(), e);
return;
}
try {
long start = clock.currentTimeMillis();
long start = System.currentTimeMillis();
boolean started = plugin.start();
long duration = clock.currentTimeMillis() - start;
long duration = System.currentTimeMillis() - start;
if (started) {
plugins.put(id, plugin);
duplexPlugins.add(plugin);
@@ -307,9 +265,9 @@ class PluginManagerImpl implements PluginManager, Service {
public void run() {
try {
long start = clock.currentTimeMillis();
long start = System.currentTimeMillis();
plugin.stop();
long duration = clock.currentTimeMillis() - start;
long duration = System.currentTimeMillis() - start;
if (LOG.isLoggable(INFO)) {
String name = plugin.getClass().getSimpleName();
LOG.info("Stopping " + name + " took " + duration + " ms");

View File

@@ -1,8 +1,5 @@
package org.briarproject.plugins;
import javax.inject.Inject;
import javax.inject.Singleton;
import org.briarproject.api.event.EventBus;
import org.briarproject.api.lifecycle.IoExecutor;
import org.briarproject.api.lifecycle.LifecycleManager;
@@ -19,10 +16,12 @@ import org.briarproject.api.transport.StreamWriterFactory;
import java.security.SecureRandom;
import java.util.concurrent.Executor;
import javax.inject.Inject;
import javax.inject.Singleton;
import dagger.Module;
import dagger.Provides;
@Module
public class PluginsModule {
@@ -45,8 +44,8 @@ public class PluginsModule {
@Provides
ConnectionManager provideConnectionManager(
@IoExecutor Executor ioExecutor,
KeyManager keyManager, StreamReaderFactory streamReaderFactory,
@IoExecutor Executor ioExecutor, KeyManager keyManager,
StreamReaderFactory streamReaderFactory,
StreamWriterFactory streamWriterFactory,
SyncSessionFactory syncSessionFactory,
ConnectionRegistry connectionRegistry) {
@@ -61,7 +60,6 @@ public class PluginsModule {
return new ConnectionRegistryImpl(eventBus);
}
@Provides
@Singleton
PluginManager getPluginManager(LifecycleManager lifecycleManager,
@@ -69,5 +67,4 @@ public class PluginsModule {
lifecycleManager.register(pluginManager);
return pluginManager;
}
}

View File

@@ -1,13 +1,13 @@
package org.briarproject.plugins.file;
import static java.util.logging.Level.WARNING;
import org.briarproject.api.plugins.TransportConnectionReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Logger;
import org.briarproject.api.plugins.TransportConnectionReader;
import static java.util.logging.Level.WARNING;
class FileTransportReader implements TransportConnectionReader {
@@ -24,10 +24,6 @@ class FileTransportReader implements TransportConnectionReader {
this.plugin = plugin;
}
public long getMaxLatency() {
return plugin.getMaxLatency();
}
public InputStream getInputStream() {
return in;
}

View File

@@ -30,6 +30,10 @@ public class LanTcpPluginFactory implements DuplexPluginFactory {
return LanTcpPlugin.ID;
}
public int getMaxLatency() {
return MAX_LATENCY;
}
public DuplexPlugin createPlugin(DuplexPluginCallback callback) {
Backoff backoff = backoffFactory.createBackoff(MIN_POLLING_INTERVAL,
MAX_POLLING_INTERVAL, BACKOFF_BASE);

View File

@@ -1,16 +1,16 @@
package org.briarproject.plugins.tcp;
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;
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;
@@ -38,10 +38,6 @@ class TcpTransportConnection implements DuplexTransportConnection {
private class Reader implements TransportConnectionReader {
public long getMaxLatency() {
return plugin.getMaxLatency();
}
public InputStream getInputStream() throws IOException {
return socket.getInputStream();
}

View File

@@ -33,6 +33,10 @@ public class WanTcpPluginFactory implements DuplexPluginFactory {
return WanTcpPlugin.ID;
}
public int getMaxLatency() {
return MAX_LATENCY;
}
public DuplexPlugin createPlugin(DuplexPluginCallback callback) {
Backoff backoff = backoffFactory.createBackoff(MIN_POLLING_INTERVAL,
MAX_POLLING_INTERVAL, BACKOFF_BASE);