Decouple the database from IO.

This will enable asynchronous access to the database for IO threads.
This commit is contained in:
akwizgran
2011-12-07 00:23:35 +00:00
parent 45a51b4926
commit b7c3224618
73 changed files with 1120 additions and 1398 deletions

View File

@@ -17,17 +17,13 @@ import net.sf.briar.api.protocol.GroupId;
import net.sf.briar.api.protocol.Message;
import net.sf.briar.api.protocol.MessageId;
import net.sf.briar.api.protocol.Offer;
import net.sf.briar.api.protocol.RawBatch;
import net.sf.briar.api.protocol.Request;
import net.sf.briar.api.protocol.SubscriptionUpdate;
import net.sf.briar.api.protocol.Transport;
import net.sf.briar.api.protocol.TransportId;
import net.sf.briar.api.protocol.TransportIndex;
import net.sf.briar.api.protocol.TransportUpdate;
import net.sf.briar.api.protocol.writers.AckWriter;
import net.sf.briar.api.protocol.writers.BatchWriter;
import net.sf.briar.api.protocol.writers.OfferWriter;
import net.sf.briar.api.protocol.writers.RequestWriter;
import net.sf.briar.api.protocol.writers.SubscriptionUpdateWriter;
import net.sf.briar.api.protocol.writers.TransportUpdateWriter;
import net.sf.briar.api.transport.ConnectionContext;
import net.sf.briar.api.transport.ConnectionWindow;
@@ -72,43 +68,46 @@ public interface DatabaseComponent {
TransportIndex addTransport(TransportId t) throws DbException;
/**
* Generates an acknowledgement for the given contact.
* @return True if any batch IDs were added to the acknowledgement.
* Generates an acknowledgement for the given contact. Returns null if
* there are no batches to acknowledge.
*/
boolean generateAck(ContactId c, AckWriter a) throws DbException,
IOException;
Ack generateAck(ContactId c, int maxBatches) throws DbException;
/**
* Generates a batch of messages for the given contact.
* @return True if any messages were added to tbe batch.
* Generates a batch of messages for the given contact. Returns null if
* there are no sendable messages that fit in the given capacity.
*/
boolean generateBatch(ContactId c, BatchWriter b) throws DbException,
IOException;
RawBatch generateBatch(ContactId c, int capacity) throws DbException;
/**
* Generates a batch of messages for the given contact from the given
* collection of requested messages. Any messages that were either added to
* the batch, or were considered but are no longer sendable to the contact,
* are removed from the collection of requested messages before returning.
* @return True if any messages were added to the batch.
* Returns null if there are no sendable messages that fit in the given
* capacity.
*/
boolean generateBatch(ContactId c, BatchWriter b,
Collection<MessageId> requested) throws DbException, IOException;
RawBatch generateBatch(ContactId c, int capacity,
Collection<MessageId> requested) throws DbException;
/**
* Generates an offer for the given contact and returns the offered
* message IDs.
* Generates an offer for the given contact. Returns null if there are no
* messages to offer.
*/
Collection<MessageId> generateOffer(ContactId c, OfferWriter o)
throws DbException, IOException;
Offer generateOffer(ContactId c, int maxMessages) throws DbException;
/** Generates a subscription update for the given contact. */
void generateSubscriptionUpdate(ContactId c, SubscriptionUpdateWriter s)
throws DbException, IOException;
/**
* Generates a subscription update for the given contact. Returns null if
* an update is not due.
*/
SubscriptionUpdate generateSubscriptionUpdate(ContactId c)
throws DbException;
/** Generates a transport update for the given contact. */
void generateTransportUpdate(ContactId c, TransportUpdateWriter t)
throws DbException, IOException;
/**
* Generates a transport update for the given contact. Returns null if an
* update is not due.
*/
TransportUpdate generateTransportUpdate(ContactId c) throws DbException;
/** Returns the configuration for the given transport. */
TransportConfig getConfig(TransportId t) throws DbException;
@@ -185,8 +184,7 @@ public interface DatabaseComponent {
* to the contact are requested just as though they were not present in the
* database.
*/
void receiveOffer(ContactId c, Offer o, RequestWriter r) throws DbException,
IOException;
Request receiveOffer(ContactId c, Offer o) throws DbException;
/** Processes a subscription update from the given contact. */
void receiveSubscriptionUpdate(ContactId c, SubscriptionUpdate s)

View File

@@ -2,7 +2,7 @@ package net.sf.briar.api.protocol;
import java.util.Collection;
/** A packet containing messages. */
/** An incoming packet containing messages. */
public interface Batch {
/** Returns the batch's unique identifier. */

View File

@@ -23,9 +23,6 @@ public interface Message {
/** Returns the timestamp created by the message's author. */
long getTimestamp();
/** Returns the length of the serialised message in bytes. */
int getLength();
/** Returns the serialised message. */
byte[] getSerialised();

View File

@@ -0,0 +1,22 @@
package net.sf.briar.api.protocol;
import java.util.BitSet;
import java.util.Collection;
import java.util.Map;
public interface PacketFactory {
Ack createAck(Collection<BatchId> acked);
RawBatch createBatch(Collection<byte[]> messages);
Offer createOffer(Collection<MessageId> offered);
Request createRequest(BitSet requested, int length);
SubscriptionUpdate createSubscriptionUpdate(Map<Group, Long> subs,
long timestamp);
TransportUpdate createTransportUpdate(Collection<Transport> transports,
long timestamp);
}

View File

@@ -0,0 +1,24 @@
package net.sf.briar.api.protocol;
import java.io.IOException;
public interface ProtocolWriter {
int getMaxBatchesForAck(long capacity);
int getMaxMessagesForOffer(long capacity);
int getMessageCapacityForBatch(long capacity);
void writeAck(Ack a) throws IOException;
void writeBatch(RawBatch b) throws IOException;
void writeOffer(Offer o) throws IOException;
void writeRequest(Request r) throws IOException;
void writeSubscriptionUpdate(SubscriptionUpdate s) throws IOException;
void writeTransportUpdate(TransportUpdate t) throws IOException;
}

View File

@@ -0,0 +1,8 @@
package net.sf.briar.api.protocol;
import java.io.OutputStream;
public interface ProtocolWriterFactory {
ProtocolWriter createProtocolWriter(OutputStream out);
}

View File

@@ -0,0 +1,13 @@
package net.sf.briar.api.protocol;
import java.util.Collection;
/** An outgoing packet containing messages. */
public interface RawBatch {
/** Returns the batch's unique identifier. */
BatchId getId();
/** Returns the serialised messages contained in the batch. */
Collection<byte[]> getMessages();
}

View File

@@ -10,4 +10,7 @@ public interface Request {
* the offer, where the i^th bit is set if the i^th message should be sent.
*/
BitSet getBitmap();
/** Returns the length of the bitmap in bits. */
int getLength();
}

View File

@@ -3,6 +3,7 @@ package net.sf.briar.api.protocol;
/** Struct identifiers for encoding and decoding protocol objects. */
public interface Types {
// FIXME: Batch ID, message ID don't need to be structs
static final int ACK = 0;
static final int AUTHOR = 1;
static final int BATCH = 2;

View File

@@ -1,24 +0,0 @@
package net.sf.briar.api.protocol.writers;
import java.io.IOException;
import net.sf.briar.api.protocol.BatchId;
/** An interface for creating an ack packet. */
public interface AckWriter {
/**
* Sets the maximum length of the serialised ack. If this method is not
* called, the default is ProtocolConstants.MAX_PACKET_LENGTH;
*/
void setMaxPacketLength(int length);
/**
* Attempts to add the given BatchId to the ack and returns true if it
* was added.
*/
boolean writeBatchId(BatchId b) throws IOException;
/** Finishes writing the ack. */
void finish() throws IOException;
}

View File

@@ -1,27 +0,0 @@
package net.sf.briar.api.protocol.writers;
import java.io.IOException;
import net.sf.briar.api.protocol.BatchId;
/** An interface for creating a batch packet. */
public interface BatchWriter {
/** Returns the capacity of the batch in bytes. */
int getCapacity();
/**
* Sets the maximum length of the serialised batch; the default is
* ProtocolConstants.MAX_PACKET_LENGTH;
*/
void setMaxPacketLength(int length);
/**
* Attempts to add the given raw message to the batch and returns true if
* it was added.
*/
boolean writeMessage(byte[] raw) throws IOException;
/** Finishes writing the batch and returns its unique identifier. */
BatchId finish() throws IOException;
}

View File

@@ -1,24 +0,0 @@
package net.sf.briar.api.protocol.writers;
import java.io.IOException;
import net.sf.briar.api.protocol.MessageId;
/** An interface for creating an offer packet. */
public interface OfferWriter {
/**
* Sets the maximum length of the serialised offer. If this method is not
* called, the default is ProtocolConstants.MAX_PACKET_LENGTH;
*/
void setMaxPacketLength(int length);
/**
* Attempts to add the given message ID to the offer and returns true if it
* was added.
*/
boolean writeMessageId(MessageId m) throws IOException;
/** Finishes writing the offer. */
void finish() throws IOException;
}

View File

@@ -1,18 +0,0 @@
package net.sf.briar.api.protocol.writers;
import java.io.OutputStream;
public interface ProtocolWriterFactory {
AckWriter createAckWriter(OutputStream out);
BatchWriter createBatchWriter(OutputStream out);
OfferWriter createOfferWriter(OutputStream out);
RequestWriter createRequestWriter(OutputStream out);
SubscriptionUpdateWriter createSubscriptionUpdateWriter(OutputStream out);
TransportUpdateWriter createTransportUpdateWriter(OutputStream out);
}

View File

@@ -1,11 +0,0 @@
package net.sf.briar.api.protocol.writers;
import java.io.IOException;
import java.util.BitSet;
/** An interface for creating a request packet. */
public interface RequestWriter {
/** Writes the contents of the request. */
void writeRequest(BitSet b, int length) throws IOException;
}

View File

@@ -1,14 +0,0 @@
package net.sf.briar.api.protocol.writers;
import java.io.IOException;
import java.util.Map;
import net.sf.briar.api.protocol.Group;
/** An interface for creating a subscription update. */
public interface SubscriptionUpdateWriter {
/** Writes the contents of the update. */
void writeSubscriptions(Map<Group, Long> subs, long timestamp)
throws IOException;
}

View File

@@ -1,14 +0,0 @@
package net.sf.briar.api.protocol.writers;
import java.io.IOException;
import java.util.Collection;
import net.sf.briar.api.protocol.Transport;
/** An interface for creating a transport update. */
public interface TransportUpdateWriter {
/** Writes the contents of the update. */
void writeTransports(Collection<Transport> transports, long timestamp)
throws IOException;
}

View File

@@ -6,7 +6,7 @@ public interface SerialComponent {
int getSerialisedListStartLength();
int getSerialisedUniqueIdLength(int id);
int getSerialisedStructIdLength(int id);
int getSerialisedUniqueIdLength(int id);
}