mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-13 19:29:06 +01:00
Added the ability to store transport configuration details in the
database - unlike transport properties, these are not shared with contacts. For example, when using email as a transport, the address for sending and receiving emails would be a transport property, while the username and password for the email server would be transport configuration details. Transport plugins can update their configuration details atomically. Also clarified the terminology for transport and subscription updates.
This commit is contained in:
@@ -268,14 +268,22 @@ interface Database<T> {
|
||||
Collection<Group> getSubscriptions(T txn, ContactId c) throws DbException;
|
||||
|
||||
/**
|
||||
* Returns the local transport properties.
|
||||
* Returns the configuration for the transport with the given name.
|
||||
* <p>
|
||||
* Locking: transports read.
|
||||
*/
|
||||
Map<String, String> getTransportConfig(T txn, String name)
|
||||
throws DbException;
|
||||
|
||||
/**
|
||||
* Returns all local transport properties.
|
||||
* <p>
|
||||
* Locking: transports read.
|
||||
*/
|
||||
Map<String, Map<String, String>> getTransports(T txn) throws DbException;
|
||||
|
||||
/**
|
||||
* Returns the transport properties for the given contact.
|
||||
* Returns all transport properties for the given contact.
|
||||
* <p>
|
||||
* Locking: contacts read, transports read.
|
||||
*/
|
||||
@@ -398,14 +406,23 @@ interface Database<T> {
|
||||
long timestamp) throws DbException;
|
||||
|
||||
/**
|
||||
* Sets the local transport properties for the transport with the given
|
||||
* name, replacing any existing properties for that transport.
|
||||
* Sets the configuration for the transport with the given name, replacing
|
||||
* any existing configuration for that transport.
|
||||
* <p>
|
||||
* Locking: transports write.
|
||||
*/
|
||||
void setTransports(T txn, String name, Map<String, String> transports)
|
||||
void setTransportConfig(T txn, String name, Map<String, String> config)
|
||||
throws DbException;
|
||||
|
||||
/**
|
||||
* Sets the transport properties for the transport with the given name,
|
||||
* replacing any existing properties for that transport.
|
||||
* <p>
|
||||
* Locking: transports write.
|
||||
*/
|
||||
void setTransportProperties(T txn, String name,
|
||||
Map<String, String> properties) throws DbException;
|
||||
|
||||
/**
|
||||
* Sets the transport properties for the given contact, replacing any
|
||||
* existing properties unless the existing properties have a newer
|
||||
|
||||
@@ -173,6 +173,13 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
+ " value VARCHAR NOT NULL,"
|
||||
+ " PRIMARY KEY (transportName, key))";
|
||||
|
||||
private static final String CREATE_TRANSPORT_CONFIG =
|
||||
"CREATE TABLE transportConfig"
|
||||
+ " (transportName VARCHAR NOT NULL,"
|
||||
+ " key VARCHAR NOT NULL,"
|
||||
+ " value VARCHAR NOT NULL,"
|
||||
+ " PRIMARY KEY (transportName, key))";
|
||||
|
||||
private static final Logger LOG =
|
||||
Logger.getLogger(JdbcDatabase.class.getName());
|
||||
|
||||
@@ -255,6 +262,7 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
s.executeUpdate(INDEX_STATUSES_BY_CONTACT);
|
||||
s.executeUpdate(insertTypeNames(CREATE_CONTACT_TRANSPORTS));
|
||||
s.executeUpdate(insertTypeNames(CREATE_TRANSPORTS));
|
||||
s.executeUpdate(insertTypeNames(CREATE_TRANSPORT_CONFIG));
|
||||
s.close();
|
||||
} catch(SQLException e) {
|
||||
tryToClose(s);
|
||||
@@ -1096,6 +1104,115 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
}
|
||||
}
|
||||
|
||||
public Map<String, String> getTransportConfig(Connection txn,
|
||||
String name) throws DbException {
|
||||
PreparedStatement ps = null;
|
||||
ResultSet rs = null;
|
||||
try {
|
||||
String sql = "SELECT key, value FROM transportConfig"
|
||||
+ " WHERE transportName = ?";
|
||||
ps = txn.prepareStatement(sql);
|
||||
ps.setString(1, name);
|
||||
rs = ps.executeQuery();
|
||||
Map<String, String> config = new TreeMap<String, String>();
|
||||
while(rs.next()) config.put(rs.getString(1), rs.getString(2));
|
||||
rs.close();
|
||||
ps.close();
|
||||
return config;
|
||||
} catch(SQLException e) {
|
||||
tryToClose(rs);
|
||||
tryToClose(ps);
|
||||
throw new DbException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public Map<String, Map<String, String>> getTransports(Connection txn)
|
||||
throws DbException {
|
||||
PreparedStatement ps = null;
|
||||
ResultSet rs = null;
|
||||
try {
|
||||
String sql = "SELECT transportName, key, value"
|
||||
+ " FROM transports"
|
||||
+ " ORDER BY transportName";
|
||||
ps = txn.prepareStatement(sql);
|
||||
rs = ps.executeQuery();
|
||||
Map<String, Map<String, String>> transports =
|
||||
new TreeMap<String, Map<String, String>>();
|
||||
Map<String, String> properties = null;
|
||||
String lastName = null;
|
||||
while(rs.next()) {
|
||||
String name = rs.getString(1);
|
||||
if(!name.equals(lastName)) {
|
||||
properties = new TreeMap<String, String>();
|
||||
transports.put(name, properties);
|
||||
}
|
||||
properties.put(rs.getString(2), rs.getString(3));
|
||||
}
|
||||
rs.close();
|
||||
ps.close();
|
||||
return transports;
|
||||
} catch(SQLException e) {
|
||||
tryToClose(rs);
|
||||
tryToClose(ps);
|
||||
throw new DbException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public Map<String, Map<String, String>> getTransports(Connection txn,
|
||||
ContactId c) throws DbException {
|
||||
PreparedStatement ps = null;
|
||||
ResultSet rs = null;
|
||||
try {
|
||||
String sql = "SELECT transportName, key, value"
|
||||
+ " FROM contactTransports"
|
||||
+ " WHERE contactId = ?"
|
||||
+ " ORDER BY transportName";
|
||||
ps = txn.prepareStatement(sql);
|
||||
ps.setInt(1, c.getInt());
|
||||
rs = ps.executeQuery();
|
||||
Map<String, Map<String, String>> transports =
|
||||
new TreeMap<String, Map<String, String>>();
|
||||
Map<String, String> properties = null;
|
||||
String lastName = null;
|
||||
while(rs.next()) {
|
||||
String name = rs.getString(1);
|
||||
if(!name.equals(lastName)) {
|
||||
properties = new TreeMap<String, String>();
|
||||
transports.put(name, properties);
|
||||
}
|
||||
properties.put(rs.getString(2), rs.getString(3));
|
||||
}
|
||||
rs.close();
|
||||
ps.close();
|
||||
return transports;
|
||||
} catch(SQLException e) {
|
||||
tryToClose(rs);
|
||||
tryToClose(ps);
|
||||
throw new DbException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public Collection<ContactId> getVisibility(Connection txn, GroupId g)
|
||||
throws DbException {
|
||||
PreparedStatement ps = null;
|
||||
ResultSet rs = null;
|
||||
try {
|
||||
String sql = "SELECT contactId FROM visibilities WHERE groupId = ?";
|
||||
ps = txn.prepareStatement(sql);
|
||||
ps.setBytes(1, g.getBytes());
|
||||
rs = ps.executeQuery();
|
||||
Collection<ContactId> visible = new ArrayList<ContactId>();
|
||||
while(rs.next()) visible.add(new ContactId(rs.getInt(1)));
|
||||
rs.close();
|
||||
ps.close();
|
||||
return visible;
|
||||
} catch(SQLException e) {
|
||||
tryToClose(rs);
|
||||
tryToClose(ps);
|
||||
throw new DbException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public Collection<Group> getVisibleSubscriptions(Connection txn,
|
||||
ContactId c) throws DbException {
|
||||
PreparedStatement ps = null;
|
||||
@@ -1125,93 +1242,6 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
}
|
||||
}
|
||||
|
||||
public Map<String, Map<String, String>> getTransports(Connection txn)
|
||||
throws DbException {
|
||||
PreparedStatement ps = null;
|
||||
ResultSet rs = null;
|
||||
try {
|
||||
String sql = "SELECT transportName, key, value"
|
||||
+ " FROM transports"
|
||||
+ " ORDER BY transportName";
|
||||
ps = txn.prepareStatement(sql);
|
||||
rs = ps.executeQuery();
|
||||
Map<String, Map<String, String>> outer =
|
||||
new TreeMap<String, Map<String, String>>();
|
||||
Map<String, String> inner = null;
|
||||
String lastName = null;
|
||||
while(rs.next()) {
|
||||
String name = rs.getString(1);
|
||||
if(!name.equals(lastName)) {
|
||||
inner = new TreeMap<String, String>();
|
||||
outer.put(name, inner);
|
||||
}
|
||||
inner.put(rs.getString(2), rs.getString(3));
|
||||
}
|
||||
rs.close();
|
||||
ps.close();
|
||||
return outer;
|
||||
} catch(SQLException e) {
|
||||
tryToClose(rs);
|
||||
tryToClose(ps);
|
||||
throw new DbException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public Map<String, Map<String, String>> getTransports(Connection txn,
|
||||
ContactId c) throws DbException {
|
||||
PreparedStatement ps = null;
|
||||
ResultSet rs = null;
|
||||
try {
|
||||
String sql = "SELECT transportName, key, value"
|
||||
+ " FROM contactTransports"
|
||||
+ " WHERE contactId = ?"
|
||||
+ " ORDER BY transportName";
|
||||
ps = txn.prepareStatement(sql);
|
||||
ps.setInt(1, c.getInt());
|
||||
rs = ps.executeQuery();
|
||||
Map<String, Map<String, String>> outer =
|
||||
new TreeMap<String, Map<String, String>>();
|
||||
Map<String, String> inner = null;
|
||||
String lastName = null;
|
||||
while(rs.next()) {
|
||||
String name = rs.getString(1);
|
||||
if(!name.equals(lastName)) {
|
||||
inner = new TreeMap<String, String>();
|
||||
outer.put(name, inner);
|
||||
}
|
||||
inner.put(rs.getString(2), rs.getString(3));
|
||||
}
|
||||
rs.close();
|
||||
ps.close();
|
||||
return outer;
|
||||
} catch(SQLException e) {
|
||||
tryToClose(rs);
|
||||
tryToClose(ps);
|
||||
throw new DbException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public Collection<ContactId> getVisibility(Connection txn, GroupId g)
|
||||
throws DbException {
|
||||
PreparedStatement ps = null;
|
||||
ResultSet rs = null;
|
||||
try {
|
||||
String sql = "SELECT contactId FROM visibilities WHERE groupId = ?";
|
||||
ps = txn.prepareStatement(sql);
|
||||
ps.setBytes(1, g.getBytes());
|
||||
rs = ps.executeQuery();
|
||||
Collection<ContactId> visible = new ArrayList<ContactId>();
|
||||
while(rs.next()) visible.add(new ContactId(rs.getInt(1)));
|
||||
rs.close();
|
||||
ps.close();
|
||||
return visible;
|
||||
} catch(SQLException e) {
|
||||
tryToClose(rs);
|
||||
tryToClose(ps);
|
||||
throw new DbException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean hasSendableMessages(Connection txn, ContactId c)
|
||||
throws DbException {
|
||||
PreparedStatement ps = null;
|
||||
@@ -1609,28 +1639,33 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
}
|
||||
}
|
||||
|
||||
public void setTransports(Connection txn, String name,
|
||||
Map<String, String> transports) throws DbException {
|
||||
public void setTransportConfig(Connection txn, String name,
|
||||
Map<String, String> config) throws DbException {
|
||||
setTransportDetails(txn, name, config, "transportConfig");
|
||||
}
|
||||
|
||||
private void setTransportDetails(Connection txn, String name,
|
||||
Map<String, String> details, String table) throws DbException {
|
||||
PreparedStatement ps = null;
|
||||
try {
|
||||
// Delete any existing properties for the named transport
|
||||
String sql = "DELETE FROM transports WHERE transportName = ?";
|
||||
// Delete any existing details for the named transport
|
||||
String sql = "DELETE FROM " + table + " WHERE transportName = ?";
|
||||
ps = txn.prepareStatement(sql);
|
||||
ps.setString(1, name);
|
||||
ps.executeUpdate();
|
||||
ps.close();
|
||||
// Store the new properties
|
||||
sql = "INSERT INTO transports (transportName, key, value)"
|
||||
// Store the new details
|
||||
sql = "INSERT INTO " + table + " (transportName, key, value)"
|
||||
+ " VALUES (?, ?, ?)";
|
||||
ps = txn.prepareStatement(sql);
|
||||
ps.setString(1, name);
|
||||
for(Entry<String, String> e : transports.entrySet()) {
|
||||
for(Entry<String, String> e : details.entrySet()) {
|
||||
ps.setString(2, e.getKey());
|
||||
ps.setString(3, e.getValue());
|
||||
ps.addBatch();
|
||||
}
|
||||
int[] batchAffected = ps.executeBatch();
|
||||
if(batchAffected.length != transports.size())
|
||||
if(batchAffected.length != details.size())
|
||||
throw new DbStateException();
|
||||
for(int i = 0; i < batchAffected.length; i++) {
|
||||
if(batchAffected[i] != 1) throw new DbStateException();
|
||||
@@ -1642,6 +1677,11 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
}
|
||||
}
|
||||
|
||||
public void setTransportProperties(Connection txn, String name,
|
||||
Map<String, String> properties) throws DbException {
|
||||
setTransportDetails(txn, name, properties, "transports");
|
||||
}
|
||||
|
||||
public void setTransports(Connection txn, ContactId c,
|
||||
Map<String, Map<String, String>> transports, long timestamp)
|
||||
throws DbException {
|
||||
|
||||
@@ -24,8 +24,8 @@ import net.sf.briar.api.protocol.GroupId;
|
||||
import net.sf.briar.api.protocol.Message;
|
||||
import net.sf.briar.api.protocol.MessageId;
|
||||
import net.sf.briar.api.protocol.Offer;
|
||||
import net.sf.briar.api.protocol.Subscriptions;
|
||||
import net.sf.briar.api.protocol.Transports;
|
||||
import net.sf.briar.api.protocol.SubscriptionUpdate;
|
||||
import net.sf.briar.api.protocol.TransportUpdate;
|
||||
import net.sf.briar.api.protocol.writers.AckWriter;
|
||||
import net.sf.briar.api.protocol.writers.BatchWriter;
|
||||
import net.sf.briar.api.protocol.writers.OfferWriter;
|
||||
@@ -107,7 +107,12 @@ class ReadWriteLockDatabaseComponent<Txn> extends DatabaseComponentImpl<Txn> {
|
||||
try {
|
||||
subscriptionLock.writeLock().lock();
|
||||
try {
|
||||
db.close();
|
||||
transportLock.writeLock().lock();
|
||||
try {
|
||||
db.close();
|
||||
} finally {
|
||||
transportLock.writeLock().unlock();
|
||||
}
|
||||
} finally {
|
||||
subscriptionLock.writeLock().unlock();
|
||||
}
|
||||
@@ -459,7 +464,7 @@ class ReadWriteLockDatabaseComponent<Txn> extends DatabaseComponentImpl<Txn> {
|
||||
}
|
||||
}
|
||||
|
||||
public void generateSubscriptions(ContactId c, SubscriptionWriter s)
|
||||
public void generateSubscriptionUpdate(ContactId c, SubscriptionWriter s)
|
||||
throws DbException, IOException {
|
||||
contactLock.readLock().lock();
|
||||
try {
|
||||
@@ -488,7 +493,7 @@ class ReadWriteLockDatabaseComponent<Txn> extends DatabaseComponentImpl<Txn> {
|
||||
}
|
||||
}
|
||||
|
||||
public void generateTransports(ContactId c, TransportWriter t)
|
||||
public void generateTransportUpdate(ContactId c, TransportWriter t)
|
||||
throws DbException, IOException {
|
||||
contactLock.readLock().lock();
|
||||
try {
|
||||
@@ -569,6 +574,24 @@ class ReadWriteLockDatabaseComponent<Txn> extends DatabaseComponentImpl<Txn> {
|
||||
}
|
||||
}
|
||||
|
||||
public Map<String, String> getTransportConfig(String name)
|
||||
throws DbException {
|
||||
transportLock.readLock().lock();
|
||||
try {
|
||||
Txn txn = db.startTransaction();
|
||||
try {
|
||||
Map<String, String> config = db.getTransportConfig(txn, name);
|
||||
db.commitTransaction(txn);
|
||||
return config;
|
||||
} catch(DbException e) {
|
||||
db.abortTransaction(txn);
|
||||
throw e;
|
||||
}
|
||||
} finally {
|
||||
transportLock.readLock().unlock();
|
||||
}
|
||||
}
|
||||
|
||||
public Map<String, Map<String, String>> getTransports() throws DbException {
|
||||
transportLock.readLock().lock();
|
||||
try {
|
||||
@@ -793,7 +816,7 @@ class ReadWriteLockDatabaseComponent<Txn> extends DatabaseComponentImpl<Txn> {
|
||||
}
|
||||
}
|
||||
|
||||
public void receiveSubscriptions(ContactId c, Subscriptions s)
|
||||
public void receiveSubscriptionUpdate(ContactId c, SubscriptionUpdate s)
|
||||
throws DbException {
|
||||
// Update the contact's subscriptions
|
||||
contactLock.writeLock().lock();
|
||||
@@ -820,7 +843,7 @@ class ReadWriteLockDatabaseComponent<Txn> extends DatabaseComponentImpl<Txn> {
|
||||
}
|
||||
}
|
||||
|
||||
public void receiveTransports(ContactId c, Transports t)
|
||||
public void receiveTransportUpdate(ContactId c, TransportUpdate t)
|
||||
throws DbException {
|
||||
// Update the contact's transport properties
|
||||
contactLock.writeLock().lock();
|
||||
@@ -907,15 +930,40 @@ class ReadWriteLockDatabaseComponent<Txn> extends DatabaseComponentImpl<Txn> {
|
||||
}
|
||||
}
|
||||
|
||||
public void setTransports(String name, Map<String, String> transports)
|
||||
throws DbException {
|
||||
public void setTransportConfig(String name,
|
||||
Map<String, String> config) throws DbException {
|
||||
boolean changed = false;
|
||||
transportLock.writeLock().lock();
|
||||
try {
|
||||
Txn txn = db.startTransaction();
|
||||
try {
|
||||
if(!transports.equals(db.getTransports(txn).get(name))) {
|
||||
db.setTransports(txn, name, transports);
|
||||
Map<String, String> old = db.getTransportConfig(txn, name);
|
||||
if(!config.equals(old)) {
|
||||
db.setTransportConfig(txn, name, config);
|
||||
changed = true;
|
||||
}
|
||||
db.commitTransaction(txn);
|
||||
} catch(DbException e) {
|
||||
db.abortTransaction(txn);
|
||||
throw e;
|
||||
}
|
||||
} finally {
|
||||
transportLock.writeLock().unlock();
|
||||
}
|
||||
// Call the listeners outside the lock
|
||||
if(changed) callListeners(DatabaseListener.Event.TRANSPORTS_UPDATED);
|
||||
}
|
||||
|
||||
public void setTransportProperties(String name,
|
||||
Map<String, String> properties) throws DbException {
|
||||
boolean changed = false;
|
||||
transportLock.writeLock().lock();
|
||||
try {
|
||||
Txn txn = db.startTransaction();
|
||||
try {
|
||||
Map<String, String> old = db.getTransports(txn).get(name);
|
||||
if(!properties.equals(old)) {
|
||||
db.setTransportProperties(txn, name, properties);
|
||||
changed = true;
|
||||
}
|
||||
db.commitTransaction(txn);
|
||||
|
||||
@@ -23,8 +23,8 @@ import net.sf.briar.api.protocol.GroupId;
|
||||
import net.sf.briar.api.protocol.Message;
|
||||
import net.sf.briar.api.protocol.MessageId;
|
||||
import net.sf.briar.api.protocol.Offer;
|
||||
import net.sf.briar.api.protocol.Subscriptions;
|
||||
import net.sf.briar.api.protocol.Transports;
|
||||
import net.sf.briar.api.protocol.SubscriptionUpdate;
|
||||
import net.sf.briar.api.protocol.TransportUpdate;
|
||||
import net.sf.briar.api.protocol.writers.AckWriter;
|
||||
import net.sf.briar.api.protocol.writers.BatchWriter;
|
||||
import net.sf.briar.api.protocol.writers.OfferWriter;
|
||||
@@ -336,7 +336,7 @@ class SynchronizedDatabaseComponent<Txn> extends DatabaseComponentImpl<Txn> {
|
||||
}
|
||||
}
|
||||
|
||||
public void generateSubscriptions(ContactId c, SubscriptionWriter s)
|
||||
public void generateSubscriptionUpdate(ContactId c, SubscriptionWriter s)
|
||||
throws DbException, IOException {
|
||||
synchronized(contactLock) {
|
||||
if(!containsContact(c)) throw new NoSuchContactException();
|
||||
@@ -359,7 +359,7 @@ class SynchronizedDatabaseComponent<Txn> extends DatabaseComponentImpl<Txn> {
|
||||
}
|
||||
}
|
||||
|
||||
public void generateTransports(ContactId c, TransportWriter t)
|
||||
public void generateTransportUpdate(ContactId c, TransportWriter t)
|
||||
throws DbException, IOException {
|
||||
synchronized(contactLock) {
|
||||
if(!containsContact(c)) throw new NoSuchContactException();
|
||||
@@ -425,6 +425,21 @@ class SynchronizedDatabaseComponent<Txn> extends DatabaseComponentImpl<Txn> {
|
||||
}
|
||||
}
|
||||
|
||||
public Map<String, String> getTransportConfig(String name)
|
||||
throws DbException {
|
||||
synchronized(transportLock) {
|
||||
Txn txn = db.startTransaction();
|
||||
try {
|
||||
Map<String, String> config = db.getTransportConfig(txn, name);
|
||||
db.commitTransaction(txn);
|
||||
return config;
|
||||
} catch(DbException e) {
|
||||
db.abortTransaction(txn);
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Map<String, Map<String, String>> getTransports() throws DbException {
|
||||
synchronized(transportLock) {
|
||||
Txn txn = db.startTransaction();
|
||||
@@ -589,7 +604,7 @@ class SynchronizedDatabaseComponent<Txn> extends DatabaseComponentImpl<Txn> {
|
||||
}
|
||||
}
|
||||
|
||||
public void receiveSubscriptions(ContactId c, Subscriptions s)
|
||||
public void receiveSubscriptionUpdate(ContactId c, SubscriptionUpdate s)
|
||||
throws DbException {
|
||||
// Update the contact's subscriptions
|
||||
synchronized(contactLock) {
|
||||
@@ -610,7 +625,7 @@ class SynchronizedDatabaseComponent<Txn> extends DatabaseComponentImpl<Txn> {
|
||||
}
|
||||
}
|
||||
|
||||
public void receiveTransports(ContactId c, Transports t)
|
||||
public void receiveTransportUpdate(ContactId c, TransportUpdate t)
|
||||
throws DbException {
|
||||
// Update the contact's transport properties
|
||||
synchronized(contactLock) {
|
||||
@@ -673,14 +688,36 @@ class SynchronizedDatabaseComponent<Txn> extends DatabaseComponentImpl<Txn> {
|
||||
}
|
||||
}
|
||||
|
||||
public void setTransports(String name, Map<String, String> transports)
|
||||
throws DbException {
|
||||
public void setTransportConfig(String name,
|
||||
Map<String, String> config) throws DbException {
|
||||
boolean changed = false;
|
||||
synchronized(transportLock) {
|
||||
Txn txn = db.startTransaction();
|
||||
try {
|
||||
if(!transports.equals(db.getTransports(txn).get(name))) {
|
||||
db.setTransports(txn, name, transports);
|
||||
Map<String, String> old = db.getTransportConfig(txn, name);
|
||||
if(!config.equals(old)) {
|
||||
db.setTransportConfig(txn, name, config);
|
||||
changed = true;
|
||||
}
|
||||
db.commitTransaction(txn);
|
||||
} catch(DbException e) {
|
||||
db.abortTransaction(txn);
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
// Call the listeners outside the lock
|
||||
if(changed) callListeners(DatabaseListener.Event.TRANSPORTS_UPDATED);
|
||||
}
|
||||
|
||||
public void setTransportProperties(String name,
|
||||
Map<String, String> properties) throws DbException {
|
||||
boolean changed = false;
|
||||
synchronized(transportLock) {
|
||||
Txn txn = db.startTransaction();
|
||||
try {
|
||||
Map<String, String> old = db.getTransports(txn).get(name);
|
||||
if(!properties.equals(old)) {
|
||||
db.setTransportProperties(txn, name, properties);
|
||||
changed = true;
|
||||
}
|
||||
db.commitTransaction(txn);
|
||||
|
||||
@@ -3,9 +3,9 @@ package net.sf.briar.protocol;
|
||||
import java.util.Collection;
|
||||
|
||||
import net.sf.briar.api.protocol.Group;
|
||||
import net.sf.briar.api.protocol.Subscriptions;
|
||||
import net.sf.briar.api.protocol.SubscriptionUpdate;
|
||||
|
||||
interface SubscriptionFactory {
|
||||
|
||||
Subscriptions createSubscriptions(Collection<Group> subs, long timestamp);
|
||||
SubscriptionUpdate createSubscriptions(Collection<Group> subs, long timestamp);
|
||||
}
|
||||
|
||||
@@ -3,11 +3,11 @@ package net.sf.briar.protocol;
|
||||
import java.util.Collection;
|
||||
|
||||
import net.sf.briar.api.protocol.Group;
|
||||
import net.sf.briar.api.protocol.Subscriptions;
|
||||
import net.sf.briar.api.protocol.SubscriptionUpdate;
|
||||
|
||||
class SubscriptionFactoryImpl implements SubscriptionFactory {
|
||||
|
||||
public Subscriptions createSubscriptions(Collection<Group> subs,
|
||||
public SubscriptionUpdate createSubscriptions(Collection<Group> subs,
|
||||
long timestamp) {
|
||||
return new SubscriptionsImpl(subs, timestamp);
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ import java.io.IOException;
|
||||
import java.util.Collection;
|
||||
|
||||
import net.sf.briar.api.protocol.Group;
|
||||
import net.sf.briar.api.protocol.Subscriptions;
|
||||
import net.sf.briar.api.protocol.SubscriptionUpdate;
|
||||
import net.sf.briar.api.protocol.Tags;
|
||||
import net.sf.briar.api.serial.Consumer;
|
||||
import net.sf.briar.api.serial.ObjectReader;
|
||||
@@ -12,7 +12,7 @@ import net.sf.briar.api.serial.Reader;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
|
||||
class SubscriptionReader implements ObjectReader<Subscriptions> {
|
||||
class SubscriptionReader implements ObjectReader<SubscriptionUpdate> {
|
||||
|
||||
private final ObjectReader<Group> groupReader;
|
||||
private final SubscriptionFactory subscriptionFactory;
|
||||
@@ -24,9 +24,9 @@ class SubscriptionReader implements ObjectReader<Subscriptions> {
|
||||
this.subscriptionFactory = subscriptionFactory;
|
||||
}
|
||||
|
||||
public Subscriptions readObject(Reader r) throws IOException {
|
||||
public SubscriptionUpdate readObject(Reader r) throws IOException {
|
||||
// Initialise the consumer
|
||||
Consumer counting = new CountingConsumer(Subscriptions.MAX_SIZE);
|
||||
Consumer counting = new CountingConsumer(SubscriptionUpdate.MAX_SIZE);
|
||||
// Read the data
|
||||
r.addConsumer(counting);
|
||||
r.readUserDefinedTag(Tags.SUBSCRIPTIONS);
|
||||
@@ -35,7 +35,7 @@ class SubscriptionReader implements ObjectReader<Subscriptions> {
|
||||
r.removeObjectReader(Tags.GROUP);
|
||||
long timestamp = r.readInt64();
|
||||
r.removeConsumer(counting);
|
||||
// Build and return the subscriptions update
|
||||
// Build and return the subscription update
|
||||
return subscriptionFactory.createSubscriptions(subs, timestamp);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,9 +3,9 @@ package net.sf.briar.protocol;
|
||||
import java.util.Collection;
|
||||
|
||||
import net.sf.briar.api.protocol.Group;
|
||||
import net.sf.briar.api.protocol.Subscriptions;
|
||||
import net.sf.briar.api.protocol.SubscriptionUpdate;
|
||||
|
||||
class SubscriptionsImpl implements Subscriptions {
|
||||
class SubscriptionsImpl implements SubscriptionUpdate {
|
||||
|
||||
private final Collection<Group> subs;
|
||||
private final long timestamp;
|
||||
|
||||
@@ -2,10 +2,10 @@ package net.sf.briar.protocol;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import net.sf.briar.api.protocol.Transports;
|
||||
import net.sf.briar.api.protocol.TransportUpdate;
|
||||
|
||||
interface TransportFactory {
|
||||
|
||||
Transports createTransports(Map<String, Map<String, String>> transports,
|
||||
TransportUpdate createTransports(Map<String, Map<String, String>> transports,
|
||||
long timestamp);
|
||||
}
|
||||
|
||||
@@ -2,11 +2,11 @@ package net.sf.briar.protocol;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import net.sf.briar.api.protocol.Transports;
|
||||
import net.sf.briar.api.protocol.TransportUpdate;
|
||||
|
||||
class TransportFactoryImpl implements TransportFactory {
|
||||
|
||||
public Transports createTransports(Map<String, Map<String, String>> transports,
|
||||
public TransportUpdate createTransports(Map<String, Map<String, String>> transports,
|
||||
long timestamp) {
|
||||
return new TransportsImpl(transports, timestamp);
|
||||
}
|
||||
|
||||
@@ -5,14 +5,14 @@ import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
|
||||
import net.sf.briar.api.protocol.Tags;
|
||||
import net.sf.briar.api.protocol.Transports;
|
||||
import net.sf.briar.api.protocol.TransportUpdate;
|
||||
import net.sf.briar.api.serial.Consumer;
|
||||
import net.sf.briar.api.serial.ObjectReader;
|
||||
import net.sf.briar.api.serial.Reader;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
|
||||
class TransportReader implements ObjectReader<Transports> {
|
||||
class TransportReader implements ObjectReader<TransportUpdate> {
|
||||
|
||||
private final TransportFactory transportFactory;
|
||||
|
||||
@@ -21,32 +21,32 @@ class TransportReader implements ObjectReader<Transports> {
|
||||
this.transportFactory = transportFactory;
|
||||
}
|
||||
|
||||
public Transports readObject(Reader r) throws IOException {
|
||||
public TransportUpdate readObject(Reader r) throws IOException {
|
||||
// Initialise the consumer
|
||||
Consumer counting = new CountingConsumer(Transports.MAX_SIZE);
|
||||
Consumer counting = new CountingConsumer(TransportUpdate.MAX_SIZE);
|
||||
// Read the data
|
||||
r.addConsumer(counting);
|
||||
r.readUserDefinedTag(Tags.TRANSPORTS);
|
||||
// Transport maps are always written in delimited form
|
||||
Map<String, Map<String, String>> outer =
|
||||
Map<String, Map<String, String>> transports =
|
||||
new TreeMap<String, Map<String, String>>();
|
||||
r.readMapStart();
|
||||
while(!r.hasMapEnd()) {
|
||||
String name = r.readString(Transports.MAX_SIZE);
|
||||
Map<String, String> inner = new TreeMap<String, String>();
|
||||
String name = r.readString(TransportUpdate.MAX_SIZE);
|
||||
Map<String, String> properties = new TreeMap<String, String>();
|
||||
r.readMapStart();
|
||||
while(!r.hasMapEnd()) {
|
||||
String key = r.readString(Transports.MAX_SIZE);
|
||||
String value = r.readString(Transports.MAX_SIZE);
|
||||
inner.put(key, value);
|
||||
String key = r.readString(TransportUpdate.MAX_SIZE);
|
||||
String value = r.readString(TransportUpdate.MAX_SIZE);
|
||||
properties.put(key, value);
|
||||
}
|
||||
r.readMapEnd();
|
||||
outer.put(name, inner);
|
||||
transports.put(name, properties);
|
||||
}
|
||||
r.readMapEnd();
|
||||
long timestamp = r.readInt64();
|
||||
r.removeConsumer(counting);
|
||||
// Build and return the transports update
|
||||
return transportFactory.createTransports(outer, timestamp);
|
||||
// Build and return the transport update
|
||||
return transportFactory.createTransports(transports, timestamp);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,9 +2,9 @@ package net.sf.briar.protocol;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import net.sf.briar.api.protocol.Transports;
|
||||
import net.sf.briar.api.protocol.TransportUpdate;
|
||||
|
||||
class TransportsImpl implements Transports {
|
||||
class TransportsImpl implements TransportUpdate {
|
||||
|
||||
private final Map<String, Map<String, String>> transports;
|
||||
private final long timestamp;
|
||||
|
||||
Reference in New Issue
Block a user