mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-12 18:59:06 +01:00
Request reader and unit test.
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -7,5 +7,5 @@ import net.sf.briar.api.protocol.BatchId;
|
||||
|
||||
interface AckFactory {
|
||||
|
||||
Ack createAck(Collection<BatchId> batches);
|
||||
Ack createAck(Collection<BatchId> acked);
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,5 +7,5 @@ import net.sf.briar.api.protocol.Offer;
|
||||
|
||||
interface OfferFactory {
|
||||
|
||||
Offer createOffer(Collection<MessageId> messages);
|
||||
Offer createOffer(Collection<MessageId> offered);
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
10
components/net/sf/briar/protocol/RequestFactory.java
Normal file
10
components/net/sf/briar/protocol/RequestFactory.java
Normal 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);
|
||||
}
|
||||
12
components/net/sf/briar/protocol/RequestFactoryImpl.java
Normal file
12
components/net/sf/briar/protocol/RequestFactoryImpl.java
Normal 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);
|
||||
}
|
||||
}
|
||||
18
components/net/sf/briar/protocol/RequestImpl.java
Normal file
18
components/net/sf/briar/protocol/RequestImpl.java
Normal 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;
|
||||
}
|
||||
}
|
||||
41
components/net/sf/briar/protocol/RequestReader.java
Normal file
41
components/net/sf/briar/protocol/RequestReader.java
Normal 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user