Request reader and unit test.

This commit is contained in:
akwizgran
2011-07-27 11:06:54 +01:00
parent 0933092295
commit b161e5ed1d
20 changed files with 305 additions and 48 deletions

View File

@@ -597,7 +597,7 @@ class ReadWriteLockDatabaseComponent<Txn> extends DatabaseComponentImpl<Txn> {
try {
messageStatusLock.writeLock().lock();
try {
Collection<BatchId> acks = a.getBatches();
Collection<BatchId> acks = a.getBatchIds();
for(BatchId ack : acks) {
Txn txn = db.startTransaction();
try {
@@ -676,7 +676,7 @@ class ReadWriteLockDatabaseComponent<Txn> extends DatabaseComponentImpl<Txn> {
try {
subscriptionLock.readLock().lock();
try {
Collection<MessageId> offered = o.getMessages();
Collection<MessageId> offered = o.getMessageIds();
BitSet request = new BitSet(offered.size());
Txn txn = db.startTransaction();
try {

View File

@@ -440,7 +440,7 @@ class SynchronizedDatabaseComponent<Txn> extends DatabaseComponentImpl<Txn> {
if(!containsContact(c)) throw new NoSuchContactException();
synchronized(messageLock) {
synchronized(messageStatusLock) {
Collection<BatchId> acks = a.getBatches();
Collection<BatchId> acks = a.getBatchIds();
for(BatchId ack : acks) {
Txn txn = db.startTransaction();
try {
@@ -497,7 +497,7 @@ class SynchronizedDatabaseComponent<Txn> extends DatabaseComponentImpl<Txn> {
synchronized(messageLock) {
synchronized(messageStatusLock) {
synchronized(subscriptionLock) {
Collection<MessageId> offered = o.getMessages();
Collection<MessageId> offered = o.getMessageIds();
BitSet request = new BitSet(offered.size());
Txn txn = db.startTransaction();
try {

View File

@@ -7,5 +7,5 @@ import net.sf.briar.api.protocol.BatchId;
interface AckFactory {
Ack createAck(Collection<BatchId> batches);
Ack createAck(Collection<BatchId> acked);
}

View File

@@ -7,7 +7,7 @@ import net.sf.briar.api.protocol.BatchId;
class AckFactoryImpl implements AckFactory {
public Ack createAck(Collection<BatchId> batches) {
return new AckImpl(batches);
public Ack createAck(Collection<BatchId> acked) {
return new AckImpl(acked);
}
}

View File

@@ -7,13 +7,13 @@ import net.sf.briar.api.protocol.BatchId;
class AckImpl implements Ack {
private final Collection<BatchId> batches;
private final Collection<BatchId> acked;
AckImpl(Collection<BatchId> batches) {
this.batches = batches;
AckImpl(Collection<BatchId> acked) {
this.acked = acked;
}
public Collection<BatchId> getBatches() {
return batches;
public Collection<BatchId> getBatchIds() {
return acked;
}
}

View File

@@ -7,5 +7,5 @@ import net.sf.briar.api.protocol.Offer;
interface OfferFactory {
Offer createOffer(Collection<MessageId> messages);
Offer createOffer(Collection<MessageId> offered);
}

View File

@@ -7,7 +7,7 @@ import net.sf.briar.api.protocol.Offer;
class OfferFactoryImpl implements OfferFactory {
public Offer createOffer(Collection<MessageId> messages) {
return new OfferImpl(messages);
public Offer createOffer(Collection<MessageId> offered) {
return new OfferImpl(offered);
}
}

View File

@@ -7,13 +7,13 @@ import net.sf.briar.api.protocol.Offer;
class OfferImpl implements Offer {
private final Collection<MessageId> messages;
private final Collection<MessageId> offered;
OfferImpl(Collection<MessageId> messages) {
this.messages = messages;
OfferImpl(Collection<MessageId> offered) {
this.offered = offered;
}
public Collection<MessageId> getMessages() {
return messages;
public Collection<MessageId> getMessageIds() {
return offered;
}
}

View File

@@ -23,19 +23,21 @@ public class ProtocolModule extends AbstractModule {
bind(BatchFactory.class).to(BatchFactoryImpl.class);
bind(GroupFactory.class).to(GroupFactoryImpl.class);
bind(OfferFactory.class).to(OfferFactoryImpl.class);
bind(RequestFactory.class).to(RequestFactoryImpl.class);
bind(SubscriptionFactory.class).to(SubscriptionFactoryImpl.class);
bind(TransportFactory.class).to(TransportFactoryImpl.class);
bind(MessageEncoder.class).to(MessageEncoderImpl.class);
}
@Provides
ObjectReader<BatchId> getBatchIdReader() {
return new BatchIdReader();
ObjectReader<Author> getAuthorReader(CryptoComponent crypto,
AuthorFactory authorFactory) {
return new AuthorReader(crypto, authorFactory);
}
@Provides
ObjectReader<MessageId> getMessageIdReader() {
return new MessageIdReader();
ObjectReader<BatchId> getBatchIdReader() {
return new BatchIdReader();
}
@Provides
@@ -45,9 +47,8 @@ public class ProtocolModule extends AbstractModule {
}
@Provides
ObjectReader<Author> getAuthorReader(CryptoComponent crypto,
AuthorFactory authorFactory) {
return new AuthorReader(crypto, authorFactory);
ObjectReader<MessageId> getMessageIdReader() {
return new MessageIdReader();
}
@Provides

View File

@@ -0,0 +1,10 @@
package net.sf.briar.protocol;
import java.util.BitSet;
import net.sf.briar.api.protocol.Request;
interface RequestFactory {
Request createRequest(BitSet requested);
}

View File

@@ -0,0 +1,12 @@
package net.sf.briar.protocol;
import java.util.BitSet;
import net.sf.briar.api.protocol.Request;
class RequestFactoryImpl implements RequestFactory {
public Request createRequest(BitSet requested) {
return new RequestImpl(requested);
}
}

View File

@@ -0,0 +1,18 @@
package net.sf.briar.protocol;
import java.util.BitSet;
import net.sf.briar.api.protocol.Request;
class RequestImpl implements Request {
private final BitSet requested;
RequestImpl(BitSet requested) {
this.requested = requested;
}
public BitSet getBitmap() {
return requested;
}
}

View File

@@ -0,0 +1,41 @@
package net.sf.briar.protocol;
import java.io.IOException;
import java.util.BitSet;
import net.sf.briar.api.protocol.Request;
import net.sf.briar.api.protocol.Tags;
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 RequestReader implements ObjectReader<Request> {
private final RequestFactory requestFactory;
@Inject
RequestReader(RequestFactory requestFactory) {
this.requestFactory = requestFactory;
}
public Request readObject(Reader r) throws IOException {
// Initialise the consumer
Consumer counting = new CountingConsumer(Request.MAX_SIZE);
// Read the data
r.addConsumer(counting);
r.readUserDefinedTag(Tags.REQUEST);
byte[] bitmap = r.readBytes();
r.removeConsumer(counting);
// Convert the bitmap into a BitSet
BitSet b = new BitSet(bitmap.length * 8);
for(int i = 0; i < bitmap.length; i++) {
for(int j = 0; j < 8; j++) {
byte bit = (byte) (128 >> j);
if((bitmap[i] & bit) != 0) b.set(i * 8 + j);
}
}
return requestFactory.createRequest(b);
}
}