mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-18 13:49:53 +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.ContactId;
|
||||||
import net.sf.briar.api.transport.BatchTransportReader;
|
import net.sf.briar.api.transport.BatchTransportReader;
|
||||||
import net.sf.briar.api.transport.BatchTransportWriter;
|
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
|
* 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.ContactId;
|
||||||
import net.sf.briar.api.transport.StreamTransportConnection;
|
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
|
* 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;
|
package net.sf.briar.api.plugins;
|
||||||
|
|
||||||
import java.io.IOException;
|
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.TransportId;
|
||||||
import net.sf.briar.api.TransportProperties;
|
|
||||||
|
|
||||||
public interface TransportPlugin {
|
public interface TransportPlugin {
|
||||||
|
|
||||||
@@ -14,22 +10,11 @@ public interface TransportPlugin {
|
|||||||
TransportId getId();
|
TransportId getId();
|
||||||
|
|
||||||
/** Starts the plugin. */
|
/** Starts the plugin. */
|
||||||
void start(TransportProperties localProperties,
|
void start() throws IOException;
|
||||||
Map<ContactId, TransportProperties> remoteProperties,
|
|
||||||
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. */
|
|
||||||
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
|
* Returns true if the plugin's poll() method should be called
|
||||||
* periodically to attempt to establish connections.
|
* 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;
|
package net.sf.briar.plugins;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Map;
|
|
||||||
import java.util.concurrent.Executor;
|
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;
|
import net.sf.briar.api.plugins.TransportPlugin;
|
||||||
|
|
||||||
public abstract class AbstractPlugin implements TransportPlugin {
|
public abstract class AbstractPlugin implements TransportPlugin {
|
||||||
|
|
||||||
protected final Executor executor;
|
protected final Executor executor;
|
||||||
|
|
||||||
// These fields should be accessed with this's lock held
|
// This field must only be accessed with this's lock held
|
||||||
protected TransportProperties localProperties = null;
|
|
||||||
protected Map<ContactId, TransportProperties> remoteProperties = 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(TransportProperties localProperties,
|
public synchronized void start() throws IOException {
|
||||||
Map<ContactId, TransportProperties> remoteProperties,
|
|
||||||
TransportConfig config) throws IOException {
|
|
||||||
if(started) throw new IllegalStateException();
|
if(started) throw new IllegalStateException();
|
||||||
started = true;
|
started = true;
|
||||||
this.localProperties = localProperties;
|
|
||||||
this.remoteProperties = remoteProperties;
|
|
||||||
this.config = config;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public synchronized void stop() throws IOException {
|
public synchronized void stop() throws IOException {
|
||||||
if(!started) throw new IllegalStateException();
|
if(!started) throw new IllegalStateException();
|
||||||
started = false;
|
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;
|
package net.sf.briar.plugins.bluetooth;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Collection;
|
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@@ -55,13 +54,11 @@ class BluetoothPlugin extends AbstractPlugin implements StreamTransportPlugin {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void start(TransportProperties localProperties,
|
public void start() throws IOException {
|
||||||
Map<ContactId, TransportProperties> remoteProperties,
|
|
||||||
TransportConfig config) throws IOException {
|
|
||||||
// Initialise the Bluetooth stack
|
// Initialise the Bluetooth stack
|
||||||
try {
|
try {
|
||||||
synchronized(this) {
|
synchronized(this) {
|
||||||
super.start(localProperties, remoteProperties, config);
|
super.start();
|
||||||
localDevice = LocalDevice.getLocalDevice();
|
localDevice = LocalDevice.getLocalDevice();
|
||||||
}
|
}
|
||||||
} catch(UnsatisfiedLinkError e) {
|
} catch(UnsatisfiedLinkError e) {
|
||||||
@@ -95,10 +92,11 @@ class BluetoothPlugin extends AbstractPlugin implements StreamTransportPlugin {
|
|||||||
LocalDevice ld;
|
LocalDevice ld;
|
||||||
synchronized(this) {
|
synchronized(this) {
|
||||||
if(!started) return;
|
if(!started) return;
|
||||||
uuid = config.get("uuid");
|
TransportConfig c = callback.getConfig();
|
||||||
|
uuid = c.get("uuid");
|
||||||
|
if(uuid == null) uuid = createAndSetUuid(c);
|
||||||
ld = localDevice;
|
ld = localDevice;
|
||||||
}
|
}
|
||||||
if(uuid == null) uuid = createAndSetUuid();
|
|
||||||
// Try to make the device discoverable (requires root on Linux)
|
// Try to make the device discoverable (requires root on Linux)
|
||||||
try {
|
try {
|
||||||
ld.setDiscoverable(DiscoveryAgent.GIAC);
|
ld.setDiscoverable(DiscoveryAgent.GIAC);
|
||||||
@@ -130,15 +128,10 @@ class BluetoothPlugin extends AbstractPlugin implements StreamTransportPlugin {
|
|||||||
setLocalBluetoothAddress(ld.getBluetoothAddress());
|
setLocalBluetoothAddress(ld.getBluetoothAddress());
|
||||||
}
|
}
|
||||||
|
|
||||||
private String createAndSetUuid() {
|
private synchronized String createAndSetUuid(TransportConfig c) {
|
||||||
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);
|
||||||
TransportConfig c;
|
|
||||||
synchronized(this) {
|
|
||||||
if(!started) return uuid;
|
|
||||||
c = new TransportConfig(config);
|
|
||||||
}
|
|
||||||
c.put("uuid", uuid);
|
c.put("uuid", uuid);
|
||||||
callback.setConfig(c);
|
callback.setConfig(c);
|
||||||
return uuid;
|
return uuid;
|
||||||
@@ -174,12 +167,9 @@ class BluetoothPlugin extends AbstractPlugin implements StreamTransportPlugin {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setLocalBluetoothAddress(String address) {
|
private synchronized void setLocalBluetoothAddress(String address) {
|
||||||
TransportProperties p;
|
if(!started) return;
|
||||||
synchronized(this) {
|
TransportProperties p = callback.getLocalProperties();
|
||||||
if(!started) return;
|
|
||||||
p = new TransportProperties(localProperties);
|
|
||||||
}
|
|
||||||
p.put("address", address);
|
p.put("address", address);
|
||||||
callback.setLocalProperties(p);
|
callback.setLocalProperties(p);
|
||||||
}
|
}
|
||||||
@@ -194,28 +184,28 @@ class BluetoothPlugin extends AbstractPlugin implements StreamTransportPlugin {
|
|||||||
|
|
||||||
public synchronized void poll() {
|
public synchronized void poll() {
|
||||||
if(!started) return;
|
if(!started) return;
|
||||||
executor.execute(createConnectors(remoteProperties.keySet()));
|
executor.execute(createConnectors());
|
||||||
}
|
}
|
||||||
|
|
||||||
private Runnable createConnectors(final Collection<ContactId> contacts) {
|
private Runnable createConnectors() {
|
||||||
return new Runnable() {
|
return new Runnable() {
|
||||||
public void run() {
|
public void run() {
|
||||||
connectAndCallBack(contacts);
|
connectAndCallBack();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
private void connectAndCallBack(Collection<ContactId> contacts) {
|
private void connectAndCallBack() {
|
||||||
Map<ContactId, String> discovered = discover(contacts);
|
Map<ContactId, String> discovered = discover();
|
||||||
for(Entry<ContactId, String> e : discovered.entrySet()) {
|
for(Entry<ContactId, String> e : discovered.entrySet()) {
|
||||||
ContactId c = e.getKey();
|
ContactId c = e.getKey();
|
||||||
String url = e.getValue();
|
String url = e.getValue();
|
||||||
StreamTransportConnection conn = createConnection(c, url);
|
StreamTransportConnection conn = connect(c, url);
|
||||||
if(conn != null) callback.outgoingConnectionCreated(c, conn);
|
if(conn != null) callback.outgoingConnectionCreated(c, conn);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private Map<ContactId, String> discover(Collection<ContactId> contacts) {
|
private Map<ContactId, String> discover() {
|
||||||
DiscoveryAgent discoveryAgent;
|
DiscoveryAgent discoveryAgent;
|
||||||
Map<String, ContactId> addresses;
|
Map<String, ContactId> addresses;
|
||||||
Map<ContactId, String> uuids;
|
Map<ContactId, String> uuids;
|
||||||
@@ -225,12 +215,13 @@ 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, TransportProperties> e
|
Map<ContactId, TransportProperties> remote =
|
||||||
: remoteProperties.entrySet()) {
|
callback.getRemoteProperties();
|
||||||
|
for(Entry<ContactId, TransportProperties> e : remote.entrySet()) {
|
||||||
ContactId c = e.getKey();
|
ContactId c = e.getKey();
|
||||||
TransportProperties properties = e.getValue();
|
TransportProperties p = e.getValue();
|
||||||
String address = properties.get("address");
|
String address = p.get("address");
|
||||||
String uuid = properties.get("uuid");
|
String uuid = p.get("uuid");
|
||||||
if(address != null && uuid != null) {
|
if(address != null && uuid != null) {
|
||||||
addresses.put(address, c);
|
addresses.put(address, c);
|
||||||
uuids.put(c, uuid);
|
uuids.put(c, uuid);
|
||||||
@@ -250,12 +241,13 @@ class BluetoothPlugin extends AbstractPlugin implements StreamTransportPlugin {
|
|||||||
return listener.getUrls();
|
return listener.getUrls();
|
||||||
}
|
}
|
||||||
|
|
||||||
private StreamTransportConnection createConnection(ContactId c,
|
private StreamTransportConnection connect(ContactId c, String url) {
|
||||||
String url) {
|
|
||||||
try {
|
try {
|
||||||
synchronized(this) {
|
synchronized(this) {
|
||||||
if(!started) return null;
|
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);
|
StreamConnection s = (StreamConnection) Connector.open(url);
|
||||||
return new BluetoothTransportConnection(s);
|
return new BluetoothTransportConnection(s);
|
||||||
@@ -266,8 +258,8 @@ class BluetoothPlugin extends AbstractPlugin implements StreamTransportPlugin {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public StreamTransportConnection createConnection(ContactId c) {
|
public StreamTransportConnection createConnection(ContactId c) {
|
||||||
Map<ContactId, String> discovered = discover(Collections.singleton(c));
|
Map<ContactId, String> discovered = discover();
|
||||||
String url = discovered.get(c);
|
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.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
|
||||||
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;
|
||||||
|
|
||||||
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
|
||||||
@@ -38,15 +34,13 @@ implements RemovableDriveMonitor.Callback {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void start(TransportProperties localProperties,
|
public synchronized void start() throws IOException {
|
||||||
Map<ContactId, TransportProperties> remoteProperties,
|
super.start();
|
||||||
TransportConfig config) throws IOException {
|
|
||||||
super.start(localProperties, remoteProperties, config);
|
|
||||||
monitor.start(this);
|
monitor.start(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void stop() throws IOException {
|
public synchronized void stop() throws IOException {
|
||||||
super.stop();
|
super.stop();
|
||||||
monitor.stop();
|
monitor.stop();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -39,23 +39,36 @@ class SimpleSocketPlugin extends SocketPlugin {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected SocketAddress getLocalSocketAddress() {
|
protected Socket createClientSocket() throws IOException {
|
||||||
assert started;
|
assert started;
|
||||||
return createSocketAddress(localProperties);
|
return new Socket();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected SocketAddress getSocketAddress(ContactId c) {
|
protected ServerSocket createServerSocket() throws IOException {
|
||||||
assert started;
|
assert started;
|
||||||
TransportProperties properties = remoteProperties.get(c);
|
return new ServerSocket();
|
||||||
if(properties == null) return null;
|
|
||||||
return createSocketAddress(properties);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private SocketAddress createSocketAddress(TransportProperties properties) {
|
@Override
|
||||||
assert properties != null;
|
protected synchronized SocketAddress getLocalSocketAddress() {
|
||||||
String host = properties.get("host");
|
assert started;
|
||||||
String portString = properties.get("port");
|
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;
|
if(host == null || portString == null) return null;
|
||||||
int port;
|
int port;
|
||||||
try {
|
try {
|
||||||
@@ -67,32 +80,17 @@ class SimpleSocketPlugin extends SocketPlugin {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void setLocalSocketAddress(SocketAddress s) {
|
protected synchronized void setLocalSocketAddress(SocketAddress s) {
|
||||||
TransportProperties p;
|
assert started;
|
||||||
synchronized(this) {
|
|
||||||
if(!started) return;
|
|
||||||
p = new TransportProperties(localProperties);
|
|
||||||
}
|
|
||||||
if(!(s instanceof InetSocketAddress))
|
if(!(s instanceof InetSocketAddress))
|
||||||
throw new IllegalArgumentException();
|
throw new IllegalArgumentException();
|
||||||
InetSocketAddress i = (InetSocketAddress) s;
|
InetSocketAddress i = (InetSocketAddress) s;
|
||||||
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?
|
||||||
|
TransportProperties p = callback.getLocalProperties();
|
||||||
p.put("host", host);
|
p.put("host", host);
|
||||||
p.put("port", port);
|
p.put("port", port);
|
||||||
callback.setLocalProperties(p);
|
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.ServerSocket;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
import java.net.SocketAddress;
|
import java.net.SocketAddress;
|
||||||
import java.util.Map;
|
|
||||||
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;
|
||||||
|
|
||||||
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;
|
||||||
@@ -25,16 +22,17 @@ implements StreamTransportPlugin {
|
|||||||
|
|
||||||
protected final StreamTransportCallback callback;
|
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 ServerSocket socket = null;
|
||||||
|
|
||||||
protected abstract void setLocalSocketAddress(SocketAddress s);
|
protected abstract void setLocalSocketAddress(SocketAddress s);
|
||||||
|
|
||||||
// These methods should be called with this's lock held and started == true
|
// These methods must only be called with this's lock held and
|
||||||
protected abstract SocketAddress getLocalSocketAddress();
|
// started == true
|
||||||
protected abstract SocketAddress getSocketAddress(ContactId c);
|
|
||||||
protected abstract Socket createClientSocket() throws IOException;
|
protected abstract Socket createClientSocket() throws IOException;
|
||||||
protected abstract ServerSocket createServerSocket() throws IOException;
|
protected abstract ServerSocket createServerSocket() throws IOException;
|
||||||
|
protected abstract SocketAddress getLocalSocketAddress();
|
||||||
|
protected abstract SocketAddress getRemoteSocketAddress(ContactId c);
|
||||||
|
|
||||||
protected SocketPlugin(Executor executor,
|
protected SocketPlugin(Executor executor,
|
||||||
StreamTransportCallback callback) {
|
StreamTransportCallback callback) {
|
||||||
@@ -43,10 +41,8 @@ implements StreamTransportPlugin {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized void start(TransportProperties localProperties,
|
public synchronized void start() throws IOException {
|
||||||
Map<ContactId, TransportProperties> remoteProperties,
|
super.start();
|
||||||
TransportConfig config) throws IOException {
|
|
||||||
super.start(localProperties, remoteProperties, config);
|
|
||||||
executor.execute(createBinder());
|
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() {
|
public synchronized void poll() {
|
||||||
// Subclasses may not support polling
|
// Subclasses may not support polling
|
||||||
if(!shouldPoll()) throw new UnsupportedOperationException();
|
if(!shouldPoll()) throw new UnsupportedOperationException();
|
||||||
if(!started) return;
|
if(!started) return;
|
||||||
for(ContactId c : remoteProperties.keySet()) {
|
for(ContactId c : callback.getRemoteProperties().keySet()) {
|
||||||
executor.execute(createConnector(c));
|
executor.execute(createConnector(c));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -177,7 +154,7 @@ implements StreamTransportPlugin {
|
|||||||
try {
|
try {
|
||||||
synchronized(this) {
|
synchronized(this) {
|
||||||
if(!started) return null;
|
if(!started) return null;
|
||||||
addr = getSocketAddress(c);
|
addr = getRemoteSocketAddress(c);
|
||||||
s = createClientSocket();
|
s = createClientSocket();
|
||||||
}
|
}
|
||||||
if(addr == null || s == null) return null;
|
if(addr == null || s == null) return null;
|
||||||
|
|||||||
@@ -24,22 +24,18 @@ public class BluetoothClientTest {
|
|||||||
System.exit(1);
|
System.exit(1);
|
||||||
}
|
}
|
||||||
ContactId contactId = new ContactId(0);
|
ContactId contactId = new ContactId(0);
|
||||||
TransportProperties localProperties = new TransportProperties();
|
ClientCallback callback = new ClientCallback();
|
||||||
Map<ContactId, TransportProperties> remoteProperties =
|
|
||||||
new HashMap<ContactId, TransportProperties>();
|
|
||||||
TransportConfig config = new TransportConfig();
|
|
||||||
StreamTransportCallback callback = new ClientCallback();
|
|
||||||
// Store the server's Bluetooth address and UUID
|
// Store the server's Bluetooth address and UUID
|
||||||
TransportProperties p = new TransportProperties();
|
TransportProperties p = new TransportProperties();
|
||||||
p.put("address", args[0]);
|
p.put("address", args[0]);
|
||||||
p.put("uuid", BluetoothServerTest.UUID);
|
p.put("uuid", BluetoothServerTest.UUID);
|
||||||
remoteProperties.put(contactId, p);
|
callback.remote.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);
|
||||||
// Start the plugin
|
// Start the plugin
|
||||||
System.out.println("Starting plugin");
|
System.out.println("Starting plugin");
|
||||||
plugin.start(localProperties, remoteProperties, config);
|
plugin.start();
|
||||||
// Try to connect to the server
|
// Try to connect to the server
|
||||||
System.out.println("Creating connection");
|
System.out.println("Creating connection");
|
||||||
StreamTransportConnection conn = plugin.createConnection(contactId);
|
StreamTransportConnection conn = plugin.createConnection(contactId);
|
||||||
@@ -66,19 +62,40 @@ public class BluetoothClientTest {
|
|||||||
|
|
||||||
private static class ClientCallback implements StreamTransportCallback {
|
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) {
|
public boolean showConfirmationMessage(String... message) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int showChoice(String[] choices, String... message) {
|
public void showMessage(String... message) {}
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void incomingConnectionCreated(StreamTransportConnection c) {}
|
public void incomingConnectionCreated(StreamTransportConnection c) {}
|
||||||
|
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ package net.sf.briar.plugins.bluetooth;
|
|||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.PrintStream;
|
import java.io.PrintStream;
|
||||||
import java.util.Collections;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
|
|
||||||
@@ -21,19 +21,15 @@ 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 {
|
||||||
TransportProperties localProperties = new TransportProperties();
|
ServerCallback callback = new ServerCallback();
|
||||||
Map<ContactId, TransportProperties> remoteProperties =
|
|
||||||
Collections.emptyMap();
|
|
||||||
TransportConfig config = new TransportConfig();
|
|
||||||
StreamTransportCallback callback = new ServerCallback();
|
|
||||||
// Store the UUID
|
// Store the UUID
|
||||||
config.put("uuid", UUID);
|
callback.config.put("uuid", UUID);
|
||||||
// Create the plugin
|
// Create the plugin
|
||||||
BluetoothPlugin plugin =
|
BluetoothPlugin plugin =
|
||||||
new BluetoothPlugin(new ImmediateExecutor(), callback, 0L);
|
new BluetoothPlugin(new ImmediateExecutor(), callback, 0L);
|
||||||
// Start the plugin
|
// Start the plugin
|
||||||
System.out.println("Starting plugin");
|
System.out.println("Starting plugin");
|
||||||
plugin.start(localProperties, remoteProperties, config);
|
plugin.start();
|
||||||
// Wait for a connection
|
// Wait for a connection
|
||||||
System.out.println("Waiting for connection");
|
System.out.println("Waiting for connection");
|
||||||
synchronized(callback) {
|
synchronized(callback) {
|
||||||
@@ -46,19 +42,40 @@ public class BluetoothServerTest {
|
|||||||
|
|
||||||
private static class ServerCallback implements StreamTransportCallback {
|
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) {
|
public boolean showConfirmationMessage(String... message) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int showChoice(String[] choices, String... message) {
|
public void showMessage(String... message) {}
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void incomingConnectionCreated(StreamTransportConnection conn) {
|
public void incomingConnectionCreated(StreamTransportConnection conn) {
|
||||||
System.out.println("Connection received");
|
System.out.println("Connection received");
|
||||||
|
|||||||
@@ -5,16 +5,12 @@ import java.io.FileOutputStream;
|
|||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
|
||||||
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;
|
||||||
@@ -32,16 +28,8 @@ 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 TransportProperties localProperties = null;
|
|
||||||
private Map<ContactId, TransportProperties> remoteProperties = null;
|
|
||||||
private TransportConfig config = null;
|
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
localProperties = new TransportProperties();
|
|
||||||
remoteProperties = new HashMap<ContactId, TransportProperties>();
|
|
||||||
remoteProperties.put(contactId, new TransportProperties());
|
|
||||||
config = new TransportConfig();
|
|
||||||
testDir.mkdirs();
|
testDir.mkdirs();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -86,7 +74,7 @@ public class RemovableDrivePluginTest extends TestCase {
|
|||||||
|
|
||||||
RemovableDrivePlugin plugin = new RemovableDrivePlugin(executor,
|
RemovableDrivePlugin plugin = new RemovableDrivePlugin(executor,
|
||||||
callback, finder, monitor);
|
callback, finder, monitor);
|
||||||
plugin.start(localProperties, remoteProperties, config);
|
plugin.start();
|
||||||
|
|
||||||
assertNull(plugin.createWriter(contactId));
|
assertNull(plugin.createWriter(contactId));
|
||||||
|
|
||||||
@@ -121,7 +109,7 @@ public class RemovableDrivePluginTest extends TestCase {
|
|||||||
|
|
||||||
RemovableDrivePlugin plugin = new RemovableDrivePlugin(executor,
|
RemovableDrivePlugin plugin = new RemovableDrivePlugin(executor,
|
||||||
callback, finder, monitor);
|
callback, finder, monitor);
|
||||||
plugin.start(localProperties, remoteProperties, config);
|
plugin.start();
|
||||||
|
|
||||||
assertNull(plugin.createWriter(contactId));
|
assertNull(plugin.createWriter(contactId));
|
||||||
File[] files = drive1.listFiles();
|
File[] files = drive1.listFiles();
|
||||||
@@ -158,7 +146,7 @@ public class RemovableDrivePluginTest extends TestCase {
|
|||||||
|
|
||||||
RemovableDrivePlugin plugin = new RemovableDrivePlugin(executor,
|
RemovableDrivePlugin plugin = new RemovableDrivePlugin(executor,
|
||||||
callback, finder, monitor);
|
callback, finder, monitor);
|
||||||
plugin.start(localProperties, remoteProperties, config);
|
plugin.start();
|
||||||
|
|
||||||
assertNull(plugin.createWriter(contactId));
|
assertNull(plugin.createWriter(contactId));
|
||||||
File[] files = drive1.listFiles();
|
File[] files = drive1.listFiles();
|
||||||
@@ -197,7 +185,7 @@ public class RemovableDrivePluginTest extends TestCase {
|
|||||||
|
|
||||||
RemovableDrivePlugin plugin = new RemovableDrivePlugin(executor,
|
RemovableDrivePlugin plugin = new RemovableDrivePlugin(executor,
|
||||||
callback, finder, monitor);
|
callback, finder, monitor);
|
||||||
plugin.start(localProperties, remoteProperties, config);
|
plugin.start();
|
||||||
|
|
||||||
assertNull(plugin.createWriter(contactId));
|
assertNull(plugin.createWriter(contactId));
|
||||||
File[] files = drive1.listFiles();
|
File[] files = drive1.listFiles();
|
||||||
@@ -236,7 +224,7 @@ public class RemovableDrivePluginTest extends TestCase {
|
|||||||
|
|
||||||
RemovableDrivePlugin plugin = new RemovableDrivePlugin(executor,
|
RemovableDrivePlugin plugin = new RemovableDrivePlugin(executor,
|
||||||
callback, finder, monitor);
|
callback, finder, monitor);
|
||||||
plugin.start(localProperties, remoteProperties, config);
|
plugin.start();
|
||||||
|
|
||||||
assertNotNull(plugin.createWriter(contactId));
|
assertNotNull(plugin.createWriter(contactId));
|
||||||
// The output file should exist and should be empty
|
// The output file should exist and should be empty
|
||||||
@@ -279,7 +267,7 @@ public class RemovableDrivePluginTest extends TestCase {
|
|||||||
|
|
||||||
RemovableDrivePlugin plugin = new RemovableDrivePlugin(executor,
|
RemovableDrivePlugin plugin = new RemovableDrivePlugin(executor,
|
||||||
callback, finder, monitor);
|
callback, finder, monitor);
|
||||||
plugin.start(localProperties, remoteProperties, config);
|
plugin.start();
|
||||||
|
|
||||||
BatchTransportWriter writer = plugin.createWriter(contactId);
|
BatchTransportWriter writer = plugin.createWriter(contactId);
|
||||||
assertNotNull(writer);
|
assertNotNull(writer);
|
||||||
@@ -319,7 +307,7 @@ public class RemovableDrivePluginTest extends TestCase {
|
|||||||
|
|
||||||
RemovableDrivePlugin plugin = new RemovableDrivePlugin(executor,
|
RemovableDrivePlugin plugin = new RemovableDrivePlugin(executor,
|
||||||
callback, finder, monitor);
|
callback, finder, monitor);
|
||||||
plugin.start(localProperties, remoteProperties, config);
|
plugin.start();
|
||||||
|
|
||||||
plugin.driveInserted(testDir);
|
plugin.driveInserted(testDir);
|
||||||
|
|
||||||
@@ -366,7 +354,7 @@ public class RemovableDrivePluginTest extends TestCase {
|
|||||||
|
|
||||||
RemovableDrivePlugin plugin = new RemovableDrivePlugin(
|
RemovableDrivePlugin plugin = new RemovableDrivePlugin(
|
||||||
new ImmediateExecutor(), callback, finder, monitor);
|
new ImmediateExecutor(), callback, finder, monitor);
|
||||||
plugin.start(localProperties, remoteProperties, config);
|
plugin.start();
|
||||||
|
|
||||||
File f = new File(testDir, "abcdefgh.dat");
|
File f = new File(testDir, "abcdefgh.dat");
|
||||||
OutputStream out = new FileOutputStream(f);
|
OutputStream out = new FileOutputStream(f);
|
||||||
@@ -396,7 +384,7 @@ public class RemovableDrivePluginTest extends TestCase {
|
|||||||
|
|
||||||
RemovableDrivePlugin plugin = new RemovableDrivePlugin(
|
RemovableDrivePlugin plugin = new RemovableDrivePlugin(
|
||||||
new ImmediateExecutor(), callback, finder, monitor);
|
new ImmediateExecutor(), callback, finder, monitor);
|
||||||
plugin.start(localProperties, remoteProperties, config);
|
plugin.start();
|
||||||
|
|
||||||
File f = new File(testDir, "abcdefgh.dat");
|
File f = new File(testDir, "abcdefgh.dat");
|
||||||
OutputStream out = new FileOutputStream(f);
|
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.api.transport.StreamTransportConnection;
|
||||||
import net.sf.briar.plugins.ImmediateExecutor;
|
import net.sf.briar.plugins.ImmediateExecutor;
|
||||||
|
|
||||||
import org.junit.Before;
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
public class SimpleSocketPluginTest extends TestCase {
|
public class SimpleSocketPluginTest extends TestCase {
|
||||||
|
|
||||||
private final ContactId contactId = new ContactId(0);
|
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
|
@Test
|
||||||
public void testIncomingConnection() throws Exception {
|
public void testIncomingConnection() throws Exception {
|
||||||
StubCallback callback = new StubCallback();
|
StreamCallback callback = new StreamCallback();
|
||||||
localProperties.put("host", "127.0.0.1");
|
callback.local.put("host", "127.0.0.1");
|
||||||
localProperties.put("port", "0");
|
callback.local.put("port", "0");
|
||||||
SimpleSocketPlugin plugin =
|
SimpleSocketPlugin plugin =
|
||||||
new SimpleSocketPlugin(new ImmediateExecutor(), callback, 0L);
|
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
|
// The plugin should have bound a socket and stored the port number
|
||||||
assertNotNull(callback.localProperties);
|
String host = callback.local.get("host");
|
||||||
String host = callback.localProperties.get("host");
|
|
||||||
assertNotNull(host);
|
assertNotNull(host);
|
||||||
assertEquals("127.0.0.1", host);
|
assertEquals("127.0.0.1", host);
|
||||||
String portString = callback.localProperties.get("port");
|
String portString = callback.local.get("port");
|
||||||
assertNotNull(portString);
|
assertNotNull(portString);
|
||||||
int port = Integer.valueOf(portString);
|
int port = Integer.valueOf(portString);
|
||||||
assertTrue(port > 0 && port < 65536);
|
assertTrue(port > 0 && port < 65536);
|
||||||
@@ -75,10 +61,10 @@ public class SimpleSocketPluginTest extends TestCase {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testOutgoingConnection() throws Exception {
|
public void testOutgoingConnection() throws Exception {
|
||||||
StubCallback callback = new StubCallback();
|
StreamCallback callback = new StreamCallback();
|
||||||
SimpleSocketPlugin plugin =
|
SimpleSocketPlugin plugin =
|
||||||
new SimpleSocketPlugin(new ImmediateExecutor(), callback, 0L);
|
new SimpleSocketPlugin(new ImmediateExecutor(), callback, 0L);
|
||||||
plugin.start(localProperties, remoteProperties, config);
|
plugin.start();
|
||||||
// Listen on a local port
|
// Listen on a local port
|
||||||
final ServerSocket ss = new ServerSocket();
|
final ServerSocket ss = new ServerSocket();
|
||||||
ss.bind(new InetSocketAddress("127.0.0.1", 0), 10);
|
ss.bind(new InetSocketAddress("127.0.0.1", 0), 10);
|
||||||
@@ -97,10 +83,10 @@ public class SimpleSocketPluginTest extends TestCase {
|
|||||||
}
|
}
|
||||||
}.start();
|
}.start();
|
||||||
// Tell the plugin about the port
|
// Tell the plugin about the port
|
||||||
TransportProperties properties = new TransportProperties();
|
TransportProperties p = new TransportProperties();
|
||||||
properties.put("host", "127.0.0.1");
|
p.put("host", "127.0.0.1");
|
||||||
properties.put("port", String.valueOf(port));
|
p.put("port", String.valueOf(port));
|
||||||
plugin.setRemoteProperties(contactId, properties);
|
callback.remote.put(contactId, p);
|
||||||
// Connect to the port
|
// Connect to the port
|
||||||
StreamTransportConnection conn = plugin.createConnection(contactId);
|
StreamTransportConnection conn = plugin.createConnection(contactId);
|
||||||
assertNotNull(conn);
|
assertNotNull(conn);
|
||||||
@@ -113,86 +99,44 @@ public class SimpleSocketPluginTest extends TestCase {
|
|||||||
plugin.stop();
|
plugin.stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
private static class StreamCallback implements StreamTransportCallback {
|
||||||
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 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;
|
private int incomingConnections = 0;
|
||||||
public volatile int incomingConnections = 0;
|
|
||||||
|
|
||||||
public void setLocalProperties(TransportProperties properties) {
|
public TransportConfig getConfig() {
|
||||||
localProperties = properties;
|
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) {
|
public boolean showConfirmationMessage(String... message) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int showChoice(String[] choices, String... message) {
|
public void showMessage(String... message) {}
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void incomingConnectionCreated(StreamTransportConnection c) {
|
public void incomingConnectionCreated(StreamTransportConnection c) {
|
||||||
incomingConnections++;
|
incomingConnections++;
|
||||||
|
|||||||
Reference in New Issue
Block a user