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

@@ -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;
}
}

View File

@@ -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) {

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 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);
}

View File

@@ -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

View File

@@ -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();