import java.awt.*; import java.awt.event.*; /** * QueryClient.java runs MPE program via Telnet and provides GUI front-end. * * This is a quick & dirty prototype, but it helps to "get the idea". * * It uses the Java Telnet implementation from the Java Telnet Applet * available at "http://www.first.gmd.de/persons/leo/java/Telnet". */ class QueryClient { /* * The host connectivity. */ static HostIO host; /* * The GUI I/O elements. */ static Choice ch; static TextField tf; static List ls; /** * Main program opens telnet connection to host, logs on and launches the * MPE program to be interacted with, opens GUI window for the user and * then translates user actions to appropriate transactions with the host * application. */ public static void main(String[] args) throws Exception { // Lots of hardcoded stuff to keep the example simple and short. There // should also be a better error handling and more resilient parsing of // host responses in a "real world" version. host = new HostIO(); host.open("my3000.grc.hp.com"); host.answerTo( "MPE/iX:", "hello java,user.test/pw ;info='run query.pub.sys' ;parm=1" ); host.answerTo(">", "base=music"); host.answerTo("PASSWORD = >>", ";"); host.answerTo("MODE = >>", "5"); host.answerTo(">", "set=composers"); Frame fr = new Frame("Remote MPE Invocation"); fr.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { exitProgram(); } }); fr.setLayout(new BorderLayout()); ch = new Choice(); ch.add("Find composer starting with..."); ch.add("Find composer containing..."); tf = new TextField(20); tf.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { performQuery(); } }); Panel pn = new Panel(); pn.add(ch); pn.add(tf); ls = new List(5); fr.add(pn, "North"); fr.add(ls, "Center"); fr.pack(); fr.setVisible(true); } /** * Exit host application when user closes window. */ static void exitProgram() { host.answerTo(">", "exit"); host.skipUntilStartsWith("CPU="); try { host.close(); } catch (Exception e) { /* ignore */ }; System.exit(0); } /** * Perform host transaction depending on query type selected by user */ static void performQuery() { switch (ch.getSelectedIndex()) { case 0: performFindAndReport( "find composername matching \"" + tf.getText() + "?*\"" ); break; case 1: performFindAndReport( "find composername matching \"?*" + tf.getText() + "?*\"" ); break; } } /** * Trigger QUERY FIND and parse resulting REPORT, if entries are found */ static void performFindAndReport(String findCmd) { ls.removeAll(); // empty result in gui host.answerTo(">", findCmd); host.skipUntilEndsWith("ENTRIES QUALIFIED"); if (! host.getLine().startsWith("0 ")) { host.answerTo(">", "report d,composername,16; d,birthplace,58; end"); host.skipLines(1+9); // 1x echo plus 9x heading while (! host.readln().equals(">")) { ls.add(host.getLine()); // add item to gui } host.pushBack(); // for next skip-to-prompt } } }