mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-16 04:39:54 +01:00
Use dedicated classes for transport properties and configs.
This commit is contained in:
@@ -6,7 +6,9 @@ import java.util.Map;
|
||||
|
||||
import net.sf.briar.api.ContactId;
|
||||
import net.sf.briar.api.Rating;
|
||||
import net.sf.briar.api.TransportConfig;
|
||||
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.Status;
|
||||
import net.sf.briar.api.protocol.AuthorId;
|
||||
@@ -86,7 +88,7 @@ interface Database<T> {
|
||||
* Locking: contacts write, transports write.
|
||||
*/
|
||||
ContactId addContact(T txn,
|
||||
Map<TransportId, Map<String, String>> transports, byte[] secret)
|
||||
Map<TransportId, TransportProperties> transports, byte[] secret)
|
||||
throws DbException;
|
||||
|
||||
/**
|
||||
@@ -216,7 +218,7 @@ interface Database<T> {
|
||||
* <p>
|
||||
* Locking: transports read.
|
||||
*/
|
||||
Map<TransportId, Map<String, String>> getLocalTransports(T txn)
|
||||
Map<TransportId, TransportProperties> getLocalTransports(T txn)
|
||||
throws DbException;
|
||||
|
||||
/**
|
||||
@@ -282,7 +284,7 @@ interface Database<T> {
|
||||
* <p>
|
||||
* Locking: contacts read, transports read.
|
||||
*/
|
||||
Map<TransportId, Map<ContactId, Map<String, String>>>
|
||||
Map<TransportId, Map<ContactId, TransportProperties>>
|
||||
getRemoteTransports(T txn) throws DbException;
|
||||
|
||||
/**
|
||||
@@ -338,8 +340,7 @@ interface Database<T> {
|
||||
* <p>
|
||||
* Locking: transports read.
|
||||
*/
|
||||
Map<String, String> getTransportConfig(T txn, TransportId t)
|
||||
throws DbException;
|
||||
TransportConfig getTransportConfig(T txn, TransportId t) throws DbException;
|
||||
|
||||
/**
|
||||
* Returns the contacts to which the given group is visible.
|
||||
@@ -480,7 +481,7 @@ interface Database<T> {
|
||||
* <p>
|
||||
* Locking: transports write.
|
||||
*/
|
||||
void setTransportConfig(T txn, TransportId t, Map<String, String> config)
|
||||
void setTransportConfig(T txn, TransportId t, TransportConfig config)
|
||||
throws DbException;
|
||||
|
||||
/**
|
||||
@@ -490,7 +491,7 @@ interface Database<T> {
|
||||
* Locking: transports write.
|
||||
*/
|
||||
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
|
||||
@@ -500,7 +501,7 @@ interface Database<T> {
|
||||
* Locking: contacts read, transports write.
|
||||
*/
|
||||
void setTransports(T txn, ContactId c,
|
||||
Map<TransportId, Map<String, String>> transports, long timestamp)
|
||||
Map<TransportId, TransportProperties> transports, long timestamp)
|
||||
throws DbException;
|
||||
|
||||
/**
|
||||
|
||||
@@ -21,7 +21,9 @@ import java.util.logging.Logger;
|
||||
import net.sf.briar.api.Bytes;
|
||||
import net.sf.briar.api.ContactId;
|
||||
import net.sf.briar.api.Rating;
|
||||
import net.sf.briar.api.TransportConfig;
|
||||
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.DatabaseListener;
|
||||
import net.sf.briar.api.db.DatabaseListener.Event;
|
||||
@@ -119,7 +121,7 @@ DatabaseCleaner.Callback {
|
||||
}
|
||||
|
||||
public ContactId addContact(
|
||||
Map<TransportId, Map<String, String>> transports, byte[] secret)
|
||||
Map<TransportId, TransportProperties> transports, byte[] secret)
|
||||
throws DbException {
|
||||
if(LOG.isLoggable(Level.FINE)) LOG.fine("Adding contact");
|
||||
ContactId c;
|
||||
@@ -606,7 +608,7 @@ DatabaseCleaner.Callback {
|
||||
|
||||
public void generateTransportUpdate(ContactId c, TransportWriter t)
|
||||
throws DbException, IOException {
|
||||
Map<TransportId, Map<String, String>> transports;
|
||||
Map<TransportId, TransportProperties> transports;
|
||||
long timestamp;
|
||||
contactLock.readLock().lock();
|
||||
try {
|
||||
@@ -699,13 +701,13 @@ DatabaseCleaner.Callback {
|
||||
}
|
||||
}
|
||||
|
||||
public Map<TransportId, Map<String, String>> getLocalTransports()
|
||||
public Map<TransportId, TransportProperties> getLocalTransports()
|
||||
throws DbException {
|
||||
transportLock.readLock().lock();
|
||||
try {
|
||||
T txn = db.startTransaction();
|
||||
try {
|
||||
Map<TransportId, Map<String, String>> transports =
|
||||
Map<TransportId, TransportProperties> transports =
|
||||
db.getLocalTransports(txn);
|
||||
db.commitTransaction(txn);
|
||||
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 {
|
||||
contactLock.readLock().lock();
|
||||
try {
|
||||
@@ -743,7 +745,7 @@ DatabaseCleaner.Callback {
|
||||
try {
|
||||
T txn = db.startTransaction();
|
||||
try {
|
||||
Map<TransportId, Map<ContactId, Map<String, String>>>
|
||||
Map<TransportId, Map<ContactId, TransportProperties>>
|
||||
transports = db.getRemoteTransports(txn);
|
||||
db.commitTransaction(txn);
|
||||
return transports;
|
||||
@@ -794,13 +796,13 @@ DatabaseCleaner.Callback {
|
||||
}
|
||||
}
|
||||
|
||||
public Map<String, String> getTransportConfig(TransportId t)
|
||||
public TransportConfig getTransportConfig(TransportId t)
|
||||
throws DbException {
|
||||
transportLock.readLock().lock();
|
||||
try {
|
||||
T txn = db.startTransaction();
|
||||
try {
|
||||
Map<String, String> config = db.getTransportConfig(txn, t);
|
||||
TransportConfig config = db.getTransportConfig(txn, t);
|
||||
db.commitTransaction(txn);
|
||||
return config;
|
||||
} catch(DbException e) {
|
||||
@@ -1045,7 +1047,7 @@ DatabaseCleaner.Callback {
|
||||
try {
|
||||
T txn = db.startTransaction();
|
||||
try {
|
||||
Map<TransportId, Map<String, String>> transports =
|
||||
Map<TransportId, TransportProperties> transports =
|
||||
t.getTransports();
|
||||
db.setTransports(txn, c, transports, t.getTimestamp());
|
||||
if(LOG.isLoggable(Level.FINE))
|
||||
@@ -1219,14 +1221,14 @@ DatabaseCleaner.Callback {
|
||||
+ indirect + " indirectly");
|
||||
}
|
||||
|
||||
public void setTransportConfig(TransportId t,
|
||||
Map<String, String> config) throws DbException {
|
||||
public void setTransportConfig(TransportId t, TransportConfig config)
|
||||
throws DbException {
|
||||
boolean changed = false;
|
||||
transportLock.writeLock().lock();
|
||||
try {
|
||||
T txn = db.startTransaction();
|
||||
try {
|
||||
Map<String, String> old = db.getTransportConfig(txn, t);
|
||||
TransportConfig old = db.getTransportConfig(txn, t);
|
||||
if(!config.equals(old)) {
|
||||
db.setTransportConfig(txn, t, config);
|
||||
changed = true;
|
||||
@@ -1244,15 +1246,15 @@ DatabaseCleaner.Callback {
|
||||
}
|
||||
|
||||
public void setTransportProperties(TransportId t,
|
||||
Map<String, String> properties) throws DbException {
|
||||
TransportProperties properties) throws DbException {
|
||||
boolean changed = false;
|
||||
transportLock.writeLock().lock();
|
||||
try {
|
||||
T txn = db.startTransaction();
|
||||
try {
|
||||
Map<TransportId, Map<String, String>> transports =
|
||||
Map<TransportId, TransportProperties> transports =
|
||||
db.getLocalTransports(txn);
|
||||
Map<String, String> old = transports.get(t);
|
||||
TransportProperties old = transports.get(t);
|
||||
if(!properties.equals(old)) {
|
||||
db.setTransportProperties(txn, t, properties);
|
||||
changed = true;
|
||||
|
||||
@@ -22,7 +22,9 @@ import java.util.logging.Logger;
|
||||
|
||||
import net.sf.briar.api.ContactId;
|
||||
import net.sf.briar.api.Rating;
|
||||
import net.sf.briar.api.TransportConfig;
|
||||
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.Status;
|
||||
import net.sf.briar.api.protocol.AuthorId;
|
||||
@@ -457,7 +459,7 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
}
|
||||
|
||||
public ContactId addContact(Connection txn,
|
||||
Map<TransportId, Map<String, String>> transports, byte[] secret)
|
||||
Map<TransportId, TransportProperties> transports, byte[] secret)
|
||||
throws DbException {
|
||||
PreparedStatement ps = null;
|
||||
ResultSet rs = null;
|
||||
@@ -488,7 +490,7 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
ps = txn.prepareStatement(sql);
|
||||
ps.setInt(1, c.getInt());
|
||||
int batchSize = 0;
|
||||
for(Entry<TransportId, Map<String, String>> e
|
||||
for(Entry<TransportId, TransportProperties> e
|
||||
: transports.entrySet()) {
|
||||
ps.setInt(2, e.getKey().getInt());
|
||||
for(Entry<String, String> e1 : e.getValue().entrySet()) {
|
||||
@@ -916,7 +918,7 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
} else return f.length();
|
||||
}
|
||||
|
||||
public Map<TransportId, Map<String, String>> getLocalTransports(
|
||||
public Map<TransportId, TransportProperties> getLocalTransports(
|
||||
Connection txn) throws DbException {
|
||||
PreparedStatement ps = null;
|
||||
ResultSet rs = null;
|
||||
@@ -925,14 +927,14 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
+ " ORDER BY transportId";
|
||||
ps = txn.prepareStatement(sql);
|
||||
rs = ps.executeQuery();
|
||||
Map<TransportId, Map<String, String>> transports =
|
||||
new TreeMap<TransportId, Map<String, String>>();
|
||||
Map<String, String> properties = null;
|
||||
Map<TransportId, TransportProperties> transports =
|
||||
new TreeMap<TransportId, TransportProperties>();
|
||||
TransportProperties properties = null;
|
||||
TransportId lastId = null;
|
||||
while(rs.next()) {
|
||||
TransportId id = new TransportId(rs.getInt(1));
|
||||
if(!id.equals(lastId)) {
|
||||
properties = new TreeMap<String, String>();
|
||||
properties = new TransportProperties();
|
||||
transports.put(id, properties);
|
||||
}
|
||||
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 {
|
||||
PreparedStatement ps = null;
|
||||
ResultSet rs = null;
|
||||
@@ -1222,22 +1224,22 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
+ " ORDER BY transportId";
|
||||
ps = txn.prepareStatement(sql);
|
||||
rs = ps.executeQuery();
|
||||
Map<TransportId, Map<ContactId, Map<String, String>>> transports =
|
||||
new TreeMap<TransportId, Map<ContactId, Map<String, String>>>();
|
||||
Map<ContactId, Map<String, String>> contacts = null;
|
||||
Map<String, String> properties = null;
|
||||
Map<TransportId, Map<ContactId, TransportProperties>> transports =
|
||||
new TreeMap<TransportId, Map<ContactId, TransportProperties>>();
|
||||
Map<ContactId, TransportProperties> contacts = null;
|
||||
TransportProperties properties = null;
|
||||
TransportId lastTransportId = null;
|
||||
ContactId lastContactId = null;
|
||||
while(rs.next()) {
|
||||
TransportId transportId = new TransportId(rs.getInt(1));
|
||||
if(!transportId.equals(lastTransportId)) {
|
||||
contacts = new HashMap<ContactId, Map<String, String>>();
|
||||
contacts = new HashMap<ContactId, TransportProperties>();
|
||||
transports.put(transportId, contacts);
|
||||
lastContactId = null;
|
||||
}
|
||||
ContactId contactId = new ContactId(rs.getInt(2));
|
||||
if(!contactId.equals(lastContactId)) {
|
||||
properties = new TreeMap<String, String>();
|
||||
properties = new TransportProperties();
|
||||
contacts.put(contactId, properties);
|
||||
}
|
||||
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 {
|
||||
PreparedStatement ps = null;
|
||||
ResultSet rs = null;
|
||||
@@ -1477,7 +1479,7 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
ps = txn.prepareStatement(sql);
|
||||
ps.setInt(1, t.getInt());
|
||||
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));
|
||||
rs.close();
|
||||
ps.close();
|
||||
@@ -2050,7 +2052,7 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
}
|
||||
|
||||
public void setTransportConfig(Connection txn, TransportId t,
|
||||
Map<String, String> config) throws DbException {
|
||||
TransportConfig config) throws DbException {
|
||||
setTransportDetails(txn, t, config, "transportConfig");
|
||||
}
|
||||
|
||||
@@ -2088,12 +2090,12 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
}
|
||||
|
||||
public void setTransportProperties(Connection txn, TransportId t,
|
||||
Map<String, String> properties) throws DbException {
|
||||
TransportProperties properties) throws DbException {
|
||||
setTransportDetails(txn, t, properties, "transports");
|
||||
}
|
||||
|
||||
public void setTransports(Connection txn, ContactId c,
|
||||
Map<TransportId, Map<String, String>> transports, long timestamp)
|
||||
Map<TransportId, TransportProperties> transports, long timestamp)
|
||||
throws DbException {
|
||||
PreparedStatement ps = null;
|
||||
ResultSet rs = null;
|
||||
@@ -2123,7 +2125,7 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
ps = txn.prepareStatement(sql);
|
||||
ps.setInt(1, c.getInt());
|
||||
int batchSize = 0;
|
||||
for(Entry<TransportId, Map<String, String>> e
|
||||
for(Entry<TransportId, TransportProperties> e
|
||||
: transports.entrySet()) {
|
||||
ps.setInt(2, e.getKey().getInt());
|
||||
for(Entry<String, String> e1 : e.getValue().entrySet()) {
|
||||
|
||||
@@ -9,6 +9,7 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
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.DbException;
|
||||
import net.sf.briar.api.invitation.InvitationCallback;
|
||||
@@ -71,7 +72,7 @@ class InvitationWorker implements Runnable {
|
||||
File invitationDat = new File(dir, "invitation.dat");
|
||||
callback.encryptingFile(invitationDat);
|
||||
// FIXME: Create a real invitation
|
||||
Map<TransportId, Map<String, String>> transports;
|
||||
Map<TransportId, TransportProperties> transports;
|
||||
try {
|
||||
transports = db.getLocalTransports();
|
||||
} catch(DbException e) {
|
||||
|
||||
@@ -1,13 +1,12 @@
|
||||
package net.sf.briar.plugins;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.concurrent.Executor;
|
||||
|
||||
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;
|
||||
|
||||
public abstract class AbstractPlugin implements TransportPlugin {
|
||||
@@ -15,31 +14,23 @@ public abstract class AbstractPlugin implements TransportPlugin {
|
||||
protected final Executor executor;
|
||||
|
||||
// These fields should be accessed with this's lock held
|
||||
protected Map<String, String> localProperties = null;
|
||||
protected Map<ContactId, Map<String, String>> remoteProperties = null;
|
||||
protected Map<String, String> config = null;
|
||||
protected TransportProperties localProperties = null;
|
||||
protected Map<ContactId, TransportProperties> remoteProperties = null;
|
||||
protected TransportConfig config = null;
|
||||
protected boolean started = false;
|
||||
|
||||
protected AbstractPlugin(Executor executor) {
|
||||
this.executor = executor;
|
||||
}
|
||||
|
||||
public synchronized void start(Map<String, String> localProperties,
|
||||
Map<ContactId, Map<String, String>> remoteProperties,
|
||||
Map<String, String> config) throws IOException {
|
||||
public synchronized void start(TransportProperties localProperties,
|
||||
Map<ContactId, TransportProperties> remoteProperties,
|
||||
TransportConfig config) throws IOException {
|
||||
if(started) throw new IllegalStateException();
|
||||
started = true;
|
||||
this.localProperties = Collections.unmodifiableMap(localProperties);
|
||||
// Copy the remoteProperties map to make its values unmodifiable
|
||||
int size = remoteProperties.size();
|
||||
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);
|
||||
this.localProperties = localProperties;
|
||||
this.remoteProperties = remoteProperties;
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
public synchronized void stop() throws IOException {
|
||||
@@ -47,20 +38,19 @@ public abstract class AbstractPlugin implements TransportPlugin {
|
||||
started = false;
|
||||
}
|
||||
|
||||
public synchronized void setLocalProperties(
|
||||
Map<String, String> properties) {
|
||||
public synchronized void setLocalProperties(TransportProperties p) {
|
||||
if(!started) throw new IllegalStateException();
|
||||
localProperties = Collections.unmodifiableMap(properties);
|
||||
localProperties = p;
|
||||
}
|
||||
|
||||
public synchronized void setRemoteProperties(ContactId c,
|
||||
Map<String, String> properties) {
|
||||
TransportProperties p) {
|
||||
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();
|
||||
this.config = Collections.unmodifiableMap(config);
|
||||
this.config = c;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,7 +7,6 @@ import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Random;
|
||||
import java.util.TreeMap;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
@@ -20,7 +19,9 @@ import javax.microedition.io.StreamConnection;
|
||||
import javax.microedition.io.StreamConnectionNotifier;
|
||||
|
||||
import net.sf.briar.api.ContactId;
|
||||
import net.sf.briar.api.TransportConfig;
|
||||
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.StreamTransportPlugin;
|
||||
import net.sf.briar.api.transport.StreamTransportConnection;
|
||||
@@ -54,9 +55,9 @@ class BluetoothPlugin extends AbstractPlugin implements StreamTransportPlugin {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void start(Map<String, String> localProperties,
|
||||
Map<ContactId, Map<String, String>> remoteProperties,
|
||||
Map<String, String> config) throws IOException {
|
||||
public void start(TransportProperties localProperties,
|
||||
Map<ContactId, TransportProperties> remoteProperties,
|
||||
TransportConfig config) throws IOException {
|
||||
// Initialise the Bluetooth stack
|
||||
try {
|
||||
synchronized(this) {
|
||||
@@ -133,13 +134,13 @@ class BluetoothPlugin extends AbstractPlugin implements StreamTransportPlugin {
|
||||
byte[] b = new byte[16];
|
||||
new Random().nextBytes(b); // FIXME: Use a SecureRandom?
|
||||
String uuid = StringUtils.toHexString(b);
|
||||
Map<String, String> m;
|
||||
TransportConfig c;
|
||||
synchronized(this) {
|
||||
if(!started) return uuid;
|
||||
m = new TreeMap<String, String>(config);
|
||||
c = new TransportConfig(config);
|
||||
}
|
||||
m.put("uuid", uuid);
|
||||
callback.setConfig(m);
|
||||
c.put("uuid", uuid);
|
||||
callback.setConfig(c);
|
||||
return uuid;
|
||||
}
|
||||
|
||||
@@ -174,13 +175,13 @@ class BluetoothPlugin extends AbstractPlugin implements StreamTransportPlugin {
|
||||
}
|
||||
|
||||
private void setLocalBluetoothAddress(String address) {
|
||||
Map<String, String> m;
|
||||
TransportProperties p;
|
||||
synchronized(this) {
|
||||
if(!started) return;
|
||||
m = new TreeMap<String, String>(localProperties);
|
||||
p = new TransportProperties(localProperties);
|
||||
}
|
||||
m.put("address", address);
|
||||
callback.setLocalProperties(m);
|
||||
p.put("address", address);
|
||||
callback.setLocalProperties(p);
|
||||
}
|
||||
|
||||
public boolean shouldPoll() {
|
||||
@@ -224,10 +225,10 @@ class BluetoothPlugin extends AbstractPlugin implements StreamTransportPlugin {
|
||||
discoveryAgent = localDevice.getDiscoveryAgent();
|
||||
addresses = new HashMap<String, ContactId>();
|
||||
uuids = new HashMap<ContactId, String>();
|
||||
for(Entry<ContactId, Map<String, String>> e
|
||||
for(Entry<ContactId, TransportProperties> e
|
||||
: remoteProperties.entrySet()) {
|
||||
ContactId c = e.getKey();
|
||||
Map<String, String> properties = e.getValue();
|
||||
TransportProperties properties = e.getValue();
|
||||
String address = properties.get("address");
|
||||
String uuid = properties.get("uuid");
|
||||
if(address != null && uuid != null) {
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -9,7 +9,9 @@ import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import net.sf.briar.api.ContactId;
|
||||
import net.sf.briar.api.TransportConfig;
|
||||
import net.sf.briar.api.TransportId;
|
||||
import net.sf.briar.api.TransportProperties;
|
||||
import net.sf.briar.api.plugins.BatchTransportCallback;
|
||||
|
||||
class RemovableDrivePlugin extends FilePlugin
|
||||
@@ -36,9 +38,9 @@ implements RemovableDriveMonitor.Callback {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void start(Map<String, String> localProperties,
|
||||
Map<ContactId, Map<String, String>> remoteProperties,
|
||||
Map<String, String> config) throws IOException {
|
||||
public void start(TransportProperties localProperties,
|
||||
Map<ContactId, TransportProperties> remoteProperties,
|
||||
TransportConfig config) throws IOException {
|
||||
super.start(localProperties, remoteProperties, config);
|
||||
monitor.start(this);
|
||||
}
|
||||
|
||||
@@ -5,12 +5,11 @@ import java.net.InetSocketAddress;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
import java.net.SocketAddress;
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
import java.util.concurrent.Executor;
|
||||
|
||||
import net.sf.briar.api.ContactId;
|
||||
import net.sf.briar.api.TransportId;
|
||||
import net.sf.briar.api.TransportProperties;
|
||||
import net.sf.briar.api.plugins.StreamTransportCallback;
|
||||
|
||||
class SimpleSocketPlugin extends SocketPlugin {
|
||||
@@ -48,12 +47,12 @@ class SimpleSocketPlugin extends SocketPlugin {
|
||||
@Override
|
||||
protected SocketAddress getSocketAddress(ContactId c) {
|
||||
assert started;
|
||||
Map<String, String> properties = remoteProperties.get(c);
|
||||
TransportProperties properties = remoteProperties.get(c);
|
||||
if(properties == null) return null;
|
||||
return createSocketAddress(properties);
|
||||
}
|
||||
|
||||
private SocketAddress createSocketAddress(Map<String, String> properties) {
|
||||
private SocketAddress createSocketAddress(TransportProperties properties) {
|
||||
assert properties != null;
|
||||
String host = properties.get("host");
|
||||
String portString = properties.get("port");
|
||||
@@ -69,10 +68,10 @@ class SimpleSocketPlugin extends SocketPlugin {
|
||||
|
||||
@Override
|
||||
protected void setLocalSocketAddress(SocketAddress s) {
|
||||
Map<String, String> m;
|
||||
TransportProperties p;
|
||||
synchronized(this) {
|
||||
if(!started) return;
|
||||
m = new TreeMap<String, String>(localProperties);
|
||||
p = new TransportProperties(localProperties);
|
||||
}
|
||||
if(!(s instanceof InetSocketAddress))
|
||||
throw new IllegalArgumentException();
|
||||
@@ -80,9 +79,9 @@ class SimpleSocketPlugin extends SocketPlugin {
|
||||
String host = i.getAddress().getHostAddress();
|
||||
String port = String.valueOf(i.getPort());
|
||||
// FIXME: Special handling for private IP addresses?
|
||||
m.put("host", host);
|
||||
m.put("port", port);
|
||||
callback.setLocalProperties(m);
|
||||
p.put("host", host);
|
||||
p.put("port", port);
|
||||
callback.setLocalProperties(p);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -10,6 +10,8 @@ import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
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.StreamTransportPlugin;
|
||||
import net.sf.briar.api.transport.StreamTransportConnection;
|
||||
@@ -41,9 +43,9 @@ implements StreamTransportPlugin {
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized void start(Map<String, String> localProperties,
|
||||
Map<ContactId, Map<String, String>> remoteProperties,
|
||||
Map<String, String> config) throws IOException {
|
||||
public synchronized void start(TransportProperties localProperties,
|
||||
Map<ContactId, TransportProperties> remoteProperties,
|
||||
TransportConfig config) throws IOException {
|
||||
super.start(localProperties, remoteProperties, config);
|
||||
executor.execute(createBinder());
|
||||
}
|
||||
@@ -129,9 +131,8 @@ implements StreamTransportPlugin {
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized void setLocalProperties(
|
||||
Map<String, String> properties) {
|
||||
super.setLocalProperties(properties);
|
||||
public synchronized void setLocalProperties(TransportProperties p) {
|
||||
super.setLocalProperties(p);
|
||||
// Close and reopen the socket if its address has changed
|
||||
if(socket != null) {
|
||||
SocketAddress addr = socket.getLocalSocketAddress();
|
||||
|
||||
@@ -3,10 +3,11 @@ package net.sf.briar.protocol;
|
||||
import java.util.Map;
|
||||
|
||||
import net.sf.briar.api.TransportId;
|
||||
import net.sf.briar.api.TransportProperties;
|
||||
import net.sf.briar.api.protocol.TransportUpdate;
|
||||
|
||||
interface TransportFactory {
|
||||
|
||||
TransportUpdate createTransportUpdate(
|
||||
Map<TransportId, Map<String, String>> transports, long timestamp);
|
||||
Map<TransportId, TransportProperties> transports, long timestamp);
|
||||
}
|
||||
|
||||
@@ -3,12 +3,13 @@ package net.sf.briar.protocol;
|
||||
import java.util.Map;
|
||||
|
||||
import net.sf.briar.api.TransportId;
|
||||
import net.sf.briar.api.TransportProperties;
|
||||
import net.sf.briar.api.protocol.TransportUpdate;
|
||||
|
||||
class TransportFactoryImpl implements TransportFactory {
|
||||
|
||||
public TransportUpdate createTransportUpdate(
|
||||
Map<TransportId, Map<String, String>> transports, long timestamp) {
|
||||
Map<TransportId, TransportProperties> transports, long timestamp) {
|
||||
return new TransportUpdateImpl(transports, timestamp);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import java.util.TreeMap;
|
||||
|
||||
import net.sf.briar.api.FormatException;
|
||||
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.TransportUpdate;
|
||||
import net.sf.briar.api.protocol.Types;
|
||||
@@ -17,11 +18,11 @@ import net.sf.briar.api.serial.Reader;
|
||||
class TransportReader implements ObjectReader<TransportUpdate> {
|
||||
|
||||
private final TransportFactory transportFactory;
|
||||
private final ObjectReader<TransportProperties> propertiesReader;
|
||||
private final ObjectReader<Transport> propertiesReader;
|
||||
|
||||
TransportReader(TransportFactory transportFactory) {
|
||||
this.transportFactory = transportFactory;
|
||||
propertiesReader = new TransportPropertiesReader();
|
||||
propertiesReader = new PropertiesReader();
|
||||
}
|
||||
|
||||
public TransportUpdate readObject(Reader r) throws IOException {
|
||||
@@ -33,14 +34,14 @@ class TransportReader implements ObjectReader<TransportUpdate> {
|
||||
r.readUserDefinedId(Types.TRANSPORT_UPDATE);
|
||||
r.addObjectReader(Types.TRANSPORT_PROPERTIES, propertiesReader);
|
||||
r.setMaxStringLength(ProtocolConstants.MAX_PACKET_LENGTH);
|
||||
List<TransportProperties> l = r.readList(TransportProperties.class);
|
||||
List<Transport> l = r.readList(Transport.class);
|
||||
r.resetMaxStringLength();
|
||||
r.removeObjectReader(Types.TRANSPORT_PROPERTIES);
|
||||
if(l.size() > TransportUpdate.MAX_PLUGINS_PER_UPDATE)
|
||||
throw new FormatException();
|
||||
Map<TransportId, Map<String, String>> transports =
|
||||
new TreeMap<TransportId, Map<String, String>>();
|
||||
for(TransportProperties t : l) {
|
||||
Map<TransportId, TransportProperties> transports =
|
||||
new TreeMap<TransportId, TransportProperties>();
|
||||
for(Transport t : l) {
|
||||
if(transports.put(t.id, t.properties) != null)
|
||||
throw new FormatException(); // Duplicate transport ID
|
||||
}
|
||||
@@ -50,33 +51,31 @@ class TransportReader implements ObjectReader<TransportUpdate> {
|
||||
return transportFactory.createTransportUpdate(transports, timestamp);
|
||||
}
|
||||
|
||||
private static class TransportProperties {
|
||||
private static class Transport {
|
||||
|
||||
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.properties = properties;
|
||||
}
|
||||
}
|
||||
|
||||
private static class TransportPropertiesReader
|
||||
implements ObjectReader<TransportProperties> {
|
||||
private static class PropertiesReader implements ObjectReader<Transport> {
|
||||
|
||||
public TransportProperties readObject(Reader r) throws IOException {
|
||||
public Transport readObject(Reader r) throws IOException {
|
||||
r.readUserDefinedId(Types.TRANSPORT_PROPERTIES);
|
||||
int i = r.readInt32();
|
||||
if(i < TransportId.MIN_ID || i > TransportId.MAX_ID)
|
||||
throw new FormatException();
|
||||
TransportId id = new TransportId(i);
|
||||
r.setMaxStringLength(TransportUpdate.MAX_KEY_OR_VALUE_LENGTH);
|
||||
Map<String, String> properties =
|
||||
r.readMap(String.class, String.class);
|
||||
Map<String, String> m = r.readMap(String.class, String.class);
|
||||
r.resetMaxStringLength();
|
||||
if(properties.size() > TransportUpdate.MAX_PROPERTIES_PER_PLUGIN)
|
||||
if(m.size() > TransportUpdate.MAX_PROPERTIES_PER_PLUGIN)
|
||||
throw new FormatException();
|
||||
return new TransportProperties(id, properties);
|
||||
return new Transport(id, new TransportProperties(m));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,20 +3,21 @@ package net.sf.briar.protocol;
|
||||
import java.util.Map;
|
||||
|
||||
import net.sf.briar.api.TransportId;
|
||||
import net.sf.briar.api.TransportProperties;
|
||||
import net.sf.briar.api.protocol.TransportUpdate;
|
||||
|
||||
class TransportUpdateImpl implements TransportUpdate {
|
||||
|
||||
private final Map<TransportId, Map<String, String>> transports;
|
||||
private final Map<TransportId, TransportProperties> transports;
|
||||
private final long timestamp;
|
||||
|
||||
TransportUpdateImpl(Map<TransportId, Map<String, String>> transports,
|
||||
TransportUpdateImpl(Map<TransportId, TransportProperties> transports,
|
||||
long timestamp) {
|
||||
this.transports = transports;
|
||||
this.timestamp = timestamp;
|
||||
}
|
||||
|
||||
public Map<TransportId, Map<String, String>> getTransports() {
|
||||
public Map<TransportId, TransportProperties> getTransports() {
|
||||
return transports;
|
||||
}
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@ import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
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.writers.TransportWriter;
|
||||
import net.sf.briar.api.serial.Writer;
|
||||
@@ -22,11 +23,11 @@ class TransportWriterImpl implements TransportWriter {
|
||||
}
|
||||
|
||||
public void writeTransports(
|
||||
Map<TransportId, Map<String, String>> transports, long timestamp)
|
||||
Map<TransportId, TransportProperties> transports, long timestamp)
|
||||
throws IOException {
|
||||
w.writeUserDefinedId(Types.TRANSPORT_UPDATE);
|
||||
w.writeListStart();
|
||||
for(Entry<TransportId, Map<String, String>> e : transports.entrySet()) {
|
||||
for(Entry<TransportId, TransportProperties> e : transports.entrySet()) {
|
||||
w.writeUserDefinedId(Types.TRANSPORT_PROPERTIES);
|
||||
w.writeInt32(e.getKey().getInt());
|
||||
w.writeMap(e.getValue());
|
||||
|
||||
Reference in New Issue
Block a user