altosui: Abstract remote connection timeout stuff
authorKeith Packard <keithp@keithp.com>
Thu, 12 Jul 2012 02:15:32 +0000 (19:15 -0700)
committerKeith Packard <keithp@keithp.com>
Thu, 12 Jul 2012 02:15:32 +0000 (19:15 -0700)
This moves some of the logic for managing when to present the 'cancel'
dialog for remote operations to altoslib.

Signed-off-by: Keith Packard <keithp@keithp.com>
altoslib/AltosLink.java
altosui/AltosSerial.java

index a39204ac3ba26e8892f6c79658625ca7f036067a..779c849606dfe22dfabe2e87b20d538a98ce38f5 100644 (file)
@@ -60,16 +60,66 @@ public abstract class AltosLink {
                return null;
        }
 
-       public String get_reply(int timeout) throws InterruptedException {
-               try {
-                       return get_reply_no_dialog(timeout);
-               } catch (TimeoutException te) {
-                       return null;
+       public String get_reply() throws InterruptedException {
+               return get_reply(5000);
+       }
+
+               
+       public abstract boolean can_cancel_reply();
+       public abstract boolean show_reply_timeout();
+       public abstract void hide_reply_timeout();
+
+       public boolean  reply_abort;
+       public int      in_reply;
+
+       boolean         reply_timeout_shown = false;
+
+       private boolean check_reply_timeout() {
+               if (!reply_timeout_shown)
+                       reply_timeout_shown = show_reply_timeout();
+               return reply_abort;
+       }
+
+       private void cleanup_reply_timeout() {
+               if (reply_timeout_shown) {
+                       reply_timeout_shown = false;
+                       hide_reply_timeout();
                }
        }
 
-       public String get_reply() throws InterruptedException {
-               return get_reply(5000);
+
+       public String get_reply(int timeout) throws InterruptedException {
+               boolean can_cancel = can_cancel_reply();
+               String  reply = null;
+
+               if (!can_cancel && remote)
+                       System.out.printf("Uh-oh, reading remote serial device from swing thread\n");
+
+               if (remote && can_cancel)
+                       timeout = 500;
+               try {
+                       ++in_reply;
+
+                       flush_output();
+
+                       reply_abort = false;
+                       reply_timeout_shown = false;
+                       for (;;) {
+                               AltosLine line = reply_queue.poll(timeout, TimeUnit.MILLISECONDS);
+                               if (line != null) {
+                                       cleanup_reply_timeout();
+                                       reply = line.line;
+                                       break;
+                               }
+                               if (!remote || !can_cancel || check_reply_timeout()) {
+                                       reply = null;
+                                       break;
+                               }
+                       }
+               } finally {
+                       --in_reply;
+               }
+               return reply;
        }
 
        public void add_telem(AltosLine line) throws InterruptedException {
@@ -124,7 +174,10 @@ public abstract class AltosLink {
 
 
        public void flush_input() throws InterruptedException {
-               flush_input(100);
+               if (remote)
+                       flush_input(500);
+               else
+                       flush_input(100);
        }
 
 
index 8b60dd547e3f0f4bc3a3c4d267a5e86df84fbbff..35704d40202519965462f6892b9668d99807dfb0 100644 (file)
@@ -102,21 +102,7 @@ public class AltosSerial extends AltosLink implements Runnable {
                }
        }
 
-       boolean         abort;
        JDialog         timeout_dialog;
-       boolean timeout_started = false;
-
-       private void stop_timeout_dialog() {
-               if (timeout_started) {
-                       timeout_started = false;
-                       Runnable r = new Runnable() {
-                                       public void run() {
-                                               timeout_dialog.setVisible(false);
-                                       }
-                               };
-                       SwingUtilities.invokeLater(r);
-               }
-       }
 
        private void start_timeout_dialog_internal() {
 
@@ -135,69 +121,42 @@ public class AltosSerial extends AltosLink implements Runnable {
                if (o == null)
                        return;
                if (options[0].equals(o))
-                       abort = true;
+                       reply_abort = true;
                timeout_dialog.dispose();
                timeout_dialog = null;
        }
 
-       private boolean check_timeout() {
-               if (!timeout_started && frame != null) {
-                       if (!SwingUtilities.isEventDispatchThread()) {
-                               timeout_started = true;
-                               Runnable r = new Runnable() {
-                                               public void run() {
-                                                       start_timeout_dialog_internal();
-                                               }
-                                       };
-                               SwingUtilities.invokeLater(r);
-                       }
-               }
-               return abort;
-       }
+       /*
+        * These are required by the AltosLink implementation
+        */
 
-       public void flush_input() throws InterruptedException {
-               if (remote)
-                       flush_input(500);
-               else
-                       flush_input(100);
+       public boolean can_cancel_reply() {
+               /*
+                * Can cancel any replies not called from the dispatch thread
+                */
+               return !SwingUtilities.isEventDispatchThread();
        }
 
-       int     in_reply;
-
-       public String get_reply(int timeout) throws InterruptedException {
-               boolean can_cancel = true;
-               String  reply = null;
-
-               try {
-                       ++in_reply;
+       public boolean show_reply_timeout() {
+               if (!SwingUtilities.isEventDispatchThread() && frame != null) {
+                       Runnable r = new Runnable() {
+                                       public void run() {
+                                               start_timeout_dialog_internal();
+                                       }
+                               };
+                       SwingUtilities.invokeLater(r);
+                       return true;
+               }
+               return false;
+       }
 
-                       if (SwingUtilities.isEventDispatchThread()) {
-                               can_cancel = false;
-                               if (remote)
-                                       System.out.printf("Uh-oh, reading remote serial device from swing thread\n");
-                       }
-                       flush_output();
-                       if (remote && can_cancel) {
-                               timeout = 500;
-                       }
-                       abort = false;
-                       timeout_started = false;
-                       for (;;) {
-                               AltosLine line = reply_queue.poll(timeout, TimeUnit.MILLISECONDS);
-                               if (line != null) {
-                                       stop_timeout_dialog();
-                                       reply = line.line;
-                                       break;
+       public void hide_reply_timeout() {
+               Runnable r = new Runnable() {
+                               public void run() {
+                                       timeout_dialog.setVisible(false);
                                }
-                               if (!remote || !can_cancel || check_timeout()) {
-                                       reply = null;
-                                       break;
-                               }
-                       }
-               } finally {
-                       --in_reply;
-               }
-               return reply;
+                       };
+               SwingUtilities.invokeLater(r);
        }
 
        public void close() {