Builders for batches and bundles.

This commit is contained in:
akwizgran
2011-07-11 12:25:04 +01:00
parent 51e371f7ca
commit 4f5eb21180
13 changed files with 162 additions and 119 deletions

View File

@@ -15,8 +15,10 @@ import net.sf.briar.api.db.DbException;
import net.sf.briar.api.db.NoSuchContactException;
import net.sf.briar.api.protocol.AuthorId;
import net.sf.briar.api.protocol.Batch;
import net.sf.briar.api.protocol.BatchBuilder;
import net.sf.briar.api.protocol.BatchId;
import net.sf.briar.api.protocol.Bundle;
import net.sf.briar.api.protocol.BundleBuilder;
import net.sf.briar.api.protocol.GroupId;
import net.sf.briar.api.protocol.Message;
import net.sf.briar.api.protocol.MessageId;
@@ -53,8 +55,8 @@ class ReadWriteLockDatabaseComponent<Txn> extends DatabaseComponentImpl<Txn> {
@Inject
ReadWriteLockDatabaseComponent(Database<Txn> db, DatabaseCleaner cleaner,
Provider<Batch> batchProvider) {
super(db, cleaner, batchProvider);
Provider<BatchBuilder> batchBuilderProvider) {
super(db, cleaner, batchBuilderProvider);
}
public void close() throws DbException {
@@ -184,7 +186,8 @@ class ReadWriteLockDatabaseComponent<Txn> extends DatabaseComponentImpl<Txn> {
}
}
public void generateBundle(ContactId c, Bundle b) throws DbException {
public Bundle generateBundle(ContactId c, BundleBuilder b)
throws DbException {
if(LOG.isLoggable(Level.FINE)) LOG.fine("Generating bundle for " + c);
// Ack all batches received from c
contactLock.readLock().lock();
@@ -277,10 +280,11 @@ class ReadWriteLockDatabaseComponent<Txn> extends DatabaseComponentImpl<Txn> {
// more messages trickling in but we can't wait forever
if(size * 2 < Batch.CAPACITY) break;
}
b.seal();
Bundle bundle = b.build();
if(LOG.isLoggable(Level.FINE))
LOG.fine("Bundle sent, " + b.getSize() + " bytes");
LOG.fine("Bundle generated, " + bundle.getSize() + " bytes");
System.gc();
return bundle;
}
private Batch fillBatch(ContactId c, long capacity) throws DbException {
@@ -290,7 +294,8 @@ class ReadWriteLockDatabaseComponent<Txn> extends DatabaseComponentImpl<Txn> {
messageLock.readLock().lock();
try {
Set<MessageId> sent;
Batch b;
BatchBuilder b;
Batch batch;
messageStatusLock.readLock().lock();
try {
Txn txn = db.startTransaction();
@@ -303,13 +308,13 @@ class ReadWriteLockDatabaseComponent<Txn> extends DatabaseComponentImpl<Txn> {
return null; // No more messages to send
}
sent = new HashSet<MessageId>();
b = batchProvider.get();
b = batchBuilderProvider.get();
while(it.hasNext()) {
MessageId m = it.next();
b.addMessage(db.getMessage(txn, m));
sent.add(m);
}
b.seal();
batch = b.build();
db.commitTransaction(txn);
} catch(DbException e) {
db.abortTransaction(txn);
@@ -324,9 +329,9 @@ class ReadWriteLockDatabaseComponent<Txn> extends DatabaseComponentImpl<Txn> {
Txn txn = db.startTransaction();
try {
assert !sent.isEmpty();
db.addOutstandingBatch(txn, c, b.getId(), sent);
db.addOutstandingBatch(txn, c, batch.getId(), sent);
db.commitTransaction(txn);
return b;
return batch;
} catch(DbException e) {
db.abortTransaction(txn);
throw e;