Author: jflesch
Date: 2007-07-11 19:33:35 +0000 (Wed, 11 Jul 2007)
New Revision: 14025

Added:
   trunk/apps/Thaw/images/mDns.png
   trunk/apps/Thaw/images/min-mDns.png
   trunk/apps/Thaw/src/thaw/plugins/MDns.java
   trunk/apps/Thaw/src/thaw/plugins/mDns/
   trunk/apps/Thaw/src/thaw/plugins/mDns/MDNSDiscovery.java
   trunk/apps/Thaw/src/thaw/plugins/mDns/MDNSDiscoveryPanel.java
Removed:
   trunk/apps/Thaw/src/thaw/core/MDNSDiscovery.java
   trunk/apps/Thaw/src/thaw/core/MDNSDiscoveryPanel.java
Modified:
   trunk/apps/Thaw/src/thaw/core/Core.java
   trunk/apps/Thaw/src/thaw/core/NodeConfigPanel.java
   trunk/apps/Thaw/src/thaw/core/PluginManager.java
   trunk/apps/Thaw/src/thaw/gui/IconBox.java
   trunk/apps/Thaw/src/thaw/i18n/source.thaw_fr.properties
   trunk/apps/Thaw/src/thaw/i18n/thaw.properties
   trunk/apps/Thaw/src/thaw/i18n/thaw_fr.properties
Log:
Fix #1418 : mDns support is now a plugin

Added: trunk/apps/Thaw/images/mDns.png
===================================================================
(Binary files differ)


Property changes on: trunk/apps/Thaw/images/mDns.png
___________________________________________________________________
Name: svn:mime-type
   + application/octet-stream

Added: trunk/apps/Thaw/images/min-mDns.png
===================================================================
(Binary files differ)


Property changes on: trunk/apps/Thaw/images/min-mDns.png
___________________________________________________________________
Name: svn:mime-type
   + application/octet-stream

Modified: trunk/apps/Thaw/src/thaw/core/Core.java
===================================================================
--- trunk/apps/Thaw/src/thaw/core/Core.java     2007-07-11 18:27:16 UTC (rev 
14024)
+++ trunk/apps/Thaw/src/thaw/core/Core.java     2007-07-11 19:33:35 UTC (rev 
14025)
@@ -50,8 +50,6 @@

        private boolean isStopping = false;

-       // MDNS stuffs
-       private MDNSDiscovery discovery;

        /**
         * Creates a core, but do nothing else (no initialization).
@@ -121,12 +119,7 @@
                if (!initializeLookAndFeel())
                        return false;

-               splashScreen.setProgressionAndStatus(20, "Starting node 
autodection ...");
-               splashScreen.addIcon(IconBox.connectAction);
-               if (!initMDNS())
-                       return false;
-
-               splashScreen.setProgressionAndStatus(25, "Connecting ...");
+               splashScreen.setProgressionAndStatus(20, "Connecting ...");
                if(!initConnection())
                        new thaw.gui.WarningWindow(this, 
I18n.getMessage("thaw.warning.unableToConnectTo")+
                                                   " "+ 
config.getValue("nodeAddress")+
@@ -156,18 +149,7 @@
        }


-       public boolean initMDNS() {
-               discovery = new MDNSDiscovery(this);

-               return true;
-       }
-
-
-       public MDNSDiscovery getMDNSDiscovery() {
-               return discovery;
-       }
-
-
        /**
         * Init configuration. May re-set I18n.
         */
@@ -545,8 +527,6 @@
                        }
                }

-               discovery.stop();
-
                Logger.info(this, "Stopping scheduler ...");
                if(queueManager != null)
                    queueManager.stopScheduler();

Deleted: trunk/apps/Thaw/src/thaw/core/MDNSDiscovery.java
===================================================================
--- trunk/apps/Thaw/src/thaw/core/MDNSDiscovery.java    2007-07-11 18:27:16 UTC 
(rev 14024)
+++ trunk/apps/Thaw/src/thaw/core/MDNSDiscovery.java    2007-07-11 19:33:35 UTC 
(rev 14025)
@@ -1,90 +0,0 @@
-/* This code is part of Freenet. It is distributed under the GNU General
- * Public License, version 2 (or at your option any later version). See
- * http://www.gnu.org/ for further details of the GPL. */
-
-package thaw.core;
-
-
-import java.util.LinkedList;
-
-import javax.jmdns.JmDNS;
-import javax.jmdns.ServiceEvent;
-import javax.jmdns.ServiceInfo;
-import javax.jmdns.ServiceListener;
-
-
-public class MDNSDiscovery {
-       // SYNC IT!!!
-       private final LinkedList foundNodes;
-       private final JmDNS jmdns;
-       private Core core;
-
-       public MDNSDiscovery(Core core) {
-               this.core = core;
-               this.foundNodes = new LinkedList();
-
-               try {
-                       // Spawn the mdns listener
-                       Logger.info(this, "Starting JMDNS ...");
-                       this.jmdns = new JmDNS();
-
-                       // Start listening for new nodes
-                       
jmdns.addServiceListener(MDNSDiscoveryPanel.FCP_SERVICE_TYPE, new 
FCPMDNSListener());
-
-               } catch (Exception e) {
-                       e.printStackTrace();
-                       throw new RuntimeException("Error loading 
MDNSDiscoveryPanel : " + e.getMessage());
-               }
-       }
-
-
-       private class FCPMDNSListener implements ServiceListener {
-               public void serviceAdded(ServiceEvent event) {
-                       Logger.notice(this, "Service added   : " + 
event.getName()+"."+event.getType());
-                       // Force the gathering of informations
-                       
jmdns.getServiceInfo(MDNSDiscoveryPanel.FCP_SERVICE_TYPE, event.getName());
-               }
-
-               public void serviceRemoved(ServiceEvent event) {
-                       Logger.notice(this, "Service removed : " + 
event.getName()+"."+event.getType());
-                       ServiceInfo service = event.getInfo();
-
-                       synchronized (foundNodes) {
-                               foundNodes.remove(service);
-                               synchronized 
(core.getConfigWindow().getNodeConfigPanel().getMdnsPanel()) {
-                                       
core.getConfigWindow().getNodeConfigPanel().getMdnsPanel().notifyAll();
-                               }
-                       }
-               }
-
-               public void serviceResolved(ServiceEvent event) {
-                       Logger.debug(this, "Service resolved: " + 
event.getInfo());
-                       ServiceInfo service = event.getInfo();
-
-                       synchronized (foundNodes) {
-                               foundNodes.add(service);
-                               synchronized 
(core.getConfigWindow().getNodeConfigPanel().getMdnsPanel()) {
-                                       
core.getConfigWindow().getNodeConfigPanel().getMdnsPanel().notifyAll();
-                               }
-                       }
-               }
-       }
-
-
-       public boolean isHasTheSameIPAddress(ServiceInfo host) {
-               try{
-                       return (jmdns.getInterface().equals(host.getAddress()) 
? true : false);
-               } catch (java.io.IOException e) {
-                       return false;
-               }
-       }
-
-       public LinkedList getFoundNodes() {
-               return foundNodes;
-       }
-
-       public void stop() {
-               Logger.info(this, "Stopping JMDNS ...");
-               jmdns.close();
-       }
-}

Deleted: trunk/apps/Thaw/src/thaw/core/MDNSDiscoveryPanel.java
===================================================================
--- trunk/apps/Thaw/src/thaw/core/MDNSDiscoveryPanel.java       2007-07-11 
18:27:16 UTC (rev 14024)
+++ trunk/apps/Thaw/src/thaw/core/MDNSDiscoveryPanel.java       2007-07-11 
19:33:35 UTC (rev 14025)
@@ -1,216 +0,0 @@
-/* This code is part of Freenet. It is distributed under the GNU General
- * Public License, version 2 (or at your option any later version). See
- * http://www.gnu.org/ for further details of the GPL. */
-
-package thaw.core;
-
-import javax.jmdns.ServiceInfo;
-import javax.swing.DefaultListModel;
-
-import java.awt.Dialog;
-import java.awt.GridLayout;
-import java.awt.BorderLayout;
-import javax.swing.JPanel;
-import javax.swing.JDialog;
-import javax.swing.JLabel;
-import javax.swing.JList;
-import javax.swing.JScrollPane;
-import java.awt.event.ActionListener;
-import java.awt.event.ActionEvent;
-import java.io.IOException;
-import java.net.Socket;
-import java.util.HashMap;
-import java.util.Iterator;
-
-import javax.swing.JButton;
-
-import thaw.core.Logger;
-
-/**
- * This panel implements Zeroconf (called Bonjour/RendezVous by apple) 
discovery for Thaw
- *
- * WARNING: for it to work, you must have a running freenet node on the same 
network subnet, using the MDNSDiscovery panel
- *
- * @author Florent Daignière <nextgens at freenetproject.org>
- *
- * @see http://wiki.freenetproject.org/MDNSDiscoverypanel
- *
- * @see http://www.dns-sd.org/ServiceTypes.html
- * @see http://www.multicastdns.org/
- * @see http://jmdns.sourceforge.net/
- *
- * TODO: implement the "Manual" mode
- * TODO: maybe we should have a small progressbar shown in a new popup to 
introduce a "delay" at startup
- */
-public class MDNSDiscoveryPanel extends JDialog implements ActionListener, 
Runnable {
-       public interface MDNSDiscoveryPanelCallback {
-               /**
-                * Called upon exit from MDNSDiscoveryPanelCallback
-                * It runs on its own thread but still, don't abuse it :)
-                */
-               public void onMDNSDiscoverPanelClosure(boolean 
hasBeenCancelled);
-       }
-       private static final long serialVersionUID = 1L;
-
-       public static final String FCP_SERVICE_TYPE = "_fcp._tcp.local.";
-
-       private boolean goon;
-       private boolean cancelledByUser = false;
-       private ServiceInfo selectedValue;
-       private final MDNSDiscoveryPanelCallback cb;
-       private final Dialog owner;
-       private final HashMap displayedServiceInfos;
-
-       private final JList list;
-       private final DefaultListModel listModel;
-       private final JLabel label;
-
-       private final Core core;
-
-       private JButton okButton;
-       private JButton cancelButton;
-
-
-       public MDNSDiscoveryPanel(Dialog owner, Core core, 
MDNSDiscoveryPanelCallback cb) {
-               super(owner, "ZeroConf");
-               this.core = core;
-               this.owner = owner;
-               this.cb = cb;
-               this.displayedServiceInfos = new HashMap();
-
-               // The UI
-               list = new JList();
-               listModel = new DefaultListModel();
-               label = new JLabel();
-
-               list.setModel(listModel);
-               
list.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
-
-               label.setText(I18n.getMessage("thaw.zeroconf.searchingNode")
-                             +" ; "+
-                             I18n.getMessage("thaw.zeroconf.nodeList"));
-
-               getContentPane().setLayout(new BorderLayout());
-               getContentPane().add(label, BorderLayout.NORTH);
-               getContentPane().add(new JScrollPane(list), 
BorderLayout.CENTER);
-
-               okButton = new JButton(I18n.getMessage("thaw.common.ok"));
-               cancelButton = new 
JButton(I18n.getMessage("thaw.common.cancel"));
-
-               okButton.addActionListener(this);
-               cancelButton.addActionListener(this);
-
-               JPanel buttonPanel = new JPanel(new GridLayout(1, 2));
-               buttonPanel.add(okButton);
-               buttonPanel.add(cancelButton);
-
-               getContentPane().add(buttonPanel, BorderLayout.SOUTH);
-               pack();
-       }
-
-
-
-       /**
-        * The user has selected something: notify the main loop and process 
the data.
-        */
-       public void actionPerformed(ActionEvent e) {
-               Object source = e.getSource();
-
-               if ((source == okButton) || (source == cancelButton)){
-                       goon = false;
-                       if(source == okButton) {
-                               if(list.getSelectedValue() == null) return;
-                               selectedValue = (ServiceInfo) 
displayedServiceInfos.get(list.getSelectedValue());
-                       } else
-                               cancelledByUser = true;
-
-                       synchronized (this) {
-                               notifyAll();
-                       }
-                       Logger.info(this, "User has selected : " + 
selectedValue);
-               }
-       }
-
-       /**
-        * The main loop : TheRealMeat(TM)
-        *
-        */
-       public void run() {
-               super.setLocationRelativeTo(this.getParent());
-               this.setVisible(true);
-               owner.setEnabled(false);
-
-               Logger.notice(this, "Show the MDNSDiscoveryPanel");
-               Socket testSocket = null;
-               boolean isConfigValid = false;
-               goon = true;
-
-               do {
-                       // Loop until a selection is done
-                       while(goon) {
-                               synchronized 
(core.getMDNSDiscovery().getFoundNodes()) {
-                                       
if(core.getMDNSDiscovery().getFoundNodes().size() > 0) {
-                                               listModel.clear();
-                                               Iterator it = 
core.getMDNSDiscovery().getFoundNodes().iterator();
-                                               while(it.hasNext()) {
-                                                       ServiceInfo current = 
(ServiceInfo) it.next();
-                                                       
listModel.addElement(current.getName());
-                                                       
displayedServiceInfos.put(current.getName(), current);
-                                               }
-                                               list.repaint();
-                                       }
-                               }
-
-                               try {
-                                       synchronized (this) {
-                                               wait(Integer.MAX_VALUE);
-                                       }
-                               } catch (InterruptedException e) {}
-                       }
-
-                       if(cancelledByUser) break;
-                       else if(selectedValue == null) continue;
-
-                       Logger.debug(this, "We got something from the UI : 
let's try to connect");
-
-                       try {
-                               // We try to connect to the server
-                               // TODO: implement a proper test!
-                               testSocket = new 
Socket(selectedValue.getHostAddress(), selectedValue.getPort());
-                               isConfigValid = testSocket.isConnected();
-
-                               // Close the fcp socket we have openned, cleanup
-                               testSocket.close();
-                       } catch (IOException e) {
-                               isConfigValid = false;
-                       }
-
-                       Logger.debug(this, "isConfigValid ="+isConfigValid);
-
-                       // Reload, just in  case it failed...
-                       goon = true;
-                       list.removeSelectionInterval(0, 
core.getMDNSDiscovery().getFoundNodes().size());
-               } while(!isConfigValid);
-
-
-               this.setVisible(false);
-               owner.setEnabled(true);
-
-               if (!cancelledByUser) {
-                       Logger.debug(this, "We got something that looks valid 
from the UI : let's propagate changes to  the config");
-                       // Save the config. now that we know it's valid
-                       core.getConfig().setValue("nodeAddress", 
selectedValue.getHostAddress());
-                       core.getConfig().setValue("nodePort", new 
Integer(selectedValue.getPort()).toString());
-                       core.getConfig().setValue("sameComputer", 
String.valueOf(core.getMDNSDiscovery().isHasTheSameIPAddress(selectedValue)));
-
-
-                       Logger.info(this, "We are done : configuration has been 
saved sucessfully.");
-
-               }else
-                       Logger.info(this, "The user has cancelled!");
-               cb.onMDNSDiscoverPanelClosure(cancelledByUser);
-               Logger.notice(this, "We got back from the MDNSDiscoveryPanel 
callback");
-       }
-
-
-}

Modified: trunk/apps/Thaw/src/thaw/core/NodeConfigPanel.java
===================================================================
--- trunk/apps/Thaw/src/thaw/core/NodeConfigPanel.java  2007-07-11 18:27:16 UTC 
(rev 14024)
+++ trunk/apps/Thaw/src/thaw/core/NodeConfigPanel.java  2007-07-11 19:33:35 UTC 
(rev 14025)
@@ -11,17 +11,13 @@
 import javax.swing.JTextField;
 import javax.swing.JButton;

-import thaw.core.MDNSDiscoveryPanel.MDNSDiscoveryPanelCallback;
-
 /**
  * NodeConfigPanel. Creates and manages the panel containing all the things to 
configure
  *  the settings to access the node.
  */
-public class NodeConfigPanel implements Observer, 
java.awt.event.ActionListener, MDNSDiscoveryPanelCallback {
+public class NodeConfigPanel implements Observer {
        private Core core;
        private JPanel nodeConfigPanel = null;
-       private final MDNSDiscoveryPanel mdnsPanel;
-       private boolean isMDNSPanerShown = false;


        private final static String[] paramNames = {
@@ -63,8 +59,6 @@
                this.core = core;
                this.configWindow = configWindow;

-               mdnsPanel = new MDNSDiscoveryPanel(configWindow.getFrame(), 
core, this);
-
                sameComputer = new 
JCheckBox(I18n.getMessage("thaw.config.sameComputer") + " " +
                                             
I18n.getMessage("thaw.config.desactivateIfTroubles"),
                                             
Boolean.valueOf(core.getConfig().getValue("sameComputer")).booleanValue());
@@ -89,8 +83,9 @@
                                nodeConfigPanel.add(paramFields[i]);
                        } else {
                                /* autodetection button ! :) */
-                               autodetect = new 
JButton(I18n.getMessage("thaw.common.autodetect"));
-                               autodetect.addActionListener(this);
+                               autodetect = new 
JButton(I18n.getMessage("thaw.common.autodetect"),
+                                                        
thaw.gui.IconBox.minMDns);
+                               autodetect.setEnabled(false);

                                JPanel sub = new JPanel(new BorderLayout());
                                sub.add(paramFields[i], BorderLayout.CENTER);
@@ -120,10 +115,12 @@
                return nodeConfigPanel;
        }

-       public MDNSDiscoveryPanel getMdnsPanel() {
-               return mdnsPanel;
+       public JButton getAutodetectButton() {
+               return autodetect;
        }

+
+
        private void setVisibility(final boolean advancedMode) {
                for(int i= 2; i < NodeConfigPanel.paramNames.length;i++) {
                        paramLabels[i].setVisible(advancedMode);
@@ -200,24 +197,4 @@
                
sameComputer.setSelected(Boolean.valueOf(core.getConfig().getValue("sameComputer")).booleanValue());
        }

-       public void actionPerformed(java.awt.event.ActionEvent event) {
-               if (event.getSource() == autodetect) {
-                       synchronized (this) {
-                               if(isMDNSPanerShown) return;
-                               isMDNSPanerShown = true;
-                       }
-                       autodetect.setEnabled(false);
-                       new Thread(mdnsPanel).start();
-               }
-       }
-       
-       public void onMDNSDiscoverPanelClosure(boolean hasBeenCancelled) { 
-               // We got back !        
-               synchronized (this) {
-                       isMDNSPanerShown = false;
-               }
-               autodetect.setEnabled(true);
-               if(!hasBeenCancelled)
-                       resetValues();
-       }
 }

Modified: trunk/apps/Thaw/src/thaw/core/PluginManager.java
===================================================================
--- trunk/apps/Thaw/src/thaw/core/PluginManager.java    2007-07-11 18:27:16 UTC 
(rev 14024)
+++ trunk/apps/Thaw/src/thaw/core/PluginManager.java    2007-07-11 19:33:35 UTC 
(rev 14025)
@@ -34,6 +34,7 @@
                "thaw.plugins.IndexExporter",
                "thaw.plugins.Restarter",
                "thaw.plugins.TransferLogs",
+               "thaw.plugins.MDns",
                "thaw.plugins.SqlConsole",
                "thaw.plugins.LogConsole"
        };

Modified: trunk/apps/Thaw/src/thaw/gui/IconBox.java
===================================================================
--- trunk/apps/Thaw/src/thaw/gui/IconBox.java   2007-07-11 18:27:16 UTC (rev 
14024)
+++ trunk/apps/Thaw/src/thaw/gui/IconBox.java   2007-07-11 19:33:35 UTC (rev 
14025)
@@ -134,6 +134,9 @@

        public static ImageIcon minDetails;

+       public static ImageIcon mDns;
+       public static ImageIcon minMDns;
+
        /**
         * Not really used
         */
@@ -249,6 +252,8 @@
                IconBox.minOrange           = 
IconBox.loadIcon("min-orange.png");
                IconBox.minGreen            = IconBox.loadIcon("min-green.png");
                IconBox.minDetails          = 
IconBox.loadIcon("min-details.png");
+               IconBox.mDns                = IconBox.loadIcon("mDns.png");
+               IconBox.minMDns             = IconBox.loadIcon("min-mDns.png");
        }

 }

Modified: trunk/apps/Thaw/src/thaw/i18n/source.thaw_fr.properties
===================================================================
--- trunk/apps/Thaw/src/thaw/i18n/source.thaw_fr.properties     2007-07-11 
18:27:16 UTC (rev 14024)
+++ trunk/apps/Thaw/src/thaw/i18n/source.thaw_fr.properties     2007-07-11 
19:33:35 UTC (rev 14025)
@@ -468,12 +468,6 @@
 thaw.plugin.signature.delete.cant=Impossible de supprimer cette identit? : 
Quelques messages doivent probablement ?tre toujours rattach?s ? cette identit?


-# Zeroconf
-
-thaw.zeroconf.searchingNode=Recherche de noeuds Freenet ...
-thaw.zeroconf.nodeList=Noeuds trouv?s :
-
-
 # Transfer logs

 thaw.plugin.transferLogs.transferLogs=Journal des transfers
@@ -512,4 +506,6 @@

 ## MDNS

-thaw.plugin.MDNS=Node autodetection
+thaw.plugin.MDNS=Autod?tection de la node
+thaw.plugin.MDNS.searchingNode=Recherche de noeuds Freenet ...
+thaw.plugin.MDNS.nodeList=Noeuds trouv?s

Modified: trunk/apps/Thaw/src/thaw/i18n/thaw.properties
===================================================================
--- trunk/apps/Thaw/src/thaw/i18n/thaw.properties       2007-07-11 18:27:16 UTC 
(rev 14024)
+++ trunk/apps/Thaw/src/thaw/i18n/thaw.properties       2007-07-11 19:33:35 UTC 
(rev 14025)
@@ -480,12 +480,7 @@

 thaw.plugin.signature.delete.cant=Can't delete this identity : Some messages 
are probably still linked to this identity

-# ZeroConf

-thaw.zeroconf.searchingNode=Searching freenet nodes ...
-thaw.zeroconf.nodeList=Nodes found:
-
-
 # Transfer logs

 thaw.plugin.transferLogs.transferLogs=Transfer logs
@@ -522,3 +517,8 @@
 thaw.plugin.trayIcon.dialogTitle=Freenet transfers
 thaw.plugin.trayIcon.java1.6=Sorry, this plugin requires at least Java 1.6 
(aka J2SE 6)

+## MDNS
+
+thaw.plugin.MDNS=Node autodetection
+thaw.plugin.MDNS.searchingNode=Searching freenet nodes ...
+thaw.plugin.MDNS.nodeList=Nodes found:

Modified: trunk/apps/Thaw/src/thaw/i18n/thaw_fr.properties
===================================================================
--- trunk/apps/Thaw/src/thaw/i18n/thaw_fr.properties    2007-07-11 18:27:16 UTC 
(rev 14024)
+++ trunk/apps/Thaw/src/thaw/i18n/thaw_fr.properties    2007-07-11 19:33:35 UTC 
(rev 14025)
@@ -468,12 +468,6 @@
 thaw.plugin.signature.delete.cant=Impossible de supprimer cette identit\u00e9 
: Quelques messages doivent probablement \u00eatre toujours rattach\u00e9s 
\u00e0 cette identit\u00e9


-# Zeroconf
-
-thaw.zeroconf.searchingNode=Recherche de noeuds Freenet ...
-thaw.zeroconf.nodeList=Noeuds trouv\u00e9s :
-
-
 # Transfer logs

 thaw.plugin.transferLogs.transferLogs=Journal des transfers
@@ -510,3 +504,8 @@
 thaw.plugin.trayIcon.dialogTitle=Transferts Freenet
 thaw.plugin.trayIcon.java1.6=D\u00e9sol\u00e9, ce plugin n\u00e9cessite au 
moins Java 1.6 (parfois appell\u00e9 J2SE 6)

+## MDNS
+
+thaw.plugin.MDNS=Autod\u00e9tection de la node
+thaw.plugin.MDNS.searchingNode=Recherche de noeuds Freenet ...
+thaw.plugin.MDNS.nodeList=Noeuds trouv\u00e9s

Added: trunk/apps/Thaw/src/thaw/plugins/MDns.java
===================================================================
--- trunk/apps/Thaw/src/thaw/plugins/MDns.java                          (rev 0)
+++ trunk/apps/Thaw/src/thaw/plugins/MDns.java  2007-07-11 19:33:35 UTC (rev 
14025)
@@ -0,0 +1,97 @@
+package thaw.plugins;
+
+import java.awt.event.ActionListener;
+import java.awt.event.ActionEvent;
+
+import thaw.plugins.mDns.MDNSDiscovery;
+import thaw.plugins.mDns.MDNSDiscoveryPanel;
+import thaw.plugins.mDns.MDNSDiscoveryPanel.MDNSDiscoveryPanelCallback;
+
+import thaw.core.I18n;
+import thaw.core.Core;
+
+public class MDns implements thaw.core.Plugin, ActionListener, 
MDNSDiscoveryPanelCallback {
+       private Core core;
+
+       private MDNSDiscovery discovery;
+       private MDNSDiscoveryPanel mdnsPanel;
+       private boolean isMDNSPanerShown = false;
+
+
+       public MDns() {
+
+       }
+
+       public boolean run(Core core) {
+               this.core = core;
+
+               if (discovery == null) { /* first call */
+                       discovery = new MDNSDiscovery(this);
+
+                       isMDNSPanerShown = false;
+                       mdnsPanel = new 
MDNSDiscoveryPanel(core.getConfigWindow().getFrame(), this, this);
+               } else { /* not first call */
+                       discovery.start();
+               }
+
+               
core.getConfigWindow().getNodeConfigPanel().getAutodetectButton().addActionListener(this);
+               
core.getConfigWindow().getNodeConfigPanel().getAutodetectButton().setEnabled(true);
+
+               return false;
+       }
+
+       public MDNSDiscovery getMDNSDiscovery() {
+               return discovery;
+       }
+
+       public MDNSDiscoveryPanel getMdnsPanel() {
+               return mdnsPanel;
+       }
+
+       public thaw.core.Config getConfig() {
+               return core.getConfig();
+       }
+
+       public boolean stop() {
+               discovery.stop();
+
+               
core.getConfigWindow().getNodeConfigPanel().getAutodetectButton().removeActionListener(this);
+               
core.getConfigWindow().getNodeConfigPanel().getAutodetectButton().setEnabled(false);
+
+               return false;
+       }
+
+
+
+       public void actionPerformed(ActionEvent e) {
+               if (e.getSource() ==
+                   
core.getConfigWindow().getNodeConfigPanel().getAutodetectButton()) {
+
+                       synchronized (this) {
+                               if(isMDNSPanerShown) return;
+                               isMDNSPanerShown = true;
+                       }
+                       
core.getConfigWindow().getNodeConfigPanel().getAutodetectButton().setEnabled(false);
+                       new Thread(mdnsPanel).start();
+
+               }
+       }
+
+
+       public void onMDNSDiscoverPanelClosure(boolean hasBeenCancelled) {
+               // We got back !
+               synchronized (this) {
+                       isMDNSPanerShown = false;
+               }
+               
core.getConfigWindow().getNodeConfigPanel().getAutodetectButton().setEnabled(true);
+       }
+
+
+       public String getNameForUser() {
+               return I18n.getMessage("thaw.plugin.MDNS");
+       }
+
+       public javax.swing.ImageIcon getIcon() {
+               return thaw.gui.IconBox.mDns;
+       }
+}

Copied: trunk/apps/Thaw/src/thaw/plugins/mDns/MDNSDiscovery.java (from rev 
14023, trunk/apps/Thaw/src/thaw/core/MDNSDiscovery.java)
===================================================================
--- trunk/apps/Thaw/src/thaw/plugins/mDns/MDNSDiscovery.java                    
        (rev 0)
+++ trunk/apps/Thaw/src/thaw/plugins/mDns/MDNSDiscovery.java    2007-07-11 
19:33:35 UTC (rev 14025)
@@ -0,0 +1,98 @@
+/* This code is part of Freenet. It is distributed under the GNU General
+ * Public License, version 2 (or at your option any later version). See
+ * http://www.gnu.org/ for further details of the GPL. */
+
+package thaw.plugins.mDns;
+
+
+import java.util.LinkedList;
+
+import javax.jmdns.JmDNS;
+import javax.jmdns.ServiceEvent;
+import javax.jmdns.ServiceInfo;
+import javax.jmdns.ServiceListener;
+
+
+import thaw.plugins.MDns;
+import thaw.core.Logger;
+
+public class MDNSDiscovery {
+       // SYNC IT!!!
+       private LinkedList foundNodes;
+       private JmDNS jmdns;
+       private MDns mDns;
+
+       public MDNSDiscovery(MDns mDns) {
+               this.mDns = mDns;
+               this.foundNodes = new LinkedList();
+
+               start();
+       }
+
+       public void start() {
+               this.foundNodes = new LinkedList();
+               try {
+                       // Spawn the mdns listener
+                       Logger.info(this, "Starting JMDNS ...");
+                       this.jmdns = new JmDNS();
+
+                       // Start listening for new nodes
+                       
jmdns.addServiceListener(MDNSDiscoveryPanel.FCP_SERVICE_TYPE, new 
FCPMDNSListener());
+
+               } catch (Exception e) {
+                       e.printStackTrace();
+                       throw new RuntimeException("Error loading 
MDNSDiscoveryPanel : " + e.getMessage());
+               }
+       }
+
+
+       private class FCPMDNSListener implements ServiceListener {
+               public void serviceAdded(ServiceEvent event) {
+                       Logger.notice(this, "Service added   : " + 
event.getName()+"."+event.getType());
+                       // Force the gathering of informations
+                       
jmdns.getServiceInfo(MDNSDiscoveryPanel.FCP_SERVICE_TYPE, event.getName());
+               }
+
+               public void serviceRemoved(ServiceEvent event) {
+                       Logger.notice(this, "Service removed : " + 
event.getName()+"."+event.getType());
+                       ServiceInfo service = event.getInfo();
+
+                       synchronized (foundNodes) {
+                               foundNodes.remove(service);
+                               synchronized (mDns.getMdnsPanel()) {
+                                       mDns.getMdnsPanel().notifyAll();
+                               }
+                       }
+               }
+
+               public void serviceResolved(ServiceEvent event) {
+                       Logger.debug(this, "Service resolved: " + 
event.getInfo());
+                       ServiceInfo service = event.getInfo();
+
+                       synchronized (foundNodes) {
+                               foundNodes.add(service);
+                               synchronized (mDns.getMdnsPanel()) {
+                                       mDns.getMdnsPanel().notifyAll();
+                               }
+                       }
+               }
+       }
+
+
+       public boolean isHasTheSameIPAddress(ServiceInfo host) {
+               try{
+                       return (jmdns.getInterface().equals(host.getAddress()) 
? true : false);
+               } catch (java.io.IOException e) {
+                       return false;
+               }
+       }
+
+       public LinkedList getFoundNodes() {
+               return foundNodes;
+       }
+
+       public void stop() {
+               Logger.info(this, "Stopping JMDNS ...");
+               jmdns.close();
+       }
+}

Copied: trunk/apps/Thaw/src/thaw/plugins/mDns/MDNSDiscoveryPanel.java (from rev 
14023, trunk/apps/Thaw/src/thaw/core/MDNSDiscoveryPanel.java)
===================================================================
--- trunk/apps/Thaw/src/thaw/plugins/mDns/MDNSDiscoveryPanel.java               
                (rev 0)
+++ trunk/apps/Thaw/src/thaw/plugins/mDns/MDNSDiscoveryPanel.java       
2007-07-11 19:33:35 UTC (rev 14025)
@@ -0,0 +1,223 @@
+/* This code is part of Freenet. It is distributed under the GNU General
+ * Public License, version 2 (or at your option any later version). See
+ * http://www.gnu.org/ for further details of the GPL. */
+
+package thaw.plugins.mDns;
+
+import javax.jmdns.ServiceInfo;
+import javax.swing.DefaultListModel;
+
+import java.awt.Dialog;
+import java.awt.GridLayout;
+import java.awt.BorderLayout;
+import javax.swing.JPanel;
+import javax.swing.JDialog;
+import javax.swing.JLabel;
+import javax.swing.JList;
+import javax.swing.JScrollPane;
+import java.awt.event.ActionListener;
+import java.awt.event.ActionEvent;
+import java.io.IOException;
+import java.net.Socket;
+import java.util.HashMap;
+import java.util.Iterator;
+
+import javax.swing.JButton;
+
+import thaw.core.Logger;
+import thaw.core.I18n;
+import thaw.plugins.MDns;
+
+
+/**
+ * This panel implements Zeroconf (called Bonjour/RendezVous by apple) 
discovery for Thaw
+ *
+ * WARNING: for it to work, you must have a running freenet node on the same 
network subnet, using the MDNSDiscovery panel
+ *
+ * @author Florent Daigni&egrave;re &lt;nextgens at freenetproject.org&gt;
+ *
+ * @see http://wiki.freenetproject.org/MDNSDiscoverypanel
+ *
+ * @see http://www.dns-sd.org/ServiceTypes.html
+ * @see http://www.multicastdns.org/
+ * @see http://jmdns.sourceforge.net/
+ *
+ * TODO: implement the "Manual" mode
+ * TODO: maybe we should have a small progressbar shown in a new popup to 
introduce a "delay" at startup
+ */
+public class MDNSDiscoveryPanel extends JDialog implements ActionListener, 
Runnable {
+       public interface MDNSDiscoveryPanelCallback {
+               /**
+                * Called upon exit from MDNSDiscoveryPanelCallback
+                * It runs on its own thread but still, don't abuse it :)
+                */
+               public void onMDNSDiscoverPanelClosure(boolean 
hasBeenCancelled);
+       }
+       private static final long serialVersionUID = 1L;
+
+       public static final String FCP_SERVICE_TYPE = "_fcp._tcp.local.";
+
+       private boolean goon;
+       private boolean cancelledByUser = false;
+       private ServiceInfo selectedValue;
+       private final MDNSDiscoveryPanelCallback cb;
+       private final Dialog owner;
+       private final HashMap displayedServiceInfos;
+
+       private final JList list;
+       private final DefaultListModel listModel;
+
+       private final MDns mDns;
+
+       private JButton okButton;
+       private JButton cancelButton;
+
+
+       public MDNSDiscoveryPanel(Dialog owner, MDns mDns, 
MDNSDiscoveryPanelCallback cb) {
+               super(owner, "ZeroConf");
+               this.mDns = mDns;
+               this.owner = owner;
+               this.cb = cb;
+               this.displayedServiceInfos = new HashMap();
+
+               // The UI
+               list = new JList();
+               listModel = new DefaultListModel();
+
+               JLabel mainLabel = new 
JLabel(I18n.getMessage("thaw.plugin.MDNS.searchingNode"));
+               JLabel titleListLabel = new 
JLabel(I18n.getMessage("thaw.plugin.MDNS.nodeList"));
+
+               list.setModel(listModel);
+               
list.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
+
+               mainLabel.setIcon(thaw.gui.IconBox.mDns);
+
+               JPanel listPanel = new JPanel(new BorderLayout());
+               listPanel.add(titleListLabel, BorderLayout.NORTH);
+               listPanel.add(new JScrollPane(list), BorderLayout.CENTER);
+
+               getContentPane().setLayout(new BorderLayout(5, 5));
+               getContentPane().add(mainLabel, BorderLayout.NORTH);
+               getContentPane().add(listPanel, BorderLayout.CENTER);
+
+               okButton = new JButton(I18n.getMessage("thaw.common.ok"));
+               cancelButton = new 
JButton(I18n.getMessage("thaw.common.cancel"));
+
+               okButton.addActionListener(this);
+               cancelButton.addActionListener(this);
+
+               JPanel buttonPanel = new JPanel(new GridLayout(1, 2));
+               buttonPanel.add(okButton);
+               buttonPanel.add(cancelButton);
+
+               getContentPane().add(buttonPanel, BorderLayout.SOUTH);
+
+               super.setSize(300, 300);
+       }
+
+
+
+       /**
+        * The user has selected something: notify the main loop and process 
the data.
+        */
+       public void actionPerformed(ActionEvent e) {
+               Object source = e.getSource();
+
+               if ((source == okButton) || (source == cancelButton)){
+                       goon = false;
+                       if(source == okButton) {
+                               if(list.getSelectedValue() == null) return;
+                               selectedValue = (ServiceInfo) 
displayedServiceInfos.get(list.getSelectedValue());
+                       } else
+                               cancelledByUser = true;
+
+                       synchronized (this) {
+                               notifyAll();
+                       }
+                       Logger.info(this, "User has selected : " + 
selectedValue);
+               }
+       }
+
+       /**
+        * The main loop : TheRealMeat(TM)
+        *
+        */
+       public void run() {
+               super.setLocationRelativeTo(this.getParent());
+               this.setVisible(true);
+               owner.setEnabled(false);
+
+               Logger.notice(this, "Show the MDNSDiscoveryPanel");
+               Socket testSocket = null;
+               boolean isConfigValid = false;
+               goon = true;
+
+               do {
+                       // Loop until a selection is done
+                       while(goon) {
+                               synchronized 
(mDns.getMDNSDiscovery().getFoundNodes()) {
+                                       
if(mDns.getMDNSDiscovery().getFoundNodes().size() > 0) {
+                                               listModel.clear();
+                                               Iterator it = 
mDns.getMDNSDiscovery().getFoundNodes().iterator();
+                                               while(it.hasNext()) {
+                                                       ServiceInfo current = 
(ServiceInfo) it.next();
+                                                       
listModel.addElement(current.getName());
+                                                       
displayedServiceInfos.put(current.getName(), current);
+                                               }
+                                               list.repaint();
+                                       }
+                               }
+
+                               try {
+                                       synchronized (this) {
+                                               wait(Integer.MAX_VALUE);
+                                       }
+                               } catch (InterruptedException e) {}
+                       }
+
+                       if(cancelledByUser) break;
+                       else if(selectedValue == null) continue;
+
+                       Logger.debug(this, "We got something from the UI : 
let's try to connect");
+
+                       try {
+                               // We try to connect to the server
+                               // TODO: implement a proper test!
+                               testSocket = new 
Socket(selectedValue.getHostAddress(), selectedValue.getPort());
+                               isConfigValid = testSocket.isConnected();
+
+                               // Close the fcp socket we have openned, cleanup
+                               testSocket.close();
+                       } catch (IOException e) {
+                               isConfigValid = false;
+                       }
+
+                       Logger.debug(this, "isConfigValid ="+isConfigValid);
+
+                       // Reload, just in  case it failed...
+                       goon = true;
+                       list.removeSelectionInterval(0, 
mDns.getMDNSDiscovery().getFoundNodes().size());
+               } while(!isConfigValid);
+
+
+               this.setVisible(false);
+               owner.setEnabled(true);
+
+               if (!cancelledByUser) {
+                       Logger.debug(this, "We got something that looks valid 
from the UI : let's propagate changes to  the config");
+                       // Save the config. now that we know it's valid
+                       mDns.getConfig().setValue("nodeAddress", 
selectedValue.getHostAddress());
+                       mDns.getConfig().setValue("nodePort", new 
Integer(selectedValue.getPort()).toString());
+                       mDns.getConfig().setValue("sameComputer", 
String.valueOf(mDns.getMDNSDiscovery().isHasTheSameIPAddress(selectedValue)));
+
+
+                       Logger.info(this, "We are done : configuration has been 
saved sucessfully.");
+
+               }else
+                       Logger.info(this, "The user has cancelled!");
+               cb.onMDNSDiscoverPanelClosure(cancelledByUser);
+               Logger.notice(this, "We got back from the MDNSDiscoveryPanel 
callback");
+       }
+
+
+}


Reply via email to