ENH: Replaces transport config with namespaced settings

This commit is contained in:
Santiago Torres
2016-01-17 15:42:45 -05:00
parent 2b02db3023
commit 190bb12964
27 changed files with 206 additions and 238 deletions

View File

@@ -17,5 +17,6 @@
<item>org.briarproject.sync.SyncModule</item>
<item>org.briarproject.system.AndroidSystemModule</item>
<item>org.briarproject.transport.TransportModule</item>
<item>org.briarproject.settings.SettingsModule</item>
</string-array>
</resources>
</resources>

View File

@@ -93,7 +93,7 @@ EventListener {
dbExecutor.execute(new Runnable() {
public void run() {
try {
settings = db.getSettings();
settings = db.getSettings("settings-activity");
} catch (DbException e) {
if (LOG.isLoggable(WARNING))
LOG.log(WARNING, e.toString(), e);

View File

@@ -23,7 +23,7 @@ import org.briarproject.android.util.HorizontalBorder;
import org.briarproject.android.util.LayoutUtils;
import org.briarproject.android.util.ListLoadingProgressBar;
import org.briarproject.api.Settings;
import org.briarproject.api.TransportConfig;
import org.briarproject.api.settings.SettingsManager;
import org.briarproject.api.TransportId;
import org.briarproject.api.db.DatabaseComponent;
import org.briarproject.api.db.DbException;
@@ -77,6 +77,7 @@ OnClickListener {
// Fields that are accessed from background threads must be volatile
@Inject private volatile DatabaseComponent db;
@Inject private volatile SettingsManager settingsManager;
@Inject private volatile EventBus eventBus;
private volatile Settings settings;
private volatile boolean bluetoothSetting = true, torSetting = false;
@@ -262,14 +263,15 @@ OnClickListener {
runOnDbThread(new Runnable() {
public void run() {
try {
settings = settingsManager.getSettings("settings-activity");
long now = System.currentTimeMillis();
TransportConfig c = db.getConfig(new TransportId("bt"));
settings = db.getSettings();
Settings btSettings = settingsManager.getSettings("bt");
Settings torSettings = settingsManager.getSettings("tor");
long duration = System.currentTimeMillis() - now;
if (LOG.isLoggable(INFO))
LOG.info("Loading settings took " + duration + " ms");
bluetoothSetting = c.getBoolean("enable", false);
torSetting = settings.getBoolean("torOverWifi", false);
bluetoothSetting = btSettings.getBoolean("enable", false);
torSetting = torSettings.getBoolean("torOverWifi", false);
displaySettings();
} catch (DbException e) {
if (LOG.isLoggable(WARNING))
@@ -338,6 +340,7 @@ OnClickListener {
} else if (view == torOverWifi || view == torOverWifiHint) {
torSetting = !torSetting;
storeTorSettings();
displaySettings();
} else if (view == notifyPrivateMessages) {
Settings s = new Settings();
s.putBoolean("notifyPrivateMessages",
@@ -378,11 +381,8 @@ OnClickListener {
try {
Settings s = new Settings();
s.putBoolean("torOverWifi", torSetting);
TransportConfig c = new TransportConfig();
c.putBoolean("torOverWifi", torSetting);
long now = System.currentTimeMillis();
db.mergeSettings(s);
db.mergeConfig(new TransportId("tor"), c);
settingsManager.mergeSettings(s, "tor");
long duration = System.currentTimeMillis() - now;
if (LOG.isLoggable(INFO))
LOG.info("Merging config took " + duration + " ms");
@@ -398,10 +398,10 @@ OnClickListener {
runOnDbThread(new Runnable() {
public void run() {
try {
TransportConfig c = new TransportConfig();
c.putBoolean("enable", bluetoothSetting);
Settings s = new Settings();
s.putBoolean("enable", bluetoothSetting);
long now = System.currentTimeMillis();
db.mergeConfig(new TransportId("bt"), c);
settingsManager.mergeSettings(s, "bt");
long duration = System.currentTimeMillis() - now;
if (LOG.isLoggable(INFO))
LOG.info("Merging config took " + duration + " ms");
@@ -418,7 +418,7 @@ OnClickListener {
public void run() {
try {
long now = System.currentTimeMillis();
db.mergeSettings(settings);
settingsManager.mergeSettings(settings, "settings-activity");
long duration = System.currentTimeMillis() - now;
if (LOG.isLoggable(INFO))
LOG.info("Merging settings took " + duration + " ms");

View File

@@ -8,7 +8,7 @@ import android.widget.Toast;
import org.briarproject.R;
import org.briarproject.android.BriarActivity;
import org.briarproject.android.util.AndroidUtils;
import org.briarproject.api.TransportConfig;
import org.briarproject.api.Settings;
import org.briarproject.api.TransportId;
import org.briarproject.api.android.ReferenceManager;
import org.briarproject.api.crypto.CryptoComponent;
@@ -170,12 +170,12 @@ implements InvitationListener {
public void run() {
try {
long now = System.currentTimeMillis();
TransportConfig c = db.getConfig(new TransportId("bt"));
Settings s = db.getSettings("bt");
long duration = System.currentTimeMillis() - now;
if (LOG.isLoggable(INFO))
LOG.info("Loading setting took " + duration + " ms");
leaveBluetoothEnabled = bluetoothWasEnabled
|| c.getBoolean("enable", false);
|| s.getBoolean("enable", false);
} catch (DbException e) {
if (LOG.isLoggable(WARNING))
LOG.log(WARNING, e.toString(), e);

View File

@@ -137,7 +137,7 @@ class DroidtoothPlugin implements DuplexPlugin {
bind();
} else {
wasDisabled = true;
if (callback.getConfig().getBoolean("enable", false)) {
if (callback.getSettings().getBoolean("enable", false)) {
if (adapter.enable()) LOG.info("Enabling Bluetooth");
else LOG.info("Could not enable Bluetooth");
} else {

View File

@@ -11,7 +11,7 @@ import android.os.FileObserver;
import net.freehaven.tor.control.EventHandler;
import net.freehaven.tor.control.TorControlConnection;
import org.briarproject.api.TransportConfig;
import org.briarproject.api.Settings;
import org.briarproject.api.TransportId;
import org.briarproject.api.TransportProperties;
import org.briarproject.api.contact.ContactId;
@@ -23,6 +23,7 @@ import org.briarproject.api.plugins.duplex.DuplexPlugin;
import org.briarproject.api.plugins.duplex.DuplexPluginCallback;
import org.briarproject.api.plugins.duplex.DuplexTransportConnection;
import org.briarproject.api.system.LocationUtils;
import org.briarproject.api.settings.SettingsManager;
import org.briarproject.util.StringUtils;
import java.io.EOFException;
@@ -349,7 +350,7 @@ class TorPlugin implements DuplexPlugin, EventHandler,
ioExecutor.execute(new Runnable() {
public void run() {
// If there's already a port number stored in config, reuse it
String portString = callback.getConfig().get("port");
String portString = callback.getSettings().get("port");
int port;
if (StringUtils.isNullOrEmpty(portString)) port = 0;
else port = Integer.parseInt(portString);
@@ -371,9 +372,9 @@ class TorPlugin implements DuplexPlugin, EventHandler,
socket = ss;
// Store the port number
final String localPort = String.valueOf(ss.getLocalPort());
TransportConfig c = new TransportConfig();
c.put("port", localPort);
callback.mergeConfig(c);
Settings s = new Settings();
s.put("port", localPort);
callback.mergeSettings(s);
// Create a hidden service if necessary
ioExecutor.execute(new Runnable() {
public void run() {
@@ -614,8 +615,8 @@ class TorPlugin implements DuplexPlugin, EventHandler,
}
boolean blocked = TorNetworkMetadata.isTorProbablyBlocked(
country);
TransportConfig c = callback.getConfig();
boolean wifiOnly = c.getBoolean("torOverWifi", false);
Settings s = callback.getSettings();
boolean wifiOnly = s.getBoolean("torOverWifi", false);
try {
if (!online) {

View File

@@ -1,16 +0,0 @@
package org.briarproject.api;
import java.util.Map;
public class TransportConfig extends StringMap {
private static final long serialVersionUID = 2330384620787778596L;
public TransportConfig(Map<String, String> m) {
super(m);
}
public TransportConfig() {
super();
}
}

View File

@@ -1,7 +1,6 @@
package org.briarproject.api.db;
import org.briarproject.api.Settings;
import org.briarproject.api.TransportConfig;
import org.briarproject.api.TransportId;
import org.briarproject.api.TransportProperties;
import org.briarproject.api.contact.Contact;
@@ -139,9 +138,6 @@ public interface DatabaseComponent {
/** Returns all groups to which the user could subscribe. */
Collection<Group> getAvailableGroups() throws DbException;
/** Returns the configuration for the given transport. */
TransportConfig getConfig(TransportId t) throws DbException;
/** Returns the contact with the given ID. */
Contact getContact(ContactId c) throws DbException;
@@ -194,8 +190,8 @@ public interface DatabaseComponent {
Map<ContactId, TransportProperties> getRemoteProperties(TransportId t)
throws DbException;
/** Returns all settings. */
Settings getSettings() throws DbException;
/** Returns all settings for a given namespace. */
Settings getSettings(String namespace) throws DbException;
/** Returns all contacts who subscribe to the given group. */
Collection<Contact> getSubscribers(GroupId g) throws DbException;
@@ -220,12 +216,6 @@ public interface DatabaseComponent {
void incrementStreamCounter(ContactId c, TransportId t, long rotationPeriod)
throws DbException;
/**
* Merges the given configuration with existing configuration for the
* given transport.
*/
void mergeConfig(TransportId t, TransportConfig c) throws DbException;
/**
* Merges the given properties with the existing local properties for the
* given transport.
@@ -234,7 +224,7 @@ public interface DatabaseComponent {
throws DbException;
/** Merges the given settings with the existing settings. */
void mergeSettings(Settings s) throws DbException;
void mergeSettings(Settings s, String namespace) throws DbException;
/** Processes an ack from the given contact. */
void receiveAck(ContactId c, Ack a) throws DbException;

View File

@@ -1,8 +1,8 @@
package org.briarproject.api.plugins;
import org.briarproject.api.TransportConfig;
import org.briarproject.api.TransportProperties;
import org.briarproject.api.contact.ContactId;
import org.briarproject.api.Settings;
import java.util.Map;
@@ -12,8 +12,8 @@ import java.util.Map;
*/
public interface PluginCallback {
/** Returns the plugin's configuration. */
TransportConfig getConfig();
/** Returns the plugin's settings */
Settings getSettings();
/** Returns the plugin's local transport properties. */
TransportProperties getLocalProperties();
@@ -21,8 +21,8 @@ public interface PluginCallback {
/** Returns the plugin's remote transport properties. */
Map<ContactId, TransportProperties> getRemoteProperties();
/** Merges the given configuration with the plugin's configuration. */
void mergeConfig(TransportConfig c);
/** Merges the given settings with the namespaced settings */
void mergeSettings(Settings s);
/**
* Merges the given properties with the plugin's local transport properties.

View File

@@ -0,0 +1,20 @@
package org.briarproject.api.settings;
import org.briarproject.api.db.DbException;
import org.briarproject.api.Settings;
public interface SettingsManager {
/**
* Returns the settings object identified by the provided namespace
* string
*/
Settings getSettings(String namespace) throws DbException;
/**
* Merges (read syncs) the provided settings identified by the provided namespace
* string
*/
void mergeSettings(Settings s, String namespace) throws DbException;
}

View File

@@ -1,7 +1,6 @@
package org.briarproject.db;
import org.briarproject.api.Settings;
import org.briarproject.api.TransportConfig;
import org.briarproject.api.TransportId;
import org.briarproject.api.TransportProperties;
import org.briarproject.api.contact.Contact;
@@ -223,13 +222,6 @@ interface Database<T> {
*/
Collection<Group> getAvailableGroups(T txn) throws DbException;
/**
* Returns the configuration for the given transport.
* <p>
* Locking: read.
*/
TransportConfig getConfig(T txn, TransportId t) throws DbException;
/**
* Returns the contact with the given ID.
* <p>
@@ -419,11 +411,11 @@ interface Database<T> {
int maxLength) throws DbException;
/**
* Returns all settings.
* Returns all settings that belong to a namespace.
* <p>
* Locking: read.
*/
Settings getSettings(T txn) throws DbException;
Settings getSettings(T txn, String namespace) throws DbException;
/**
* Returns all contacts who subscribe to the given group.
@@ -524,15 +516,6 @@ interface Database<T> {
void lowerRequestedFlag(T txn, ContactId c, Collection<MessageId> requested)
throws DbException;
/**
* Merges the given configuration with the existing configuration for the
* given transport.
* <p>
* Locking: write.
*/
void mergeConfig(T txn, TransportId t, TransportConfig config)
throws DbException;
/**
* Merges the given properties with the existing local properties for the
* given transport.
@@ -547,7 +530,7 @@ interface Database<T> {
* <p>
* Locking: write.
*/
void mergeSettings(T txn, Settings s) throws DbException;
void mergeSettings(T txn, Settings s, String Namespace) throws DbException;
/**
* Marks a message as needing to be acknowledged to the given contact.

View File

@@ -1,7 +1,6 @@
package org.briarproject.db;
import org.briarproject.api.Settings;
import org.briarproject.api.TransportConfig;
import org.briarproject.api.TransportId;
import org.briarproject.api.TransportProperties;
import org.briarproject.api.contact.Contact;
@@ -524,25 +523,6 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
}
}
public TransportConfig getConfig(TransportId t) throws DbException {
lock.readLock().lock();
try {
T txn = db.startTransaction();
try {
if (!db.containsTransport(txn, t))
throw new NoSuchTransportException();
TransportConfig config = db.getConfig(txn, t);
db.commitTransaction(txn);
return config;
} catch (DbException e) {
db.abortTransaction(txn);
throw e;
}
} finally {
lock.readLock().unlock();
}
}
public Contact getContact(ContactId c) throws DbException {
lock.readLock().lock();
try {
@@ -808,12 +788,12 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
}
}
public Settings getSettings() throws DbException {
public Settings getSettings(String namespace) throws DbException {
lock.readLock().lock();
try {
T txn = db.startTransaction();
try {
Settings s = db.getSettings(txn);
Settings s = db.getSettings(txn, namespace);
db.commitTransaction(txn);
return s;
} catch (DbException e) {
@@ -939,25 +919,6 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
}
}
public void mergeConfig(TransportId t, TransportConfig c)
throws DbException {
lock.writeLock().lock();
try {
T txn = db.startTransaction();
try {
if (!db.containsTransport(txn, t))
throw new NoSuchTransportException();
db.mergeConfig(txn, t, c);
db.commitTransaction(txn);
} catch (DbException e) {
db.abortTransaction(txn);
throw e;
}
} finally {
lock.writeLock().unlock();
}
}
public void mergeLocalProperties(TransportId t, TransportProperties p)
throws DbException {
boolean changed = false;
@@ -982,14 +943,14 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
if (changed) eventBus.broadcast(new LocalTransportsUpdatedEvent());
}
public void mergeSettings(Settings s) throws DbException {
public void mergeSettings(Settings s, String namespace) throws DbException {
boolean changed = false;
lock.writeLock().lock();
try {
T txn = db.startTransaction();
try {
if (!s.equals(db.getSettings(txn))) {
db.mergeSettings(txn, s);
if (!s.equals(db.getSettings(txn, namespace))) {
db.mergeSettings(txn, s, namespace);
changed = true;
}
db.commitTransaction(txn);

View File

@@ -1,7 +1,6 @@
package org.briarproject.db;
import org.briarproject.api.Settings;
import org.briarproject.api.TransportConfig;
import org.briarproject.api.TransportId;
import org.briarproject.api.TransportProperties;
import org.briarproject.api.contact.Contact;
@@ -64,14 +63,15 @@ import static org.briarproject.db.ExponentialBackoff.calculateExpiry;
*/
abstract class JdbcDatabase implements Database<Connection> {
private static final int SCHEMA_VERSION = 11;
private static final int MIN_SCHEMA_VERSION = 10;
private static final int SCHEMA_VERSION = 12;
private static final int MIN_SCHEMA_VERSION = 12;
private static final String CREATE_SETTINGS =
"CREATE TABLE settings"
+ " (key VARCHAR NOT NULL,"
+ " value VARCHAR NOT NULL,"
+ " PRIMARY KEY (key))";
+ " namespace VARCHAR NOT NULL,"
+ " PRIMARY KEY (key, namespace))";
private static final String CREATE_LOCAL_AUTHORS =
"CREATE TABLE localAuthors"
@@ -343,7 +343,7 @@ abstract class JdbcDatabase implements Database<Connection> {
Settings s = new Settings();
s.put("schemaVersion", String.valueOf(SCHEMA_VERSION));
s.put("minSchemaVersion", String.valueOf(MIN_SCHEMA_VERSION));
mergeSettings(txn, s);
mergeSettings(txn, s, "db");
}
commitTransaction(txn);
} catch (DbException e) {
@@ -354,7 +354,7 @@ abstract class JdbcDatabase implements Database<Connection> {
private boolean checkSchemaVersion(Connection txn) throws DbException {
try {
Settings s = getSettings(txn);
Settings s = getSettings(txn, "db");
int schemaVersion = Integer.valueOf(s.get("schemaVersion"));
if (schemaVersion == SCHEMA_VERSION) return true;
if (schemaVersion < MIN_SCHEMA_VERSION) return false;
@@ -1179,28 +1179,6 @@ abstract class JdbcDatabase implements Database<Connection> {
}
}
public TransportConfig getConfig(Connection txn, TransportId t)
throws DbException {
PreparedStatement ps = null;
ResultSet rs = null;
try {
String sql = "SELECT key, value FROM transportConfigs"
+ " WHERE transportId = ?";
ps = txn.prepareStatement(sql);
ps.setString(1, t.getString());
rs = ps.executeQuery();
TransportConfig c = new TransportConfig();
while (rs.next()) c.put(rs.getString(1), rs.getString(2));
rs.close();
ps.close();
return c;
} catch (SQLException e) {
tryToClose(rs);
tryToClose(ps);
throw new DbException(e);
}
}
public Contact getContact(Connection txn, ContactId c) throws DbException {
PreparedStatement ps = null;
ResultSet rs = null;
@@ -1921,12 +1899,13 @@ abstract class JdbcDatabase implements Database<Connection> {
}
}
public Settings getSettings(Connection txn) throws DbException {
public Settings getSettings(Connection txn, String namespace) throws DbException {
PreparedStatement ps = null;
ResultSet rs = null;
try {
String sql = "SELECT key, value FROM settings";
String sql = "SELECT key, value FROM settings WHERE namespace = ?";
ps = txn.prepareStatement(sql);
ps.setString(1, namespace);
rs = ps.executeQuery();
Settings s = new Settings();
while (rs.next()) s.put(rs.getString(1), rs.getString(2));
@@ -2373,12 +2352,6 @@ abstract class JdbcDatabase implements Database<Connection> {
}
}
public void mergeConfig(Connection txn, TransportId t, TransportConfig c)
throws DbException {
// Merge the new configuration with the existing one
mergeStringMap(txn, t, c, "transportConfigs");
}
public void mergeLocalProperties(Connection txn, TransportId t,
TransportProperties p) throws DbException {
// Merge the new properties with the existing ones
@@ -2446,15 +2419,16 @@ abstract class JdbcDatabase implements Database<Connection> {
}
}
public void mergeSettings(Connection txn, Settings s) throws DbException {
public void mergeSettings(Connection txn, Settings s, String namespace) throws DbException {
PreparedStatement ps = null;
try {
// Update any settings that already exist
String sql = "UPDATE settings SET value = ? WHERE key = ?";
String sql = "UPDATE settings SET value = ? WHERE key = ? AND namespace = ?";
ps = txn.prepareStatement(sql);
for (Entry<String, String> e : s.entrySet()) {
ps.setString(1, e.getValue());
ps.setString(2, e.getKey());
ps.setString(3, namespace);
ps.addBatch();
}
int[] batchAffected = ps.executeBatch();
@@ -2464,13 +2438,14 @@ abstract class JdbcDatabase implements Database<Connection> {
if (batchAffected[i] > 1) throw new DbStateException();
}
// Insert any settings that don't already exist
sql = "INSERT INTO settings (key, value) VALUES (?, ?)";
sql = "INSERT INTO settings (key, value, namespace) VALUES (?, ?, ?)";
ps = txn.prepareStatement(sql);
int updateIndex = 0, inserted = 0;
for (Entry<String, String> e : s.entrySet()) {
if (batchAffected[updateIndex] == 0) {
ps.setString(1, e.getKey());
ps.setString(2, e.getValue());
ps.setString(3, namespace);
ps.addBatch();
inserted++;
}

View File

@@ -1,6 +1,5 @@
package org.briarproject.plugins;
import org.briarproject.api.TransportConfig;
import org.briarproject.api.TransportId;
import org.briarproject.api.TransportProperties;
import org.briarproject.api.contact.ContactId;
@@ -28,6 +27,8 @@ import org.briarproject.api.plugins.simplex.SimplexPluginFactory;
import org.briarproject.api.property.TransportPropertyManager;
import org.briarproject.api.system.Clock;
import org.briarproject.api.ui.UiCallback;
import org.briarproject.api.Settings;
import org.briarproject.api.settings.SettingsManager;
import java.io.IOException;
import java.util.ArrayList;
@@ -167,7 +168,9 @@ class PluginManagerImpl implements PluginManager {
public void run() {
try {
TransportId id = factory.getId();
SimplexCallback callback = new SimplexCallback(id);
String namespace = id.toString();
SimplexCallback callback = new SimplexCallback(id, namespace);
SimplexPlugin plugin = factory.createPlugin(callback);
if (plugin == null) {
if (LOG.isLoggable(INFO)) {
@@ -230,7 +233,8 @@ class PluginManagerImpl implements PluginManager {
public void run() {
try {
TransportId id = factory.getId();
DuplexCallback callback = new DuplexCallback(id);
String namespace = id.toString();
DuplexCallback callback = new DuplexCallback(id, namespace);
DuplexPlugin plugin = factory.createPlugin(callback);
if (plugin == null) {
if (LOG.isLoggable(INFO)) {
@@ -309,18 +313,22 @@ class PluginManagerImpl implements PluginManager {
private abstract class PluginCallbackImpl implements PluginCallback {
protected final TransportId id;
protected final String namespace;
protected PluginCallbackImpl(TransportId id) {
protected PluginCallbackImpl(TransportId id, String namespace) {
this.id = id;
this.namespace = namespace;
}
public TransportConfig getConfig() {
public Settings getSettings() {
try {
return db.getConfig(id);
return db.getSettings(namespace);
} catch (DbException e) {
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
return new TransportConfig();
return new Settings();
}
}
public TransportProperties getLocalProperties() {
@@ -343,12 +351,14 @@ class PluginManagerImpl implements PluginManager {
}
}
public void mergeConfig(TransportConfig c) {
public void mergeSettings(Settings s) {
try {
db.mergeConfig(id, c);
db.mergeSettings(s, namespace);
} catch (DbException e) {
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
}
}
public void mergeLocalProperties(TransportProperties p) {
@@ -386,8 +396,8 @@ class PluginManagerImpl implements PluginManager {
private class SimplexCallback extends PluginCallbackImpl
implements SimplexPluginCallback {
private SimplexCallback(TransportId id) {
super(id);
private SimplexCallback(TransportId id, String namespace) {
super(id, namespace);
}
public void readerCreated(TransportConnectionReader r) {
@@ -402,8 +412,8 @@ class PluginManagerImpl implements PluginManager {
private class DuplexCallback extends PluginCallbackImpl
implements DuplexPluginCallback {
private DuplexCallback(TransportId id) {
super(id);
private DuplexCallback(TransportId id, String namespace) {
super(id, namespace);
}
public void incomingConnectionCreated(DuplexTransportConnection d) {
@@ -415,4 +425,4 @@ class PluginManagerImpl implements PluginManager {
connectionManager.manageOutgoingConnection(c, id, d);
}
}
}
}

View File

@@ -76,4 +76,4 @@ class LanTcpPlugin extends TcpPlugin {
// Unrecognised prefix - may be compatible
return true;
}
}
}

View File

@@ -0,0 +1,44 @@
package org.briarproject.settings;
import com.google.inject.Inject;
import org.briarproject.api.settings.SettingsManager;
import org.briarproject.api.Settings;
import org.briarproject.api.db.DatabaseComponent;
import org.briarproject.api.db.DbException;
import java.util.logging.Logger;
import java.util.Collection;
class SettingsManagerImpl implements SettingsManager {
private final DatabaseComponent db;
private static final Logger LOG =
Logger.getLogger("SettingsManagerImpl");
@Inject
SettingsManagerImpl(DatabaseComponent db) {
this.db = db;
}
/**
* Returns the settings object identified by the provided namespace
* string
*/
@Override
public Settings getSettings(String namespace) throws DbException {
return db.getSettings(namespace);
}
/**
* Merges (read syncs) the provided settings identified by the provided namespace
* string
*/
@Override
public void mergeSettings(Settings s, String namespace) throws DbException {
db.mergeSettings(s, namespace);
}
}

View File

@@ -0,0 +1,13 @@
package org.briarproject.settings;
import com.google.inject.AbstractModule;
import org.briarproject.api.settings.SettingsManager;
public class SettingsModule extends AbstractModule {
@Override
protected void configure() {
bind(SettingsManager.class).to(SettingsManagerImpl.class);
}
}

View File

@@ -28,7 +28,7 @@ public class ModemPluginFactory implements DuplexPluginFactory {
public DuplexPlugin createPlugin(DuplexPluginCallback callback) {
// This plugin is not enabled by default
String enabled = callback.getConfig().get("enabled");
String enabled = callback.getSettings().get("enabled");
if (StringUtils.isNullOrEmpty(enabled)) return null;
return new ModemPlugin(modemFactory, serialPortList, callback,
MAX_LATENCY);

View File

@@ -3,7 +3,7 @@ package org.briarproject.db;
import org.briarproject.BriarTestCase;
import org.briarproject.TestMessage;
import org.briarproject.TestUtils;
import org.briarproject.api.TransportConfig;
import org.briarproject.api.Settings;
import org.briarproject.api.TransportId;
import org.briarproject.api.TransportProperties;
import org.briarproject.api.contact.Contact;
@@ -611,13 +611,13 @@ public class DatabaseComponentImplTest extends BriarTestCase {
oneOf(database).commitTransaction(txn);
oneOf(eventBus).broadcast(with(any(ContactAddedEvent.class)));
// Check whether the transport is in the DB (which it's not)
exactly(8).of(database).startTransaction();
exactly(6).of(database).startTransaction();
will(returnValue(txn));
exactly(2).of(database).containsContact(txn, contactId);
will(returnValue(true));
exactly(8).of(database).containsTransport(txn, transportId);
exactly(6).of(database).containsTransport(txn, transportId);
will(returnValue(false));
exactly(8).of(database).abortTransaction(txn);
exactly(6).of(database).abortTransaction(txn);
}});
DatabaseComponent db = createDatabaseComponent(database, eventBus,
shutdown);
@@ -625,13 +625,6 @@ public class DatabaseComponentImplTest extends BriarTestCase {
db.addLocalAuthor(localAuthor);
assertEquals(contactId, db.addContact(author, localAuthorId));
try {
db.getConfig(transportId);
fail();
} catch (NoSuchTransportException expected) {
// Expected
}
try {
db.getLocalProperties(transportId);
fail();
@@ -646,13 +639,6 @@ public class DatabaseComponentImplTest extends BriarTestCase {
// Expected
}
try {
db.mergeConfig(transportId, new TransportConfig());
fail();
} catch (NoSuchTransportException expected) {
// Expected
}
try {
db.mergeLocalProperties(transportId, new TransportProperties());
fail();

View File

@@ -4,7 +4,6 @@ import org.briarproject.BriarTestCase;
import org.briarproject.TestDatabaseConfig;
import org.briarproject.TestMessage;
import org.briarproject.TestUtils;
import org.briarproject.api.TransportConfig;
import org.briarproject.api.TransportId;
import org.briarproject.api.TransportProperties;
import org.briarproject.api.contact.ContactId;
@@ -21,6 +20,7 @@ import org.briarproject.api.sync.MessageId;
import org.briarproject.api.transport.IncomingKeys;
import org.briarproject.api.transport.OutgoingKeys;
import org.briarproject.api.transport.TransportKeys;
import org.briarproject.api.Settings;
import org.briarproject.system.SystemClock;
import org.junit.After;
import org.junit.Before;
@@ -568,7 +568,7 @@ public class H2DatabaseTest extends BriarTestCase {
}
@Test
public void testUpdateTransportConfig() throws Exception {
public void testUpdateSettings() throws Exception {
Database<Connection> db = open(false);
Connection txn = db.startTransaction();
@@ -576,22 +576,22 @@ public class H2DatabaseTest extends BriarTestCase {
db.addTransport(txn, transportId, 123);
// Set the transport config
TransportConfig c = new TransportConfig();
c.put("foo", "foo");
c.put("bar", "bar");
db.mergeConfig(txn, transportId, c);
assertEquals(c, db.getConfig(txn, transportId));
Settings s = new Settings();
s.put("foo", "foo");
s.put("bar", "bar");
db.mergeSettings(txn, s, "test");
assertEquals(s, db.getSettings(txn, "test"));
// Update one of the properties and add another
TransportConfig c1 = new TransportConfig();
c1.put("bar", "baz");
c1.put("bam", "bam");
db.mergeConfig(txn, transportId, c1);
TransportConfig merged = new TransportConfig();
Settings s1 = new Settings();
s1.put("bar", "baz");
s1.put("bam", "bam");
db.mergeSettings(txn, s1, "test");
Settings merged = new Settings();
merged.put("foo", "foo");
merged.put("bar", "baz");
merged.put("bam", "bam");
assertEquals(merged, db.getConfig(txn, transportId));
assertEquals(merged, db.getSettings(txn, "test"));
db.commitTransaction(txn);
db.close();

View File

@@ -1,6 +1,6 @@
package org.briarproject.plugins;
import org.briarproject.api.TransportConfig;
import org.briarproject.api.Settings;
import org.briarproject.api.TransportProperties;
import org.briarproject.api.contact.ContactId;
import org.briarproject.api.crypto.PseudoRandom;
@@ -58,19 +58,19 @@ public abstract class DuplexClientTest extends DuplexTest {
protected static class ClientCallback implements DuplexPluginCallback {
private TransportConfig config = null;
private Settings settings = null;
private TransportProperties local = null;
private Map<ContactId, TransportProperties> remote = null;
public ClientCallback(TransportConfig config, TransportProperties local,
public ClientCallback(Settings settings, TransportProperties local,
Map<ContactId, TransportProperties> remote) {
this.config = config;
this.settings = settings;
this.local = local;
this.remote = remote;
}
public TransportConfig getConfig() {
return config;
public Settings getSettings() {
return settings;
}
public TransportProperties getLocalProperties() {
@@ -81,8 +81,8 @@ public abstract class DuplexClientTest extends DuplexTest {
return remote;
}
public void mergeConfig(TransportConfig c) {
config = c;
public void mergeSettings(Settings s) {
settings = s;
}
public void mergeLocalProperties(TransportProperties p) {

View File

@@ -1,6 +1,6 @@
package org.briarproject.plugins;
import org.briarproject.api.TransportConfig;
import org.briarproject.api.Settings;
import org.briarproject.api.TransportProperties;
import org.briarproject.api.contact.ContactId;
import org.briarproject.api.plugins.duplex.DuplexPluginCallback;
@@ -57,19 +57,19 @@ public abstract class DuplexServerTest extends DuplexTest {
private final CountDownLatch latch = new CountDownLatch(1);
private TransportConfig config;
private Settings settings;
private TransportProperties local;
private Map<ContactId, TransportProperties> remote;
public ServerCallback(TransportConfig config, TransportProperties local,
public ServerCallback(Settings settings, TransportProperties local,
Map<ContactId, TransportProperties> remote) {
this.config = config;
this.settings = settings;
this.local = local;
this.remote = remote;
}
public TransportConfig getConfig() {
return config;
public Settings getSettings() {
return settings;
}
public TransportProperties getLocalProperties() {
@@ -80,8 +80,8 @@ public abstract class DuplexServerTest extends DuplexTest {
return remote;
}
public void mergeConfig(TransportConfig c) {
config = c;
public void mergeSettings(Settings s) {
settings = s;
}
public void mergeLocalProperties(TransportProperties p) {

View File

@@ -1,6 +1,6 @@
package org.briarproject.plugins.bluetooth;
import org.briarproject.api.TransportConfig;
import org.briarproject.api.Settings;
import org.briarproject.api.TransportProperties;
import org.briarproject.api.contact.ContactId;
import org.briarproject.plugins.DuplexClientTest;
@@ -25,7 +25,7 @@ public class BluetoothClientTest extends DuplexClientTest {
Map<ContactId, TransportProperties> remote =
Collections.singletonMap(contactId, p);
// Create the plugin
callback = new ClientCallback(new TransportConfig(),
callback = new ClientCallback(new Settings(),
new TransportProperties(), remote);
plugin = new BluetoothPlugin(executor, new SystemClock(),
new SecureRandom(), callback, 0, 0);

View File

@@ -1,6 +1,6 @@
package org.briarproject.plugins.bluetooth;
import org.briarproject.api.TransportConfig;
import org.briarproject.api.Settings;
import org.briarproject.api.TransportProperties;
import org.briarproject.plugins.DuplexServerTest;
import org.briarproject.system.SystemClock;
@@ -20,7 +20,7 @@ public class BluetoothServerTest extends DuplexServerTest {
TransportProperties local = new TransportProperties();
local.put("uuid", BluetoothTest.EMPTY_UUID);
// Create the plugin
callback = new ServerCallback(new TransportConfig(), local,
callback = new ServerCallback(new Settings(), local,
Collections.singletonMap(contactId, new TransportProperties()));
plugin = new BluetoothPlugin(executor, new SystemClock(),
new SecureRandom(), callback, 0, 0);

View File

@@ -1,6 +1,6 @@
package org.briarproject.plugins.tcp;
import org.briarproject.api.TransportConfig;
import org.briarproject.api.Settings;
import org.briarproject.api.TransportProperties;
import org.briarproject.api.contact.ContactId;
import org.briarproject.plugins.DuplexClientTest;
@@ -28,7 +28,7 @@ public class LanTcpClientTest extends DuplexClientTest {
Map<ContactId, TransportProperties> remote =
Collections.singletonMap(contactId, p);
// Create the plugin
callback = new ClientCallback(new TransportConfig(),
callback = new ClientCallback(new Settings(),
new TransportProperties(), remote);
plugin = new LanTcpPlugin(executor, callback, MAX_LATENCY,
MAX_IDLE_TIME, POLLING_INTERVAL);

View File

@@ -1,7 +1,7 @@
package org.briarproject.plugins.tcp;
import org.briarproject.BriarTestCase;
import org.briarproject.api.TransportConfig;
import org.briarproject.api.Settings;
import org.briarproject.api.TransportProperties;
import org.briarproject.api.contact.ContactId;
import org.briarproject.api.plugins.duplex.DuplexPlugin;
@@ -176,8 +176,8 @@ public class LanTcpPluginTest extends BriarTestCase {
private final CountDownLatch connectionsLatch = new CountDownLatch(1);
private final TransportProperties local = new TransportProperties();
public TransportConfig getConfig() {
return new TransportConfig();
public Settings getSettings() {
return new Settings();
}
public TransportProperties getLocalProperties() {
@@ -188,7 +188,7 @@ public class LanTcpPluginTest extends BriarTestCase {
return remote;
}
public void mergeConfig(TransportConfig c) {}
public void mergeSettings(Settings s) {}
public void mergeLocalProperties(TransportProperties p) {
local.putAll(p);

View File

@@ -1,6 +1,6 @@
package org.briarproject.plugins.tcp;
import org.briarproject.api.TransportConfig;
import org.briarproject.api.Settings;
import org.briarproject.api.TransportProperties;
import org.briarproject.plugins.DuplexServerTest;
@@ -18,7 +18,7 @@ public class LanTcpServerTest extends DuplexServerTest {
private static final int POLLING_INTERVAL = 60 * 1000;
private LanTcpServerTest(Executor executor) {
callback = new ServerCallback(new TransportConfig(),
callback = new ServerCallback(new Settings(),
new TransportProperties(),
Collections.singletonMap(contactId, new TransportProperties()));
plugin = new LanTcpPlugin(executor, callback, MAX_LATENCY,