mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-16 04:39:54 +01:00
Added a lock for the connectionWindows table and exposed
getConnectionWindow() and setConnectionWindow() through the DatabaseComponent interface.
This commit is contained in:
@@ -31,6 +31,7 @@ import net.sf.briar.api.transport.ConnectionWindow;
|
||||
* <li> ratings
|
||||
* <li> subscriptions
|
||||
* <li> transports
|
||||
* <li> windows
|
||||
* </ul>
|
||||
*/
|
||||
interface Database<T> {
|
||||
@@ -159,8 +160,10 @@ interface Database<T> {
|
||||
/**
|
||||
* Returns the connection reordering window for the given contact and
|
||||
* transport.
|
||||
* <p>
|
||||
* Locking: contacts read, windows read.
|
||||
*/
|
||||
ConnectionWindow getConnectionWindow(T txn, ContactId c, int transport)
|
||||
ConnectionWindow getConnectionWindow(T txn, ContactId c, int transportId)
|
||||
throws DbException;
|
||||
|
||||
/**
|
||||
@@ -382,8 +385,10 @@ interface Database<T> {
|
||||
/**
|
||||
* Sets the connection reordering window for the given contact and
|
||||
* transport.
|
||||
* <p>
|
||||
* Locking: contacts read, windows write.
|
||||
*/
|
||||
void setConnectionWindow(T txn, ContactId c, int transport,
|
||||
void setConnectionWindow(T txn, ContactId c, int transportId,
|
||||
ConnectionWindow w) throws DbException;
|
||||
|
||||
/**
|
||||
|
||||
@@ -734,7 +734,7 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
}
|
||||
|
||||
public ConnectionWindow getConnectionWindow(Connection txn, ContactId c,
|
||||
int transport) throws DbException {
|
||||
int transportId) throws DbException {
|
||||
PreparedStatement ps = null;
|
||||
ResultSet rs = null;
|
||||
try {
|
||||
@@ -742,7 +742,7 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
+ " WHERE contactId = ? AND transportId = ?";
|
||||
ps = txn.prepareStatement(sql);
|
||||
ps.setInt(1, c.getInt());
|
||||
ps.setInt(2, transport);
|
||||
ps.setInt(2, transportId);
|
||||
rs = ps.executeQuery();
|
||||
long centre = 0L;
|
||||
int bitmap = 0;
|
||||
@@ -1528,8 +1528,8 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
}
|
||||
}
|
||||
|
||||
public void setConnectionWindow(Connection txn, ContactId c, int transport,
|
||||
ConnectionWindow w) throws DbException {
|
||||
public void setConnectionWindow(Connection txn, ContactId c,
|
||||
int transportId, ConnectionWindow w) throws DbException {
|
||||
PreparedStatement ps = null;
|
||||
ResultSet rs = null;
|
||||
try {
|
||||
@@ -1537,7 +1537,7 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
+ " WHERE contactId = ? AND transportId = ?";
|
||||
ps = txn.prepareStatement(sql);
|
||||
ps.setInt(1, c.getInt());
|
||||
ps.setInt(2, transport);
|
||||
ps.setInt(2, transportId);
|
||||
rs = ps.executeQuery();
|
||||
if(rs.next()) {
|
||||
if(rs.next()) throw new DbStateException();
|
||||
@@ -1556,7 +1556,7 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
+ " VALUES(?, ?, ?, ?)";
|
||||
ps = txn.prepareStatement(sql);
|
||||
ps.setInt(1, c.getInt());
|
||||
ps.setInt(2, transport);
|
||||
ps.setInt(2, transportId);
|
||||
ps.setLong(3, w.getCentre());
|
||||
ps.setInt(4, w.getBitmap());
|
||||
int affected = ps.executeUpdate();
|
||||
|
||||
@@ -32,6 +32,7 @@ import net.sf.briar.api.protocol.writers.OfferWriter;
|
||||
import net.sf.briar.api.protocol.writers.RequestWriter;
|
||||
import net.sf.briar.api.protocol.writers.SubscriptionWriter;
|
||||
import net.sf.briar.api.protocol.writers.TransportWriter;
|
||||
import net.sf.briar.api.transport.ConnectionWindow;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
|
||||
@@ -61,6 +62,8 @@ class ReadWriteLockDatabaseComponent<Txn> extends DatabaseComponentImpl<Txn> {
|
||||
new ReentrantReadWriteLock(true);
|
||||
private final ReentrantReadWriteLock transportLock =
|
||||
new ReentrantReadWriteLock(true);
|
||||
private final ReentrantReadWriteLock windowLock =
|
||||
new ReentrantReadWriteLock(true);
|
||||
|
||||
@Inject
|
||||
ReadWriteLockDatabaseComponent(Database<Txn> db, DatabaseCleaner cleaner) {
|
||||
@@ -488,6 +491,31 @@ class ReadWriteLockDatabaseComponent<Txn> extends DatabaseComponentImpl<Txn> {
|
||||
}
|
||||
}
|
||||
|
||||
public ConnectionWindow getConnectionWindow(ContactId c, int transportId)
|
||||
throws DbException {
|
||||
contactLock.readLock().lock();
|
||||
try {
|
||||
if(!containsContact(c)) throw new NoSuchContactException();
|
||||
windowLock.readLock().lock();
|
||||
try {
|
||||
Txn txn = db.startTransaction();
|
||||
try {
|
||||
ConnectionWindow w =
|
||||
db.getConnectionWindow(txn, c, transportId);
|
||||
db.commitTransaction(txn);
|
||||
return w;
|
||||
} catch(DbException e) {
|
||||
db.abortTransaction(txn);
|
||||
throw e;
|
||||
}
|
||||
} finally {
|
||||
windowLock.readLock().unlock();
|
||||
}
|
||||
} finally {
|
||||
contactLock.readLock().unlock();
|
||||
}
|
||||
}
|
||||
|
||||
public Collection<ContactId> getContacts() throws DbException {
|
||||
contactLock.readLock().lock();
|
||||
try {
|
||||
@@ -869,6 +897,28 @@ class ReadWriteLockDatabaseComponent<Txn> extends DatabaseComponentImpl<Txn> {
|
||||
}
|
||||
}
|
||||
|
||||
public void setConnectionWindow(ContactId c, int transportId,
|
||||
ConnectionWindow w) throws DbException {
|
||||
contactLock.readLock().lock();
|
||||
try {
|
||||
if(!containsContact(c)) throw new NoSuchContactException();
|
||||
windowLock.writeLock().lock();
|
||||
try {
|
||||
Txn txn = db.startTransaction();
|
||||
try {
|
||||
db.setConnectionWindow(txn, c, transportId, w);
|
||||
db.commitTransaction(txn);
|
||||
} catch(DbException e) {
|
||||
db.abortTransaction(txn);
|
||||
}
|
||||
} finally {
|
||||
windowLock.writeLock().unlock();
|
||||
}
|
||||
} finally {
|
||||
contactLock.readLock().unlock();
|
||||
}
|
||||
}
|
||||
|
||||
public void setRating(AuthorId a, Rating r) throws DbException {
|
||||
messageLock.writeLock().lock();
|
||||
try {
|
||||
|
||||
@@ -31,6 +31,7 @@ import net.sf.briar.api.protocol.writers.OfferWriter;
|
||||
import net.sf.briar.api.protocol.writers.RequestWriter;
|
||||
import net.sf.briar.api.protocol.writers.SubscriptionWriter;
|
||||
import net.sf.briar.api.protocol.writers.TransportWriter;
|
||||
import net.sf.briar.api.transport.ConnectionWindow;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
|
||||
@@ -54,6 +55,7 @@ class SynchronizedDatabaseComponent<Txn> extends DatabaseComponentImpl<Txn> {
|
||||
private final Object ratingLock = new Object();
|
||||
private final Object subscriptionLock = new Object();
|
||||
private final Object transportLock = new Object();
|
||||
private final Object windowLock = new Object();
|
||||
|
||||
@Inject
|
||||
SynchronizedDatabaseComponent(Database<Txn> db, DatabaseCleaner cleaner) {
|
||||
@@ -373,6 +375,25 @@ class SynchronizedDatabaseComponent<Txn> extends DatabaseComponentImpl<Txn> {
|
||||
}
|
||||
}
|
||||
|
||||
public ConnectionWindow getConnectionWindow(ContactId c, int transportId)
|
||||
throws DbException {
|
||||
synchronized(contactLock) {
|
||||
if(!containsContact(c)) throw new NoSuchContactException();
|
||||
synchronized(windowLock) {
|
||||
Txn txn = db.startTransaction();
|
||||
try {
|
||||
ConnectionWindow w =
|
||||
db.getConnectionWindow(txn, c, transportId);
|
||||
db.commitTransaction(txn);
|
||||
return w;
|
||||
} catch(DbException e) {
|
||||
db.abortTransaction(txn);
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Collection<ContactId> getContacts() throws DbException {
|
||||
synchronized(contactLock) {
|
||||
Txn txn = db.startTransaction();
|
||||
@@ -659,6 +680,22 @@ class SynchronizedDatabaseComponent<Txn> extends DatabaseComponentImpl<Txn> {
|
||||
}
|
||||
}
|
||||
|
||||
public void setConnectionWindow(ContactId c, int transportId,
|
||||
ConnectionWindow w) throws DbException {
|
||||
synchronized(contactLock) {
|
||||
if(!containsContact(c)) throw new NoSuchContactException();
|
||||
synchronized(windowLock) {
|
||||
Txn txn = db.startTransaction();
|
||||
try {
|
||||
db.setConnectionWindow(txn, c, transportId, w);
|
||||
db.commitTransaction(txn);
|
||||
} catch(DbException e) {
|
||||
db.abortTransaction(txn);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void setRating(AuthorId a, Rating r) throws DbException {
|
||||
synchronized(messageLock) {
|
||||
synchronized(ratingLock) {
|
||||
|
||||
@@ -20,16 +20,15 @@ class InvitationWorker implements Runnable {
|
||||
|
||||
private final InvitationCallback callback;
|
||||
private final InvitationParameters parameters;
|
||||
private final DatabaseComponent databaseComponent;
|
||||
private final DatabaseComponent db;
|
||||
private final WriterFactory writerFactory;
|
||||
|
||||
InvitationWorker(final InvitationCallback callback,
|
||||
InvitationParameters parameters,
|
||||
DatabaseComponent databaseComponent,
|
||||
InvitationParameters parameters, DatabaseComponent db,
|
||||
WriterFactory writerFactory) {
|
||||
this.callback = callback;
|
||||
this.parameters = parameters;
|
||||
this.databaseComponent = databaseComponent;
|
||||
this.db = db;
|
||||
this.writerFactory = writerFactory;
|
||||
}
|
||||
|
||||
@@ -73,7 +72,7 @@ class InvitationWorker implements Runnable {
|
||||
// FIXME: Create a real invitation
|
||||
Map<String, Map<String, String>> transports;
|
||||
try {
|
||||
transports = databaseComponent.getTransports();
|
||||
transports = db.getTransports();
|
||||
} catch(DbException e) {
|
||||
throw new IOException(e.getMessage());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user