Author: jflesch
Date: 2007-08-02 14:37:49 +0000 (Thu, 02 Aug 2007)
New Revision: 14465

Modified:
   trunk/apps/Thaw/src/thaw/fcp/FCPConnection.java
   trunk/apps/Thaw/src/thaw/fcp/FCPQueryManager.java
   trunk/apps/Thaw/src/thaw/plugins/miniFrost/MessageTreeTable.java
Log:
Multithread the FCP message processings seems to solve the freeze problem 
(don't ask me why)

Modified: trunk/apps/Thaw/src/thaw/fcp/FCPConnection.java
===================================================================
--- trunk/apps/Thaw/src/thaw/fcp/FCPConnection.java     2007-08-02 13:15:55 UTC 
(rev 14464)
+++ trunk/apps/Thaw/src/thaw/fcp/FCPConnection.java     2007-08-02 14:37:49 UTC 
(rev 14465)
@@ -211,7 +211,7 @@
        /**
         * Doesn't check the lock state ! You have to manage it yourself.
         */
-       public boolean rawWrite(final byte[] data) {
+       public synchronized boolean rawWrite(final byte[] data) {
                if(bufferedOut != null)
                        return bufferedOut.write(data);
                else {
@@ -224,7 +224,7 @@
        /**
         * Should be call by FCPBufferedStream. Not you.
         */
-       public boolean realRawWrite(final byte[] data) {
+       public synchronized boolean realRawWrite(final byte[] data) {
                if((out != null) && (socket != null) && socket.isConnected()) {
                        try {
                                lastWrite = System.currentTimeMillis();

Modified: trunk/apps/Thaw/src/thaw/fcp/FCPQueryManager.java
===================================================================
--- trunk/apps/Thaw/src/thaw/fcp/FCPQueryManager.java   2007-08-02 13:15:55 UTC 
(rev 14464)
+++ trunk/apps/Thaw/src/thaw/fcp/FCPQueryManager.java   2007-08-02 14:37:49 UTC 
(rev 14465)
@@ -13,12 +13,10 @@
        private Thread me;

        private FCPConnection connection;
-       private FCPMessage latestMessage;


        public FCPQueryManager(final FCPConnection connection) {
                me = null;
-               latestMessage = null;
                setConnection(connection);
        }

@@ -94,25 +92,93 @@
        }


+       public class Notifier implements Runnable {
+               FCPMessage msg;
+
+               public Notifier(FCPMessage msg) {
+                       this.msg = msg;
+               }
+
+               public void run() {
+                       try {
+                               setChanged();
+                               notifyObservers(msg);
+                       } catch(final Exception e) {
+                               /* it's really bad ... because if data are 
waiting on the socket ... */
+                               Logger.error(this, "EXCEPTION FROM ONE OF 
LISTENER : "+e.toString());
+                               Logger.error(this, "ERROR : "+e.getMessage());
+                               e.printStackTrace();
+                       }
+               }
+       }
+
+
+
+       public class WatchDog implements Runnable {
+               public final static int TIMEOUT = 5000;
+
+               Runnable runnable;
+
+               public WatchDog(Runnable runnable) {
+                       this.runnable = runnable;
+               }
+
+               private boolean isRunning(Thread th) {
+                       return (th.getState() != Thread.State.TERMINATED);
+               }
+
+               public void run() {
+                       Thread th = new Thread(runnable);
+                       th.start();
+
+                       for (int i = 0 ; i < TIMEOUT && isRunning(th) ; i += 
300) {
+                               try {
+                                       Thread.sleep(100);
+                               } catch(InterruptedException e) {
+                                       /* \_o< */
+                               }
+                       }
+
+                       if (isRunning(th)) {
+                               Logger.warning(this, "Notifier thread seems to 
be blocked !!");
+                               th.dumpStack();
+                       }
+               }
+
+       }
+
        /**
+        * Multithreading allow the use of a watchdog
+        */
+       public final static boolean MULTITHREADED = true;
+
+       /**
         * Will listen in loop for new incoming messages.
         */
        public void run() {
-
                while(true) {
-                       latestMessage = readMessage();
+                       FCPMessage latestMessage = readMessage();

                        Logger.verbose(this, "Message received. Notifying 
observers");

                        if(latestMessage != null) {
-                               try {
-                                       setChanged();
-                                       this.notifyObservers(latestMessage);
-                               } catch(final Exception e) {
-                                       /* it's really bad ... because if data 
are waiting on the socket ... */
-                                       Logger.error(this, "EXCEPTION FROM ONE 
OF LISTENER : "+e.toString());
-                                       Logger.error(this, "ERROR : 
"+e.getMessage());
-                                       e.printStackTrace();
+                               /*
+                                * can't multithread if data are waiting
+                                */
+                               if (MULTITHREADED && 
latestMessage.getAmountOfDataWaiting() == 0) {
+                                       Thread notifierTh = new Thread(new 
WatchDog(new Notifier(latestMessage)));
+                                       notifierTh.start();
+                               } else {
+                                       try {
+                                               setChanged();
+                                               notifyObservers(latestMessage);
+                                       } catch(final Exception e) {
+                                               /* it's really bad ... because 
if data are waiting on the socket ... */
+                                               Logger.error(this, "EXCEPTION 
FROM ONE OF LISTENER : "+e.toString());
+                                               Logger.error(this, "ERROR : 
"+e.getMessage());
+                                               e.printStackTrace();
+                                       }
+
                                }
                        } else {
                                Logger.info(this, "Stopping listening");

Modified: trunk/apps/Thaw/src/thaw/plugins/miniFrost/MessageTreeTable.java
===================================================================
--- trunk/apps/Thaw/src/thaw/plugins/miniFrost/MessageTreeTable.java    
2007-08-02 13:15:55 UTC (rev 14464)
+++ trunk/apps/Thaw/src/thaw/plugins/miniFrost/MessageTreeTable.java    
2007-08-02 14:37:49 UTC (rev 14465)
@@ -76,7 +76,12 @@
                                         MouseListener,
                                         ActionListener
 {
+       /**
+        * Just here to avoid an infinite recursion
+        */
+       public final static int MAX_DEPTH = 30;

+
        public final static String[] COLUMNS = {
                "", /* checkboxes */
                I18n.getMessage("thaw.plugin.miniFrost.subject"),
@@ -718,7 +723,7 @@
                if (node instanceof MessageNode)
                        msgs.add(node);

-               if (depth >= 15) {
+               if (depth >= MAX_DEPTH) {
                        Logger.notice(this, "Too much depths, sorry");
                        return false;
                }
@@ -773,7 +778,7 @@
                        msgs = new Vector();
                }

-               Logger.info(this, "Nmb msgs in the tree : 
"+Integer.toString(msgs.size()));
+               Logger.info(this, "Nmb msgs in the tree (before) : 
"+Integer.toString(msgs.size()));

                Vector rootNodes;

@@ -851,7 +856,7 @@

                rebuildMsgList(msgs, rootNode, 0);

-               Logger.info(this, "Nmb msgs in the tree : 
"+Integer.toString(msgs.size()));
+               Logger.info(this, "Nmb msgs in the tree (after) : 
"+Integer.toString(msgs.size()));

                model.setMessages(msgs);



Reply via email to