mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-12 18:59:06 +01:00
Plugins don't need their own copies of configs and properties.
This commit is contained in:
@@ -3,7 +3,6 @@ package net.sf.briar.api.plugins;
|
||||
import net.sf.briar.api.ContactId;
|
||||
import net.sf.briar.api.transport.BatchTransportReader;
|
||||
import net.sf.briar.api.transport.BatchTransportWriter;
|
||||
import net.sf.briar.api.transport.TransportCallback;
|
||||
|
||||
/**
|
||||
* An interface for receiving readers and writers created by a batch-mode
|
||||
|
||||
@@ -2,7 +2,6 @@ package net.sf.briar.api.plugins;
|
||||
|
||||
import net.sf.briar.api.ContactId;
|
||||
import net.sf.briar.api.transport.StreamTransportConnection;
|
||||
import net.sf.briar.api.transport.TransportCallback;
|
||||
|
||||
/**
|
||||
* An interface for receiving connections created by a stream-mode transport
|
||||
|
||||
50
api/net/sf/briar/api/plugins/TransportCallback.java
Normal file
50
api/net/sf/briar/api/plugins/TransportCallback.java
Normal file
@@ -0,0 +1,50 @@
|
||||
package net.sf.briar.api.plugins;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import net.sf.briar.api.ContactId;
|
||||
import net.sf.briar.api.TransportConfig;
|
||||
import net.sf.briar.api.TransportProperties;
|
||||
|
||||
/**
|
||||
* An interface through which a transport plugin interacts with the rest of
|
||||
* the application.
|
||||
*/
|
||||
public interface TransportCallback {
|
||||
|
||||
/** Returns the plugin's configuration. */
|
||||
TransportConfig getConfig();
|
||||
|
||||
/** Returns the plugin's local transport properties. */
|
||||
TransportProperties getLocalProperties();
|
||||
|
||||
/** Returns the plugin's remote transport properties. */
|
||||
Map<ContactId, TransportProperties> getRemoteProperties();
|
||||
|
||||
/** Stores the plugin's configuration. */
|
||||
void setConfig(TransportConfig c);
|
||||
|
||||
/** Stores the plugin's local transport properties. */
|
||||
void setLocalProperties(TransportProperties p);
|
||||
|
||||
/**
|
||||
* Presents the user with a choice among two or more named options and
|
||||
* returns the user's response. The message may consist of a translatable
|
||||
* format string and arguments.
|
||||
* @return An index into the array of options indicating the user's choice,
|
||||
* or -1 if the user cancelled the choice.
|
||||
*/
|
||||
int showChoice(String[] options, String... message);
|
||||
|
||||
/**
|
||||
* Asks the user to confirm an action and returns the user's response. The
|
||||
* message may consist of a translatable format string and arguments.
|
||||
*/
|
||||
boolean showConfirmationMessage(String... message);
|
||||
|
||||
/**
|
||||
* Shows a message to the user. The message may consist of a translatable
|
||||
* format string and arguments.
|
||||
*/
|
||||
void showMessage(String... message);
|
||||
}
|
||||
@@ -1,12 +1,8 @@
|
||||
package net.sf.briar.api.plugins;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
|
||||
import net.sf.briar.api.ContactId;
|
||||
import net.sf.briar.api.TransportConfig;
|
||||
import net.sf.briar.api.TransportId;
|
||||
import net.sf.briar.api.TransportProperties;
|
||||
|
||||
public interface TransportPlugin {
|
||||
|
||||
@@ -14,22 +10,11 @@ public interface TransportPlugin {
|
||||
TransportId getId();
|
||||
|
||||
/** Starts the plugin. */
|
||||
void start(TransportProperties localProperties,
|
||||
Map<ContactId, TransportProperties> remoteProperties,
|
||||
TransportConfig config) throws IOException;
|
||||
void start() throws IOException;
|
||||
|
||||
/** Stops the plugin. */
|
||||
void stop() throws IOException;
|
||||
|
||||
/** Updates the plugin's local transport properties. */
|
||||
void setLocalProperties(TransportProperties p);
|
||||
|
||||
/** Updates the plugin's transport properties for the given contact. */
|
||||
void setRemoteProperties(ContactId c, TransportProperties p);
|
||||
|
||||
/** Updates the plugin's configuration properties. */
|
||||
void setConfig(TransportConfig c);
|
||||
|
||||
/**
|
||||
* Returns true if the plugin's poll() method should be called
|
||||
* periodically to attempt to establish connections.
|
||||
|
||||
@@ -1,17 +0,0 @@
|
||||
package net.sf.briar.api.transport;
|
||||
|
||||
import net.sf.briar.api.TransportConfig;
|
||||
import net.sf.briar.api.TransportProperties;
|
||||
|
||||
public interface TransportCallback {
|
||||
|
||||
void setLocalProperties(TransportProperties p);
|
||||
|
||||
void setConfig(TransportConfig c);
|
||||
|
||||
void showMessage(String... message);
|
||||
|
||||
boolean showConfirmationMessage(String... message);
|
||||
|
||||
int showChoice(String[] choices, String... message);
|
||||
}
|
||||
@@ -1,56 +1,28 @@
|
||||
package net.sf.briar.plugins;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
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 {
|
||||
|
||||
protected final Executor executor;
|
||||
|
||||
// These fields should be accessed with this's lock held
|
||||
protected TransportProperties localProperties = null;
|
||||
protected Map<ContactId, TransportProperties> remoteProperties = null;
|
||||
protected TransportConfig config = null;
|
||||
// This field must only be accessed with this's lock held
|
||||
protected boolean started = false;
|
||||
|
||||
protected AbstractPlugin(Executor executor) {
|
||||
this.executor = executor;
|
||||
}
|
||||
|
||||
public synchronized void start(TransportProperties localProperties,
|
||||
Map<ContactId, TransportProperties> remoteProperties,
|
||||
TransportConfig config) throws IOException {
|
||||
public synchronized void start() throws IOException {
|
||||
if(started) throw new IllegalStateException();
|
||||
started = true;
|
||||
this.localProperties = localProperties;
|
||||
this.remoteProperties = remoteProperties;
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
public synchronized void stop() throws IOException {
|
||||
if(!started) throw new IllegalStateException();
|
||||
started = false;
|
||||
}
|
||||
|
||||
public synchronized void setLocalProperties(TransportProperties p) {
|
||||
if(!started) throw new IllegalStateException();
|
||||
localProperties = p;
|
||||
}
|
||||
|
||||
public synchronized void setRemoteProperties(ContactId c,
|
||||
TransportProperties p) {
|
||||
if(!started) throw new IllegalStateException();
|
||||
remoteProperties.put(c, p);
|
||||
}
|
||||
|
||||
public synchronized void setConfig(TransportConfig c) {
|
||||
if(!started) throw new IllegalStateException();
|
||||
this.config = c;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package net.sf.briar.plugins.bluetooth;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
@@ -55,13 +54,11 @@ class BluetoothPlugin extends AbstractPlugin implements StreamTransportPlugin {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void start(TransportProperties localProperties,
|
||||
Map<ContactId, TransportProperties> remoteProperties,
|
||||
TransportConfig config) throws IOException {
|
||||
public void start() throws IOException {
|
||||
// Initialise the Bluetooth stack
|
||||
try {
|
||||
synchronized(this) {
|
||||
super.start(localProperties, remoteProperties, config);
|
||||
super.start();
|
||||
localDevice = LocalDevice.getLocalDevice();
|
||||
}
|
||||
} catch(UnsatisfiedLinkError e) {
|
||||
@@ -95,10 +92,11 @@ class BluetoothPlugin extends AbstractPlugin implements StreamTransportPlugin {
|
||||
LocalDevice ld;
|
||||
synchronized(this) {
|
||||
if(!started) return;
|
||||
uuid = config.get("uuid");
|
||||
TransportConfig c = callback.getConfig();
|
||||
uuid = c.get("uuid");
|
||||
if(uuid == null) uuid = createAndSetUuid(c);
|
||||
ld = localDevice;
|
||||
}
|
||||
if(uuid == null) uuid = createAndSetUuid();
|
||||
// Try to make the device discoverable (requires root on Linux)
|
||||
try {
|
||||
ld.setDiscoverable(DiscoveryAgent.GIAC);
|
||||
@@ -130,15 +128,10 @@ class BluetoothPlugin extends AbstractPlugin implements StreamTransportPlugin {
|
||||
setLocalBluetoothAddress(ld.getBluetoothAddress());
|
||||
}
|
||||
|
||||
private String createAndSetUuid() {
|
||||
private synchronized String createAndSetUuid(TransportConfig c) {
|
||||
byte[] b = new byte[16];
|
||||
new Random().nextBytes(b); // FIXME: Use a SecureRandom?
|
||||
String uuid = StringUtils.toHexString(b);
|
||||
TransportConfig c;
|
||||
synchronized(this) {
|
||||
if(!started) return uuid;
|
||||
c = new TransportConfig(config);
|
||||
}
|
||||
c.put("uuid", uuid);
|
||||
callback.setConfig(c);
|
||||
return uuid;
|
||||
@@ -174,12 +167,9 @@ class BluetoothPlugin extends AbstractPlugin implements StreamTransportPlugin {
|
||||
}
|
||||
}
|
||||
|
||||
private void setLocalBluetoothAddress(String address) {
|
||||
TransportProperties p;
|
||||
synchronized(this) {
|
||||
if(!started) return;
|
||||
p = new TransportProperties(localProperties);
|
||||
}
|
||||
private synchronized void setLocalBluetoothAddress(String address) {
|
||||
if(!started) return;
|
||||
TransportProperties p = callback.getLocalProperties();
|
||||
p.put("address", address);
|
||||
callback.setLocalProperties(p);
|
||||
}
|
||||
@@ -194,28 +184,28 @@ class BluetoothPlugin extends AbstractPlugin implements StreamTransportPlugin {
|
||||
|
||||
public synchronized void poll() {
|
||||
if(!started) return;
|
||||
executor.execute(createConnectors(remoteProperties.keySet()));
|
||||
executor.execute(createConnectors());
|
||||
}
|
||||
|
||||
private Runnable createConnectors(final Collection<ContactId> contacts) {
|
||||
private Runnable createConnectors() {
|
||||
return new Runnable() {
|
||||
public void run() {
|
||||
connectAndCallBack(contacts);
|
||||
connectAndCallBack();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private void connectAndCallBack(Collection<ContactId> contacts) {
|
||||
Map<ContactId, String> discovered = discover(contacts);
|
||||
private void connectAndCallBack() {
|
||||
Map<ContactId, String> discovered = discover();
|
||||
for(Entry<ContactId, String> e : discovered.entrySet()) {
|
||||
ContactId c = e.getKey();
|
||||
String url = e.getValue();
|
||||
StreamTransportConnection conn = createConnection(c, url);
|
||||
StreamTransportConnection conn = connect(c, url);
|
||||
if(conn != null) callback.outgoingConnectionCreated(c, conn);
|
||||
}
|
||||
}
|
||||
|
||||
private Map<ContactId, String> discover(Collection<ContactId> contacts) {
|
||||
private Map<ContactId, String> discover() {
|
||||
DiscoveryAgent discoveryAgent;
|
||||
Map<String, ContactId> addresses;
|
||||
Map<ContactId, String> uuids;
|
||||
@@ -225,12 +215,13 @@ class BluetoothPlugin extends AbstractPlugin implements StreamTransportPlugin {
|
||||
discoveryAgent = localDevice.getDiscoveryAgent();
|
||||
addresses = new HashMap<String, ContactId>();
|
||||
uuids = new HashMap<ContactId, String>();
|
||||
for(Entry<ContactId, TransportProperties> e
|
||||
: remoteProperties.entrySet()) {
|
||||
Map<ContactId, TransportProperties> remote =
|
||||
callback.getRemoteProperties();
|
||||
for(Entry<ContactId, TransportProperties> e : remote.entrySet()) {
|
||||
ContactId c = e.getKey();
|
||||
TransportProperties properties = e.getValue();
|
||||
String address = properties.get("address");
|
||||
String uuid = properties.get("uuid");
|
||||
TransportProperties p = e.getValue();
|
||||
String address = p.get("address");
|
||||
String uuid = p.get("uuid");
|
||||
if(address != null && uuid != null) {
|
||||
addresses.put(address, c);
|
||||
uuids.put(c, uuid);
|
||||
@@ -250,12 +241,13 @@ class BluetoothPlugin extends AbstractPlugin implements StreamTransportPlugin {
|
||||
return listener.getUrls();
|
||||
}
|
||||
|
||||
private StreamTransportConnection createConnection(ContactId c,
|
||||
String url) {
|
||||
private StreamTransportConnection connect(ContactId c, String url) {
|
||||
try {
|
||||
synchronized(this) {
|
||||
if(!started) return null;
|
||||
if(!remoteProperties.containsKey(c)) return null;
|
||||
Map<ContactId, TransportProperties> remote =
|
||||
callback.getRemoteProperties();
|
||||
if(!remote.containsKey(c)) return null;
|
||||
}
|
||||
StreamConnection s = (StreamConnection) Connector.open(url);
|
||||
return new BluetoothTransportConnection(s);
|
||||
@@ -266,8 +258,8 @@ class BluetoothPlugin extends AbstractPlugin implements StreamTransportPlugin {
|
||||
}
|
||||
|
||||
public StreamTransportConnection createConnection(ContactId c) {
|
||||
Map<ContactId, String> discovered = discover(Collections.singleton(c));
|
||||
Map<ContactId, String> discovered = discover();
|
||||
String url = discovered.get(c);
|
||||
return url == null ? null : createConnection(c, url);
|
||||
return url == null ? null : connect(c, url);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,15 +3,11 @@ package net.sf.briar.plugins.file;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.Executor;
|
||||
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
|
||||
@@ -38,15 +34,13 @@ implements RemovableDriveMonitor.Callback {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void start(TransportProperties localProperties,
|
||||
Map<ContactId, TransportProperties> remoteProperties,
|
||||
TransportConfig config) throws IOException {
|
||||
super.start(localProperties, remoteProperties, config);
|
||||
public synchronized void start() throws IOException {
|
||||
super.start();
|
||||
monitor.start(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stop() throws IOException {
|
||||
public synchronized void stop() throws IOException {
|
||||
super.stop();
|
||||
monitor.stop();
|
||||
}
|
||||
|
||||
@@ -39,23 +39,36 @@ class SimpleSocketPlugin extends SocketPlugin {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected SocketAddress getLocalSocketAddress() {
|
||||
protected Socket createClientSocket() throws IOException {
|
||||
assert started;
|
||||
return createSocketAddress(localProperties);
|
||||
return new Socket();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected SocketAddress getSocketAddress(ContactId c) {
|
||||
protected ServerSocket createServerSocket() throws IOException {
|
||||
assert started;
|
||||
TransportProperties properties = remoteProperties.get(c);
|
||||
if(properties == null) return null;
|
||||
return createSocketAddress(properties);
|
||||
return new ServerSocket();
|
||||
}
|
||||
|
||||
private SocketAddress createSocketAddress(TransportProperties properties) {
|
||||
assert properties != null;
|
||||
String host = properties.get("host");
|
||||
String portString = properties.get("port");
|
||||
@Override
|
||||
protected synchronized SocketAddress getLocalSocketAddress() {
|
||||
assert started;
|
||||
return createSocketAddress(callback.getLocalProperties());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected synchronized SocketAddress getRemoteSocketAddress(ContactId c) {
|
||||
assert started;
|
||||
TransportProperties p = callback.getRemoteProperties().get(c);
|
||||
return p == null ? null : createSocketAddress(p);
|
||||
}
|
||||
|
||||
private synchronized SocketAddress createSocketAddress(
|
||||
TransportProperties p) {
|
||||
assert started;
|
||||
assert p != null;
|
||||
String host = p.get("host");
|
||||
String portString = p.get("port");
|
||||
if(host == null || portString == null) return null;
|
||||
int port;
|
||||
try {
|
||||
@@ -67,32 +80,17 @@ class SimpleSocketPlugin extends SocketPlugin {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void setLocalSocketAddress(SocketAddress s) {
|
||||
TransportProperties p;
|
||||
synchronized(this) {
|
||||
if(!started) return;
|
||||
p = new TransportProperties(localProperties);
|
||||
}
|
||||
protected synchronized void setLocalSocketAddress(SocketAddress s) {
|
||||
assert started;
|
||||
if(!(s instanceof InetSocketAddress))
|
||||
throw new IllegalArgumentException();
|
||||
InetSocketAddress i = (InetSocketAddress) s;
|
||||
String host = i.getAddress().getHostAddress();
|
||||
String port = String.valueOf(i.getPort());
|
||||
// FIXME: Special handling for private IP addresses?
|
||||
TransportProperties p = callback.getLocalProperties();
|
||||
p.put("host", host);
|
||||
p.put("port", port);
|
||||
callback.setLocalProperties(p);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Socket createClientSocket() throws IOException {
|
||||
assert started;
|
||||
return new Socket();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ServerSocket createServerSocket() throws IOException {
|
||||
assert started;
|
||||
return new ServerSocket();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,14 +4,11 @@ import java.io.IOException;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
import java.net.SocketAddress;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.Executor;
|
||||
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;
|
||||
@@ -25,16 +22,17 @@ implements StreamTransportPlugin {
|
||||
|
||||
protected final StreamTransportCallback callback;
|
||||
|
||||
// This field should be accessed with this's lock held
|
||||
// This field must only be accessed with this's lock held
|
||||
protected ServerSocket socket = null;
|
||||
|
||||
protected abstract void setLocalSocketAddress(SocketAddress s);
|
||||
|
||||
// These methods should be called with this's lock held and started == true
|
||||
protected abstract SocketAddress getLocalSocketAddress();
|
||||
protected abstract SocketAddress getSocketAddress(ContactId c);
|
||||
// These methods must only be called with this's lock held and
|
||||
// started == true
|
||||
protected abstract Socket createClientSocket() throws IOException;
|
||||
protected abstract ServerSocket createServerSocket() throws IOException;
|
||||
protected abstract SocketAddress getLocalSocketAddress();
|
||||
protected abstract SocketAddress getRemoteSocketAddress(ContactId c);
|
||||
|
||||
protected SocketPlugin(Executor executor,
|
||||
StreamTransportCallback callback) {
|
||||
@@ -43,10 +41,8 @@ implements StreamTransportPlugin {
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized void start(TransportProperties localProperties,
|
||||
Map<ContactId, TransportProperties> remoteProperties,
|
||||
TransportConfig config) throws IOException {
|
||||
super.start(localProperties, remoteProperties, config);
|
||||
public synchronized void start() throws IOException {
|
||||
super.start();
|
||||
executor.execute(createBinder());
|
||||
}
|
||||
|
||||
@@ -130,30 +126,11 @@ implements StreamTransportPlugin {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
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();
|
||||
if(!getLocalSocketAddress().equals(addr)) {
|
||||
try {
|
||||
socket.close();
|
||||
} catch(IOException e) {
|
||||
if(LOG.isLoggable(Level.WARNING))
|
||||
LOG.warning(e.getMessage());
|
||||
}
|
||||
socket = null;
|
||||
executor.execute(createBinder());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized void poll() {
|
||||
// Subclasses may not support polling
|
||||
if(!shouldPoll()) throw new UnsupportedOperationException();
|
||||
if(!started) return;
|
||||
for(ContactId c : remoteProperties.keySet()) {
|
||||
for(ContactId c : callback.getRemoteProperties().keySet()) {
|
||||
executor.execute(createConnector(c));
|
||||
}
|
||||
}
|
||||
@@ -177,7 +154,7 @@ implements StreamTransportPlugin {
|
||||
try {
|
||||
synchronized(this) {
|
||||
if(!started) return null;
|
||||
addr = getSocketAddress(c);
|
||||
addr = getRemoteSocketAddress(c);
|
||||
s = createClientSocket();
|
||||
}
|
||||
if(addr == null || s == null) return null;
|
||||
|
||||
@@ -24,22 +24,18 @@ public class BluetoothClientTest {
|
||||
System.exit(1);
|
||||
}
|
||||
ContactId contactId = new ContactId(0);
|
||||
TransportProperties localProperties = new TransportProperties();
|
||||
Map<ContactId, TransportProperties> remoteProperties =
|
||||
new HashMap<ContactId, TransportProperties>();
|
||||
TransportConfig config = new TransportConfig();
|
||||
StreamTransportCallback callback = new ClientCallback();
|
||||
ClientCallback callback = new ClientCallback();
|
||||
// Store the server's Bluetooth address and UUID
|
||||
TransportProperties p = new TransportProperties();
|
||||
p.put("address", args[0]);
|
||||
p.put("uuid", BluetoothServerTest.UUID);
|
||||
remoteProperties.put(contactId, p);
|
||||
callback.remote.put(contactId, p);
|
||||
// Create the plugin
|
||||
BluetoothPlugin plugin =
|
||||
new BluetoothPlugin(new ImmediateExecutor(), callback, 0L);
|
||||
// Start the plugin
|
||||
System.out.println("Starting plugin");
|
||||
plugin.start(localProperties, remoteProperties, config);
|
||||
plugin.start();
|
||||
// Try to connect to the server
|
||||
System.out.println("Creating connection");
|
||||
StreamTransportConnection conn = plugin.createConnection(contactId);
|
||||
@@ -66,19 +62,40 @@ public class BluetoothClientTest {
|
||||
|
||||
private static class ClientCallback implements StreamTransportCallback {
|
||||
|
||||
public void setLocalProperties(TransportProperties p) {}
|
||||
private TransportConfig config = new TransportConfig();
|
||||
private TransportProperties local = new TransportProperties();
|
||||
private Map<ContactId, TransportProperties> remote =
|
||||
new HashMap<ContactId, TransportProperties>();
|
||||
|
||||
public void setConfig(TransportConfig c) {}
|
||||
public TransportConfig getConfig() {
|
||||
return config;
|
||||
}
|
||||
|
||||
public void showMessage(String... message) {}
|
||||
public TransportProperties getLocalProperties() {
|
||||
return local;
|
||||
}
|
||||
|
||||
public Map<ContactId, TransportProperties> getRemoteProperties() {
|
||||
return remote;
|
||||
}
|
||||
|
||||
public void setConfig(TransportConfig c) {
|
||||
config = c;
|
||||
}
|
||||
|
||||
public void setLocalProperties(TransportProperties p) {
|
||||
local = p;
|
||||
}
|
||||
|
||||
public int showChoice(String[] options, String... message) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
public boolean showConfirmationMessage(String... message) {
|
||||
return false;
|
||||
}
|
||||
|
||||
public int showChoice(String[] choices, String... message) {
|
||||
return -1;
|
||||
}
|
||||
public void showMessage(String... message) {}
|
||||
|
||||
public void incomingConnectionCreated(StreamTransportConnection c) {}
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@ package net.sf.briar.plugins.bluetooth;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.PrintStream;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Scanner;
|
||||
|
||||
@@ -21,19 +21,15 @@ public class BluetoothServerTest {
|
||||
public static final String CHALLENGE = "Potatoes!";
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
TransportProperties localProperties = new TransportProperties();
|
||||
Map<ContactId, TransportProperties> remoteProperties =
|
||||
Collections.emptyMap();
|
||||
TransportConfig config = new TransportConfig();
|
||||
StreamTransportCallback callback = new ServerCallback();
|
||||
ServerCallback callback = new ServerCallback();
|
||||
// Store the UUID
|
||||
config.put("uuid", UUID);
|
||||
callback.config.put("uuid", UUID);
|
||||
// Create the plugin
|
||||
BluetoothPlugin plugin =
|
||||
new BluetoothPlugin(new ImmediateExecutor(), callback, 0L);
|
||||
// Start the plugin
|
||||
System.out.println("Starting plugin");
|
||||
plugin.start(localProperties, remoteProperties, config);
|
||||
plugin.start();
|
||||
// Wait for a connection
|
||||
System.out.println("Waiting for connection");
|
||||
synchronized(callback) {
|
||||
@@ -46,19 +42,40 @@ public class BluetoothServerTest {
|
||||
|
||||
private static class ServerCallback implements StreamTransportCallback {
|
||||
|
||||
public void setLocalProperties(TransportProperties p) {}
|
||||
private TransportConfig config = new TransportConfig();
|
||||
private TransportProperties local = new TransportProperties();
|
||||
private Map<ContactId, TransportProperties> remote =
|
||||
new HashMap<ContactId, TransportProperties>();
|
||||
|
||||
public void setConfig(TransportConfig c) {}
|
||||
public TransportConfig getConfig() {
|
||||
return config;
|
||||
}
|
||||
|
||||
public void showMessage(String... message) {}
|
||||
public TransportProperties getLocalProperties() {
|
||||
return local;
|
||||
}
|
||||
|
||||
public Map<ContactId, TransportProperties> getRemoteProperties() {
|
||||
return remote;
|
||||
}
|
||||
|
||||
public void setConfig(TransportConfig c) {
|
||||
config = c;
|
||||
}
|
||||
|
||||
public void setLocalProperties(TransportProperties p) {
|
||||
local = p;
|
||||
}
|
||||
|
||||
public int showChoice(String[] options, String... message) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
public boolean showConfirmationMessage(String... message) {
|
||||
return false;
|
||||
}
|
||||
|
||||
public int showChoice(String[] choices, String... message) {
|
||||
return -1;
|
||||
}
|
||||
public void showMessage(String... message) {}
|
||||
|
||||
public void incomingConnectionCreated(StreamTransportConnection conn) {
|
||||
System.out.println("Connection received");
|
||||
|
||||
@@ -5,16 +5,12 @@ import java.io.FileOutputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.Executor;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import net.sf.briar.TestUtils;
|
||||
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.transport.BatchTransportWriter;
|
||||
import net.sf.briar.api.transport.TransportConstants;
|
||||
@@ -32,16 +28,8 @@ public class RemovableDrivePluginTest extends TestCase {
|
||||
private final File testDir = TestUtils.getTestDirectory();
|
||||
private final ContactId contactId = new ContactId(0);
|
||||
|
||||
private TransportProperties localProperties = null;
|
||||
private Map<ContactId, TransportProperties> remoteProperties = null;
|
||||
private TransportConfig config = null;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
localProperties = new TransportProperties();
|
||||
remoteProperties = new HashMap<ContactId, TransportProperties>();
|
||||
remoteProperties.put(contactId, new TransportProperties());
|
||||
config = new TransportConfig();
|
||||
testDir.mkdirs();
|
||||
}
|
||||
|
||||
@@ -86,7 +74,7 @@ public class RemovableDrivePluginTest extends TestCase {
|
||||
|
||||
RemovableDrivePlugin plugin = new RemovableDrivePlugin(executor,
|
||||
callback, finder, monitor);
|
||||
plugin.start(localProperties, remoteProperties, config);
|
||||
plugin.start();
|
||||
|
||||
assertNull(plugin.createWriter(contactId));
|
||||
|
||||
@@ -121,7 +109,7 @@ public class RemovableDrivePluginTest extends TestCase {
|
||||
|
||||
RemovableDrivePlugin plugin = new RemovableDrivePlugin(executor,
|
||||
callback, finder, monitor);
|
||||
plugin.start(localProperties, remoteProperties, config);
|
||||
plugin.start();
|
||||
|
||||
assertNull(plugin.createWriter(contactId));
|
||||
File[] files = drive1.listFiles();
|
||||
@@ -158,7 +146,7 @@ public class RemovableDrivePluginTest extends TestCase {
|
||||
|
||||
RemovableDrivePlugin plugin = new RemovableDrivePlugin(executor,
|
||||
callback, finder, monitor);
|
||||
plugin.start(localProperties, remoteProperties, config);
|
||||
plugin.start();
|
||||
|
||||
assertNull(plugin.createWriter(contactId));
|
||||
File[] files = drive1.listFiles();
|
||||
@@ -197,7 +185,7 @@ public class RemovableDrivePluginTest extends TestCase {
|
||||
|
||||
RemovableDrivePlugin plugin = new RemovableDrivePlugin(executor,
|
||||
callback, finder, monitor);
|
||||
plugin.start(localProperties, remoteProperties, config);
|
||||
plugin.start();
|
||||
|
||||
assertNull(plugin.createWriter(contactId));
|
||||
File[] files = drive1.listFiles();
|
||||
@@ -236,7 +224,7 @@ public class RemovableDrivePluginTest extends TestCase {
|
||||
|
||||
RemovableDrivePlugin plugin = new RemovableDrivePlugin(executor,
|
||||
callback, finder, monitor);
|
||||
plugin.start(localProperties, remoteProperties, config);
|
||||
plugin.start();
|
||||
|
||||
assertNotNull(plugin.createWriter(contactId));
|
||||
// The output file should exist and should be empty
|
||||
@@ -279,7 +267,7 @@ public class RemovableDrivePluginTest extends TestCase {
|
||||
|
||||
RemovableDrivePlugin plugin = new RemovableDrivePlugin(executor,
|
||||
callback, finder, monitor);
|
||||
plugin.start(localProperties, remoteProperties, config);
|
||||
plugin.start();
|
||||
|
||||
BatchTransportWriter writer = plugin.createWriter(contactId);
|
||||
assertNotNull(writer);
|
||||
@@ -319,7 +307,7 @@ public class RemovableDrivePluginTest extends TestCase {
|
||||
|
||||
RemovableDrivePlugin plugin = new RemovableDrivePlugin(executor,
|
||||
callback, finder, monitor);
|
||||
plugin.start(localProperties, remoteProperties, config);
|
||||
plugin.start();
|
||||
|
||||
plugin.driveInserted(testDir);
|
||||
|
||||
@@ -366,7 +354,7 @@ public class RemovableDrivePluginTest extends TestCase {
|
||||
|
||||
RemovableDrivePlugin plugin = new RemovableDrivePlugin(
|
||||
new ImmediateExecutor(), callback, finder, monitor);
|
||||
plugin.start(localProperties, remoteProperties, config);
|
||||
plugin.start();
|
||||
|
||||
File f = new File(testDir, "abcdefgh.dat");
|
||||
OutputStream out = new FileOutputStream(f);
|
||||
@@ -396,7 +384,7 @@ public class RemovableDrivePluginTest extends TestCase {
|
||||
|
||||
RemovableDrivePlugin plugin = new RemovableDrivePlugin(
|
||||
new ImmediateExecutor(), callback, finder, monitor);
|
||||
plugin.start(localProperties, remoteProperties, config);
|
||||
plugin.start();
|
||||
|
||||
File f = new File(testDir, "abcdefgh.dat");
|
||||
OutputStream out = new FileOutputStream(f);
|
||||
|
||||
@@ -18,39 +18,25 @@ import net.sf.briar.api.plugins.StreamTransportCallback;
|
||||
import net.sf.briar.api.transport.StreamTransportConnection;
|
||||
import net.sf.briar.plugins.ImmediateExecutor;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class SimpleSocketPluginTest extends TestCase {
|
||||
|
||||
private final ContactId contactId = new ContactId(0);
|
||||
|
||||
private TransportProperties localProperties = null;
|
||||
private Map<ContactId, TransportProperties> remoteProperties = null;
|
||||
private TransportConfig config = null;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
localProperties = new TransportProperties();
|
||||
remoteProperties = new HashMap<ContactId, TransportProperties>();
|
||||
remoteProperties.put(contactId, new TransportProperties());
|
||||
config = new TransportConfig();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIncomingConnection() throws Exception {
|
||||
StubCallback callback = new StubCallback();
|
||||
localProperties.put("host", "127.0.0.1");
|
||||
localProperties.put("port", "0");
|
||||
StreamCallback callback = new StreamCallback();
|
||||
callback.local.put("host", "127.0.0.1");
|
||||
callback.local.put("port", "0");
|
||||
SimpleSocketPlugin plugin =
|
||||
new SimpleSocketPlugin(new ImmediateExecutor(), callback, 0L);
|
||||
plugin.start(localProperties, remoteProperties, config);
|
||||
plugin.start();
|
||||
// The plugin should have bound a socket and stored the port number
|
||||
assertNotNull(callback.localProperties);
|
||||
String host = callback.localProperties.get("host");
|
||||
String host = callback.local.get("host");
|
||||
assertNotNull(host);
|
||||
assertEquals("127.0.0.1", host);
|
||||
String portString = callback.localProperties.get("port");
|
||||
String portString = callback.local.get("port");
|
||||
assertNotNull(portString);
|
||||
int port = Integer.valueOf(portString);
|
||||
assertTrue(port > 0 && port < 65536);
|
||||
@@ -75,10 +61,10 @@ public class SimpleSocketPluginTest extends TestCase {
|
||||
|
||||
@Test
|
||||
public void testOutgoingConnection() throws Exception {
|
||||
StubCallback callback = new StubCallback();
|
||||
StreamCallback callback = new StreamCallback();
|
||||
SimpleSocketPlugin plugin =
|
||||
new SimpleSocketPlugin(new ImmediateExecutor(), callback, 0L);
|
||||
plugin.start(localProperties, remoteProperties, config);
|
||||
plugin.start();
|
||||
// Listen on a local port
|
||||
final ServerSocket ss = new ServerSocket();
|
||||
ss.bind(new InetSocketAddress("127.0.0.1", 0), 10);
|
||||
@@ -97,10 +83,10 @@ public class SimpleSocketPluginTest extends TestCase {
|
||||
}
|
||||
}.start();
|
||||
// Tell the plugin about the port
|
||||
TransportProperties properties = new TransportProperties();
|
||||
properties.put("host", "127.0.0.1");
|
||||
properties.put("port", String.valueOf(port));
|
||||
plugin.setRemoteProperties(contactId, properties);
|
||||
TransportProperties p = new TransportProperties();
|
||||
p.put("host", "127.0.0.1");
|
||||
p.put("port", String.valueOf(port));
|
||||
callback.remote.put(contactId, p);
|
||||
// Connect to the port
|
||||
StreamTransportConnection conn = plugin.createConnection(contactId);
|
||||
assertNotNull(conn);
|
||||
@@ -113,86 +99,44 @@ public class SimpleSocketPluginTest extends TestCase {
|
||||
plugin.stop();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdatingPropertiesReopensSocket() throws Exception {
|
||||
StubCallback callback = new StubCallback();
|
||||
localProperties.put("host", "127.0.0.1");
|
||||
localProperties.put("port", "0");
|
||||
SimpleSocketPlugin plugin =
|
||||
new SimpleSocketPlugin(new ImmediateExecutor(), callback, 0L);
|
||||
plugin.start(localProperties, remoteProperties, config);
|
||||
// The plugin should have bound a socket and stored the port number
|
||||
assertNotNull(callback.localProperties);
|
||||
String host = callback.localProperties.get("host");
|
||||
assertNotNull(host);
|
||||
assertEquals("127.0.0.1", host);
|
||||
String portString = callback.localProperties.get("port");
|
||||
assertNotNull(portString);
|
||||
int port = Integer.valueOf(portString);
|
||||
assertTrue(port > 0 && port < 65536);
|
||||
// The plugin should be listening on the port
|
||||
InetSocketAddress addr = new InetSocketAddress(host, port);
|
||||
Socket s = new Socket();
|
||||
assertEquals(0, callback.incomingConnections);
|
||||
s.connect(addr, 100);
|
||||
Thread.sleep(10);
|
||||
assertEquals(1, callback.incomingConnections);
|
||||
s.close();
|
||||
// Update the local properties with a new port number
|
||||
localProperties.put("port", "0");
|
||||
plugin.setLocalProperties(localProperties);
|
||||
// The plugin should no longer be listening on the old port
|
||||
try {
|
||||
s = new Socket();
|
||||
s.connect(addr, 100);
|
||||
fail();
|
||||
} catch(IOException expected) {}
|
||||
// Find out what the new port is
|
||||
portString = callback.localProperties.get("port");
|
||||
assertNotNull(portString);
|
||||
int newPort = Integer.valueOf(portString);
|
||||
assertFalse(newPort == port);
|
||||
// The plugin should be listening on the new port
|
||||
addr = new InetSocketAddress(host, newPort);
|
||||
assertEquals(1, callback.incomingConnections);
|
||||
s = new Socket();
|
||||
s.connect(addr, 100);
|
||||
Thread.sleep(10);
|
||||
assertEquals(2, callback.incomingConnections);
|
||||
s.close();
|
||||
// Stop the plugin
|
||||
plugin.stop();
|
||||
Thread.sleep(10);
|
||||
// The plugin should no longer be listening
|
||||
try {
|
||||
s = new Socket();
|
||||
s.connect(addr, 100);
|
||||
fail();
|
||||
} catch(IOException expected) {}
|
||||
}
|
||||
private static class StreamCallback implements StreamTransportCallback {
|
||||
|
||||
private static class StubCallback implements StreamTransportCallback {
|
||||
private TransportConfig config = new TransportConfig();
|
||||
private TransportProperties local = new TransportProperties();
|
||||
private Map<ContactId, TransportProperties> remote =
|
||||
new HashMap<ContactId, TransportProperties>();
|
||||
|
||||
public TransportProperties localProperties = null;
|
||||
public volatile int incomingConnections = 0;
|
||||
private int incomingConnections = 0;
|
||||
|
||||
public void setLocalProperties(TransportProperties properties) {
|
||||
localProperties = properties;
|
||||
public TransportConfig getConfig() {
|
||||
return config;
|
||||
}
|
||||
|
||||
public void setConfig(TransportConfig config) {
|
||||
public TransportProperties getLocalProperties() {
|
||||
return local;
|
||||
}
|
||||
|
||||
public void showMessage(String... message) {
|
||||
public Map<ContactId, TransportProperties> getRemoteProperties() {
|
||||
return remote;
|
||||
}
|
||||
|
||||
public void setConfig(TransportConfig c) {
|
||||
config = c;
|
||||
}
|
||||
|
||||
public void setLocalProperties(TransportProperties p) {
|
||||
local = p;
|
||||
}
|
||||
|
||||
public int showChoice(String[] options, String... message) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
public boolean showConfirmationMessage(String... message) {
|
||||
return false;
|
||||
}
|
||||
|
||||
public int showChoice(String[] choices, String... message) {
|
||||
return -1;
|
||||
}
|
||||
public void showMessage(String... message) {}
|
||||
|
||||
public void incomingConnectionCreated(StreamTransportConnection c) {
|
||||
incomingConnections++;
|
||||
|
||||
Reference in New Issue
Block a user