diff --git a/api/net/sf/briar/api/plugins/Plugin.java b/api/net/sf/briar/api/plugins/Plugin.java index 1a303bcb049aa83852ccf14aa8783a09e3888c9d..da396d4f22579c00cf75ae13f99649e970d41139 100644 --- a/api/net/sf/briar/api/plugins/Plugin.java +++ b/api/net/sf/briar/api/plugins/Plugin.java @@ -35,7 +35,4 @@ public interface Plugin { * the plugin may exclude the given contacts from polling. */ void poll(Collection<ContactId> connected); - - /** Returns true if the plugin supports exchanging invitations. */ - boolean supportsInvitations(); } diff --git a/api/net/sf/briar/api/plugins/PluginManager.java b/api/net/sf/briar/api/plugins/PluginManager.java index 17887added8925eb1a738e5657fdc34075d9f9f8..da0c5e14594c069ff1b183c335dc2c9a4ada6063 100644 --- a/api/net/sf/briar/api/plugins/PluginManager.java +++ b/api/net/sf/briar/api/plugins/PluginManager.java @@ -3,13 +3,13 @@ package net.sf.briar.api.plugins; import java.util.Collection; import net.sf.briar.api.plugins.duplex.DuplexPlugin; -import net.sf.briar.api.plugins.simplex.SimplexPlugin; public interface PluginManager { /** * Starts the plugins and returns the number of plugins successfully - * started. + * started. This method must not be called until the database has been + * opened. */ int start(); @@ -19,8 +19,5 @@ public interface PluginManager { int stop(); /** Returns any duplex plugins that support invitations. */ - Collection<DuplexPlugin> getDuplexInvitationPlugins(); - - /** Returns any simplex plugins that support invitations. */ - Collection<SimplexPlugin> getSimplexInvitationPlugins(); + Collection<DuplexPlugin> getInvitationPlugins(); } diff --git a/api/net/sf/briar/api/plugins/duplex/DuplexPlugin.java b/api/net/sf/briar/api/plugins/duplex/DuplexPlugin.java index 2de756dfaeb870ed01443ed3fd012f7337efa329..8b7c48437993200753b0c18c4b5acb00e5efa02d 100644 --- a/api/net/sf/briar/api/plugins/duplex/DuplexPlugin.java +++ b/api/net/sf/briar/api/plugins/duplex/DuplexPlugin.java @@ -14,6 +14,9 @@ public interface DuplexPlugin extends Plugin { */ DuplexTransportConnection createConnection(ContactId c); + /** Returns true if the plugin supports exchanging invitations. */ + boolean supportsInvitations(); + /** * Starts the invitation process from the inviter's side. Returns null if * no connection can be established within the given timeout. diff --git a/api/net/sf/briar/api/plugins/simplex/SimplexPlugin.java b/api/net/sf/briar/api/plugins/simplex/SimplexPlugin.java index fd11d6646c3f6802b1d5c608de4577fdcf50b1cc..5e1fe8b0ff26826728d6f844b29fd39afc1e72a0 100644 --- a/api/net/sf/briar/api/plugins/simplex/SimplexPlugin.java +++ b/api/net/sf/briar/api/plugins/simplex/SimplexPlugin.java @@ -1,7 +1,6 @@ package net.sf.briar.api.plugins.simplex; import net.sf.briar.api.ContactId; -import net.sf.briar.api.crypto.PseudoRandom; import net.sf.briar.api.plugins.Plugin; /** An interface for transport plugins that support simplex communication. */ @@ -20,29 +19,4 @@ public interface SimplexPlugin extends Plugin { * could not be created. */ SimplexTransportWriter createWriter(ContactId c); - - /** - * Starts the invitation process from the inviter's side. Returns null if - * no connection can be established within the given timeout. - */ - SimplexTransportWriter sendInvitation(PseudoRandom r, long timeout); - - /** - * Starts the invitation process from the invitee's side. Returns null if - * no connection can be established within the given timeout. - */ - SimplexTransportReader acceptInvitation(PseudoRandom r, long timeout); - - /** - * Continues the invitation process from the invitee's side. Returns null - * if no connection can be established within the given timeout. - */ - SimplexTransportWriter sendInvitationResponse(PseudoRandom r, long timeout); - - /** - * Continues the invitation process from the inviter's side. Returns null - * if no connection can be established within the given timeout. - */ - SimplexTransportReader acceptInvitationResponse(PseudoRandom r, - long timeout); } diff --git a/components/net/sf/briar/plugins/PluginManagerImpl.java b/components/net/sf/briar/plugins/PluginManagerImpl.java index 79782b912030a70a9c6b27256a78abb0d6dd5151..74f45a0ba3a7dbc935f61f9f16f3ad2f92a79242 100644 --- a/components/net/sf/briar/plugins/PluginManagerImpl.java +++ b/components/net/sf/briar/plugins/PluginManagerImpl.java @@ -73,10 +73,6 @@ class PluginManagerImpl implements PluginManager { duplexPlugins = new ArrayList<DuplexPlugin>(); } - public synchronized int getPluginCount() { - return simplexPlugins.size() + duplexPlugins.size(); - } - public synchronized int start() { Set<TransportId> ids = new HashSet<TransportId>(); // Instantiate and start the simplex plugins @@ -184,7 +180,7 @@ class PluginManagerImpl implements PluginManager { return stopped; } - public Collection<DuplexPlugin> getDuplexInvitationPlugins() { + public Collection<DuplexPlugin> getInvitationPlugins() { Collection<DuplexPlugin> supported = new ArrayList<DuplexPlugin>(); synchronized(this) { for(DuplexPlugin d : duplexPlugins) { @@ -194,16 +190,6 @@ class PluginManagerImpl implements PluginManager { return supported; } - public Collection<SimplexPlugin> getSimplexInvitationPlugins() { - Collection<SimplexPlugin> supported = new ArrayList<SimplexPlugin>(); - synchronized(this) { - for(SimplexPlugin s : simplexPlugins) { - if(s.supportsInvitations()) supported.add(s); - } - } - return supported; - } - private abstract class PluginCallbackImpl implements PluginCallback { protected volatile TransportId id = null; diff --git a/components/net/sf/briar/plugins/email/GmailPlugin.java b/components/net/sf/briar/plugins/email/GmailPlugin.java index 32bda238e56ebdf5df4c9ef0c4e0d42012535b34..dddf78e1f566668219f36c2c2cc275ad48cac407 100644 --- a/components/net/sf/briar/plugins/email/GmailPlugin.java +++ b/components/net/sf/briar/plugins/email/GmailPlugin.java @@ -14,6 +14,7 @@ import java.util.logging.Logger; import javax.activation.DataHandler; import javax.activation.DataSource; +import javax.mail.Authenticator; import javax.mail.Flags; import javax.mail.Flags.Flag; import javax.mail.Folder; @@ -34,7 +35,6 @@ import javax.mail.util.ByteArrayDataSource; import net.sf.briar.api.ContactId; import net.sf.briar.api.TransportConfig; import net.sf.briar.api.TransportProperties; -import net.sf.briar.api.crypto.PseudoRandom; import net.sf.briar.api.plugins.simplex.SimplexPlugin; import net.sf.briar.api.plugins.simplex.SimplexPluginCallback; import net.sf.briar.api.plugins.simplex.SimplexTransportReader; @@ -42,7 +42,7 @@ import net.sf.briar.api.plugins.simplex.SimplexTransportWriter; import net.sf.briar.api.protocol.TransportId; import net.sf.briar.util.StringUtils; -public class GmailPlugin implements SimplexPlugin { +class GmailPlugin implements SimplexPlugin { public static final byte[] TRANSPORT_ID = StringUtils .fromHexString("57ead1961d2120bbbbe8256ff9ce6ae2" @@ -50,12 +50,11 @@ public class GmailPlugin implements SimplexPlugin { + "e8dd928ed1d7a9e7b89fd62210aa30bf"); private static final TransportId ID = new TransportId(TRANSPORT_ID); + private static final Logger LOG = + Logger.getLogger(GmailPlugin.class.getName()); + private final Executor pluginExecutor; private final SimplexPluginCallback callback; - private static final Logger LOG = Logger.getLogger(GmailPlugin.class - .getName()); - - // private static GmailTransportConnectionWriter writer; public GmailPlugin(Executor pluginExecutor, SimplexPluginCallback callback) { this.pluginExecutor = pluginExecutor; @@ -78,15 +77,14 @@ public class GmailPlugin implements SimplexPlugin { try { FlagTerm ft = new FlagTerm(new Flags(Flags.Flag.SEEN), false); Message msg[] = inbox.search(ft); - // System.out.println("Unread Messages: "+ msg.length); - for (final Message message : msg) { + for(final Message message : msg) { callback.readerCreated(new SimplexTransportReader() { public InputStream getInputStream() throws IOException { try { return message.getInputStream(); - } catch (MessagingException e) { - if (LOG.isLoggable(Level.WARNING)) + } catch(MessagingException e) { + if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.toString()); } return null; @@ -97,17 +95,15 @@ public class GmailPlugin implements SimplexPlugin { try { message.setFlag(Flag.DELETED, recognised); message.setFlag(Flag.SEEN, recognised); - } catch (MessagingException e) { - if (LOG.isLoggable(Level.WARNING)) + } catch(MessagingException e) { + if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.toString()); } } }); - } - } catch (MessagingException e) { - if (LOG.isLoggable(Level.WARNING)) - LOG.warning(e.toString()); + } catch(MessagingException e) { + if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.toString()); } } @@ -116,7 +112,7 @@ public class GmailPlugin implements SimplexPlugin { props.setProperty("mail.store.protocol", "imaps"); final ArrayList<String> userPass = getAuthenticationDetails(callback .getConfig()); - if (userPass != null) { + if(userPass != null) { try { Session session = Session.getInstance(props, null); Store store = session.getStore("imaps"); @@ -125,19 +121,17 @@ public class GmailPlugin implements SimplexPlugin { Folder inbox = store.getFolder("Inbox"); inbox.open(Folder.READ_ONLY); checkUnreadEmails(inbox); - } catch (NoSuchProviderException e) { - if (LOG.isLoggable(Level.WARNING)) - LOG.warning(e.toString()); - } catch (MessagingException e) { - if (LOG.isLoggable(Level.WARNING)) - LOG.warning(e.toString()); + } catch(NoSuchProviderException e) { + if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.toString()); + } catch(MessagingException e) { + if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.toString()); } } } public boolean connectSMTP(ContactId cid) { boolean sent = false; - if (discoverContactEmail(cid) != null) { + if(discoverContactEmail(cid) != null) { Properties props = new Properties(); props.put("mail.smtp.host", "smtp.gmail.com"); props.put("mail.smtp.socketFactory.port", "465"); @@ -146,19 +140,18 @@ public class GmailPlugin implements SimplexPlugin { props.put("mail.smtp.auth", "true"); props.put("mail.smtp.port", "465"); - final ArrayList<String> userPass = getAuthenticationDetails(callback - .getConfig()); + final ArrayList<String> userPass = + getAuthenticationDetails(callback.getConfig()); - if (userPass != null) { + if(userPass != null) { Session session; session = Session.getInstance(props, - new javax.mail.Authenticator() { + new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { - return new PasswordAuthentication(userPass - .get(0), userPass.get(1)); + return new PasswordAuthentication( + userPass.get(0), userPass.get(1)); } }); - sent = sendMessage(session, cid); } } @@ -166,7 +159,6 @@ public class GmailPlugin implements SimplexPlugin { } private boolean sendMessage(Session session, ContactId cid) { - boolean sent = false; ByteArrayOutputStream outputStream = null; try { Message message = new MimeMessage(session); @@ -187,15 +179,15 @@ public class GmailPlugin implements SimplexPlugin { } public OutputStream getOutputStream() throws IOException { - return null; + return null; // FIXME } public long getCapacity() { - return 0; + return 0; // FIXME } public void dispose(boolean exception) throws IOException { - + // FIXME } }); @@ -211,22 +203,18 @@ public class GmailPlugin implements SimplexPlugin { message.setContent(mimeMultipart); - // message.setText("Test content"); - Transport.send(message); - sent = true; - return sent; - } catch (MessagingException e) { - return sent; + return true; + } catch(MessagingException e) { + return false; } finally { - if (null != outputStream) { + if(outputStream != null) { try { outputStream.close(); outputStream = null; - } catch (Exception e) { - if (LOG.isLoggable(Level.WARNING)) - LOG.warning(e.toString()); + } catch(Exception e) { + if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.toString()); } } } @@ -234,8 +222,8 @@ public class GmailPlugin implements SimplexPlugin { } public void stop() throws IOException { - synchronized (this) { - // close open connections + synchronized(this) { + // FIXME: Close open connections } } @@ -251,10 +239,6 @@ public class GmailPlugin implements SimplexPlugin { throw new UnsupportedOperationException(); } - public boolean supportsInvitations() { - return false; - } - /* * Gets the user's authentication details ArrayList.get(0) = username, * ArrayList.get(1) = password, or null if either value is null. @@ -264,31 +248,35 @@ public class GmailPlugin implements SimplexPlugin { ArrayList<String> usernamePass = new ArrayList<String>(); usernamePass.add(0, config.get("username")); usernamePass.add(1, config.get("password")); - if (usernamePass.get(0) != null && usernamePass.get(1) != null) + if(usernamePass.get(0) != null && usernamePass.get(1) != null) { return usernamePass; - else + } else { return null; - } catch (Exception e) { + } + } catch(Exception e) { return null; } } /* * Looks up the contact's email address given the contactID + * * @param ContactId + * * @return String email */ private String discoverContactEmail(ContactId cid) { try { - Map<ContactId, TransportProperties> remote = callback - .getRemoteProperties(); + Map<ContactId, TransportProperties> remote = + callback.getRemoteProperties(); TransportProperties tp = remote.get(cid); - if (tp != null) { + if(tp != null) { String address = tp.get("email"); return address; - } else + } else { return null; - } catch (Exception e) { + } + } catch(Exception e) { return null; } } @@ -300,23 +288,4 @@ public class GmailPlugin implements SimplexPlugin { public SimplexTransportWriter createWriter(ContactId c) { return null; } - - public SimplexTransportWriter sendInvitation(PseudoRandom r, long timeout) { - throw new UnsupportedOperationException(); - } - - public SimplexTransportReader acceptInvitation(PseudoRandom r, long timeout) { - throw new UnsupportedOperationException(); - } - - public SimplexTransportWriter sendInvitationResponse(PseudoRandom r, - long timeout) { - throw new UnsupportedOperationException(); - } - - public SimplexTransportReader acceptInvitationResponse(PseudoRandom r, - long timeout) { - throw new UnsupportedOperationException(); - } - } diff --git a/components/net/sf/briar/plugins/email/GmailPluginFactory.java b/components/net/sf/briar/plugins/email/GmailPluginFactory.java index 244a89373d3b91c62e38f2cae87c279fa5a12590..87c7c42f100b2c4c5c30a1701aea19c762138728 100644 --- a/components/net/sf/briar/plugins/email/GmailPluginFactory.java +++ b/components/net/sf/briar/plugins/email/GmailPluginFactory.java @@ -10,9 +10,6 @@ public class GmailPluginFactory implements SimplexPluginFactory { public SimplexPlugin createPlugin(Executor pluginExecutor, SimplexPluginCallback callback) { - return new GmailPlugin(pluginExecutor, callback); } - - } diff --git a/components/net/sf/briar/plugins/email/GmailTransportConnectionWriter.java b/components/net/sf/briar/plugins/email/GmailTransportConnectionWriter.java index a215bd550edacec4acbf6d7c5b6cf956bf077c41..3a75ea21be80656c93ea10fc7e47ad73e0abdcf0 100644 --- a/components/net/sf/briar/plugins/email/GmailTransportConnectionWriter.java +++ b/components/net/sf/briar/plugins/email/GmailTransportConnectionWriter.java @@ -9,16 +9,17 @@ import javax.microedition.io.StreamConnection; import net.sf.briar.api.plugins.simplex.SimplexTransportWriter; -public class GmailTransportConnectionWriter implements SimplexTransportWriter { +class GmailTransportConnectionWriter implements SimplexTransportWriter { - private static final Logger LOG = Logger.getLogger(GmailTransportConnectionWriter.class.getName()); + private static final Logger LOG = + Logger.getLogger(GmailTransportConnectionWriter.class.getName()); private final StreamConnection stream; private final long capacity = 25 * 1000 * 1000; - + public GmailTransportConnectionWriter(StreamConnection stream) { this.stream = stream; } - + public long getCapacity() { return capacity; } @@ -38,5 +39,4 @@ public class GmailTransportConnectionWriter implements SimplexTransportWriter { if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.toString()); } } - } diff --git a/components/net/sf/briar/plugins/file/FileListener.java b/components/net/sf/briar/plugins/file/FileListener.java deleted file mode 100644 index 53142c138b4cb14c9423543dc3328738f2589367..0000000000000000000000000000000000000000 --- a/components/net/sf/briar/plugins/file/FileListener.java +++ /dev/null @@ -1,31 +0,0 @@ -package net.sf.briar.plugins.file; - -import java.io.File; -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.TimeUnit; - -class FileListener { - - private final String filename; - private final long end; - private final CountDownLatch finished = new CountDownLatch(1); - - private volatile File file = null; - - FileListener(String filename, long timeout) { - this.filename = filename; - end = System.currentTimeMillis() + timeout; - } - - File waitForFile() throws InterruptedException { - finished.await(end - System.currentTimeMillis(), TimeUnit.MILLISECONDS); - return file; - } - - void addFile(File f) { - if(filename.equals(f.getName())) { - file = f; - finished.countDown(); - } - } -} diff --git a/components/net/sf/briar/plugins/file/FilePlugin.java b/components/net/sf/briar/plugins/file/FilePlugin.java index 2d5a6582e407ac4edbc2f02698d13c37ee701f70..511657ecb624a850e2ef8326f3c38761346cd658 100644 --- a/components/net/sf/briar/plugins/file/FilePlugin.java +++ b/components/net/sf/briar/plugins/file/FilePlugin.java @@ -11,14 +11,12 @@ import java.util.logging.Level; import java.util.logging.Logger; import net.sf.briar.api.ContactId; -import net.sf.briar.api.crypto.PseudoRandom; import net.sf.briar.api.plugins.PluginExecutor; import net.sf.briar.api.plugins.simplex.SimplexPlugin; import net.sf.briar.api.plugins.simplex.SimplexPluginCallback; import net.sf.briar.api.plugins.simplex.SimplexTransportReader; import net.sf.briar.api.plugins.simplex.SimplexTransportWriter; import net.sf.briar.api.transport.TransportConstants; -import net.sf.briar.util.StringUtils; import org.apache.commons.io.FileSystemUtils; @@ -32,10 +30,6 @@ public abstract class FilePlugin implements SimplexPlugin { protected volatile boolean running = false; - private final Object listenerLock = new Object(); - - private FileListener listener = null; // Locking: listenerLock - protected abstract File chooseOutputDirectory(); protected abstract Collection<File> findFilesByName(String filename); protected abstract void writerFinished(File f); @@ -94,75 +88,6 @@ public abstract class FilePlugin implements SimplexPlugin { pluginExecutor.execute(new ReaderCreator(f)); } - public SimplexTransportWriter sendInvitation(PseudoRandom r, long timeout) { - if(!running) return null; - return createWriter(createInvitationFilename(r, false)); - } - - public SimplexTransportReader acceptInvitation(PseudoRandom r, - long timeout) { - if(!running) return null; - String filename = createInvitationFilename(r, false); - return createInvitationReader(filename, timeout); - } - - public SimplexTransportWriter sendInvitationResponse(PseudoRandom r, - long timeout) { - if(!running) return null; - return createWriter(createInvitationFilename(r, true)); - } - - public SimplexTransportReader acceptInvitationResponse(PseudoRandom r, - long timeout) { - if(!running) return null; - String filename = createInvitationFilename(r, true); - return createInvitationReader(filename, timeout); - } - - private SimplexTransportReader createInvitationReader(String filename, - long timeout) { - Collection<File> files; - // FIXME: Avoid making alien calls with a lock held - synchronized(listenerLock) { - // Find any matching files that have already arrived - files = findFilesByName(filename); - if(files.isEmpty()) { - // Wait for a matching file to arrive - listener = new FileListener(filename, timeout); - File f; - try { - f = listener.waitForFile(); - if(f != null) files.add(f); - } catch(InterruptedException e) { - if(LOG.isLoggable(Level.INFO)) - LOG.info("Interrupted while waiting for file"); - Thread.currentThread().interrupt(); - } - listener = null; - } - } - // Return the first match that can be opened - for(File f : files) { - try { - FileInputStream in = new FileInputStream(f); - return new FileTransportReader(f, in, FilePlugin.this); - } catch(IOException e) { - if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.toString()); - } - } - return null; - } - - private String createInvitationFilename(PseudoRandom r, boolean response) { - String digits = StringUtils.toHexString(r.nextBytes(3)); - return String.format("%c%s.dat", response ? 'b' : 'a', digits); - } - - // Package access for testing - boolean isPossibleInvitationFilename(String filename) { - return filename.toLowerCase().matches("[ab][0-9a-f]{6}.dat"); - } - private class ReaderCreator implements Runnable { private final File file; @@ -172,13 +97,6 @@ public abstract class FilePlugin implements SimplexPlugin { } public void run() { - String filename = file.getName(); - if(isPossibleInvitationFilename(filename)) { - // FIXME: Avoid making alien calls with a lock held - synchronized(listenerLock) { - if(listener != null) listener.addFile(file); - } - } if(isPossibleConnectionFilename(file.getName())) { try { FileInputStream in = new FileInputStream(file); diff --git a/components/net/sf/briar/plugins/file/RemovableDrivePlugin.java b/components/net/sf/briar/plugins/file/RemovableDrivePlugin.java index 0fd61460f754a4c5f4ad486997cb74cf2bd6790d..bb437e101b30b721e841e33e8efbb9da01ac80c6 100644 --- a/components/net/sf/briar/plugins/file/RemovableDrivePlugin.java +++ b/components/net/sf/briar/plugins/file/RemovableDrivePlugin.java @@ -65,10 +65,6 @@ implements RemovableDriveMonitor.Callback { throw new UnsupportedOperationException(); } - public boolean supportsInvitations() { - return true; - } - @Override protected File chooseOutputDirectory() { try {