Simple TCP socket plugin (untested).

This commit is contained in:
akwizgran
2011-10-06 09:17:22 +01:00
parent 80cba1e7f7
commit 45fd4c9060
10 changed files with 261 additions and 25 deletions

View File

@@ -1,7 +1,6 @@
package net.sf.briar.api.transport.batch;
import net.sf.briar.api.ContactId;
import net.sf.briar.api.TransportId;
import net.sf.briar.api.transport.TransportCallback;
/**
@@ -12,6 +11,5 @@ public interface BatchTransportCallback extends TransportCallback {
void readerCreated(BatchTransportReader r);
void writerCreated(ContactId contactId, TransportId t, long connection,
BatchTransportWriter w);
void writerCreated(ContactId contactId, BatchTransportWriter w);
}

View File

@@ -50,10 +50,10 @@ public interface BatchTransportPlugin {
boolean shouldPoll();
/**
* Returns the desired interval in seconds between calls to the plugin's
* poll() method.
* Returns the desired interval in milliseconds between calls to the
* plugin's poll() method.
*/
int getPollingInterval();
long getPollingInterval();
/**
* Attempts to establish incoming and/or outgoing connections using the

View File

@@ -1,7 +1,6 @@
package net.sf.briar.api.transport.stream;
import net.sf.briar.api.ContactId;
import net.sf.briar.api.TransportId;
import net.sf.briar.api.transport.TransportCallback;
/**
@@ -12,6 +11,6 @@ public interface StreamTransportCallback extends TransportCallback {
void incomingConnectionCreated(StreamTransportConnection c);
void outgoingConnectionCreated(ContactId contactId, TransportId t,
long connection, StreamTransportConnection c);
void outgoingConnectionCreated(ContactId contactId,
StreamTransportConnection c);
}

View File

@@ -28,5 +28,5 @@ public interface StreamTransportConnection {
* connection is not used, or if an exception is thrown while using the
* connection.
*/
void close() throws IOException;
void dispose() throws IOException;
}

View File

@@ -49,10 +49,10 @@ public interface StreamTransportPlugin {
boolean shouldPoll();
/**
* Returns the desired interval in seconds between calls to the plugin's
* poll() method.
* Returns the desired interval in milliseconds between calls to the
* plugin's poll() method.
*/
int getPollingInterval();
long getPollingInterval();
/**
* Attempts to establish connections using the current transport and

View File

@@ -74,18 +74,6 @@ abstract class FilePlugin implements BatchTransportPlugin {
this.config = config;
}
public boolean shouldPoll() {
return false;
}
public int getPollingInterval() {
return 0;
}
public void poll() {
throw new UnsupportedOperationException();
}
public BatchTransportReader createReader(ContactId c) {
return null;
}

View File

@@ -48,6 +48,18 @@ implements RemovableDriveMonitor.Callback {
monitor.stop();
}
public boolean shouldPoll() {
return false;
}
public long getPollingInterval() {
return 0L;
}
public void poll() {
throw new UnsupportedOperationException();
}
@Override
protected File chooseOutputDirectory() {
try {

View File

@@ -0,0 +1,79 @@
package net.sf.briar.plugins.socket;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.util.Map;
import java.util.concurrent.Executor;
import net.sf.briar.api.ContactId;
import net.sf.briar.api.TransportId;
public class SimpleSocketPlugin extends SocketPlugin {
public static final int TRANSPORT_ID = 1;
private static final TransportId id = new TransportId(TRANSPORT_ID);
private final long pollingInterval;
SimpleSocketPlugin(Executor executor, long pollingInterval) {
super(executor);
this.pollingInterval = pollingInterval;
}
public TransportId getId() {
return id;
}
public boolean shouldPoll() {
return true;
}
public long getPollingInterval() {
return pollingInterval;
}
@Override
protected SocketAddress getLocalSocketAddress() {
Map<String, String> properties;
synchronized(this) {
properties = localProperties;
}
if(properties == null) return null;
return createSocketAddress(properties);
}
@Override
protected SocketAddress getSocketAddress(ContactId c) {
Map<String, String> properties;
synchronized(this) {
properties = remoteProperties.get(c);
}
if(properties == null) return null;
return createSocketAddress(properties);
}
private SocketAddress createSocketAddress(Map<String, String> properties) {
String host = properties.get("host");
String portString = properties.get("port");
if(host == null || portString == null) return null;
int port;
try {
port = Integer.valueOf(portString);
} catch(NumberFormatException e) {
return null;
}
return InetSocketAddress.createUnresolved(host, port);
}
@Override
protected Socket createClientSocket() {
return new Socket();
}
@Override
protected Socket createServerSocket() {
return new Socket();
}
}

View File

@@ -0,0 +1,122 @@
package net.sf.briar.plugins.socket;
import java.io.IOException;
import java.net.Socket;
import java.net.SocketAddress;
import java.util.Map;
import java.util.concurrent.Executor;
import net.sf.briar.api.ContactId;
import net.sf.briar.api.transport.InvalidConfigException;
import net.sf.briar.api.transport.InvalidTransportException;
import net.sf.briar.api.transport.stream.StreamTransportCallback;
import net.sf.briar.api.transport.stream.StreamTransportConnection;
import net.sf.briar.api.transport.stream.StreamTransportPlugin;
abstract class SocketPlugin implements StreamTransportPlugin {
private final Executor executor;
protected Map<String, String> localProperties = null;
protected Map<ContactId, Map<String, String>> remoteProperties = null;
protected Map<String, String> config = null;
protected StreamTransportCallback callback = null;
private volatile boolean started = false;
protected abstract SocketAddress getLocalSocketAddress();
protected abstract SocketAddress getSocketAddress(ContactId c);
protected abstract Socket createClientSocket();
protected abstract Socket createServerSocket();
SocketPlugin(Executor executor) {
this.executor = executor;
}
public synchronized void start(Map<String, String> localProperties,
Map<ContactId, Map<String, String>> remoteProperties,
Map<String, String> config, StreamTransportCallback callback)
throws InvalidTransportException, InvalidConfigException {
if(started) throw new IllegalStateException();
started = true;
this.localProperties = localProperties;
this.remoteProperties = remoteProperties;
this.config = config;
this.callback = callback;
executor.execute(createBinder());
}
protected Runnable createBinder() {
return new Runnable() {
public void run() {
SocketAddress addr = getLocalSocketAddress();
if(addr == null) return;
Socket s = createServerSocket();
try {
s.bind(addr);
} catch(IOException e) {
return;
}
}
};
}
public synchronized void stop() {
if(!started) throw new IllegalStateException();
started = false;
}
public synchronized void setLocalProperties(Map<String, String> properties)
throws InvalidTransportException {
if(!started) throw new IllegalStateException();
localProperties = properties;
}
public synchronized void setRemoteProperties(ContactId c,
Map<String, String> properties)
throws InvalidTransportException {
if(!started) throw new IllegalStateException();
remoteProperties.put(c, properties);
}
public synchronized void setConfig(Map<String, String> config)
throws InvalidConfigException {
if(!started) throw new IllegalStateException();
this.config = config;
}
public synchronized void poll() {
if(!shouldPoll()) throw new UnsupportedOperationException();
if(!started) throw new IllegalStateException();
for(ContactId c : remoteProperties.keySet()) {
executor.execute(createConnector(c));
}
}
protected Runnable createConnector(final ContactId c) {
return new Runnable() {
public void run() {
StreamTransportConnection conn = createAndConnectSocket(c);
if(conn != null) callback.outgoingConnectionCreated(c, conn);
}
};
}
public StreamTransportConnection createConnection(ContactId c) {
if(!started) throw new IllegalStateException();
return createAndConnectSocket(c);
}
private StreamTransportConnection createAndConnectSocket(ContactId c) {
if(!started) return null;
SocketAddress addr = getSocketAddress(c);
if(addr == null) return null;
Socket s = createClientSocket();
try {
s.connect(addr);
} catch(IOException e) {
return null;
}
return new SocketTransportConnection(s);
}
}

View File

@@ -0,0 +1,38 @@
package net.sf.briar.plugins.socket;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import net.sf.briar.api.transport.stream.StreamTransportConnection;
class SocketTransportConnection implements StreamTransportConnection {
private final Socket socket;
private boolean streamInUse = false;
SocketTransportConnection(Socket socket) {
this.socket = socket;
}
public InputStream getInputStream() throws IOException {
streamInUse = true;
return socket.getInputStream();
}
public OutputStream getOutputStream() throws IOException {
streamInUse = true;
return socket.getOutputStream();
}
public void finish() throws IOException {
// FIXME: Tell the plugin?
streamInUse = false;
}
public void dispose() throws IOException {
if(streamInUse) socket.close();
}
}