[Swift-commit] r6839 - in trunk/src/org/griphyn/vdl/karajan/monitor/monitors/ansi: . tui

hategan at ci.uchicago.edu hategan at ci.uchicago.edu
Tue Aug 13 15:55:41 CDT 2013


Author: hategan
Date: 2013-08-13 15:55:41 -0500 (Tue, 13 Aug 2013)
New Revision: 6839

Modified:
   trunk/src/org/griphyn/vdl/karajan/monitor/monitors/ansi/WorkerTerminalInputHandler.java
   trunk/src/org/griphyn/vdl/karajan/monitor/monitors/ansi/tui/Terminal.java
Log:
basic support for long-running commands in worker terminal

Modified: trunk/src/org/griphyn/vdl/karajan/monitor/monitors/ansi/WorkerTerminalInputHandler.java
===================================================================
--- trunk/src/org/griphyn/vdl/karajan/monitor/monitors/ansi/WorkerTerminalInputHandler.java	2013-08-13 20:32:10 UTC (rev 6838)
+++ trunk/src/org/griphyn/vdl/karajan/monitor/monitors/ansi/WorkerTerminalInputHandler.java	2013-08-13 20:55:41 UTC (rev 6839)
@@ -29,6 +29,8 @@
 import org.globus.cog.coaster.ProtocolException;
 import org.globus.cog.coaster.channels.ChannelManager;
 import org.globus.cog.coaster.channels.CoasterChannel;
+import org.globus.cog.coaster.commands.Command;
+import org.globus.cog.coaster.commands.Command.Callback;
 import org.griphyn.vdl.karajan.monitor.monitors.ansi.tui.Dialog;
 import org.griphyn.vdl.karajan.monitor.monitors.ansi.tui.Terminal;
 import org.griphyn.vdl.karajan.monitor.monitors.ansi.tui.Terminal.InputHandler;
@@ -61,55 +63,73 @@
         }
     }
 
-    public void handleInput(String in) {
+    public WorkerShellCommand startCommand(String in) {
         if (in.equals("exit")) {
             dialog.close();
+            return null;
         }
         else {
-            String result = runcmd(in);
-            if (result != null && !result.equals("")) {
-                term.append(runcmd(in));
-            }
+            return startCmd(in, term, new Callback() {
+                @Override
+                public void replyReceived(Command cmd) {
+                    term.commandCompleted();
+                }
+
+                @Override
+                public void errorReceived(Command cmd, String msg, Exception t) {
+                    term.commandFailed(msg, t);
+                }
+            });
         }
     }
 
-    private String runcmd(String cmd) {
+    private WorkerShellCommand startCmd(String cmd, final Terminal term, final Callback cb) {
         try {
             CoasterChannel channel = ChannelManager.getManager()
                 .reserveChannel(contact, cred, LocalRequestManager.INSTANCE);
-            WorkerShellCommand wsc = new WorkerShellCommand(workerId, cmd);
-            wsc.execute(channel);
-            return wsc.getInDataAsString(0);
+            WorkerShellCommand wsc = new WorkerShellCommand(workerId, cmd) {
+                @Override
+                public void handleSignal(byte[] data) {
+                    term.append(new String(data));
+                }
+            };
+            wsc.executeAsync(channel, cb);
+            return wsc;
         }
         catch (ProtocolException e) {
             term.append(e.getMessage());
-            return null;
         }
         catch (Exception e) {
             logger.warn("Cannot execute worker command", e);
             CharArrayWriter caw = new CharArrayWriter();
             e.printStackTrace(new PrintWriter(caw));
             term.append(caw.toString());
-            return null;
         }
+        return null;
     }
 
-    public String autoComplete(String in) {
-        String result = runcmd("mls " + in + "*");
-        if (result == null) {
-            return null;
-        }
-        String[] r = result.split("\\s+");
-        if (r.length == 0) {
-            return null;
-        }
-        else if (r.length == 1) {
-            return r[0];
-        }
-        else {
-            term.append(join(r));
-            return null;
-        }
+    public void autoComplete(String in, final Terminal term) {
+        startCmd("mls " + in + "*", term, new Callback() {
+
+            @Override
+            public void replyReceived(Command cmd) {
+                String result = cmd.getInDataAsString(1);
+                if (result != null) {
+                    String[] r = result.split("\\s+");
+                    if (r.length == 1) {
+                        term.autoCompleteCallback(r[0]);
+                    }
+                    else {
+                        term.append(join(r));
+                    }
+                }
+            }
+
+            @Override
+            public void errorReceived(Command cmd, String msg, Exception t) {
+                term.append(msg);
+            }
+        });
     }
     
     private String join(String[] s) {

Modified: trunk/src/org/griphyn/vdl/karajan/monitor/monitors/ansi/tui/Terminal.java
===================================================================
--- trunk/src/org/griphyn/vdl/karajan/monitor/monitors/ansi/tui/Terminal.java	2013-08-13 20:32:10 UTC (rev 6838)
+++ trunk/src/org/griphyn/vdl/karajan/monitor/monitors/ansi/tui/Terminal.java	2013-08-13 20:55:41 UTC (rev 6839)
@@ -21,7 +21,12 @@
 import java.util.LinkedList;
 import java.util.ListIterator;
 
+import org.apache.log4j.Logger;
+import org.globus.cog.abstraction.impl.execution.coaster.WorkerShellCommand;
+
 public class Terminal extends Component {
+    public static final Logger logger = Logger.getLogger(Terminal.class);
+    
     public static final int SCROLLBACK_BUFFER_SIZE = 512;
     
     private LinkedList<String> lines;
@@ -29,6 +34,7 @@
     private StringBuffer input;
     private InputHandler inputHandler;
     private int end = -1;
+    private WorkerShellCommand activeCmd;
 
     public Terminal() {
         lines = new LinkedList<String>();
@@ -47,7 +53,9 @@
             int crt = Math.min(height - 2, lines.size()) + 1;
             if (end < 0) {
                 context.moveTo(sx, sy + crt);
-                context.text(prompt);
+                if (activeCmd == null) {
+                    context.text(prompt);
+                }
                 context.text(input.toString());
                 context.bgColor(fgColor);
                 context.putChar(' ');
@@ -82,24 +90,32 @@
     }
 
     public boolean keyboardEvent(Key key) {
+        synchronized(this) {
+            if (activeCmd != null) {
+                activeCmd.input(translateKey(key));
+                return true;
+            }
+        }
         if (key.isEnter()) {
             end = -1;
-            processInput();
+            synchronized(this) {
+                activeCmd = startCommand();
+            }
+            if (activeCmd != null) {
+                append("\n");
+            }
         }
         else if (key.equals(Key.BACKSPACE)) {
-            end = -1;
-            input.deleteCharAt(input.length() - 1);
+            if (input.length() > 0) {
+                end = -1;
+                input.deleteCharAt(input.length() - 1);
+            }
         }
         else if (key.equals(Key.TAB)) {
             end = -1;
             if (inputHandler != null) {
                 int si = input.lastIndexOf(" ");
-                String ac = inputHandler.autoComplete(input.substring(si + 1));
-                if (ac != null) {
-                    input.delete(si + 1, input.length());
-                    input.append(ac);
-                }
-                redraw();
+                inputHandler.autoComplete(input.substring(si + 1), this);
             }
         }
         else if (key.equals(Key.HOME)) {
@@ -120,17 +136,26 @@
         }
         return true;
     }
+    
+    private String translateKey(Key key) {
+        return null;
+    }
 
-    private void processInput() {
+    public void autoCompleteCallback(String ac) {
+        if (ac != null) {
+            int si = input.lastIndexOf(" ");
+            input.delete(si + 1, input.length());
+            input.append(ac);
+        }
+        redraw();
+    }
+
+    private WorkerShellCommand startCommand() {
+        logger.info("Start command");
         String in = input.toString().trim();
         append(prompt + in);
         input = new StringBuffer();
-        if (inputHandler != null) {
-            inputHandler.handleInput(in);
-        }
-        else {
-            append("Invalid command: " + in);
-        }
+        return inputHandler.startCommand(in);
     }
 
     public void append(String str) {
@@ -160,9 +185,31 @@
     }
 
     public static interface InputHandler {
-        void handleInput(String in);
+        WorkerShellCommand startCommand(String in);
 
-        String autoComplete(String in);
+        void autoComplete(String in, Terminal term);
     }
 
+    public void commandCompleted() {
+        synchronized(this) {
+            logger.info("Command completed");
+            if (activeCmd.getInDataSize() > 1) {
+                String s = activeCmd.getInDataAsString(1);
+                if (s != null && s.startsWith("CWD: ")) {
+                    setPrompt(s.substring(5) + " >");
+                }
+            }
+            activeCmd = null;
+        }
+        redraw();
+    }
+
+    public void commandFailed(String msg, Exception t) {
+        append(msg);
+        synchronized(this) {
+            activeCmd = null;
+        }
+        redraw();
+    }
+
 }




More information about the Swift-commit mailing list