[Swift-commit] r2821 - trunk/src/org/griphyn/vdl/karajan/monitor/monitors/ansi

noreply at svn.ci.uchicago.edu noreply at svn.ci.uchicago.edu
Sat Apr 4 16:17:52 CDT 2009


Author: hategan
Date: 2009-04-04 16:17:51 -0500 (Sat, 04 Apr 2009)
New Revision: 2821

Added:
   trunk/src/org/griphyn/vdl/karajan/monitor/monitors/ansi/WorkerTerminalInputHandler.java
Modified:
   trunk/src/org/griphyn/vdl/karajan/monitor/monitors/ansi/STable.java
Log:
added worker terminals

Modified: trunk/src/org/griphyn/vdl/karajan/monitor/monitors/ansi/STable.java
===================================================================
--- trunk/src/org/griphyn/vdl/karajan/monitor/monitors/ansi/STable.java	2009-04-04 21:17:03 UTC (rev 2820)
+++ trunk/src/org/griphyn/vdl/karajan/monitor/monitors/ansi/STable.java	2009-04-04 21:17:51 UTC (rev 2821)
@@ -7,6 +7,7 @@
 
 import org.globus.cog.abstraction.interfaces.FileTransferSpecification;
 import org.globus.cog.abstraction.interfaces.JobSpecification;
+import org.globus.cog.abstraction.interfaces.Status;
 import org.globus.cog.abstraction.interfaces.Task;
 import org.griphyn.vdl.karajan.monitor.common.StatefulItemModel;
 import org.griphyn.vdl.karajan.monitor.items.ApplicationItem;
@@ -18,24 +19,29 @@
 import org.griphyn.vdl.karajan.monitor.monitors.ansi.tui.Dialog;
 import org.griphyn.vdl.karajan.monitor.monitors.ansi.tui.Key;
 import org.griphyn.vdl.karajan.monitor.monitors.ansi.tui.Table;
+import org.griphyn.vdl.karajan.monitor.monitors.ansi.tui.Terminal;
 import org.griphyn.vdl.karajan.monitor.monitors.ansi.tui.TextArea;
 import org.griphyn.vdl.karajan.monitor.monitors.swing.SimpleTableClassRenderer.Model;
 
 public class STable extends Table implements ActionListener {
-	private Dialog d;
-	private Button close;
-	public boolean keyboardEvent(Key key) {
-		if (key.isEnter()) {
-			createDialog();			
-			return true;
-		}
-		else {
-			return super.keyboardEvent(key);
-		}
-	}
-	
-	protected void createDialog() {
+    private Dialog d, term;
+    private Button close, wterm;
+    private Task termTask;
+    private String termWorkerId;
+
+    public boolean keyboardEvent(Key key) {
+        if (key.isEnter()) {
+            createDialog();
+            return true;
+        }
+        else {
+            return super.keyboardEvent(key);
+        }
+    }
+
+    protected void createDialog() {
 		d = new Dialog();
+		d.setTitle("Details");
 		d.setSize(getScreen().getWidth() * 2 / 3, getScreen().getHeight() * 1 / 3);
 		d.center(getScreen());
 		TextArea ta = new TextArea();
@@ -43,89 +49,164 @@
 		d.add(ta);
 		ta.setSize(d.getWidth() - 2, d.getHeight() - 4);
 		ta.setLocation(1, 1);
+		boolean term = hasCoasterWorker();
+		int btncount = term ? 1 : 0;
 		close = new Button("&Close");
-		close.setLocation(d.getWidth() / 2 - 5, d.getHeight() - 2);
-		close.setSize(9, 1);
+		close.setLocation(d.getWidth() / 2 - 7 - (btncount) * 9, d.getHeight() - 2);
+		close.setSize(15, 1);
 		close.addActionListener(this);
 		d.add(close);
+		
+		if (term) {
+		    wterm = new Button("Worker &Terminal");
+		    wterm.setLocation(d.getWidth() / 2 - 7 + (btncount) * 9, d.getHeight() - 2);
+		    wterm.setSize(15, 1);
+		    wterm.addActionListener(this);
+		    d.add(wterm);
+		}
+		
 		d.display(getScreen());
 		close.focus();
 	}
 
-	protected String getText() {
-		StatefulItemModel model = (StatefulItemModel) getModel();
-		return format(model.getItem(getSelectedRow()));
-	}
-	
-	protected StatefulItem getItem() {
-		Model model = (Model) getModel();
-		return model.getItem(getSelectedRow());
-	}
+    protected String getText() {
+        StatefulItemModel model = (StatefulItemModel) getModel();
+        return format(model.getItem(getSelectedRow()));
+    }
 
-	public void actionPerformed(Component source) {
-		if (source == close) {
-			d.close();
-		}
-	}
-	
-	protected static String format(Object o) {
-		if (o instanceof TaskItem) {
-			Task t = ((TaskItem) o).getTask();
-			if (t == null) {
-				return "?";
-			}
-			if (t.getType() == Task.FILE_TRANSFER) {
-				FileTransferSpecification spec = (FileTransferSpecification) t.getSpecification();
-				StringBuffer sb = new StringBuffer();
-				sb.append("Source:      ");
-				sb.append(spec.getSource());
-				sb.append('\n');
-				sb.append("Destination: ");
-				sb.append(spec.getDestination());
-				sb.append('\n');
-				return sb.toString();
-			}
-			else if (t.getType() == Task.JOB_SUBMISSION) {
-				JobSpecification spec = (JobSpecification) t.getSpecification();
-				StringBuffer sb = new StringBuffer();
-				sb.append("Executable: ");
-				sb.append(spec.getExecutable());
-				sb.append("\nArguments: ");
-				sb.append(spec.getArguments());
-				sb.append("\nDirectory: ");
-				sb.append(spec.getDirectory());
-				sb.append("\nAttributes: ");
-				Iterator i = spec.getAttributeNames().iterator();
-				while (i.hasNext()) {
-					String name = (String) i.next();
-					sb.append(name);
-					sb.append("=");
-					sb.append(spec.getAttribute(name));
-					if (i.hasNext()) {
-						sb.append(", ");
-					}
-				}
-				return sb.toString();
-			}
-			else {
-				return t.toString();
-			}
-		}
-		else if (o instanceof ApplicationItem) {
-		    ApplicationItem app = (ApplicationItem) o;
-		    StringBuffer sb = new StringBuffer();
-		    sb.append("Name: ");
-		    sb.append(app.getName());
-		    sb.append("\nArguments: ");
-		    sb.append(app.getArguments());
-		    sb.append("\nHost: ");
-		    sb.append(app.getHost());
-		    sb.append("\nStart time: ");
-		    sb.append(app.getStartTime());
-		    return sb.toString();
-		}
-		else {
-			return String.valueOf(o);
-		}
-	}
+    protected boolean hasCoasterWorker() {
+        StatefulItemModel model = (StatefulItemModel) getModel();
+        StatefulItem si = model.getItem(getSelectedRow());
+        if (si instanceof TaskItem) {
+            Task t = ((TaskItem) si).getTask();
+            if (t == null) {
+                return false;
+            }
+            else {
+                Status s = t.getStatus();
+                if (s == null) {
+                    return false;
+                }
+                else {
+                    if (s.getMessage() != null) {
+                        String msg = s.getMessage();
+                        int index = msg.indexOf("workerid=");
+                        if (index == -1) {
+                            return false;
+                        }
+                        else {
+                            termTask = t;
+                            termWorkerId = msg.substring(index + "workerid=".length());
+                            return true;
+                        }
+                    }
+                    else {
+                        return false;
+                    }
+                }
+            }
+        }
+        else {
+            return false;
+        }
+    }
+
+    protected StatefulItem getItem() {
+        Model model = (Model) getModel();
+        return model.getItem(getSelectedRow());
+    }
+
+    public void actionPerformed(Component source) {
+        if (source == close) {
+            d.close();
+        }
+        else if (source == wterm) {
+            openWorkerTerminal();
+        }
+    }
+
+    private void openWorkerTerminal() {
+        term = new Dialog();
+        term.setTitle("Worker terminal");
+        term.setSize(getScreen().getWidth() * 3 / 4, getScreen().getHeight() * 3 / 4);
+        Terminal t = new Terminal();
+        t.setSize(term.getWidth() - 2, term.getHeight() - 2);
+        t.setLocation(1, 1);
+        t.append("Type exit to close terminal");
+        t.append("");
+        t.setInputHandler(new WorkerTerminalInputHandler(term, t, termTask, termWorkerId));
+        t.setPrompt(termTask.getService(0).getServiceContact().getContact() + "/" + termWorkerId + "$ ");
+        term.add(t);
+        term.center(getScreen());
+        term.display(getScreen());
+    }
+
+    protected static String format(Object o) {
+        if (o instanceof TaskItem) {
+            Task t = ((TaskItem) o).getTask();
+            if (t == null) {
+                return "?";
+            }
+            if (t.getType() == Task.FILE_TRANSFER) {
+                FileTransferSpecification spec = (FileTransferSpecification) t
+                    .getSpecification();
+                StringBuffer sb = new StringBuffer();
+                sb.append("Source:      ");
+                sb.append(spec.getSource());
+                sb.append('\n');
+                sb.append("Destination: ");
+                sb.append(spec.getDestination());
+                sb.append('\n');
+                return sb.toString();
+            }
+            else if (t.getType() == Task.JOB_SUBMISSION) {
+                JobSpecification spec = (JobSpecification) t.getSpecification();
+                StringBuffer sb = new StringBuffer();
+                sb.append("Executable: ");
+                sb.append(spec.getExecutable());
+                sb.append("\nArguments: ");
+                sb.append(spec.getArguments());
+                sb.append("\nDirectory: ");
+                sb.append(spec.getDirectory());
+                sb.append("\nStatus: ");
+                sb.append(t.getStatus().getStatusString());
+                String msg = t.getStatus().getMessage();
+                if (msg != null) {
+                    sb.append(" ");
+                    sb.append(t.getStatus().getMessage());
+                }
+                sb.append("\nAttributes: ");
+                Iterator i = spec.getAttributeNames().iterator();
+                while (i.hasNext()) {
+                    String name = (String) i.next();
+                    sb.append(name);
+                    sb.append("=");
+                    sb.append(spec.getAttribute(name));
+                    if (i.hasNext()) {
+                        sb.append(", ");
+                    }
+                }
+                return sb.toString();
+            }
+            else {
+                return t.toString();
+            }
+        }
+        else if (o instanceof ApplicationItem) {
+            ApplicationItem app = (ApplicationItem) o;
+            StringBuffer sb = new StringBuffer();
+            sb.append("Name: ");
+            sb.append(app.getName());
+            sb.append("\nArguments: ");
+            sb.append(app.getArguments());
+            sb.append("\nHost: ");
+            sb.append(app.getHost());
+            sb.append("\nStart time: ");
+            sb.append(app.getStartTime());
+            return sb.toString();
+        }
+        else {
+            return String.valueOf(o);
+        }
+    }
 }

Added: trunk/src/org/griphyn/vdl/karajan/monitor/monitors/ansi/WorkerTerminalInputHandler.java
===================================================================
--- trunk/src/org/griphyn/vdl/karajan/monitor/monitors/ansi/WorkerTerminalInputHandler.java	                        (rev 0)
+++ trunk/src/org/griphyn/vdl/karajan/monitor/monitors/ansi/WorkerTerminalInputHandler.java	2009-04-04 21:17:51 UTC (rev 2821)
@@ -0,0 +1,108 @@
+//----------------------------------------------------------------------
+//This code is developed as part of the Java CoG Kit project
+//The terms of the license can be found at http://www.cogkit.org/license
+//This message may not be removed or altered.
+//----------------------------------------------------------------------
+
+/*
+ * Created on Apr 4, 2009
+ */
+package org.griphyn.vdl.karajan.monitor.monitors.ansi;
+
+import java.io.CharArrayWriter;
+import java.io.PrintWriter;
+
+import org.apache.log4j.Logger;
+import org.globus.cog.abstraction.coaster.service.local.LocalRequestManager;
+import org.globus.cog.abstraction.impl.execution.coaster.WorkerShellCommand;
+import org.globus.cog.abstraction.interfaces.Task;
+import org.globus.cog.karajan.workflow.service.ProtocolException;
+import org.globus.cog.karajan.workflow.service.channels.ChannelManager;
+import org.globus.cog.karajan.workflow.service.channels.KarajanChannel;
+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;
+import org.ietf.jgss.GSSCredential;
+
+public class WorkerTerminalInputHandler implements InputHandler {
+    public static final Logger logger = Logger
+        .getLogger(WorkerTerminalInputHandler.class);
+
+    private Dialog dialog;
+    private Terminal term;
+    private Task task;
+    private String workerId, contact;
+    private GSSCredential cred;
+
+    public WorkerTerminalInputHandler(Dialog dialog, Terminal term, Task task,
+            String workerId) {
+        this.dialog = dialog;
+        this.term = term;
+        this.task = task;
+        this.workerId = workerId;
+        this.contact = task.getService(0).getServiceContact().getContact();
+        this.cred = (GSSCredential) task.getService(0).getSecurityContext()
+            .getCredentials();
+    }
+
+    public void handleInput(String in) {
+        if (in.equals("exit")) {
+            dialog.close();
+        }
+        else {
+            String result = runcmd(in);
+            if (result != null && !result.equals("")) {
+                term.append(runcmd(in));
+            }
+        }
+    }
+
+    private String runcmd(String cmd) {
+        try {
+            KarajanChannel channel = ChannelManager.getManager()
+                .reserveChannel(contact, cred, LocalRequestManager.INSTANCE);
+            WorkerShellCommand wsc = new WorkerShellCommand(workerId, cmd);
+            wsc.execute(channel);
+            return wsc.getInDataAsString(0);
+        }
+        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;
+        }
+    }
+
+    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;
+        }
+    }
+    
+    private String join(String[] s) {
+        StringBuffer sb = new StringBuffer();
+        for (int i = 0; i < s.length - 1; i++) {
+            sb.append(s[i]);
+            sb.append(' ');
+        }
+        sb.append(s[s.length - 1]);
+        return sb.toString();
+    }
+}




More information about the Swift-commit mailing list