Plugin manager must add transports to the DB before activating them.

This commit is contained in:
akwizgran
2013-02-12 16:04:22 +00:00
parent d3a8dae583
commit d18fc1330b
6 changed files with 43 additions and 15 deletions

View File

@@ -69,8 +69,11 @@ public interface DatabaseComponent {
*/
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

View File

@@ -140,11 +140,12 @@ interface Database<T> {
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>
* 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.

View File

@@ -349,7 +349,7 @@ DatabaseCleaner.Callback {
public void addLocalPrivateMessage(Message m, ContactId c)
throws DbException {
boolean added = false;
boolean added;
contactLock.readLock().lock();
try {
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();
try {
T txn = db.startTransaction();
try {
db.addTransport(txn, t);
added = db.addTransport(txn, t);
db.commitTransaction(txn);
} catch(DbException e) {
db.abortTransaction(txn);
@@ -423,7 +424,8 @@ DatabaseCleaner.Callback {
} finally {
transportLock.writeLock().unlock();
}
callListeners(new TransportAddedEvent(t));
if(added) callListeners(new TransportAddedEvent(t));
return added;
}
/**
@@ -1845,8 +1847,4 @@ DatabaseCleaner.Callback {
}
return false;
}
public void rotateKeys() throws DbException {
}
}

View File

@@ -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;
ResultSet rs = null;
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
String sql = "INSERT INTO transports (transportId) VALUES (?)";
sql = "INSERT INTO transports (transportId) VALUES (?)";
ps = txn.prepareStatement(sql);
ps.setBytes(1, t.getBytes());
int affected = ps.executeUpdate();
@@ -807,7 +818,7 @@ abstract class JdbcDatabase implements Database<Connection> {
while(rs.next()) contacts.add(rs.getInt(1));
rs.close();
ps.close();
if(contacts.isEmpty()) return;
if(contacts.isEmpty()) return true;
sql = "INSERT INTO transportVersions (contactId, transportId,"
+ " localVersion, localAcked, expiry, txCount)"
+ " VALUES (?, ?, ?, ZERO(), ZERO(), ZERO())";
@@ -824,6 +835,7 @@ abstract class JdbcDatabase implements Database<Connection> {
for(int i = 0; i < batchAffected.length; i++) {
if(batchAffected[i] != 1) throw new DbStateException();
}
return true;
} catch(SQLException e) {
tryToClose(ps);
tryToClose(rs);

View File

@@ -88,6 +88,12 @@ class PluginManagerImpl implements PluginManager {
LOG.warning("Duplicate transport ID: " + id);
continue;
}
try {
db.addTransport(id);
} catch(DbException e) {
if(LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
continue;
}
SimplexCallback callback = new SimplexCallback(id);
SimplexPlugin plugin = factory.createPlugin(callback);
if(plugin == null) {
@@ -118,6 +124,12 @@ class PluginManagerImpl implements PluginManager {
LOG.warning("Duplicate transport ID: " + id);
continue;
}
try {
db.addTransport(id);
} catch(DbException e) {
if(LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
continue;
}
DuplexCallback callback = new DuplexCallback(id);
DuplexPlugin plugin = factory.createPlugin(callback);
if(plugin == null) {

View File

@@ -54,6 +54,8 @@ public class PluginManagerImplTest extends BriarTestCase {
will(returnValue(Arrays.asList(removableDrive)));
oneOf(duplexPluginConfig).getFactories();
will(returnValue(Arrays.asList(lanTcp)));
exactly(2).of(db).addTransport(with(any(TransportId.class)));
will(returnValue(true));
oneOf(poller).start(with(any(Collection.class)));
allowing(db).getConfig(with(any(TransportId.class)));
will(returnValue(new TransportConfig()));