mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-16 12:49:55 +01:00
Plugin manager must add transports to the DB before activating them.
This commit is contained in:
@@ -69,8 +69,11 @@ public interface DatabaseComponent {
|
|||||||
*/
|
*/
|
||||||
void addSecrets(Collection<TemporarySecret> secrets) throws DbException;
|
void addSecrets(Collection<TemporarySecret> secrets) throws DbException;
|
||||||
|
|
||||||
/** Adds a transport to the database. */
|
/**
|
||||||
void addTransport(TransportId t) throws DbException;
|
* Adds a transport to the database and returns true if the transport was
|
||||||
|
* not previously in the database.
|
||||||
|
*/
|
||||||
|
boolean addTransport(TransportId t) throws DbException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generates an acknowledgement for the given contact. Returns null if
|
* Generates an acknowledgement for the given contact. Returns null if
|
||||||
|
|||||||
@@ -140,11 +140,12 @@ interface Database<T> {
|
|||||||
boolean addSubscription(T txn, Group g) throws DbException;
|
boolean addSubscription(T txn, Group g) throws DbException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds a new transport to the database.
|
* Adds a new transport to the database and returns true if the transport
|
||||||
|
* was not previously in the database.
|
||||||
* <p>
|
* <p>
|
||||||
* Locking: transport write.
|
* Locking: transport write.
|
||||||
*/
|
*/
|
||||||
void addTransport(T txn, TransportId t) throws DbException;
|
boolean addTransport(T txn, TransportId t) throws DbException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Makes the given group visible to the given contact.
|
* Makes the given group visible to the given contact.
|
||||||
|
|||||||
@@ -349,7 +349,7 @@ DatabaseCleaner.Callback {
|
|||||||
|
|
||||||
public void addLocalPrivateMessage(Message m, ContactId c)
|
public void addLocalPrivateMessage(Message m, ContactId c)
|
||||||
throws DbException {
|
throws DbException {
|
||||||
boolean added = false;
|
boolean added;
|
||||||
contactLock.readLock().lock();
|
contactLock.readLock().lock();
|
||||||
try {
|
try {
|
||||||
messageLock.writeLock().lock();
|
messageLock.writeLock().lock();
|
||||||
@@ -409,12 +409,13 @@ DatabaseCleaner.Callback {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addTransport(TransportId t) throws DbException {
|
public boolean addTransport(TransportId t) throws DbException {
|
||||||
|
boolean added;
|
||||||
transportLock.writeLock().lock();
|
transportLock.writeLock().lock();
|
||||||
try {
|
try {
|
||||||
T txn = db.startTransaction();
|
T txn = db.startTransaction();
|
||||||
try {
|
try {
|
||||||
db.addTransport(txn, t);
|
added = db.addTransport(txn, t);
|
||||||
db.commitTransaction(txn);
|
db.commitTransaction(txn);
|
||||||
} catch(DbException e) {
|
} catch(DbException e) {
|
||||||
db.abortTransaction(txn);
|
db.abortTransaction(txn);
|
||||||
@@ -423,7 +424,8 @@ DatabaseCleaner.Callback {
|
|||||||
} finally {
|
} finally {
|
||||||
transportLock.writeLock().unlock();
|
transportLock.writeLock().unlock();
|
||||||
}
|
}
|
||||||
callListeners(new TransportAddedEvent(t));
|
if(added) callListeners(new TransportAddedEvent(t));
|
||||||
|
return added;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -1845,8 +1847,4 @@ DatabaseCleaner.Callback {
|
|||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void rotateKeys() throws DbException {
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -788,12 +788,23 @@ abstract class JdbcDatabase implements Database<Connection> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addTransport(Connection txn, TransportId t) throws DbException {
|
public boolean addTransport(Connection txn, TransportId t)
|
||||||
|
throws DbException {
|
||||||
PreparedStatement ps = null;
|
PreparedStatement ps = null;
|
||||||
ResultSet rs = null;
|
ResultSet rs = null;
|
||||||
try {
|
try {
|
||||||
|
// Return false if the transport is already in the database
|
||||||
|
String sql = "SELECT NULL FROM transports WHERE transportId = ?";
|
||||||
|
ps = txn.prepareStatement(sql);
|
||||||
|
ps.setBytes(1, t.getBytes());
|
||||||
|
rs = ps.executeQuery();
|
||||||
|
boolean found = rs.next();
|
||||||
|
if(rs.next()) throw new DbStateException();
|
||||||
|
rs.close();
|
||||||
|
ps.close();
|
||||||
|
if(found) return false;
|
||||||
// Create a transport row
|
// Create a transport row
|
||||||
String sql = "INSERT INTO transports (transportId) VALUES (?)";
|
sql = "INSERT INTO transports (transportId) VALUES (?)";
|
||||||
ps = txn.prepareStatement(sql);
|
ps = txn.prepareStatement(sql);
|
||||||
ps.setBytes(1, t.getBytes());
|
ps.setBytes(1, t.getBytes());
|
||||||
int affected = ps.executeUpdate();
|
int affected = ps.executeUpdate();
|
||||||
@@ -807,7 +818,7 @@ abstract class JdbcDatabase implements Database<Connection> {
|
|||||||
while(rs.next()) contacts.add(rs.getInt(1));
|
while(rs.next()) contacts.add(rs.getInt(1));
|
||||||
rs.close();
|
rs.close();
|
||||||
ps.close();
|
ps.close();
|
||||||
if(contacts.isEmpty()) return;
|
if(contacts.isEmpty()) return true;
|
||||||
sql = "INSERT INTO transportVersions (contactId, transportId,"
|
sql = "INSERT INTO transportVersions (contactId, transportId,"
|
||||||
+ " localVersion, localAcked, expiry, txCount)"
|
+ " localVersion, localAcked, expiry, txCount)"
|
||||||
+ " VALUES (?, ?, ?, ZERO(), ZERO(), ZERO())";
|
+ " VALUES (?, ?, ?, ZERO(), ZERO(), ZERO())";
|
||||||
@@ -824,6 +835,7 @@ abstract class JdbcDatabase implements Database<Connection> {
|
|||||||
for(int i = 0; i < batchAffected.length; i++) {
|
for(int i = 0; i < batchAffected.length; i++) {
|
||||||
if(batchAffected[i] != 1) throw new DbStateException();
|
if(batchAffected[i] != 1) throw new DbStateException();
|
||||||
}
|
}
|
||||||
|
return true;
|
||||||
} catch(SQLException e) {
|
} catch(SQLException e) {
|
||||||
tryToClose(ps);
|
tryToClose(ps);
|
||||||
tryToClose(rs);
|
tryToClose(rs);
|
||||||
|
|||||||
@@ -88,6 +88,12 @@ class PluginManagerImpl implements PluginManager {
|
|||||||
LOG.warning("Duplicate transport ID: " + id);
|
LOG.warning("Duplicate transport ID: " + id);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
try {
|
||||||
|
db.addTransport(id);
|
||||||
|
} catch(DbException e) {
|
||||||
|
if(LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
SimplexCallback callback = new SimplexCallback(id);
|
SimplexCallback callback = new SimplexCallback(id);
|
||||||
SimplexPlugin plugin = factory.createPlugin(callback);
|
SimplexPlugin plugin = factory.createPlugin(callback);
|
||||||
if(plugin == null) {
|
if(plugin == null) {
|
||||||
@@ -118,6 +124,12 @@ class PluginManagerImpl implements PluginManager {
|
|||||||
LOG.warning("Duplicate transport ID: " + id);
|
LOG.warning("Duplicate transport ID: " + id);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
try {
|
||||||
|
db.addTransport(id);
|
||||||
|
} catch(DbException e) {
|
||||||
|
if(LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
DuplexCallback callback = new DuplexCallback(id);
|
DuplexCallback callback = new DuplexCallback(id);
|
||||||
DuplexPlugin plugin = factory.createPlugin(callback);
|
DuplexPlugin plugin = factory.createPlugin(callback);
|
||||||
if(plugin == null) {
|
if(plugin == null) {
|
||||||
|
|||||||
@@ -54,6 +54,8 @@ public class PluginManagerImplTest extends BriarTestCase {
|
|||||||
will(returnValue(Arrays.asList(removableDrive)));
|
will(returnValue(Arrays.asList(removableDrive)));
|
||||||
oneOf(duplexPluginConfig).getFactories();
|
oneOf(duplexPluginConfig).getFactories();
|
||||||
will(returnValue(Arrays.asList(lanTcp)));
|
will(returnValue(Arrays.asList(lanTcp)));
|
||||||
|
exactly(2).of(db).addTransport(with(any(TransportId.class)));
|
||||||
|
will(returnValue(true));
|
||||||
oneOf(poller).start(with(any(Collection.class)));
|
oneOf(poller).start(with(any(Collection.class)));
|
||||||
allowing(db).getConfig(with(any(TransportId.class)));
|
allowing(db).getConfig(with(any(TransportId.class)));
|
||||||
will(returnValue(new TransportConfig()));
|
will(returnValue(new TransportConfig()));
|
||||||
|
|||||||
Reference in New Issue
Block a user