mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-17 13:19:52 +01:00
Simple TCP socket plugin (untested).
This commit is contained in:
@@ -1,7 +1,6 @@
|
|||||||
package net.sf.briar.api.transport.batch;
|
package net.sf.briar.api.transport.batch;
|
||||||
|
|
||||||
import net.sf.briar.api.ContactId;
|
import net.sf.briar.api.ContactId;
|
||||||
import net.sf.briar.api.TransportId;
|
|
||||||
import net.sf.briar.api.transport.TransportCallback;
|
import net.sf.briar.api.transport.TransportCallback;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -12,6 +11,5 @@ public interface BatchTransportCallback extends TransportCallback {
|
|||||||
|
|
||||||
void readerCreated(BatchTransportReader r);
|
void readerCreated(BatchTransportReader r);
|
||||||
|
|
||||||
void writerCreated(ContactId contactId, TransportId t, long connection,
|
void writerCreated(ContactId contactId, BatchTransportWriter w);
|
||||||
BatchTransportWriter w);
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -50,10 +50,10 @@ public interface BatchTransportPlugin {
|
|||||||
boolean shouldPoll();
|
boolean shouldPoll();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the desired interval in seconds between calls to the plugin's
|
* Returns the desired interval in milliseconds between calls to the
|
||||||
* poll() method.
|
* plugin's poll() method.
|
||||||
*/
|
*/
|
||||||
int getPollingInterval();
|
long getPollingInterval();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Attempts to establish incoming and/or outgoing connections using the
|
* Attempts to establish incoming and/or outgoing connections using the
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package net.sf.briar.api.transport.stream;
|
package net.sf.briar.api.transport.stream;
|
||||||
|
|
||||||
import net.sf.briar.api.ContactId;
|
import net.sf.briar.api.ContactId;
|
||||||
import net.sf.briar.api.TransportId;
|
|
||||||
import net.sf.briar.api.transport.TransportCallback;
|
import net.sf.briar.api.transport.TransportCallback;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -12,6 +11,6 @@ public interface StreamTransportCallback extends TransportCallback {
|
|||||||
|
|
||||||
void incomingConnectionCreated(StreamTransportConnection c);
|
void incomingConnectionCreated(StreamTransportConnection c);
|
||||||
|
|
||||||
void outgoingConnectionCreated(ContactId contactId, TransportId t,
|
void outgoingConnectionCreated(ContactId contactId,
|
||||||
long connection, StreamTransportConnection c);
|
StreamTransportConnection c);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,5 +28,5 @@ public interface StreamTransportConnection {
|
|||||||
* connection is not used, or if an exception is thrown while using the
|
* connection is not used, or if an exception is thrown while using the
|
||||||
* connection.
|
* connection.
|
||||||
*/
|
*/
|
||||||
void close() throws IOException;
|
void dispose() throws IOException;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -49,10 +49,10 @@ public interface StreamTransportPlugin {
|
|||||||
boolean shouldPoll();
|
boolean shouldPoll();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the desired interval in seconds between calls to the plugin's
|
* Returns the desired interval in milliseconds between calls to the
|
||||||
* poll() method.
|
* plugin's poll() method.
|
||||||
*/
|
*/
|
||||||
int getPollingInterval();
|
long getPollingInterval();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Attempts to establish connections using the current transport and
|
* Attempts to establish connections using the current transport and
|
||||||
|
|||||||
@@ -74,18 +74,6 @@ abstract class FilePlugin implements BatchTransportPlugin {
|
|||||||
this.config = config;
|
this.config = config;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean shouldPoll() {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getPollingInterval() {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void poll() {
|
|
||||||
throw new UnsupportedOperationException();
|
|
||||||
}
|
|
||||||
|
|
||||||
public BatchTransportReader createReader(ContactId c) {
|
public BatchTransportReader createReader(ContactId c) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -48,6 +48,18 @@ implements RemovableDriveMonitor.Callback {
|
|||||||
monitor.stop();
|
monitor.stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean shouldPoll() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getPollingInterval() {
|
||||||
|
return 0L;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void poll() {
|
||||||
|
throw new UnsupportedOperationException();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected File chooseOutputDirectory() {
|
protected File chooseOutputDirectory() {
|
||||||
try {
|
try {
|
||||||
|
|||||||
@@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
122
components/net/sf/briar/plugins/socket/SocketPlugin.java
Normal file
122
components/net/sf/briar/plugins/socket/SocketPlugin.java
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user