Type-safe transport IDs.

This commit is contained in:
akwizgran
2011-09-30 12:52:29 +01:00
parent 7190509ede
commit 72b594d270
32 changed files with 292 additions and 188 deletions

View File

@@ -0,0 +1,47 @@
package net.sf.briar.api;
import java.io.IOException;
import net.sf.briar.api.serial.Writable;
import net.sf.briar.api.serial.Writer;
/**
* Type-safe wrapper for an integer that uniquely identifies a transport plugin.
*/
public class TransportId implements Writable, Comparable<TransportId> {
public static final int MIN_ID = 0;
public static final int MAX_ID = 65535;
private final int id;
public TransportId(int id) {
if(id < MIN_ID || id > MAX_ID) throw new IllegalArgumentException();
this.id = id;
}
public int getInt() {
return id;
}
public void writeTo(Writer w) throws IOException {
w.writeInt32(id);
}
@Override
public boolean equals(Object o) {
if(o instanceof TransportId) return id == ((TransportId) o).id;
return false;
}
@Override
public int hashCode() {
return id;
}
public int compareTo(TransportId t) {
if(id < t.id) return -1;
if(id > t.id) return 1;
return 0;
}
}

View File

@@ -6,6 +6,7 @@ import java.util.Map;
import net.sf.briar.api.ContactId;
import net.sf.briar.api.Rating;
import net.sf.briar.api.TransportId;
import net.sf.briar.api.protocol.Ack;
import net.sf.briar.api.protocol.AuthorId;
import net.sf.briar.api.protocol.Batch;
@@ -50,7 +51,7 @@ public interface DatabaseComponent {
* Adds a new contact to the database with the given transport properties
* and shared secret, returns an ID for the contact.
*/
ContactId addContact(Map<Integer, Map<String, String>> transports,
ContactId addContact(Map<TransportId, Map<String, String>> transports,
byte[] secret) throws DbException;
/** Adds a locally generated group message to the database. */
@@ -102,13 +103,13 @@ public interface DatabaseComponent {
* Returns an outgoing connection number for the given contact and
* transport.
*/
long getConnectionNumber(ContactId c, int transportId) throws DbException;
long getConnectionNumber(ContactId c, TransportId t) throws DbException;
/**
* Returns the connection reordering window for the given contact and
* transport.
*/
ConnectionWindow getConnectionWindow(ContactId c, int transportId)
ConnectionWindow getConnectionWindow(ContactId c, TransportId t)
throws DbException;
/** Returns the IDs of all contacts. */
@@ -124,13 +125,13 @@ public interface DatabaseComponent {
Collection<Group> getSubscriptions() throws DbException;
/** Returns the configuration for the given transport. */
Map<String, String> getTransportConfig(int transportId) throws DbException;
Map<String, String> getTransportConfig(TransportId t) throws DbException;
/** Returns all local transport properties. */
Map<Integer, Map<String, String>> getTransports() throws DbException;
Map<TransportId, Map<String, String>> getTransports() throws DbException;
/** Returns all transport properties for the given contact. */
Map<Integer, Map<String, String>> getTransports(ContactId c)
Map<TransportId, Map<String, String>> getTransports(ContactId c)
throws DbException;
/** Returns the contacts to which the given group is visible. */
@@ -171,7 +172,7 @@ public interface DatabaseComponent {
* Sets the connection reordering window for the given contact and
* transport.
*/
void setConnectionWindow(ContactId c, int transportId, ConnectionWindow w)
void setConnectionWindow(ContactId c, TransportId t, ConnectionWindow w)
throws DbException;
/** Records the user's rating for the given author. */
@@ -184,14 +185,14 @@ public interface DatabaseComponent {
* Sets the configuration for the given transport, replacing any existing
* configuration for that transport.
*/
void setTransportConfig(int transportId, Map<String, String> config)
void setTransportConfig(TransportId t, Map<String, String> config)
throws DbException;
/**
* Sets the transport properties for the given transport, replacing any
* existing properties for that transport.
*/
void setTransportProperties(int transportId, Map<String, String> properties)
void setTransportProperties(TransportId t, Map<String, String> properties)
throws DbException;
/**

View File

@@ -2,6 +2,8 @@ package net.sf.briar.api.protocol;
import java.util.Map;
import net.sf.briar.api.TransportId;
/** A packet updating the sender's transport properties. */
public interface TransportUpdate {
@@ -15,7 +17,7 @@ public interface TransportUpdate {
static final int MAX_PLUGINS_PER_UPDATE = 50;
/** Returns the transport properties contained in the update. */
Map<Integer, Map<String, String>> getTransports();
Map<TransportId, Map<String, String>> getTransports();
/**
* Returns the update's timestamp. Updates that are older than the newest

View File

@@ -3,10 +3,12 @@ package net.sf.briar.api.protocol.writers;
import java.io.IOException;
import java.util.Map;
import net.sf.briar.api.TransportId;
/** An interface for creating a transport update. */
public interface TransportWriter {
/** Writes the contents of the update. */
void writeTransports(Map<Integer, Map<String, String>> transports,
void writeTransports(Map<TransportId, Map<String, String>> transports,
long timestamp) throws IOException;
}

View File

@@ -1,6 +1,8 @@
package net.sf.briar.api.transport;
import net.sf.briar.api.TransportId;
public interface ConnectionRecogniserFactory {
ConnectionRecogniser createConnectionRecogniser(int transportId);
ConnectionRecogniser createConnectionRecogniser(TransportId t);
}

View File

@@ -2,9 +2,11 @@ package net.sf.briar.api.transport;
import java.io.OutputStream;
import net.sf.briar.api.TransportId;
public interface ConnectionWriterFactory {
ConnectionWriter createConnectionWriter(OutputStream out,
long capacity, boolean initiator, int transportId, long connection,
long capacity, boolean initiator, TransportId t, long connection,
byte[] secret);
}

View File

@@ -3,6 +3,7 @@ package net.sf.briar.api.transport.batch;
import java.util.Map;
import net.sf.briar.api.ContactId;
import net.sf.briar.api.TransportId;
import net.sf.briar.api.transport.InvalidConfigException;
import net.sf.briar.api.transport.InvalidTransportException;
@@ -13,7 +14,7 @@ import net.sf.briar.api.transport.InvalidTransportException;
public interface BatchTransportPlugin {
/** Returns the plugin's transport identifier. */
int getTransportId();
TransportId getId();
/**
* Starts the plugin. Any connections that are later initiated by contacts

View File

@@ -3,6 +3,7 @@ package net.sf.briar.api.transport.stream;
import java.util.Map;
import net.sf.briar.api.ContactId;
import net.sf.briar.api.TransportId;
import net.sf.briar.api.transport.InvalidConfigException;
import net.sf.briar.api.transport.InvalidTransportException;
@@ -13,7 +14,7 @@ import net.sf.briar.api.transport.InvalidTransportException;
public interface StreamTransportPlugin {
/** Returns the plugin's transport identifier. */
int getTransportId();
TransportId getId();
/**
* Starts the plugin. Any connections that are later initiated by contacts