Use dedicated classes for transport properties and configs.

This commit is contained in:
akwizgran
2011-10-11 17:28:47 +01:00
parent 68b82ae826
commit a49a95347f
34 changed files with 341 additions and 308 deletions

View File

@@ -0,0 +1,17 @@
package net.sf.briar.api;
import java.util.Map;
import java.util.TreeMap;
public class TransportConfig extends TreeMap<String, String> {
private static final long serialVersionUID = 2330384620787778596L;
public TransportConfig(Map<String, String> c) {
super(c);
}
public TransportConfig() {
super();
}
}

View File

@@ -0,0 +1,17 @@
package net.sf.briar.api;
import java.util.Map;
import java.util.TreeMap;
public class TransportProperties extends TreeMap<String, String> {
private static final long serialVersionUID = 7533739534204953625L;
public TransportProperties(Map<String, String> p) {
super(p);
}
public TransportProperties() {
super();
}
}

View File

@@ -6,7 +6,9 @@ import java.util.Map;
import net.sf.briar.api.ContactId; import net.sf.briar.api.ContactId;
import net.sf.briar.api.Rating; import net.sf.briar.api.Rating;
import net.sf.briar.api.TransportConfig;
import net.sf.briar.api.TransportId; import net.sf.briar.api.TransportId;
import net.sf.briar.api.TransportProperties;
import net.sf.briar.api.protocol.Ack; import net.sf.briar.api.protocol.Ack;
import net.sf.briar.api.protocol.AuthorId; import net.sf.briar.api.protocol.AuthorId;
import net.sf.briar.api.protocol.Batch; import net.sf.briar.api.protocol.Batch;
@@ -51,7 +53,7 @@ public interface DatabaseComponent {
* Adds a new contact to the database with the given transport properties * Adds a new contact to the database with the given transport properties
* and shared secret, returns an ID for the contact. * and shared secret, returns an ID for the contact.
*/ */
ContactId addContact(Map<TransportId, Map<String, String>> transports, ContactId addContact(Map<TransportId, TransportProperties> transports,
byte[] secret) throws DbException; byte[] secret) throws DbException;
/** Adds a locally generated group message to the database. */ /** Adds a locally generated group message to the database. */
@@ -116,14 +118,14 @@ public interface DatabaseComponent {
Collection<ContactId> getContacts() throws DbException; Collection<ContactId> getContacts() throws DbException;
/** Returns all local transport properties. */ /** Returns all local transport properties. */
Map<TransportId, Map<String, String>> getLocalTransports() Map<TransportId, TransportProperties> getLocalTransports()
throws DbException; throws DbException;
/** Returns the user's rating for the given author. */ /** Returns the user's rating for the given author. */
Rating getRating(AuthorId a) throws DbException; Rating getRating(AuthorId a) throws DbException;
/** Returns all remote transport properties. */ /** Returns all remote transport properties. */
Map<TransportId, Map<ContactId, Map<String, String>>> getRemoteTransports() Map<TransportId, Map<ContactId, TransportProperties>> getRemoteTransports()
throws DbException; throws DbException;
/** Returns the secret shared with the given contact. */ /** Returns the secret shared with the given contact. */
@@ -133,7 +135,7 @@ public interface DatabaseComponent {
Collection<Group> getSubscriptions() throws DbException; Collection<Group> getSubscriptions() throws DbException;
/** Returns the configuration for the given transport. */ /** Returns the configuration for the given transport. */
Map<String, String> getTransportConfig(TransportId t) throws DbException; TransportConfig getTransportConfig(TransportId t) throws DbException;
/** Returns the contacts to which the given group is visible. */ /** Returns the contacts to which the given group is visible. */
Collection<ContactId> getVisibility(GroupId g) throws DbException; Collection<ContactId> getVisibility(GroupId g) throws DbException;
@@ -186,14 +188,14 @@ public interface DatabaseComponent {
* Sets the configuration for the given transport, replacing any existing * Sets the configuration for the given transport, replacing any existing
* configuration for that transport. * configuration for that transport.
*/ */
void setTransportConfig(TransportId t, Map<String, String> config) void setTransportConfig(TransportId t, TransportConfig config)
throws DbException; throws DbException;
/** /**
* Sets the transport properties for the given transport, replacing any * Sets the transport properties for the given transport, replacing any
* existing properties for that transport. * existing properties for that transport.
*/ */
void setTransportProperties(TransportId t, Map<String, String> properties) void setTransportProperties(TransportId t, TransportProperties properties)
throws DbException; throws DbException;
/** /**

View File

@@ -4,7 +4,9 @@ import java.io.IOException;
import java.util.Map; import java.util.Map;
import net.sf.briar.api.ContactId; import net.sf.briar.api.ContactId;
import net.sf.briar.api.TransportConfig;
import net.sf.briar.api.TransportId; import net.sf.briar.api.TransportId;
import net.sf.briar.api.TransportProperties;
public interface TransportPlugin { public interface TransportPlugin {
@@ -12,21 +14,21 @@ public interface TransportPlugin {
TransportId getId(); TransportId getId();
/** Starts the plugin. */ /** Starts the plugin. */
void start(Map<String, String> localProperties, void start(TransportProperties localProperties,
Map<ContactId, Map<String, String>> remoteProperties, Map<ContactId, TransportProperties> remoteProperties,
Map<String, String> config) throws IOException; TransportConfig config) throws IOException;
/** Stops the plugin. */ /** Stops the plugin. */
void stop() throws IOException; void stop() throws IOException;
/** Updates the plugin's local transport properties. */ /** Updates the plugin's local transport properties. */
void setLocalProperties(Map<String, String> properties); void setLocalProperties(TransportProperties p);
/** Updates the plugin's transport properties for the given contact. */ /** Updates the plugin's transport properties for the given contact. */
void setRemoteProperties(ContactId c, Map<String, String> properties); void setRemoteProperties(ContactId c, TransportProperties p);
/** Updates the plugin's configuration properties. */ /** Updates the plugin's configuration properties. */
void setConfig(Map<String, String> config); void setConfig(TransportConfig c);
/** /**
* Returns true if the plugin's poll() method should be called * Returns true if the plugin's poll() method should be called

View File

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

View File

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

View File

@@ -1,12 +1,13 @@
package net.sf.briar.api.transport; package net.sf.briar.api.transport;
import java.util.Map; import net.sf.briar.api.TransportConfig;
import net.sf.briar.api.TransportProperties;
public interface TransportCallback { public interface TransportCallback {
void setLocalProperties(Map<String, String> properties); void setLocalProperties(TransportProperties p);
void setConfig(Map<String, String> config); void setConfig(TransportConfig c);
void showMessage(String... message); void showMessage(String... message);

View File

@@ -6,7 +6,9 @@ import java.util.Map;
import net.sf.briar.api.ContactId; import net.sf.briar.api.ContactId;
import net.sf.briar.api.Rating; import net.sf.briar.api.Rating;
import net.sf.briar.api.TransportConfig;
import net.sf.briar.api.TransportId; import net.sf.briar.api.TransportId;
import net.sf.briar.api.TransportProperties;
import net.sf.briar.api.db.DbException; import net.sf.briar.api.db.DbException;
import net.sf.briar.api.db.Status; import net.sf.briar.api.db.Status;
import net.sf.briar.api.protocol.AuthorId; import net.sf.briar.api.protocol.AuthorId;
@@ -86,7 +88,7 @@ interface Database<T> {
* Locking: contacts write, transports write. * Locking: contacts write, transports write.
*/ */
ContactId addContact(T txn, ContactId addContact(T txn,
Map<TransportId, Map<String, String>> transports, byte[] secret) Map<TransportId, TransportProperties> transports, byte[] secret)
throws DbException; throws DbException;
/** /**
@@ -216,7 +218,7 @@ interface Database<T> {
* <p> * <p>
* Locking: transports read. * Locking: transports read.
*/ */
Map<TransportId, Map<String, String>> getLocalTransports(T txn) Map<TransportId, TransportProperties> getLocalTransports(T txn)
throws DbException; throws DbException;
/** /**
@@ -282,7 +284,7 @@ interface Database<T> {
* <p> * <p>
* Locking: contacts read, transports read. * Locking: contacts read, transports read.
*/ */
Map<TransportId, Map<ContactId, Map<String, String>>> Map<TransportId, Map<ContactId, TransportProperties>>
getRemoteTransports(T txn) throws DbException; getRemoteTransports(T txn) throws DbException;
/** /**
@@ -338,8 +340,7 @@ interface Database<T> {
* <p> * <p>
* Locking: transports read. * Locking: transports read.
*/ */
Map<String, String> getTransportConfig(T txn, TransportId t) TransportConfig getTransportConfig(T txn, TransportId t) throws DbException;
throws DbException;
/** /**
* Returns the contacts to which the given group is visible. * Returns the contacts to which the given group is visible.
@@ -480,7 +481,7 @@ interface Database<T> {
* <p> * <p>
* Locking: transports write. * Locking: transports write.
*/ */
void setTransportConfig(T txn, TransportId t, Map<String, String> config) void setTransportConfig(T txn, TransportId t, TransportConfig config)
throws DbException; throws DbException;
/** /**
@@ -490,7 +491,7 @@ interface Database<T> {
* Locking: transports write. * Locking: transports write.
*/ */
void setTransportProperties(T txn, TransportId t, void setTransportProperties(T txn, TransportId t,
Map<String, String> properties) throws DbException; TransportProperties properties) throws DbException;
/** /**
* Sets the transport properties for the given contact, replacing any * Sets the transport properties for the given contact, replacing any
@@ -500,7 +501,7 @@ interface Database<T> {
* Locking: contacts read, transports write. * Locking: contacts read, transports write.
*/ */
void setTransports(T txn, ContactId c, void setTransports(T txn, ContactId c,
Map<TransportId, Map<String, String>> transports, long timestamp) Map<TransportId, TransportProperties> transports, long timestamp)
throws DbException; throws DbException;
/** /**

View File

@@ -21,7 +21,9 @@ import java.util.logging.Logger;
import net.sf.briar.api.Bytes; import net.sf.briar.api.Bytes;
import net.sf.briar.api.ContactId; import net.sf.briar.api.ContactId;
import net.sf.briar.api.Rating; import net.sf.briar.api.Rating;
import net.sf.briar.api.TransportConfig;
import net.sf.briar.api.TransportId; import net.sf.briar.api.TransportId;
import net.sf.briar.api.TransportProperties;
import net.sf.briar.api.db.DatabaseComponent; import net.sf.briar.api.db.DatabaseComponent;
import net.sf.briar.api.db.DatabaseListener; import net.sf.briar.api.db.DatabaseListener;
import net.sf.briar.api.db.DatabaseListener.Event; import net.sf.briar.api.db.DatabaseListener.Event;
@@ -119,7 +121,7 @@ DatabaseCleaner.Callback {
} }
public ContactId addContact( public ContactId addContact(
Map<TransportId, Map<String, String>> transports, byte[] secret) Map<TransportId, TransportProperties> transports, byte[] secret)
throws DbException { throws DbException {
if(LOG.isLoggable(Level.FINE)) LOG.fine("Adding contact"); if(LOG.isLoggable(Level.FINE)) LOG.fine("Adding contact");
ContactId c; ContactId c;
@@ -606,7 +608,7 @@ DatabaseCleaner.Callback {
public void generateTransportUpdate(ContactId c, TransportWriter t) public void generateTransportUpdate(ContactId c, TransportWriter t)
throws DbException, IOException { throws DbException, IOException {
Map<TransportId, Map<String, String>> transports; Map<TransportId, TransportProperties> transports;
long timestamp; long timestamp;
contactLock.readLock().lock(); contactLock.readLock().lock();
try { try {
@@ -699,13 +701,13 @@ DatabaseCleaner.Callback {
} }
} }
public Map<TransportId, Map<String, String>> getLocalTransports() public Map<TransportId, TransportProperties> getLocalTransports()
throws DbException { throws DbException {
transportLock.readLock().lock(); transportLock.readLock().lock();
try { try {
T txn = db.startTransaction(); T txn = db.startTransaction();
try { try {
Map<TransportId, Map<String, String>> transports = Map<TransportId, TransportProperties> transports =
db.getLocalTransports(txn); db.getLocalTransports(txn);
db.commitTransaction(txn); db.commitTransaction(txn);
return transports; return transports;
@@ -735,7 +737,7 @@ DatabaseCleaner.Callback {
} }
} }
public Map<TransportId, Map<ContactId, Map<String, String>>> public Map<TransportId, Map<ContactId, TransportProperties>>
getRemoteTransports() throws DbException { getRemoteTransports() throws DbException {
contactLock.readLock().lock(); contactLock.readLock().lock();
try { try {
@@ -743,7 +745,7 @@ DatabaseCleaner.Callback {
try { try {
T txn = db.startTransaction(); T txn = db.startTransaction();
try { try {
Map<TransportId, Map<ContactId, Map<String, String>>> Map<TransportId, Map<ContactId, TransportProperties>>
transports = db.getRemoteTransports(txn); transports = db.getRemoteTransports(txn);
db.commitTransaction(txn); db.commitTransaction(txn);
return transports; return transports;
@@ -794,13 +796,13 @@ DatabaseCleaner.Callback {
} }
} }
public Map<String, String> getTransportConfig(TransportId t) public TransportConfig getTransportConfig(TransportId t)
throws DbException { throws DbException {
transportLock.readLock().lock(); transportLock.readLock().lock();
try { try {
T txn = db.startTransaction(); T txn = db.startTransaction();
try { try {
Map<String, String> config = db.getTransportConfig(txn, t); TransportConfig config = db.getTransportConfig(txn, t);
db.commitTransaction(txn); db.commitTransaction(txn);
return config; return config;
} catch(DbException e) { } catch(DbException e) {
@@ -1045,7 +1047,7 @@ DatabaseCleaner.Callback {
try { try {
T txn = db.startTransaction(); T txn = db.startTransaction();
try { try {
Map<TransportId, Map<String, String>> transports = Map<TransportId, TransportProperties> transports =
t.getTransports(); t.getTransports();
db.setTransports(txn, c, transports, t.getTimestamp()); db.setTransports(txn, c, transports, t.getTimestamp());
if(LOG.isLoggable(Level.FINE)) if(LOG.isLoggable(Level.FINE))
@@ -1219,14 +1221,14 @@ DatabaseCleaner.Callback {
+ indirect + " indirectly"); + indirect + " indirectly");
} }
public void setTransportConfig(TransportId t, public void setTransportConfig(TransportId t, TransportConfig config)
Map<String, String> config) throws DbException { throws DbException {
boolean changed = false; boolean changed = false;
transportLock.writeLock().lock(); transportLock.writeLock().lock();
try { try {
T txn = db.startTransaction(); T txn = db.startTransaction();
try { try {
Map<String, String> old = db.getTransportConfig(txn, t); TransportConfig old = db.getTransportConfig(txn, t);
if(!config.equals(old)) { if(!config.equals(old)) {
db.setTransportConfig(txn, t, config); db.setTransportConfig(txn, t, config);
changed = true; changed = true;
@@ -1244,15 +1246,15 @@ DatabaseCleaner.Callback {
} }
public void setTransportProperties(TransportId t, public void setTransportProperties(TransportId t,
Map<String, String> properties) throws DbException { TransportProperties properties) throws DbException {
boolean changed = false; boolean changed = false;
transportLock.writeLock().lock(); transportLock.writeLock().lock();
try { try {
T txn = db.startTransaction(); T txn = db.startTransaction();
try { try {
Map<TransportId, Map<String, String>> transports = Map<TransportId, TransportProperties> transports =
db.getLocalTransports(txn); db.getLocalTransports(txn);
Map<String, String> old = transports.get(t); TransportProperties old = transports.get(t);
if(!properties.equals(old)) { if(!properties.equals(old)) {
db.setTransportProperties(txn, t, properties); db.setTransportProperties(txn, t, properties);
changed = true; changed = true;

View File

@@ -22,7 +22,9 @@ import java.util.logging.Logger;
import net.sf.briar.api.ContactId; import net.sf.briar.api.ContactId;
import net.sf.briar.api.Rating; import net.sf.briar.api.Rating;
import net.sf.briar.api.TransportConfig;
import net.sf.briar.api.TransportId; import net.sf.briar.api.TransportId;
import net.sf.briar.api.TransportProperties;
import net.sf.briar.api.db.DbException; import net.sf.briar.api.db.DbException;
import net.sf.briar.api.db.Status; import net.sf.briar.api.db.Status;
import net.sf.briar.api.protocol.AuthorId; import net.sf.briar.api.protocol.AuthorId;
@@ -457,7 +459,7 @@ abstract class JdbcDatabase implements Database<Connection> {
} }
public ContactId addContact(Connection txn, public ContactId addContact(Connection txn,
Map<TransportId, Map<String, String>> transports, byte[] secret) Map<TransportId, TransportProperties> transports, byte[] secret)
throws DbException { throws DbException {
PreparedStatement ps = null; PreparedStatement ps = null;
ResultSet rs = null; ResultSet rs = null;
@@ -488,7 +490,7 @@ abstract class JdbcDatabase implements Database<Connection> {
ps = txn.prepareStatement(sql); ps = txn.prepareStatement(sql);
ps.setInt(1, c.getInt()); ps.setInt(1, c.getInt());
int batchSize = 0; int batchSize = 0;
for(Entry<TransportId, Map<String, String>> e for(Entry<TransportId, TransportProperties> e
: transports.entrySet()) { : transports.entrySet()) {
ps.setInt(2, e.getKey().getInt()); ps.setInt(2, e.getKey().getInt());
for(Entry<String, String> e1 : e.getValue().entrySet()) { for(Entry<String, String> e1 : e.getValue().entrySet()) {
@@ -916,7 +918,7 @@ abstract class JdbcDatabase implements Database<Connection> {
} else return f.length(); } else return f.length();
} }
public Map<TransportId, Map<String, String>> getLocalTransports( public Map<TransportId, TransportProperties> getLocalTransports(
Connection txn) throws DbException { Connection txn) throws DbException {
PreparedStatement ps = null; PreparedStatement ps = null;
ResultSet rs = null; ResultSet rs = null;
@@ -925,14 +927,14 @@ abstract class JdbcDatabase implements Database<Connection> {
+ " ORDER BY transportId"; + " ORDER BY transportId";
ps = txn.prepareStatement(sql); ps = txn.prepareStatement(sql);
rs = ps.executeQuery(); rs = ps.executeQuery();
Map<TransportId, Map<String, String>> transports = Map<TransportId, TransportProperties> transports =
new TreeMap<TransportId, Map<String, String>>(); new TreeMap<TransportId, TransportProperties>();
Map<String, String> properties = null; TransportProperties properties = null;
TransportId lastId = null; TransportId lastId = null;
while(rs.next()) { while(rs.next()) {
TransportId id = new TransportId(rs.getInt(1)); TransportId id = new TransportId(rs.getInt(1));
if(!id.equals(lastId)) { if(!id.equals(lastId)) {
properties = new TreeMap<String, String>(); properties = new TransportProperties();
transports.put(id, properties); transports.put(id, properties);
} }
properties.put(rs.getString(2), rs.getString(3)); properties.put(rs.getString(2), rs.getString(3));
@@ -1212,7 +1214,7 @@ abstract class JdbcDatabase implements Database<Connection> {
} }
} }
public Map<TransportId, Map<ContactId, Map<String, String>>> public Map<TransportId, Map<ContactId, TransportProperties>>
getRemoteTransports(Connection txn) throws DbException { getRemoteTransports(Connection txn) throws DbException {
PreparedStatement ps = null; PreparedStatement ps = null;
ResultSet rs = null; ResultSet rs = null;
@@ -1222,22 +1224,22 @@ abstract class JdbcDatabase implements Database<Connection> {
+ " ORDER BY transportId"; + " ORDER BY transportId";
ps = txn.prepareStatement(sql); ps = txn.prepareStatement(sql);
rs = ps.executeQuery(); rs = ps.executeQuery();
Map<TransportId, Map<ContactId, Map<String, String>>> transports = Map<TransportId, Map<ContactId, TransportProperties>> transports =
new TreeMap<TransportId, Map<ContactId, Map<String, String>>>(); new TreeMap<TransportId, Map<ContactId, TransportProperties>>();
Map<ContactId, Map<String, String>> contacts = null; Map<ContactId, TransportProperties> contacts = null;
Map<String, String> properties = null; TransportProperties properties = null;
TransportId lastTransportId = null; TransportId lastTransportId = null;
ContactId lastContactId = null; ContactId lastContactId = null;
while(rs.next()) { while(rs.next()) {
TransportId transportId = new TransportId(rs.getInt(1)); TransportId transportId = new TransportId(rs.getInt(1));
if(!transportId.equals(lastTransportId)) { if(!transportId.equals(lastTransportId)) {
contacts = new HashMap<ContactId, Map<String, String>>(); contacts = new HashMap<ContactId, TransportProperties>();
transports.put(transportId, contacts); transports.put(transportId, contacts);
lastContactId = null; lastContactId = null;
} }
ContactId contactId = new ContactId(rs.getInt(2)); ContactId contactId = new ContactId(rs.getInt(2));
if(!contactId.equals(lastContactId)) { if(!contactId.equals(lastContactId)) {
properties = new TreeMap<String, String>(); properties = new TransportProperties();
contacts.put(contactId, properties); contacts.put(contactId, properties);
} }
properties.put(rs.getString(3), rs.getString(4)); properties.put(rs.getString(3), rs.getString(4));
@@ -1467,7 +1469,7 @@ abstract class JdbcDatabase implements Database<Connection> {
} }
} }
public Map<String, String> getTransportConfig(Connection txn, public TransportConfig getTransportConfig(Connection txn,
TransportId t) throws DbException { TransportId t) throws DbException {
PreparedStatement ps = null; PreparedStatement ps = null;
ResultSet rs = null; ResultSet rs = null;
@@ -1477,7 +1479,7 @@ abstract class JdbcDatabase implements Database<Connection> {
ps = txn.prepareStatement(sql); ps = txn.prepareStatement(sql);
ps.setInt(1, t.getInt()); ps.setInt(1, t.getInt());
rs = ps.executeQuery(); rs = ps.executeQuery();
Map<String, String> config = new TreeMap<String, String>(); TransportConfig config = new TransportConfig();
while(rs.next()) config.put(rs.getString(1), rs.getString(2)); while(rs.next()) config.put(rs.getString(1), rs.getString(2));
rs.close(); rs.close();
ps.close(); ps.close();
@@ -2050,7 +2052,7 @@ abstract class JdbcDatabase implements Database<Connection> {
} }
public void setTransportConfig(Connection txn, TransportId t, public void setTransportConfig(Connection txn, TransportId t,
Map<String, String> config) throws DbException { TransportConfig config) throws DbException {
setTransportDetails(txn, t, config, "transportConfig"); setTransportDetails(txn, t, config, "transportConfig");
} }
@@ -2088,12 +2090,12 @@ abstract class JdbcDatabase implements Database<Connection> {
} }
public void setTransportProperties(Connection txn, TransportId t, public void setTransportProperties(Connection txn, TransportId t,
Map<String, String> properties) throws DbException { TransportProperties properties) throws DbException {
setTransportDetails(txn, t, properties, "transports"); setTransportDetails(txn, t, properties, "transports");
} }
public void setTransports(Connection txn, ContactId c, public void setTransports(Connection txn, ContactId c,
Map<TransportId, Map<String, String>> transports, long timestamp) Map<TransportId, TransportProperties> transports, long timestamp)
throws DbException { throws DbException {
PreparedStatement ps = null; PreparedStatement ps = null;
ResultSet rs = null; ResultSet rs = null;
@@ -2123,7 +2125,7 @@ abstract class JdbcDatabase implements Database<Connection> {
ps = txn.prepareStatement(sql); ps = txn.prepareStatement(sql);
ps.setInt(1, c.getInt()); ps.setInt(1, c.getInt());
int batchSize = 0; int batchSize = 0;
for(Entry<TransportId, Map<String, String>> e for(Entry<TransportId, TransportProperties> e
: transports.entrySet()) { : transports.entrySet()) {
ps.setInt(2, e.getKey().getInt()); ps.setInt(2, e.getKey().getInt());
for(Entry<String, String> e1 : e.getValue().entrySet()) { for(Entry<String, String> e1 : e.getValue().entrySet()) {

View File

@@ -9,6 +9,7 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import net.sf.briar.api.TransportId; import net.sf.briar.api.TransportId;
import net.sf.briar.api.TransportProperties;
import net.sf.briar.api.db.DatabaseComponent; import net.sf.briar.api.db.DatabaseComponent;
import net.sf.briar.api.db.DbException; import net.sf.briar.api.db.DbException;
import net.sf.briar.api.invitation.InvitationCallback; import net.sf.briar.api.invitation.InvitationCallback;
@@ -71,7 +72,7 @@ class InvitationWorker implements Runnable {
File invitationDat = new File(dir, "invitation.dat"); File invitationDat = new File(dir, "invitation.dat");
callback.encryptingFile(invitationDat); callback.encryptingFile(invitationDat);
// FIXME: Create a real invitation // FIXME: Create a real invitation
Map<TransportId, Map<String, String>> transports; Map<TransportId, TransportProperties> transports;
try { try {
transports = db.getLocalTransports(); transports = db.getLocalTransports();
} catch(DbException e) { } catch(DbException e) {

View File

@@ -1,13 +1,12 @@
package net.sf.briar.plugins; package net.sf.briar.plugins;
import java.io.IOException; import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.Executor; import java.util.concurrent.Executor;
import net.sf.briar.api.ContactId; import net.sf.briar.api.ContactId;
import net.sf.briar.api.TransportConfig;
import net.sf.briar.api.TransportProperties;
import net.sf.briar.api.plugins.TransportPlugin; import net.sf.briar.api.plugins.TransportPlugin;
public abstract class AbstractPlugin implements TransportPlugin { public abstract class AbstractPlugin implements TransportPlugin {
@@ -15,31 +14,23 @@ public abstract class AbstractPlugin implements TransportPlugin {
protected final Executor executor; protected final Executor executor;
// These fields should be accessed with this's lock held // These fields should be accessed with this's lock held
protected Map<String, String> localProperties = null; protected TransportProperties localProperties = null;
protected Map<ContactId, Map<String, String>> remoteProperties = null; protected Map<ContactId, TransportProperties> remoteProperties = null;
protected Map<String, String> config = null; protected TransportConfig config = null;
protected boolean started = false; protected boolean started = false;
protected AbstractPlugin(Executor executor) { protected AbstractPlugin(Executor executor) {
this.executor = executor; this.executor = executor;
} }
public synchronized void start(Map<String, String> localProperties, public synchronized void start(TransportProperties localProperties,
Map<ContactId, Map<String, String>> remoteProperties, Map<ContactId, TransportProperties> remoteProperties,
Map<String, String> config) throws IOException { TransportConfig config) throws IOException {
if(started) throw new IllegalStateException(); if(started) throw new IllegalStateException();
started = true; started = true;
this.localProperties = Collections.unmodifiableMap(localProperties); this.localProperties = localProperties;
// Copy the remoteProperties map to make its values unmodifiable this.remoteProperties = remoteProperties;
int size = remoteProperties.size(); this.config = config;
Map<ContactId, Map<String, String>> m =
new HashMap<ContactId, Map<String, String>>(size);
for(Entry<ContactId, Map<String, String>> e
: remoteProperties.entrySet()) {
m.put(e.getKey(), Collections.unmodifiableMap(e.getValue()));
}
this.remoteProperties = m;
this.config = Collections.unmodifiableMap(config);
} }
public synchronized void stop() throws IOException { public synchronized void stop() throws IOException {
@@ -47,20 +38,19 @@ public abstract class AbstractPlugin implements TransportPlugin {
started = false; started = false;
} }
public synchronized void setLocalProperties( public synchronized void setLocalProperties(TransportProperties p) {
Map<String, String> properties) {
if(!started) throw new IllegalStateException(); if(!started) throw new IllegalStateException();
localProperties = Collections.unmodifiableMap(properties); localProperties = p;
} }
public synchronized void setRemoteProperties(ContactId c, public synchronized void setRemoteProperties(ContactId c,
Map<String, String> properties) { TransportProperties p) {
if(!started) throw new IllegalStateException(); if(!started) throw new IllegalStateException();
remoteProperties.put(c, Collections.unmodifiableMap(properties)); remoteProperties.put(c, p);
} }
public synchronized void setConfig(Map<String, String> config) { public synchronized void setConfig(TransportConfig c) {
if(!started) throw new IllegalStateException(); if(!started) throw new IllegalStateException();
this.config = Collections.unmodifiableMap(config); this.config = c;
} }
} }

View File

@@ -7,7 +7,6 @@ import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.Random; import java.util.Random;
import java.util.TreeMap;
import java.util.concurrent.Executor; import java.util.concurrent.Executor;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
@@ -20,7 +19,9 @@ import javax.microedition.io.StreamConnection;
import javax.microedition.io.StreamConnectionNotifier; import javax.microedition.io.StreamConnectionNotifier;
import net.sf.briar.api.ContactId; import net.sf.briar.api.ContactId;
import net.sf.briar.api.TransportConfig;
import net.sf.briar.api.TransportId; import net.sf.briar.api.TransportId;
import net.sf.briar.api.TransportProperties;
import net.sf.briar.api.plugins.StreamTransportCallback; import net.sf.briar.api.plugins.StreamTransportCallback;
import net.sf.briar.api.plugins.StreamTransportPlugin; import net.sf.briar.api.plugins.StreamTransportPlugin;
import net.sf.briar.api.transport.StreamTransportConnection; import net.sf.briar.api.transport.StreamTransportConnection;
@@ -54,9 +55,9 @@ class BluetoothPlugin extends AbstractPlugin implements StreamTransportPlugin {
} }
@Override @Override
public void start(Map<String, String> localProperties, public void start(TransportProperties localProperties,
Map<ContactId, Map<String, String>> remoteProperties, Map<ContactId, TransportProperties> remoteProperties,
Map<String, String> config) throws IOException { TransportConfig config) throws IOException {
// Initialise the Bluetooth stack // Initialise the Bluetooth stack
try { try {
synchronized(this) { synchronized(this) {
@@ -133,13 +134,13 @@ class BluetoothPlugin extends AbstractPlugin implements StreamTransportPlugin {
byte[] b = new byte[16]; byte[] b = new byte[16];
new Random().nextBytes(b); // FIXME: Use a SecureRandom? new Random().nextBytes(b); // FIXME: Use a SecureRandom?
String uuid = StringUtils.toHexString(b); String uuid = StringUtils.toHexString(b);
Map<String, String> m; TransportConfig c;
synchronized(this) { synchronized(this) {
if(!started) return uuid; if(!started) return uuid;
m = new TreeMap<String, String>(config); c = new TransportConfig(config);
} }
m.put("uuid", uuid); c.put("uuid", uuid);
callback.setConfig(m); callback.setConfig(c);
return uuid; return uuid;
} }
@@ -174,13 +175,13 @@ class BluetoothPlugin extends AbstractPlugin implements StreamTransportPlugin {
} }
private void setLocalBluetoothAddress(String address) { private void setLocalBluetoothAddress(String address) {
Map<String, String> m; TransportProperties p;
synchronized(this) { synchronized(this) {
if(!started) return; if(!started) return;
m = new TreeMap<String, String>(localProperties); p = new TransportProperties(localProperties);
} }
m.put("address", address); p.put("address", address);
callback.setLocalProperties(m); callback.setLocalProperties(p);
} }
public boolean shouldPoll() { public boolean shouldPoll() {
@@ -224,10 +225,10 @@ class BluetoothPlugin extends AbstractPlugin implements StreamTransportPlugin {
discoveryAgent = localDevice.getDiscoveryAgent(); discoveryAgent = localDevice.getDiscoveryAgent();
addresses = new HashMap<String, ContactId>(); addresses = new HashMap<String, ContactId>();
uuids = new HashMap<ContactId, String>(); uuids = new HashMap<ContactId, String>();
for(Entry<ContactId, Map<String, String>> e for(Entry<ContactId, TransportProperties> e
: remoteProperties.entrySet()) { : remoteProperties.entrySet()) {
ContactId c = e.getKey(); ContactId c = e.getKey();
Map<String, String> properties = e.getValue(); TransportProperties properties = e.getValue();
String address = properties.get("address"); String address = properties.get("address");
String uuid = properties.get("uuid"); String uuid = properties.get("uuid");
if(address != null && uuid != null) { if(address != null && uuid != null) {

View File

@@ -1,25 +0,0 @@
package net.sf.briar.plugins.file;
import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import net.sf.briar.util.OsUtils;
class RemovableDriveFinderImpl implements RemovableDriveFinder {
private final LinuxRemovableDriveFinder linux =
new LinuxRemovableDriveFinder();
private final MacRemovableDriveFinder mac =
new MacRemovableDriveFinder();
private final WindowsRemovableDriveFinder windows =
new WindowsRemovableDriveFinder();
public List<File> findRemovableDrives() throws IOException {
if(OsUtils.isLinux()) return linux.findRemovableDrives();
else if(OsUtils.isMac()) return mac.findRemovableDrives();
else if(OsUtils.isWindows()) return windows.findRemovableDrives();
else return Collections.emptyList();
}
}

View File

@@ -9,7 +9,9 @@ import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import net.sf.briar.api.ContactId; import net.sf.briar.api.ContactId;
import net.sf.briar.api.TransportConfig;
import net.sf.briar.api.TransportId; import net.sf.briar.api.TransportId;
import net.sf.briar.api.TransportProperties;
import net.sf.briar.api.plugins.BatchTransportCallback; import net.sf.briar.api.plugins.BatchTransportCallback;
class RemovableDrivePlugin extends FilePlugin class RemovableDrivePlugin extends FilePlugin
@@ -36,9 +38,9 @@ implements RemovableDriveMonitor.Callback {
} }
@Override @Override
public void start(Map<String, String> localProperties, public void start(TransportProperties localProperties,
Map<ContactId, Map<String, String>> remoteProperties, Map<ContactId, TransportProperties> remoteProperties,
Map<String, String> config) throws IOException { TransportConfig config) throws IOException {
super.start(localProperties, remoteProperties, config); super.start(localProperties, remoteProperties, config);
monitor.start(this); monitor.start(this);
} }

View File

@@ -5,12 +5,11 @@ import java.net.InetSocketAddress;
import java.net.ServerSocket; import java.net.ServerSocket;
import java.net.Socket; import java.net.Socket;
import java.net.SocketAddress; import java.net.SocketAddress;
import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.Executor; import java.util.concurrent.Executor;
import net.sf.briar.api.ContactId; import net.sf.briar.api.ContactId;
import net.sf.briar.api.TransportId; import net.sf.briar.api.TransportId;
import net.sf.briar.api.TransportProperties;
import net.sf.briar.api.plugins.StreamTransportCallback; import net.sf.briar.api.plugins.StreamTransportCallback;
class SimpleSocketPlugin extends SocketPlugin { class SimpleSocketPlugin extends SocketPlugin {
@@ -48,12 +47,12 @@ class SimpleSocketPlugin extends SocketPlugin {
@Override @Override
protected SocketAddress getSocketAddress(ContactId c) { protected SocketAddress getSocketAddress(ContactId c) {
assert started; assert started;
Map<String, String> properties = remoteProperties.get(c); TransportProperties properties = remoteProperties.get(c);
if(properties == null) return null; if(properties == null) return null;
return createSocketAddress(properties); return createSocketAddress(properties);
} }
private SocketAddress createSocketAddress(Map<String, String> properties) { private SocketAddress createSocketAddress(TransportProperties properties) {
assert properties != null; assert properties != null;
String host = properties.get("host"); String host = properties.get("host");
String portString = properties.get("port"); String portString = properties.get("port");
@@ -69,10 +68,10 @@ class SimpleSocketPlugin extends SocketPlugin {
@Override @Override
protected void setLocalSocketAddress(SocketAddress s) { protected void setLocalSocketAddress(SocketAddress s) {
Map<String, String> m; TransportProperties p;
synchronized(this) { synchronized(this) {
if(!started) return; if(!started) return;
m = new TreeMap<String, String>(localProperties); p = new TransportProperties(localProperties);
} }
if(!(s instanceof InetSocketAddress)) if(!(s instanceof InetSocketAddress))
throw new IllegalArgumentException(); throw new IllegalArgumentException();
@@ -80,9 +79,9 @@ class SimpleSocketPlugin extends SocketPlugin {
String host = i.getAddress().getHostAddress(); String host = i.getAddress().getHostAddress();
String port = String.valueOf(i.getPort()); String port = String.valueOf(i.getPort());
// FIXME: Special handling for private IP addresses? // FIXME: Special handling for private IP addresses?
m.put("host", host); p.put("host", host);
m.put("port", port); p.put("port", port);
callback.setLocalProperties(m); callback.setLocalProperties(p);
} }
@Override @Override

View File

@@ -10,6 +10,8 @@ import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import net.sf.briar.api.ContactId; import net.sf.briar.api.ContactId;
import net.sf.briar.api.TransportConfig;
import net.sf.briar.api.TransportProperties;
import net.sf.briar.api.plugins.StreamTransportCallback; import net.sf.briar.api.plugins.StreamTransportCallback;
import net.sf.briar.api.plugins.StreamTransportPlugin; import net.sf.briar.api.plugins.StreamTransportPlugin;
import net.sf.briar.api.transport.StreamTransportConnection; import net.sf.briar.api.transport.StreamTransportConnection;
@@ -41,9 +43,9 @@ implements StreamTransportPlugin {
} }
@Override @Override
public synchronized void start(Map<String, String> localProperties, public synchronized void start(TransportProperties localProperties,
Map<ContactId, Map<String, String>> remoteProperties, Map<ContactId, TransportProperties> remoteProperties,
Map<String, String> config) throws IOException { TransportConfig config) throws IOException {
super.start(localProperties, remoteProperties, config); super.start(localProperties, remoteProperties, config);
executor.execute(createBinder()); executor.execute(createBinder());
} }
@@ -129,9 +131,8 @@ implements StreamTransportPlugin {
} }
@Override @Override
public synchronized void setLocalProperties( public synchronized void setLocalProperties(TransportProperties p) {
Map<String, String> properties) { super.setLocalProperties(p);
super.setLocalProperties(properties);
// Close and reopen the socket if its address has changed // Close and reopen the socket if its address has changed
if(socket != null) { if(socket != null) {
SocketAddress addr = socket.getLocalSocketAddress(); SocketAddress addr = socket.getLocalSocketAddress();

View File

@@ -3,10 +3,11 @@ package net.sf.briar.protocol;
import java.util.Map; import java.util.Map;
import net.sf.briar.api.TransportId; import net.sf.briar.api.TransportId;
import net.sf.briar.api.TransportProperties;
import net.sf.briar.api.protocol.TransportUpdate; import net.sf.briar.api.protocol.TransportUpdate;
interface TransportFactory { interface TransportFactory {
TransportUpdate createTransportUpdate( TransportUpdate createTransportUpdate(
Map<TransportId, Map<String, String>> transports, long timestamp); Map<TransportId, TransportProperties> transports, long timestamp);
} }

View File

@@ -3,12 +3,13 @@ package net.sf.briar.protocol;
import java.util.Map; import java.util.Map;
import net.sf.briar.api.TransportId; import net.sf.briar.api.TransportId;
import net.sf.briar.api.TransportProperties;
import net.sf.briar.api.protocol.TransportUpdate; import net.sf.briar.api.protocol.TransportUpdate;
class TransportFactoryImpl implements TransportFactory { class TransportFactoryImpl implements TransportFactory {
public TransportUpdate createTransportUpdate( public TransportUpdate createTransportUpdate(
Map<TransportId, Map<String, String>> transports, long timestamp) { Map<TransportId, TransportProperties> transports, long timestamp) {
return new TransportUpdateImpl(transports, timestamp); return new TransportUpdateImpl(transports, timestamp);
} }
} }

View File

@@ -7,6 +7,7 @@ import java.util.TreeMap;
import net.sf.briar.api.FormatException; import net.sf.briar.api.FormatException;
import net.sf.briar.api.TransportId; import net.sf.briar.api.TransportId;
import net.sf.briar.api.TransportProperties;
import net.sf.briar.api.protocol.ProtocolConstants; import net.sf.briar.api.protocol.ProtocolConstants;
import net.sf.briar.api.protocol.TransportUpdate; import net.sf.briar.api.protocol.TransportUpdate;
import net.sf.briar.api.protocol.Types; import net.sf.briar.api.protocol.Types;
@@ -17,11 +18,11 @@ import net.sf.briar.api.serial.Reader;
class TransportReader implements ObjectReader<TransportUpdate> { class TransportReader implements ObjectReader<TransportUpdate> {
private final TransportFactory transportFactory; private final TransportFactory transportFactory;
private final ObjectReader<TransportProperties> propertiesReader; private final ObjectReader<Transport> propertiesReader;
TransportReader(TransportFactory transportFactory) { TransportReader(TransportFactory transportFactory) {
this.transportFactory = transportFactory; this.transportFactory = transportFactory;
propertiesReader = new TransportPropertiesReader(); propertiesReader = new PropertiesReader();
} }
public TransportUpdate readObject(Reader r) throws IOException { public TransportUpdate readObject(Reader r) throws IOException {
@@ -33,14 +34,14 @@ class TransportReader implements ObjectReader<TransportUpdate> {
r.readUserDefinedId(Types.TRANSPORT_UPDATE); r.readUserDefinedId(Types.TRANSPORT_UPDATE);
r.addObjectReader(Types.TRANSPORT_PROPERTIES, propertiesReader); r.addObjectReader(Types.TRANSPORT_PROPERTIES, propertiesReader);
r.setMaxStringLength(ProtocolConstants.MAX_PACKET_LENGTH); r.setMaxStringLength(ProtocolConstants.MAX_PACKET_LENGTH);
List<TransportProperties> l = r.readList(TransportProperties.class); List<Transport> l = r.readList(Transport.class);
r.resetMaxStringLength(); r.resetMaxStringLength();
r.removeObjectReader(Types.TRANSPORT_PROPERTIES); r.removeObjectReader(Types.TRANSPORT_PROPERTIES);
if(l.size() > TransportUpdate.MAX_PLUGINS_PER_UPDATE) if(l.size() > TransportUpdate.MAX_PLUGINS_PER_UPDATE)
throw new FormatException(); throw new FormatException();
Map<TransportId, Map<String, String>> transports = Map<TransportId, TransportProperties> transports =
new TreeMap<TransportId, Map<String, String>>(); new TreeMap<TransportId, TransportProperties>();
for(TransportProperties t : l) { for(Transport t : l) {
if(transports.put(t.id, t.properties) != null) if(transports.put(t.id, t.properties) != null)
throw new FormatException(); // Duplicate transport ID throw new FormatException(); // Duplicate transport ID
} }
@@ -50,33 +51,31 @@ class TransportReader implements ObjectReader<TransportUpdate> {
return transportFactory.createTransportUpdate(transports, timestamp); return transportFactory.createTransportUpdate(transports, timestamp);
} }
private static class TransportProperties { private static class Transport {
private final TransportId id; private final TransportId id;
private final Map<String, String> properties; private final TransportProperties properties;
TransportProperties(TransportId id, Map<String, String> properties) { Transport(TransportId id, TransportProperties properties) {
this.id = id; this.id = id;
this.properties = properties; this.properties = properties;
} }
} }
private static class TransportPropertiesReader private static class PropertiesReader implements ObjectReader<Transport> {
implements ObjectReader<TransportProperties> {
public TransportProperties readObject(Reader r) throws IOException { public Transport readObject(Reader r) throws IOException {
r.readUserDefinedId(Types.TRANSPORT_PROPERTIES); r.readUserDefinedId(Types.TRANSPORT_PROPERTIES);
int i = r.readInt32(); int i = r.readInt32();
if(i < TransportId.MIN_ID || i > TransportId.MAX_ID) if(i < TransportId.MIN_ID || i > TransportId.MAX_ID)
throw new FormatException(); throw new FormatException();
TransportId id = new TransportId(i); TransportId id = new TransportId(i);
r.setMaxStringLength(TransportUpdate.MAX_KEY_OR_VALUE_LENGTH); r.setMaxStringLength(TransportUpdate.MAX_KEY_OR_VALUE_LENGTH);
Map<String, String> properties = Map<String, String> m = r.readMap(String.class, String.class);
r.readMap(String.class, String.class);
r.resetMaxStringLength(); r.resetMaxStringLength();
if(properties.size() > TransportUpdate.MAX_PROPERTIES_PER_PLUGIN) if(m.size() > TransportUpdate.MAX_PROPERTIES_PER_PLUGIN)
throw new FormatException(); throw new FormatException();
return new TransportProperties(id, properties); return new Transport(id, new TransportProperties(m));
} }
} }
} }

View File

@@ -3,20 +3,21 @@ package net.sf.briar.protocol;
import java.util.Map; import java.util.Map;
import net.sf.briar.api.TransportId; import net.sf.briar.api.TransportId;
import net.sf.briar.api.TransportProperties;
import net.sf.briar.api.protocol.TransportUpdate; import net.sf.briar.api.protocol.TransportUpdate;
class TransportUpdateImpl implements TransportUpdate { class TransportUpdateImpl implements TransportUpdate {
private final Map<TransportId, Map<String, String>> transports; private final Map<TransportId, TransportProperties> transports;
private final long timestamp; private final long timestamp;
TransportUpdateImpl(Map<TransportId, Map<String, String>> transports, TransportUpdateImpl(Map<TransportId, TransportProperties> transports,
long timestamp) { long timestamp) {
this.transports = transports; this.transports = transports;
this.timestamp = timestamp; this.timestamp = timestamp;
} }
public Map<TransportId, Map<String, String>> getTransports() { public Map<TransportId, TransportProperties> getTransports() {
return transports; return transports;
} }

View File

@@ -6,6 +6,7 @@ import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import net.sf.briar.api.TransportId; import net.sf.briar.api.TransportId;
import net.sf.briar.api.TransportProperties;
import net.sf.briar.api.protocol.Types; import net.sf.briar.api.protocol.Types;
import net.sf.briar.api.protocol.writers.TransportWriter; import net.sf.briar.api.protocol.writers.TransportWriter;
import net.sf.briar.api.serial.Writer; import net.sf.briar.api.serial.Writer;
@@ -22,11 +23,11 @@ class TransportWriterImpl implements TransportWriter {
} }
public void writeTransports( public void writeTransports(
Map<TransportId, Map<String, String>> transports, long timestamp) Map<TransportId, TransportProperties> transports, long timestamp)
throws IOException { throws IOException {
w.writeUserDefinedId(Types.TRANSPORT_UPDATE); w.writeUserDefinedId(Types.TRANSPORT_UPDATE);
w.writeListStart(); w.writeListStart();
for(Entry<TransportId, Map<String, String>> e : transports.entrySet()) { for(Entry<TransportId, TransportProperties> e : transports.entrySet()) {
w.writeUserDefinedId(Types.TRANSPORT_PROPERTIES); w.writeUserDefinedId(Types.TRANSPORT_PROPERTIES);
w.writeInt32(e.getKey().getInt()); w.writeInt32(e.getKey().getInt());
w.writeMap(e.getValue()); w.writeMap(e.getValue());

View File

@@ -16,6 +16,7 @@ import java.util.Map;
import junit.framework.TestCase; import junit.framework.TestCase;
import net.sf.briar.api.TransportId; import net.sf.briar.api.TransportId;
import net.sf.briar.api.TransportProperties;
import net.sf.briar.api.crypto.CryptoComponent; import net.sf.briar.api.crypto.CryptoComponent;
import net.sf.briar.api.protocol.Ack; import net.sf.briar.api.protocol.Ack;
import net.sf.briar.api.protocol.Author; import net.sf.briar.api.protocol.Author;
@@ -75,7 +76,7 @@ public class ProtocolIntegrationTest extends TestCase {
private final Message message, message1, message2, message3; private final Message message, message1, message2, message3;
private final String authorName = "Alice"; private final String authorName = "Alice";
private final String messageBody = "Hello world"; private final String messageBody = "Hello world";
private final Map<TransportId, Map<String, String>> transports; private final Map<TransportId, TransportProperties> transports;
public ProtocolIntegrationTest() throws Exception { public ProtocolIntegrationTest() throws Exception {
super(); super();
@@ -116,8 +117,9 @@ public class ProtocolIntegrationTest extends TestCase {
message3 = messageEncoder.encodeMessage(null, group1, message3 = messageEncoder.encodeMessage(null, group1,
groupKeyPair.getPrivate(), author, authorKeyPair.getPrivate(), groupKeyPair.getPrivate(), author, authorKeyPair.getPrivate(),
messageBody.getBytes("UTF-8")); messageBody.getBytes("UTF-8"));
transports = Collections.singletonMap(transportId, TransportProperties p =
Collections.singletonMap("bar", "baz")); new TransportProperties(Collections.singletonMap("bar", "baz"));
transports = Collections.singletonMap(transportId, p);
} }
@Test @Test

View File

@@ -10,7 +10,9 @@ import junit.framework.TestCase;
import net.sf.briar.TestUtils; import net.sf.briar.TestUtils;
import net.sf.briar.api.ContactId; import net.sf.briar.api.ContactId;
import net.sf.briar.api.Rating; import net.sf.briar.api.Rating;
import net.sf.briar.api.TransportConfig;
import net.sf.briar.api.TransportId; import net.sf.briar.api.TransportId;
import net.sf.briar.api.TransportProperties;
import net.sf.briar.api.db.DatabaseComponent; import net.sf.briar.api.db.DatabaseComponent;
import net.sf.briar.api.db.DatabaseListener; import net.sf.briar.api.db.DatabaseListener;
import net.sf.briar.api.db.DatabaseListener.Event; import net.sf.briar.api.db.DatabaseListener.Event;
@@ -54,8 +56,8 @@ public abstract class DatabaseComponentTest extends TestCase {
private final Message message, privateMessage; private final Message message, privateMessage;
private final Group group; private final Group group;
private final TransportId transportId; private final TransportId transportId;
private final Map<TransportId, Map<String, String>> transports; private final Map<TransportId, TransportProperties> transports;
private final Map<TransportId, Map<ContactId, Map<String, String>>> private final Map<TransportId, Map<ContactId, TransportProperties>>
remoteTransports; remoteTransports;
private final byte[] secret; private final byte[] secret;
@@ -76,10 +78,11 @@ public abstract class DatabaseComponentTest extends TestCase {
new TestMessage(messageId, null, null, null, timestamp, raw); new TestMessage(messageId, null, null, null, timestamp, raw);
group = new TestGroup(groupId, "The really exciting group", null); group = new TestGroup(groupId, "The really exciting group", null);
transportId = new TransportId(123); transportId = new TransportId(123);
Map<String, String> properties = Collections.singletonMap("foo", "bar"); TransportProperties p =
transports = Collections.singletonMap(transportId, properties); new TransportProperties(Collections.singletonMap("foo", "bar"));
transports = Collections.singletonMap(transportId, p);
remoteTransports = Collections.singletonMap(transportId, remoteTransports = Collections.singletonMap(transportId,
Collections.singletonMap(contactId, properties)); Collections.singletonMap(contactId, p));
secret = new byte[123]; secret = new byte[123];
} }
@@ -1271,10 +1274,10 @@ public abstract class DatabaseComponentTest extends TestCase {
@Test @Test
public void testTransportPropertiesChangedCallsListeners() public void testTransportPropertiesChangedCallsListeners()
throws Exception { throws Exception {
final Map<String, String> properties = final TransportProperties properties =
Collections.singletonMap("bar", "baz"); new TransportProperties(Collections.singletonMap("bar", "baz"));
final Map<String, String> properties1 = final TransportProperties properties1 =
Collections.singletonMap("baz", "bam"); new TransportProperties(Collections.singletonMap("baz", "bam"));
Mockery context = new Mockery(); Mockery context = new Mockery();
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
final Database<Object> database = context.mock(Database.class); final Database<Object> database = context.mock(Database.class);
@@ -1302,8 +1305,8 @@ public abstract class DatabaseComponentTest extends TestCase {
@Test @Test
public void testTransportPropertiesUnchangedDoesNotCallListeners() public void testTransportPropertiesUnchangedDoesNotCallListeners()
throws Exception { throws Exception {
final Map<String, String> properties = final TransportProperties properties =
Collections.singletonMap("bar", "baz"); new TransportProperties(Collections.singletonMap("bar", "baz"));
Mockery context = new Mockery(); Mockery context = new Mockery();
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
final Database<Object> database = context.mock(Database.class); final Database<Object> database = context.mock(Database.class);
@@ -1327,10 +1330,10 @@ public abstract class DatabaseComponentTest extends TestCase {
@Test @Test
public void testTransportConfigChangedCallsListeners() throws Exception { public void testTransportConfigChangedCallsListeners() throws Exception {
final Map<String, String> config = final TransportConfig config =
Collections.singletonMap("bar", "baz"); new TransportConfig(Collections.singletonMap("bar", "baz"));
final Map<String, String> config1 = final TransportConfig config1 =
Collections.singletonMap("baz", "bam"); new TransportConfig(Collections.singletonMap("baz", "bam"));
Mockery context = new Mockery(); Mockery context = new Mockery();
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
final Database<Object> database = context.mock(Database.class); final Database<Object> database = context.mock(Database.class);
@@ -1356,8 +1359,8 @@ public abstract class DatabaseComponentTest extends TestCase {
@Test @Test
public void testTransportConfigUnchangedDoesNotCallListeners() public void testTransportConfigUnchangedDoesNotCallListeners()
throws Exception { throws Exception {
final Map<String, String> config = final TransportConfig config =
Collections.singletonMap("bar", "baz"); new TransportConfig(Collections.singletonMap("bar", "baz"));
Mockery context = new Mockery(); Mockery context = new Mockery();
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
final Database<Object> database = context.mock(Database.class); final Database<Object> database = context.mock(Database.class);

View File

@@ -21,7 +21,9 @@ import net.sf.briar.TestDatabaseModule;
import net.sf.briar.TestUtils; import net.sf.briar.TestUtils;
import net.sf.briar.api.ContactId; import net.sf.briar.api.ContactId;
import net.sf.briar.api.Rating; import net.sf.briar.api.Rating;
import net.sf.briar.api.TransportConfig;
import net.sf.briar.api.TransportId; import net.sf.briar.api.TransportId;
import net.sf.briar.api.TransportProperties;
import net.sf.briar.api.crypto.Password; import net.sf.briar.api.crypto.Password;
import net.sf.briar.api.db.DbException; import net.sf.briar.api.db.DbException;
import net.sf.briar.api.db.Status; import net.sf.briar.api.db.Status;
@@ -71,8 +73,8 @@ public class H2DatabaseTest extends TestCase {
private final Message message, privateMessage; private final Message message, privateMessage;
private final Group group; private final Group group;
private final TransportId transportId; private final TransportId transportId;
private final Map<TransportId, Map<String, String>> transports; private final Map<TransportId, TransportProperties> transports;
private final Map<TransportId, Map<ContactId, Map<String, String>>> private final Map<TransportId, Map<ContactId, TransportProperties>>
remoteTransports; remoteTransports;
private final Map<Group, Long> subscriptions; private final Map<Group, Long> subscriptions;
private final byte[] secret; private final byte[] secret;
@@ -100,10 +102,11 @@ public class H2DatabaseTest extends TestCase {
new TestMessage(privateMessageId, null, null, null, timestamp, raw); new TestMessage(privateMessageId, null, null, null, timestamp, raw);
group = groupFactory.createGroup(groupId, "Group name", null); group = groupFactory.createGroup(groupId, "Group name", null);
transportId = new TransportId(0); transportId = new TransportId(0);
Map<String, String> properties = Collections.singletonMap("foo", "bar"); TransportProperties p =
transports = Collections.singletonMap(transportId, properties); new TransportProperties(Collections.singletonMap("foo", "bar"));
transports = Collections.singletonMap(transportId, p);
remoteTransports = Collections.singletonMap(transportId, remoteTransports = Collections.singletonMap(transportId,
Collections.singletonMap(contactId, properties)); Collections.singletonMap(contactId, p));
subscriptions = Collections.singletonMap(group, 0L); subscriptions = Collections.singletonMap(group, 0L);
secret = new byte[123]; secret = new byte[123];
} }
@@ -988,9 +991,10 @@ public class H2DatabaseTest extends TestCase {
@Test @Test
public void testUpdateTransportProperties() throws Exception { public void testUpdateTransportProperties() throws Exception {
Map<String, String> properties = Collections.singletonMap("foo", "bar"); TransportProperties properties =
Map<String, String> properties1 = new TransportProperties(Collections.singletonMap("foo", "bar"));
Collections.singletonMap("baz", "bam"); TransportProperties properties1 =
new TransportProperties(Collections.singletonMap("baz", "bam"));
Database<Connection> db = open(false); Database<Connection> db = open(false);
Connection txn = db.startTransaction(); Connection txn = db.startTransaction();
@@ -1001,12 +1005,12 @@ public class H2DatabaseTest extends TestCase {
// Replace the transport properties // Replace the transport properties
TransportId transportId1 = new TransportId(1); TransportId transportId1 = new TransportId(1);
Map<TransportId, Map<String, String>> transports1 = Map<TransportId, TransportProperties> transports1 =
new TreeMap<TransportId, Map<String, String>>(); new TreeMap<TransportId, TransportProperties>();
transports1.put(transportId, properties); transports1.put(transportId, properties);
transports1.put(transportId1, properties1); transports1.put(transportId1, properties1);
Map<TransportId, Map<ContactId, Map<String, String>>> remoteTransports1 Map<TransportId, Map<ContactId, TransportProperties>> remoteTransports1
= new TreeMap<TransportId, Map<ContactId, Map<String, String>>>(); = new TreeMap<TransportId, Map<ContactId, TransportProperties>>();
remoteTransports1.put(transportId, Collections.singletonMap(contactId, remoteTransports1.put(transportId, Collections.singletonMap(contactId,
properties)); properties));
remoteTransports1.put(transportId1, Collections.singletonMap(contactId, remoteTransports1.put(transportId1, Collections.singletonMap(contactId,
@@ -1016,19 +1020,18 @@ public class H2DatabaseTest extends TestCase {
// Remove the transport properties // Remove the transport properties
db.setTransports(txn, contactId, db.setTransports(txn, contactId,
Collections.<TransportId, Map<String, String>>emptyMap(), 2); Collections.<TransportId, TransportProperties>emptyMap(), 2);
assertEquals(Collections.emptyMap(), db.getRemoteTransports(txn)); assertEquals(Collections.emptyMap(), db.getRemoteTransports(txn));
// Set the local transport properties // Set the local transport properties
for(Entry<TransportId, Map<String, String>> e : transports.entrySet()) { for(Entry<TransportId, TransportProperties> e : transports.entrySet()) {
db.setTransportProperties(txn, e.getKey(), e.getValue()); db.setTransportProperties(txn, e.getKey(), e.getValue());
} }
assertEquals(transports, db.getLocalTransports(txn)); assertEquals(transports, db.getLocalTransports(txn));
// Remove the local transport properties // Remove the local transport properties
for(TransportId t : transports.keySet()) { for(TransportId t : transports.keySet()) {
db.setTransportProperties(txn, t, db.setTransportProperties(txn, t, new TransportProperties());
Collections.<String, String>emptyMap());
} }
assertEquals(Collections.emptyMap(), db.getLocalTransports(txn)); assertEquals(Collections.emptyMap(), db.getLocalTransports(txn));
@@ -1039,8 +1042,10 @@ public class H2DatabaseTest extends TestCase {
@Test @Test
public void testUpdateTransportConfig() throws Exception { public void testUpdateTransportConfig() throws Exception {
Map<String, String> config = Collections.singletonMap("foo", "bar"); TransportConfig config =
Map<String, String> config1 = Collections.singletonMap("baz", "bam"); new TransportConfig(Collections.singletonMap("foo", "bar"));
TransportConfig config1 =
new TransportConfig(Collections.singletonMap("baz", "bam"));
Database<Connection> db = open(false); Database<Connection> db = open(false);
Connection txn = db.startTransaction(); Connection txn = db.startTransaction();
@@ -1054,8 +1059,7 @@ public class H2DatabaseTest extends TestCase {
assertEquals(config1, db.getTransportConfig(txn, transportId)); assertEquals(config1, db.getTransportConfig(txn, transportId));
// Remove the transport config // Remove the transport config
db.setTransportConfig(txn, transportId, db.setTransportConfig(txn, transportId, new TransportConfig());
Collections.<String, String>emptyMap());
assertEquals(Collections.emptyMap(), assertEquals(Collections.emptyMap(),
db.getTransportConfig(txn, transportId)); db.getTransportConfig(txn, transportId));
@@ -1065,11 +1069,12 @@ public class H2DatabaseTest extends TestCase {
@Test @Test
public void testTransportsNotUpdatedIfTimestampIsOld() throws Exception { public void testTransportsNotUpdatedIfTimestampIsOld() throws Exception {
Map<String, String> properties = Collections.singletonMap("foo", "bar"); TransportProperties properties =
Map<String, String> properties1 = new TransportProperties(Collections.singletonMap("foo", "bar"));
Collections.singletonMap("baz", "bam"); TransportProperties properties1 =
Map<String, String> properties2 = new TransportProperties(Collections.singletonMap("baz", "bam"));
Collections.singletonMap("quux", "etc"); TransportProperties properties2 =
new TransportProperties(Collections.singletonMap("quux", "etc"));
Database<Connection> db = open(false); Database<Connection> db = open(false);
Connection txn = db.startTransaction(); Connection txn = db.startTransaction();
@@ -1080,12 +1085,12 @@ public class H2DatabaseTest extends TestCase {
// Replace the transport properties using a timestamp of 2 // Replace the transport properties using a timestamp of 2
TransportId transportId1 = new TransportId(1); TransportId transportId1 = new TransportId(1);
Map<TransportId, Map<String, String>> transports1 = Map<TransportId, TransportProperties> transports1 =
new TreeMap<TransportId, Map<String, String>>(); new TreeMap<TransportId, TransportProperties>();
transports1.put(transportId, properties); transports1.put(transportId, properties);
transports1.put(transportId1, properties1); transports1.put(transportId1, properties1);
Map<TransportId, Map<ContactId, Map<String, String>>> remoteTransports1 Map<TransportId, Map<ContactId, TransportProperties>> remoteTransports1
= new TreeMap<TransportId, Map<ContactId, Map<String, String>>>(); = new TreeMap<TransportId, Map<ContactId, TransportProperties>>();
remoteTransports1.put(transportId, Collections.singletonMap(contactId, remoteTransports1.put(transportId, Collections.singletonMap(contactId,
properties)); properties));
remoteTransports1.put(transportId1, Collections.singletonMap(contactId, remoteTransports1.put(transportId1, Collections.singletonMap(contactId,
@@ -1095,8 +1100,8 @@ public class H2DatabaseTest extends TestCase {
// Try to replace the transport properties using a timestamp of 1 // Try to replace the transport properties using a timestamp of 1
TransportId transportId2 = new TransportId(2); TransportId transportId2 = new TransportId(2);
Map<TransportId, Map<String, String>> transports2 = Map<TransportId, TransportProperties> transports2 =
new TreeMap<TransportId, Map<String, String>>(); new TreeMap<TransportId, TransportProperties>();
transports2.put(transportId1, properties1); transports2.put(transportId1, properties1);
transports2.put(transportId2, properties2); transports2.put(transportId2, properties2);
db.setTransports(txn, contactId, transports2, 1); db.setTransports(txn, contactId, transports2, 1);

View File

@@ -10,6 +10,8 @@ import java.util.Map;
import junit.framework.TestCase; import junit.framework.TestCase;
import net.sf.briar.TestUtils; import net.sf.briar.TestUtils;
import net.sf.briar.api.TransportId;
import net.sf.briar.api.TransportProperties;
import net.sf.briar.api.db.DatabaseComponent; import net.sf.briar.api.db.DatabaseComponent;
import net.sf.briar.api.db.DbException; import net.sf.briar.api.db.DbException;
import net.sf.briar.api.invitation.InvitationCallback; import net.sf.briar.api.invitation.InvitationCallback;
@@ -107,8 +109,11 @@ public class InvitationWorkerTest extends TestCase {
private void testInstallerCreation(final boolean createExe, private void testInstallerCreation(final boolean createExe,
final boolean createJar) throws IOException, DbException { final boolean createJar) throws IOException, DbException {
final Map<String, String> transports = TransportProperties properties =
Collections.singletonMap("foo", "bar"); new TransportProperties(Collections.singletonMap("foo", "bar"));
TransportId transportId = new TransportId(123);
final Map<TransportId, TransportProperties> transports =
Collections.singletonMap(transportId, properties);
final File setup = new File(testDir, "setup.dat"); final File setup = new File(testDir, "setup.dat");
TestUtils.createFile(setup, "foo bar baz"); TestUtils.createFile(setup, "foo bar baz");
final File invitation = new File(testDir, "invitation.dat"); final File invitation = new File(testDir, "invitation.dat");

View File

@@ -1,39 +0,0 @@
package net.sf.briar.plugins;
import java.util.Map;
import net.sf.briar.api.ContactId;
import net.sf.briar.api.plugins.StreamTransportCallback;
import net.sf.briar.api.transport.StreamTransportConnection;
public class StubStreamCallback implements StreamTransportCallback {
public Map<String, String> localProperties = null;
public volatile int incomingConnections = 0;
public void setLocalProperties(Map<String, String> properties) {
localProperties = properties;
}
public void setConfig(Map<String, String> config) {
}
public void showMessage(String... message) {
}
public boolean showConfirmationMessage(String... message) {
return false;
}
public int showChoice(String[] choices, String... message) {
return -1;
}
public void incomingConnectionCreated(StreamTransportConnection c) {
incomingConnections++;
}
public void outgoingConnectionCreated(ContactId contactId,
StreamTransportConnection c) {
}
}

View File

@@ -1,13 +1,13 @@
package net.sf.briar.plugins.bluetooth; package net.sf.briar.plugins.bluetooth;
import java.io.PrintStream; import java.io.PrintStream;
import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Scanner; import java.util.Scanner;
import java.util.TreeMap;
import net.sf.briar.api.ContactId; import net.sf.briar.api.ContactId;
import net.sf.briar.api.TransportConfig;
import net.sf.briar.api.TransportProperties;
import net.sf.briar.api.plugins.StreamTransportCallback; import net.sf.briar.api.plugins.StreamTransportCallback;
import net.sf.briar.api.transport.StreamTransportConnection; import net.sf.briar.api.transport.StreamTransportConnection;
import net.sf.briar.plugins.ImmediateExecutor; import net.sf.briar.plugins.ImmediateExecutor;
@@ -24,16 +24,16 @@ public class BluetoothClientTest {
System.exit(1); System.exit(1);
} }
ContactId contactId = new ContactId(0); ContactId contactId = new ContactId(0);
Map<String, String> localProperties = Collections.emptyMap(); TransportProperties localProperties = new TransportProperties();
Map<ContactId, Map<String, String>> remoteProperties = Map<ContactId, TransportProperties> remoteProperties =
new HashMap<ContactId, Map<String, String>>(); new HashMap<ContactId, TransportProperties>();
Map<String, String> config = Collections.emptyMap(); TransportConfig config = new TransportConfig();
StreamTransportCallback callback = new ClientCallback(); StreamTransportCallback callback = new ClientCallback();
// Store the server's Bluetooth address and UUID // Store the server's Bluetooth address and UUID
Map<String, String> properties = new TreeMap<String, String>(); TransportProperties p = new TransportProperties();
properties.put("address", args[0]); p.put("address", args[0]);
properties.put("uuid", BluetoothServerTest.UUID); p.put("uuid", BluetoothServerTest.UUID);
remoteProperties.put(contactId, properties); remoteProperties.put(contactId, p);
// Create the plugin // Create the plugin
BluetoothPlugin plugin = BluetoothPlugin plugin =
new BluetoothPlugin(new ImmediateExecutor(), callback, 0L); new BluetoothPlugin(new ImmediateExecutor(), callback, 0L);
@@ -66,9 +66,9 @@ public class BluetoothClientTest {
private static class ClientCallback implements StreamTransportCallback { private static class ClientCallback implements StreamTransportCallback {
public void setLocalProperties(Map<String, String> properties) {} public void setLocalProperties(TransportProperties p) {}
public void setConfig(Map<String, String> config) {} public void setConfig(TransportConfig c) {}
public void showMessage(String... message) {} public void showMessage(String... message) {}

View File

@@ -5,9 +5,10 @@ import java.io.PrintStream;
import java.util.Collections; import java.util.Collections;
import java.util.Map; import java.util.Map;
import java.util.Scanner; import java.util.Scanner;
import java.util.TreeMap;
import net.sf.briar.api.ContactId; import net.sf.briar.api.ContactId;
import net.sf.briar.api.TransportConfig;
import net.sf.briar.api.TransportProperties;
import net.sf.briar.api.plugins.StreamTransportCallback; import net.sf.briar.api.plugins.StreamTransportCallback;
import net.sf.briar.api.transport.StreamTransportConnection; import net.sf.briar.api.transport.StreamTransportConnection;
import net.sf.briar.plugins.ImmediateExecutor; import net.sf.briar.plugins.ImmediateExecutor;
@@ -20,10 +21,10 @@ public class BluetoothServerTest {
public static final String CHALLENGE = "Potatoes!"; public static final String CHALLENGE = "Potatoes!";
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
Map<String, String> localProperties = Collections.emptyMap(); TransportProperties localProperties = new TransportProperties();
Map<ContactId, Map<String, String>> remoteProperties = Map<ContactId, TransportProperties> remoteProperties =
Collections.emptyMap(); Collections.emptyMap();
Map<String, String> config = new TreeMap<String, String>(); TransportConfig config = new TransportConfig();
StreamTransportCallback callback = new ServerCallback(); StreamTransportCallback callback = new ServerCallback();
// Store the UUID // Store the UUID
config.put("uuid", UUID); config.put("uuid", UUID);
@@ -45,9 +46,9 @@ public class BluetoothServerTest {
private static class ServerCallback implements StreamTransportCallback { private static class ServerCallback implements StreamTransportCallback {
public void setLocalProperties(Map<String, String> properties) {} public void setLocalProperties(TransportProperties p) {}
public void setConfig(Map<String, String> config) {} public void setConfig(TransportConfig c) {}
public void showMessage(String... message) {} public void showMessage(String... message) {}

View File

@@ -8,12 +8,13 @@ import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.Executor; import java.util.concurrent.Executor;
import junit.framework.TestCase; import junit.framework.TestCase;
import net.sf.briar.TestUtils; import net.sf.briar.TestUtils;
import net.sf.briar.api.ContactId; import net.sf.briar.api.ContactId;
import net.sf.briar.api.TransportConfig;
import net.sf.briar.api.TransportProperties;
import net.sf.briar.api.plugins.BatchTransportCallback; import net.sf.briar.api.plugins.BatchTransportCallback;
import net.sf.briar.api.transport.BatchTransportWriter; import net.sf.briar.api.transport.BatchTransportWriter;
import net.sf.briar.api.transport.TransportConstants; import net.sf.briar.api.transport.TransportConstants;
@@ -31,16 +32,16 @@ public class RemovableDrivePluginTest extends TestCase {
private final File testDir = TestUtils.getTestDirectory(); private final File testDir = TestUtils.getTestDirectory();
private final ContactId contactId = new ContactId(0); private final ContactId contactId = new ContactId(0);
private Map<String, String> localProperties = null; private TransportProperties localProperties = null;
private Map<ContactId, Map<String, String>> remoteProperties = null; private Map<ContactId, TransportProperties> remoteProperties = null;
private Map<String, String> config = null; private TransportConfig config = null;
@Before @Before
public void setUp() { public void setUp() {
localProperties = new TreeMap<String, String>(); localProperties = new TransportProperties();
remoteProperties = new HashMap<ContactId, Map<String, String>>(); remoteProperties = new HashMap<ContactId, TransportProperties>();
remoteProperties.put(contactId, new TreeMap<String, String>()); remoteProperties.put(contactId, new TransportProperties());
config = new TreeMap<String, String>(); config = new TransportConfig();
testDir.mkdirs(); testDir.mkdirs();
} }

View File

@@ -6,16 +6,17 @@ import java.net.ServerSocket;
import java.net.Socket; import java.net.Socket;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.CountDownLatch; import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import junit.framework.TestCase; import junit.framework.TestCase;
import net.sf.briar.api.ContactId; import net.sf.briar.api.ContactId;
import net.sf.briar.api.TransportConfig;
import net.sf.briar.api.TransportProperties;
import net.sf.briar.api.plugins.StreamTransportCallback;
import net.sf.briar.api.transport.StreamTransportConnection; import net.sf.briar.api.transport.StreamTransportConnection;
import net.sf.briar.plugins.ImmediateExecutor; import net.sf.briar.plugins.ImmediateExecutor;
import net.sf.briar.plugins.StubStreamCallback;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@@ -24,21 +25,21 @@ public class SimpleSocketPluginTest extends TestCase {
private final ContactId contactId = new ContactId(0); private final ContactId contactId = new ContactId(0);
private Map<String, String> localProperties = null; private TransportProperties localProperties = null;
private Map<ContactId, Map<String, String>> remoteProperties = null; private Map<ContactId, TransportProperties> remoteProperties = null;
private Map<String, String> config = null; private TransportConfig config = null;
@Before @Before
public void setUp() { public void setUp() {
localProperties = new TreeMap<String, String>(); localProperties = new TransportProperties();
remoteProperties = new HashMap<ContactId, Map<String, String>>(); remoteProperties = new HashMap<ContactId, TransportProperties>();
remoteProperties.put(contactId, new TreeMap<String, String>()); remoteProperties.put(contactId, new TransportProperties());
config = new TreeMap<String, String>(); config = new TransportConfig();
} }
@Test @Test
public void testIncomingConnection() throws Exception { public void testIncomingConnection() throws Exception {
StubStreamCallback callback = new StubStreamCallback(); StubCallback callback = new StubCallback();
localProperties.put("host", "127.0.0.1"); localProperties.put("host", "127.0.0.1");
localProperties.put("port", "0"); localProperties.put("port", "0");
SimpleSocketPlugin plugin = SimpleSocketPlugin plugin =
@@ -74,7 +75,7 @@ public class SimpleSocketPluginTest extends TestCase {
@Test @Test
public void testOutgoingConnection() throws Exception { public void testOutgoingConnection() throws Exception {
StubStreamCallback callback = new StubStreamCallback(); StubCallback callback = new StubCallback();
SimpleSocketPlugin plugin = SimpleSocketPlugin plugin =
new SimpleSocketPlugin(new ImmediateExecutor(), callback, 0L); new SimpleSocketPlugin(new ImmediateExecutor(), callback, 0L);
plugin.start(localProperties, remoteProperties, config); plugin.start(localProperties, remoteProperties, config);
@@ -96,7 +97,7 @@ public class SimpleSocketPluginTest extends TestCase {
} }
}.start(); }.start();
// Tell the plugin about the port // Tell the plugin about the port
Map<String, String> properties = new TreeMap<String, String>(); TransportProperties properties = new TransportProperties();
properties.put("host", "127.0.0.1"); properties.put("host", "127.0.0.1");
properties.put("port", String.valueOf(port)); properties.put("port", String.valueOf(port));
plugin.setRemoteProperties(contactId, properties); plugin.setRemoteProperties(contactId, properties);
@@ -114,7 +115,7 @@ public class SimpleSocketPluginTest extends TestCase {
@Test @Test
public void testUpdatingPropertiesReopensSocket() throws Exception { public void testUpdatingPropertiesReopensSocket() throws Exception {
StubStreamCallback callback = new StubStreamCallback(); StubCallback callback = new StubCallback();
localProperties.put("host", "127.0.0.1"); localProperties.put("host", "127.0.0.1");
localProperties.put("port", "0"); localProperties.put("port", "0");
SimpleSocketPlugin plugin = SimpleSocketPlugin plugin =
@@ -169,4 +170,36 @@ public class SimpleSocketPluginTest extends TestCase {
fail(); fail();
} catch(IOException expected) {} } catch(IOException expected) {}
} }
private static class StubCallback implements StreamTransportCallback {
public TransportProperties localProperties = null;
public volatile int incomingConnections = 0;
public void setLocalProperties(TransportProperties properties) {
localProperties = properties;
}
public void setConfig(TransportConfig config) {
}
public void showMessage(String... message) {
}
public boolean showConfirmationMessage(String... message) {
return false;
}
public int showChoice(String[] choices, String... message) {
return -1;
}
public void incomingConnectionCreated(StreamTransportConnection c) {
incomingConnections++;
}
public void outgoingConnectionCreated(ContactId contactId,
StreamTransportConnection c) {
}
}
} }

View File

@@ -9,6 +9,7 @@ import java.util.Map;
import junit.framework.TestCase; import junit.framework.TestCase;
import net.sf.briar.TestUtils; import net.sf.briar.TestUtils;
import net.sf.briar.api.TransportId; import net.sf.briar.api.TransportId;
import net.sf.briar.api.TransportProperties;
import net.sf.briar.api.protocol.Ack; import net.sf.briar.api.protocol.Ack;
import net.sf.briar.api.protocol.Batch; import net.sf.briar.api.protocol.Batch;
import net.sf.briar.api.protocol.BatchId; import net.sf.briar.api.protocol.BatchId;
@@ -49,7 +50,7 @@ public class ProtocolReadWriteTest extends TestCase {
private final BitSet bitSet; private final BitSet bitSet;
private final Map<Group, Long> subscriptions; private final Map<Group, Long> subscriptions;
private final TransportId transportId; private final TransportId transportId;
private final Map<TransportId, Map<String, String>> transports; private final Map<TransportId, TransportProperties> transports;
private final long timestamp = System.currentTimeMillis(); private final long timestamp = System.currentTimeMillis();
public ProtocolReadWriteTest() throws Exception { public ProtocolReadWriteTest() throws Exception {
@@ -70,8 +71,9 @@ public class ProtocolReadWriteTest extends TestCase {
bitSet.set(7); bitSet.set(7);
subscriptions = Collections.singletonMap(group, 123L); subscriptions = Collections.singletonMap(group, 123L);
transportId = new TransportId(123); transportId = new TransportId(123);
transports = Collections.singletonMap(transportId, TransportProperties p =
Collections.singletonMap("bar", "baz")); new TransportProperties(Collections.singletonMap("bar", "baz"));
transports = Collections.singletonMap(transportId, p);
} }
@Test @Test

View File

@@ -9,6 +9,7 @@ import java.util.TreeMap;
import junit.framework.TestCase; import junit.framework.TestCase;
import net.sf.briar.TestUtils; import net.sf.briar.TestUtils;
import net.sf.briar.api.TransportId; import net.sf.briar.api.TransportId;
import net.sf.briar.api.TransportProperties;
import net.sf.briar.api.crypto.CryptoComponent; import net.sf.briar.api.crypto.CryptoComponent;
import net.sf.briar.api.protocol.Author; import net.sf.briar.api.protocol.Author;
import net.sf.briar.api.protocol.AuthorFactory; import net.sf.briar.api.protocol.AuthorFactory;
@@ -191,18 +192,18 @@ public class ConstantsTest extends TestCase {
public void testTransportsFitIntoUpdate() throws Exception { public void testTransportsFitIntoUpdate() throws Exception {
// Create the maximum number of plugins, each with the maximum number // Create the maximum number of plugins, each with the maximum number
// of maximum-length properties // of maximum-length properties
Map<TransportId, Map<String, String>> transports = Map<TransportId, TransportProperties> transports =
new TreeMap<TransportId, Map<String, String>>(); new TreeMap<TransportId, TransportProperties>();
for(int i = 0; i < TransportUpdate.MAX_PLUGINS_PER_UPDATE; i++) { for(int i = 0; i < TransportUpdate.MAX_PLUGINS_PER_UPDATE; i++) {
Map<String, String> properties = new TreeMap<String, String>(); TransportProperties p = new TransportProperties();
for(int j = 0; j < TransportUpdate.MAX_PROPERTIES_PER_PLUGIN; j++) { for(int j = 0; j < TransportUpdate.MAX_PROPERTIES_PER_PLUGIN; j++) {
String key = createRandomString( String key = createRandomString(
TransportUpdate.MAX_KEY_OR_VALUE_LENGTH); TransportUpdate.MAX_KEY_OR_VALUE_LENGTH);
String value = createRandomString( String value = createRandomString(
TransportUpdate.MAX_KEY_OR_VALUE_LENGTH); TransportUpdate.MAX_KEY_OR_VALUE_LENGTH);
properties.put(key, value); p.put(key, value);
} }
transports.put(new TransportId(i), properties); transports.put(new TransportId(i), p);
} }
// Add the transports to an update // Add the transports to an update
ByteArrayOutputStream out = new ByteArrayOutputStream( ByteArrayOutputStream out = new ByteArrayOutputStream(

View File

@@ -13,6 +13,7 @@ import net.sf.briar.TestDatabaseModule;
import net.sf.briar.TestUtils; import net.sf.briar.TestUtils;
import net.sf.briar.api.ContactId; import net.sf.briar.api.ContactId;
import net.sf.briar.api.TransportId; import net.sf.briar.api.TransportId;
import net.sf.briar.api.TransportProperties;
import net.sf.briar.api.db.DatabaseComponent; import net.sf.briar.api.db.DatabaseComponent;
import net.sf.briar.api.db.DatabaseListener; import net.sf.briar.api.db.DatabaseListener;
import net.sf.briar.api.protocol.Message; import net.sf.briar.api.protocol.Message;
@@ -45,7 +46,7 @@ public class BatchConnectionReadWriteTest extends TestCase {
private final File aliceDir = new File(testDir, "alice"); private final File aliceDir = new File(testDir, "alice");
private final File bobDir = new File(testDir, "bob"); private final File bobDir = new File(testDir, "bob");
private final TransportId transportId = new TransportId(123); private final TransportId transportId = new TransportId(123);
private final Map<TransportId, Map<String, String>> transports = private final Map<TransportId, TransportProperties> transports =
Collections.emptyMap(); Collections.emptyMap();
private final byte[] aliceSecret, bobSecret; private final byte[] aliceSecret, bobSecret;