Skip to content
Snippets Groups Projects
Commit 42e586ab authored by akwizgran's avatar akwizgran
Browse files

Merged code from FilePlugin and SocketPlugin into a superclass.

parent 3e522c81
No related branches found
No related tags found
No related merge requests found
package net.sf.briar.plugins;
import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.Executor;
import net.sf.briar.api.ContactId;
import net.sf.briar.api.transport.InvalidConfigException;
import net.sf.briar.api.transport.InvalidPropertiesException;
import net.sf.briar.api.transport.TransportPlugin;
import net.sf.briar.api.transport.batch.BatchTransportCallback;
public abstract class AbstractPlugin implements TransportPlugin {
protected final Executor executor;
protected Map<String, String> localProperties = null;
protected Map<ContactId, Map<String, String>> remoteProperties = null;
protected Map<String, String> config = null;
protected BatchTransportCallback callback = null;
protected boolean started = false;
protected AbstractPlugin(Executor executor) {
this.executor = executor;
}
protected void start(Map<String, String> localProperties,
Map<ContactId, Map<String, String>> remoteProperties,
Map<String, String> config) throws InvalidPropertiesException,
InvalidConfigException {
if(started) throw new IllegalStateException();
started = true;
this.localProperties = Collections.unmodifiableMap(localProperties);
// Copy the remoteProperties map to make its values unmodifiable
// Copy the remoteProperties map to make its values unmodifiable
int size = remoteProperties.size();
Map<ContactId, Map<String, String>> m =
new HashMap<ContactId, Map<String, String>>(size);
for(Entry<ContactId, Map<String, String>> e
: remoteProperties.entrySet()) {
m.put(e.getKey(), Collections.unmodifiableMap(e.getValue()));
}
this.remoteProperties = m;
this.config = Collections.unmodifiableMap(config);
}
public synchronized void stop() throws IOException {
if(!started) throw new IllegalStateException();
started = false;
}
public synchronized void setLocalProperties(Map<String, String> properties)
throws InvalidPropertiesException {
if(!started) throw new IllegalStateException();
localProperties = Collections.unmodifiableMap(properties);
}
public synchronized void setRemoteProperties(ContactId c,
Map<String, String> properties)
throws InvalidPropertiesException {
if(!started) throw new IllegalStateException();
remoteProperties.put(c, Collections.unmodifiableMap(properties));
}
public synchronized void setConfig(Map<String, String> config)
throws InvalidConfigException {
if(!started) throw new IllegalStateException();
this.config = Collections.unmodifiableMap(config);
}
}
......@@ -5,10 +5,7 @@ import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.Executor;
import net.sf.briar.api.ContactId;
......@@ -19,79 +16,37 @@ import net.sf.briar.api.transport.batch.BatchTransportCallback;
import net.sf.briar.api.transport.batch.BatchTransportPlugin;
import net.sf.briar.api.transport.batch.BatchTransportReader;
import net.sf.briar.api.transport.batch.BatchTransportWriter;
import net.sf.briar.plugins.AbstractPlugin;
import org.apache.commons.io.FileSystemUtils;
abstract class FilePlugin implements BatchTransportPlugin {
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 BatchTransportCallback callback = null;
private volatile boolean started = false;
abstract class FilePlugin extends AbstractPlugin
implements BatchTransportPlugin {
protected abstract File chooseOutputDirectory();
protected abstract void writerFinished(File f);
protected abstract void readerFinished(File f);
FilePlugin(Executor executor) {
this.executor = executor;
protected FilePlugin(Executor executor) {
super(executor);
}
public synchronized void start(Map<String, String> localProperties,
Map<ContactId, Map<String, String>> remoteProperties,
Map<String, String> config, BatchTransportCallback callback)
throws InvalidPropertiesException, InvalidConfigException, IOException {
if(started) throw new IllegalStateException();
started = true;
this.localProperties = Collections.unmodifiableMap(localProperties);
// Copy the remoteProperties map to make its values unmodifiable
// Copy the remoteProperties map to make its values unmodifiable
int size = remoteProperties.size();
Map<ContactId, Map<String, String>> m =
new HashMap<ContactId, Map<String, String>>(size);
for(Entry<ContactId, Map<String, String>> e
: remoteProperties.entrySet()) {
m.put(e.getKey(), Collections.unmodifiableMap(e.getValue()));
}
this.remoteProperties = m;
this.config = Collections.unmodifiableMap(config);
super.start(localProperties, remoteProperties, config);
this.callback = callback;
}
public synchronized void stop() throws IOException {
if(!started) throw new IllegalStateException();
started = false;
}
public synchronized void setLocalProperties(Map<String, String> properties)
throws InvalidPropertiesException {
if(!started) throw new IllegalStateException();
localProperties = Collections.unmodifiableMap(properties);
}
public synchronized void setRemoteProperties(ContactId c,
Map<String, String> properties)
throws InvalidPropertiesException {
if(!started) throw new IllegalStateException();
remoteProperties.put(c, Collections.unmodifiableMap(properties));
}
public synchronized void setConfig(Map<String, String> config)
throws InvalidConfigException {
if(!started) throw new IllegalStateException();
this.config = Collections.unmodifiableMap(config);
}
public BatchTransportReader createReader(ContactId c) {
return null;
}
public BatchTransportWriter createWriter(ContactId c) {
if(!started) return null;
synchronized(this) {
if(!started) return null;
}
File dir = chooseOutputDirectory();
if(dir == null || !dir.exists() || !dir.isDirectory()) return null;
File f = new File(dir, createFilename());
......@@ -117,7 +72,7 @@ abstract class FilePlugin implements BatchTransportPlugin {
return FileSystemUtils.freeSpaceKb(path) * 1024L;
}
protected void createReaderFromFile(final File f) {
protected synchronized void createReaderFromFile(final File f) {
if(!started) return;
executor.execute(new ReaderCreator(f));
}
......
......@@ -4,10 +4,7 @@ import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketAddress;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.Executor;
import net.sf.briar.api.ContactId;
......@@ -16,19 +13,14 @@ import net.sf.briar.api.transport.InvalidPropertiesException;
import net.sf.briar.api.transport.stream.StreamTransportCallback;
import net.sf.briar.api.transport.stream.StreamTransportConnection;
import net.sf.briar.api.transport.stream.StreamTransportPlugin;
import net.sf.briar.plugins.AbstractPlugin;
abstract class SocketPlugin implements StreamTransportPlugin {
abstract class SocketPlugin extends AbstractPlugin
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;
protected ServerSocket socket = null;
private volatile boolean started = false;
// These methods should be called with this's lock held and started == true
protected abstract SocketAddress getLocalSocketAddress();
protected abstract SocketAddress getSocketAddress(ContactId c);
......@@ -36,27 +28,15 @@ abstract class SocketPlugin implements StreamTransportPlugin {
protected abstract Socket createClientSocket() throws IOException;
protected abstract ServerSocket createServerSocket() throws IOException;
SocketPlugin(Executor executor) {
this.executor = executor;
protected SocketPlugin(Executor executor) {
super(executor);
}
public synchronized void start(Map<String, String> localProperties,
Map<ContactId, Map<String, String>> remoteProperties,
Map<String, String> config, StreamTransportCallback callback)
throws InvalidPropertiesException, InvalidConfigException {
if(started) throw new IllegalStateException();
started = true;
this.localProperties = Collections.unmodifiableMap(localProperties);
// Copy the remoteProperties map to make its values unmodifiable
int size = remoteProperties.size();
Map<ContactId, Map<String, String>> m =
new HashMap<ContactId, Map<String, String>>(size);
for(Entry<ContactId, Map<String, String>> e
: remoteProperties.entrySet()) {
m.put(e.getKey(), Collections.unmodifiableMap(e.getValue()));
}
this.remoteProperties = m;
this.config = Collections.unmodifiableMap(config);
super.start(localProperties, remoteProperties, config);
this.callback = callback;
executor.execute(createBinder());
}
......@@ -132,15 +112,13 @@ abstract class SocketPlugin implements StreamTransportPlugin {
}
public synchronized void stop() throws IOException {
if(!started) throw new IllegalStateException();
started = false;
super.stop();
if(socket != null) socket.close();
}
public synchronized void setLocalProperties(Map<String, String> properties)
throws InvalidPropertiesException {
if(!started) throw new IllegalStateException();
localProperties = Collections.unmodifiableMap(properties);
super.setLocalProperties(properties);
// Close and reopen the socket if its address has changed
if(socket != null) {
SocketAddress addr = socket.getLocalSocketAddress();
......@@ -155,19 +133,6 @@ abstract class SocketPlugin implements StreamTransportPlugin {
}
}
public synchronized void setRemoteProperties(ContactId c,
Map<String, String> properties)
throws InvalidPropertiesException {
if(!started) throw new IllegalStateException();
remoteProperties.put(c, Collections.unmodifiableMap(properties));
}
public synchronized void setConfig(Map<String, String> config)
throws InvalidConfigException {
if(!started) throw new IllegalStateException();
this.config = Collections.unmodifiableMap(config);
}
public synchronized void poll() {
if(!shouldPoll()) throw new UnsupportedOperationException();
if(!started) throw new IllegalStateException();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment