Main Page | Class Hierarchy | Class List | File List | Class Members | Related Pages

Client.java

00001 /* 00002 * Created on Apr 29, 2004 00003 */ 00004 package org.nees.rbnb; 00005 00006 import java.io.InputStreamReader; 00007 import java.io.BufferedReader; 00008 import java.io.IOException; 00009 import java.io.File; 00010 import java.io.FileInputStream; 00011 00012 import org.w3c.dom.Document; 00013 import org.w3c.dom.Element; 00014 import org.w3c.dom.NodeList; 00015 import org.w3c.dom.Node; 00016 00017 import javax.swing.JPanel; 00018 import javax.swing.JFrame; 00019 import javax.swing.JScrollPane; 00020 import javax.swing.JEditorPane; 00021 import javax.swing.JSplitPane; 00022 import javax.swing.JLabel; 00023 import javax.swing.JOptionPane; 00024 00025 import javax.swing.BorderFactory; 00026 import javax.swing.Timer; 00027 00028 import javax.swing.JTree; 00029 import javax.swing.tree.DefaultMutableTreeNode; 00030 import javax.swing.tree.TreeNode; 00031 import javax.swing.tree.TreeSelectionModel; 00032 import javax.swing.tree.TreeModel; 00033 import javax.swing.tree.DefaultTreeModel; 00034 import javax.swing.event.TreeSelectionEvent; 00035 import javax.swing.event.TreeSelectionListener; 00036 00037 import java.awt.Component; 00038 import java.awt.Container; 00039 import java.awt.BorderLayout; 00040 import java.awt.CardLayout; 00041 import java.awt.FlowLayout; 00042 import java.awt.GridLayout; 00043 import java.awt.Dimension; 00044 import java.awt.Button; 00045 import java.awt.event.ActionEvent; 00046 import java.awt.event.ActionListener; 00047 00048 import java.util.Properties; 00049 00050 import java.util.Iterator; 00051 import java.util.List; 00052 import java.util.Vector; 00053 import java.util.Hashtable; 00054 import java.util.Enumeration; 00055 import java.util.Date; 00056 import java.util.TimeZone; 00057 import java.text.SimpleDateFormat; 00058 00059 import com.rbnb.sapi.*; 00060 00061 import com.rbnb.utility.ArgHandler; //for argument parsing 00062 //import COM.Creare.Utility.ArgHandler; //for argument parsing 00063 00073 public class Client 00074 extends JPanel 00075 { 00076 private static final String SERVER_NAME = "localhost"; 00077 private static final String SERVER_PORT = "3333"; 00078 private static final String SOURCE_NAME = "neesPopClient"; 00079 private static final String SOURCE_CHANNEL_NAME = "command"; 00080 private static final String SINK_NAME = "nessPopClient_commandFeedback"; 00081 private static final String CHANNEL_LIST_SINK_NAME = "nessPopClient_channelList"; 00082 private static final String SINK_SOURCE_PATH = "neesPop/commandFeedback"; 00083 00084 private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("MMM d, yyyy h:mm aa"); 00085 private static final TimeZone TZ = TimeZone.getTimeZone("GMT"); 00086 00087 private String server = SERVER_NAME + ":" + SERVER_PORT; 00088 00089 private static final String TOP_CARD = "TopCard"; 00090 private static final String CHANNEL_CARD = "ChannelCard"; 00091 private static final String LINK_CARD = "LinkCard"; 00092 private static final String TEMPLATE_CARD = "TemplateCard"; 00093 private static final String INSTANCE_CARD = "InstanceCard"; 00094 private static final String PARAMETER_CARD = "ParameterCard"; 00095 00096 private static final String CHANNELS_TREE_LABEL = "Channels"; 00097 00098 private JFrame itsFrame; 00099 private JTree tree; 00100 private JEditorPane displayPane; 00101 private JPanel cardPanel; 00102 00103 private Hashtable buttonTable = new Hashtable(); 00104 00105 private static final boolean SHOW_HIDDEN = false; 00106 00107 private Source source = null; 00108 private String sourceName = SOURCE_NAME; 00109 private String sourceChannelName = SOURCE_CHANNEL_NAME; 00110 private int sourceChannelIndex; 00111 private ChannelMap sourceMap; 00112 private Thread sourceCommandThread; 00113 private boolean sourceConnected = false; 00114 private boolean sourceCommandRunning = false; 00115 00116 private Sink sink = null; 00117 private String sinkName = SINK_NAME; 00118 private ChannelMap sinkMap; 00119 private int sinkIndex; 00120 private String sinkRequestPath = SINK_SOURCE_PATH; 00121 private Thread sinkCommandThread; 00122 private boolean sinkConnected = false; 00123 private boolean sinkCommandRunning = false; 00124 00125 private Sink channelListSink = null; 00126 private String channelListSinkName = CHANNEL_LIST_SINK_NAME; 00127 private ChannelMap channelListSinkMap; 00128 private Thread channelListSinkThread; 00129 private boolean channelListSinkConnected = false; 00130 private boolean channelListSinkRunning = false; 00131 00132 private ChannelTree latestChannelTree; 00133 private boolean showHidden = SHOW_HIDDEN; 00134 00135 private MetaDataPropertyList pList = new MetaDataPropertyList(); 00136 private Vector commands = new Vector(); 00137 00138 private String propertiesFilePath = null; 00139 // private String propertiesFilePath = DEBUG_PARAMETER_FILE_PATH; 00140 private Properties prop = new Properties(); 00141 00142 private NeesControlMetadata metadata = new NeesControlMetadata(); 00143 00144 private String templateSelection; 00145 private JLabel templateSelectionLabel; 00146 private String instanceSelection; 00147 private JLabel instanceSelectionLabel; 00148 private String parameterSelection; 00149 private JLabel parameterSelectionLabel; 00150 00151 public Client() 00152 { 00153 super(); 00154 } 00155 00161 private static void createAndShowGUI(String[] args) { 00162 //Make sure we have nice window decorations. 00163 JFrame.setDefaultLookAndFeelDecorated(true); 00164 00165 //Create and set up the window. 00166 JFrame frame = new JFrame("NEES RBNB Control Client"); 00167 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 00168 00169 //Establish Client 00170 Client c = new Client(); 00171 c.exec(args); 00172 c.initGraphics(); 00173 c.executeCommand(Command.GET_METADATA); 00174 00175 //Set up the content pane. 00176 Container contentPane = frame.getContentPane(); 00177 contentPane.add(c); 00178 00179 //Display the window. 00180 frame.pack(); 00181 frame.setVisible(true); 00182 } 00183 00184 public static void main(String[] args) { 00185 //Schedule a job for the event-dispatching thread: 00186 //creating and showing this application's GUI. 00187 final String[] a = args; 00188 javax.swing.SwingUtilities.invokeLater(new Runnable() { 00189 public void run() { 00190 createAndShowGUI(a); 00191 } 00192 }); 00193 } 00194 00195 private void exec(String[] args) 00196 { 00197 if (setArgs(args)) 00198 { 00199 log("Args set"); 00200 if (connect()) 00201 { 00202 log("Starting threads..."); 00203 start(); 00204 log("Connected...and started"); 00205 } 00206 else 00207 log("Failed to connect."); 00208 } 00209 else 00210 log("Set args failed"); 00211 } 00212 00213 private void printUsage() 00214 { 00215 //TODO: print usage 00216 System.out.println("Print Usage Place holder"); 00217 } // printUsage 00218 00219 private boolean setArgs(String[] args) 00220 { 00221 try { 00222 ArgHandler ah=new ArgHandler(args); 00223 if (ah.checkFlag('h')) { 00224 printUsage(); 00225 return false; 00226 } 00227 if (ah.checkFlag('f')) { 00228 String a=ah.getOption('f'); 00229 if (a!=null) propertiesFilePath=a; 00230 } 00231 } catch (Exception e) { 00232 log("Failed to process command line args: " + e); 00233 return false; 00234 } 00235 00236 return 00237 processPropertiesFile(propertiesFilePath) 00238 && setPropertiesFromArgs(args) 00239 && setVarablesFromProperties(); 00240 } // setArgs 00241 00242 private boolean processPropertiesFile(String propertiesFilePath) 00243 { 00244 if (propertiesFilePath == null) return true; 00245 if (propertiesFilePath.length() == 0) return true; 00246 File f = new File(propertiesFilePath); 00247 if (!f.exists()) 00248 { 00249 log("Properties file does not exist: " + propertiesFilePath); 00250 return true; 00251 } 00252 00253 try 00254 { 00255 prop.load(new FileInputStream(f)); 00256 log("Sucessfully loaded properties file: " + propertiesFilePath); 00257 } 00258 catch (IOException e) 00259 { 00260 log("Can not process properties file, " + propertiesFilePath 00261 + ", exception = " + e); 00262 return false; 00263 } 00264 return true; 00265 } // processPropertiesFile 00266 00267 private boolean setPropertiesFromArgs(String[] args) { 00268 // TODO setPropertiesFromArgs 00269 return true; 00270 } // setPropertiesFromArgs 00271 00272 private boolean setVarablesFromProperties() { 00273 // TODO setVarablesFromProperties 00274 return true; 00275 } // setVarablesFromProperties 00276 00277 protected void initGraphics() { 00278 log ("initializing graphics"); 00279 setLayout(new BorderLayout()); 00280 setBorder(BorderFactory.createEmptyBorder(10,10,10,10)); 00281 00282 //Create the nodes. 00283 DefaultMutableTreeNode top = initObjectTree(); 00284 00285 //Create a tree that allows one selection at a time. 00286 tree = new JTree(top); 00287 tree.getSelectionModel().setSelectionMode 00288 (TreeSelectionModel.SINGLE_TREE_SELECTION); 00289 00290 //Listen for when the selection changes. 00291 tree.addTreeSelectionListener( 00292 new TreeSelectionListener() { 00293 public void valueChanged(TreeSelectionEvent e) 00294 { 00295 handleObjectSelection(e); 00296 } 00297 } 00298 ); 00299 00300 //Create the scroll pane and add the tree to it. 00301 JScrollPane treeView = new JScrollPane(tree); 00302 00303 JPanel commandPanel = setupCommandButtons(); 00304 00305 // Create the status display pane. 00306 displayPane = new JEditorPane(); 00307 displayPane.setEditable(false); 00308 JScrollPane displayView = new JScrollPane(displayPane); 00309 00310 //Add the scroll panes to a split pane. 00311 // JSplitPane commandSplitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT); 00312 // commandSplitPane.setTopComponent(commandPanel); 00313 // commandSplitPane.setBottomComponent(displayView); 00314 00315 Dimension minimumSize = new Dimension(100, 50); 00316 displayView.setMinimumSize(minimumSize); 00317 // commandSplitPane.setDividerLocation(100); //XXX: ignored in some releases 00318 //of Swing. bug 4101306 00319 //workaround for bug 4101306: 00320 //treeView.setPreferredSize(new Dimension(100, 100)); 00321 00322 // commandSplitPane.setPreferredSize(new Dimension(500, 300)); 00323 00324 //Add the scroll panes to a split pane. 00325 JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT); 00326 splitPane.setLeftComponent(treeView); 00327 // splitPane.setRightComponent(commandSplitPane); 00328 splitPane.setRightComponent(displayView); 00329 splitPane.setDividerLocation(200); //XXX: ignored in some releases 00330 //of Swing. bug 4101306 00331 //workaround for bug 4101306: 00332 //treeView.setPreferredSize(new Dimension(100, 100)); 00333 00334 splitPane.setPreferredSize(new Dimension(500, 300)); 00335 00336 //Add the split pane to this panel. 00337 add(splitPane,BorderLayout.CENTER); 00338 add(commandPanel,BorderLayout.SOUTH); 00339 00340 log ("initializing graphics.... done"); 00341 00342 } // initGraphics 00343 00344 00345 private JPanel setupCommandButtons() 00346 { 00347 return setupCommandButtonsWithFlat(); 00348 } 00349 00350 private JPanel setupCommandButtonsWithFlat() { 00351 // Create the command button panel with cards. 00352 JPanel flatPanel = new JPanel(); 00353 flatPanel.setLayout(new GridLayout(0,3)); 00354 makeButtonWithLister(flatPanel, Command.GET_METADATA, Command.GET_METADATA_LABEL); 00355 makeButtonWithLister(flatPanel, Command.CLEAR_COMMAND, Command.CLEAR_COMMAND_LABEL); 00356 makeButtonWithLister(flatPanel, Command.REFRESH_TREE, Command.REFRESH_TREE_LABEL); 00357 makeButtonWithLister(flatPanel, Command.TEST_COMMAND, Command.TEST_COMMAND_LABEL); 00358 makeButtonWithLister(flatPanel, Command.RESTART_COMMAND, Command.RESTART_COMMAND_LABEL); 00359 makeButtonWithLister(flatPanel, Command.ADD_LINK, Command.ADD_LINK_LABEL); 00360 makeButtonWithLister(flatPanel, Command.REMOVE_LINK, Command.REMOVE_LINK_LABEL); 00361 makeButtonWithLister(flatPanel, Command.FOCUS_TO_LINK, Command.FOCUS_TO_LINK_LABEL); 00362 makeButtonWithLister(flatPanel, Command.CREATE_INSTANCE, Command.CREATE_INSTANCE_LABEL); 00363 makeButtonWithLister(flatPanel, Command.DELETE_INSTANCE, Command.DELETE_INSTANCE_LABEL); 00364 makeButtonWithLister(flatPanel, Command.START_INSTANCE, Command.START_INSTANCE_LABEL); 00365 makeButtonWithLister(flatPanel, Command.STOP_INSTANCE, Command.STOP_INSTANCE_LABEL); 00366 makeButtonWithLister(flatPanel, Command.SET_PARAMETER, Command.SET_PARAMETER_LABEL); 00367 makeButtonWithLister(flatPanel, Command.RESET_PARAMETER, Command.RESET_PARAMETER_LABEL); 00368 00369 JPanel commands = new JPanel(); 00370 commands.setLayout(new BorderLayout()); 00371 commands.add(flatPanel,BorderLayout.CENTER); 00372 00373 JPanel labelPanel = new JPanel(); 00374 labelPanel.setLayout(new GridLayout(0,1)); 00375 JPanel p = new JPanel(); 00376 JLabel l = new JLabel("Selected Template: "); 00377 templateSelectionLabel = new JLabel("*************************"); 00378 p.add(l); 00379 p.add(templateSelectionLabel); 00380 labelPanel.add(p); 00381 00382 p = new JPanel(); 00383 l = new JLabel("Selected Instance: "); 00384 instanceSelectionLabel = new JLabel("*************************"); 00385 p.add(l); 00386 p.add(instanceSelectionLabel); 00387 labelPanel.add(p); 00388 00389 p = new JPanel(); 00390 l = new JLabel("Selected Instance Parameter: "); 00391 parameterSelectionLabel = new JLabel("*************************"); 00392 p.add(l); 00393 p.add(parameterSelectionLabel); 00394 labelPanel.add(p); 00395 00396 commands.add(labelPanel,BorderLayout.SOUTH); 00397 00398 disableAllButtons(); 00399 00400 enableButton(Command.CLEAR_COMMAND); 00401 enableButton(Command.GET_METADATA); 00402 enableButton(Command.RESTART_COMMAND); 00403 enableButton(Command.TEST_COMMAND); 00404 enableButton(Command.REFRESH_TREE); 00405 00406 return commands; 00407 } 00408 00409 private JPanel setupCommandButtonsWithCards() 00410 { 00411 // Create the command button panel with cards. 00412 cardPanel = new JPanel(); 00413 cardPanel.setLayout(new CardLayout()); 00414 00415 // top card 00416 JPanel card = new JPanel(); 00417 card.setLayout(new FlowLayout()); 00418 card.setOpaque(true); 00419 makeButtonWithLister(card, Command.GET_METADATA, Command.GET_METADATA_LABEL); 00420 makeButtonWithLister(card, Command.CLEAR_COMMAND, Command.CLEAR_COMMAND_LABEL); 00421 makeButtonWithLister(card, Command.REFRESH_TREE, Command.REFRESH_TREE_LABEL); 00422 makeButtonWithLister(card, Command.TEST_COMMAND, Command.TEST_COMMAND_LABEL); 00423 makeButtonWithLister(card, Command.RESTART_COMMAND, Command.RESTART_COMMAND_LABEL); 00424 cardPanel.add(card,TOP_CARD); 00425 00426 // channel card 00427 card = new JPanel(); 00428 card.setLayout(new FlowLayout()); 00429 makeButtonWithLister(card, Command.REFRESH_TREE, Command.REFRESH_TREE_LABEL); 00430 makeButtonWithLister(card, Command.SWITCH_TO_TOP, Command.SWITCH_TO_TOP_LABEL); 00431 cardPanel.add(card,CHANNEL_CARD); 00432 00433 // link card 00434 card = new JPanel(); 00435 card.setLayout(new FlowLayout()); 00436 makeButtonWithLister(card, Command.ADD_LINK, Command.ADD_LINK_LABEL); 00437 makeButtonWithLister(card, Command.REMOVE_LINK, Command.REMOVE_LINK_LABEL); 00438 makeButtonWithLister(card, Command.FOCUS_TO_LINK, Command.FOCUS_TO_LINK_LABEL); 00439 makeButtonWithLister(card, Command.SWITCH_TO_TOP, Command.SWITCH_TO_TOP_LABEL); 00440 cardPanel.add(card,LINK_CARD); 00441 00442 // template card 00443 card = new JPanel(); 00444 card.setLayout(new FlowLayout()); 00445 JPanel p = new JPanel(); 00446 JLabel l = new JLabel("Selected Template: "); 00447 templateSelectionLabel = new JLabel(" "); 00448 p.add(l); 00449 p.add(templateSelectionLabel); 00450 card.add(p); 00451 makeButtonWithLister(card, Command.CREATE_INSTANCE, Command.CREATE_INSTANCE_LABEL); 00452 makeButtonWithLister(card, Command.SWITCH_TO_TOP, Command.SWITCH_TO_TOP_LABEL); 00453 cardPanel.add(card,TEMPLATE_CARD); 00454 00455 // instance card 00456 card = new JPanel(); 00457 card.setLayout(new FlowLayout()); 00458 p = new JPanel(); 00459 l = new JLabel("Selected Instance: "); 00460 instanceSelectionLabel = new JLabel(" "); 00461 p.add(l); 00462 p.add(instanceSelectionLabel); 00463 card.add(p); 00464 makeButtonWithLister(card, Command.START_INSTANCE, Command.START_INSTANCE_LABEL); 00465 makeButtonWithLister(card, Command.STOP_INSTANCE, Command.STOP_INSTANCE_LABEL); 00466 makeButtonWithLister(card, Command.SWITCH_TO_TOP, Command.SWITCH_TO_TOP_LABEL); 00467 cardPanel.add(card,INSTANCE_CARD); 00468 00469 // parameter card 00470 card = new JPanel(); 00471 card.setLayout(new FlowLayout()); 00472 p = new JPanel(); 00473 l = new JLabel("Selected Instance: "); 00474 parameterSelectionLabel = new JLabel(" "); 00475 p.add(l); 00476 p.add(parameterSelectionLabel); 00477 card.add(p); 00478 makeButtonWithLister(card, Command.SET_PARAMETER, Command.SET_PARAMETER_LABEL); 00479 makeButtonWithLister(card, Command.RESET_PARAMETER, Command.RESET_PARAMETER_LABEL); 00480 makeButtonWithLister(card, Command.SWITCH_TO_TOP, Command.SWITCH_TO_TOP_LABEL); 00481 cardPanel.add(card,PARAMETER_CARD); 00482 00483 return cardPanel; 00484 } 00485 00488 private void makeButtonWithLister(JPanel p, String command, String label) { 00489 Button b = new Button(label); 00490 buttonTable.put(command, b); 00491 final String c = new String(command); 00492 b.addActionListener( 00493 new ActionListener(){ 00494 public void actionPerformed(ActionEvent arg0) { 00495 executeCommand(new String(c)); 00496 } 00497 } 00498 ); 00499 p.add(b); 00500 } 00501 00502 private void disableAllButtons() 00503 { 00504 Enumeration e = buttonTable.elements(); 00505 while (e.hasMoreElements()) 00506 { 00507 Button b = (Button)e.nextElement(); 00508 b.setEnabled(false); 00509 } 00510 } 00511 00512 private void enableButton(String key) 00513 { 00514 Button b = (Button)buttonTable.get(key); 00515 if (b != null) b.setEnabled(true); 00516 } 00517 00518 private void disableButton(String key) 00519 { 00520 Button b = (Button)buttonTable.get(key); 00521 if (b != null) b.setEnabled(false); 00522 } 00523 00524 protected void switchToCard(String cardLabel) 00525 { 00526 // if () 00527 // { 00528 // CardLayout c = (CardLayout) cardPanel.getLayout(); 00529 // c.show(cardPanel,cardLabel); 00530 // invalidate(); 00531 // } 00532 } 00533 00534 protected boolean connect() 00535 { 00536 try { 00537 // Create a source and connect: 00538 source=new Source(); 00539 source.OpenRBNBConnection(server,sourceName); 00540 sourceMap = new ChannelMap(); 00541 sourceChannelIndex = sourceMap.Add(sourceChannelName); 00542 sourceMap.PutTimeAuto("timeofday"); 00543 sourceConnected = true; 00544 log("Client: Source connection made to server = " 00545 + server + " as " + sourceName + "."); 00546 } 00547 catch (SAPIException se) 00548 { 00549 log("Client: Cannot connect source to " 00550 + server + " as " + sourceName + ": " + se); 00551 } 00552 00553 try { 00554 // Create sinkd and connect: 00555 sink=new Sink(); 00556 sink.OpenRBNBConnection(server,sinkName); 00557 sinkMap = new ChannelMap(); 00558 sinkIndex = sinkMap.Add(sinkRequestPath); 00559 sink.Subscribe(sinkMap,"newest"); 00560 sinkConnected = true; 00561 log("Client: Sink connection made to server = " 00562 + server + " as " + sinkName + "."); 00563 } 00564 catch (SAPIException se) 00565 { 00566 log("Client: Cannot connect sink to " 00567 + server + " as " + sinkName + ": " + se); 00568 } 00569 00570 try { 00571 // Create sinkd and connect: 00572 channelListSink = new Sink(); 00573 channelListSink.OpenRBNBConnection(server,channelListSinkName); 00574 channelListSinkConnected = true; 00575 log("Client: Channel List Sink connection made to server = " 00576 + server + " as " + channelListSinkName + "."); 00577 } 00578 catch (SAPIException se) 00579 { 00580 log("Client: Cannot connect sink to " 00581 + server + " as " + sinkName + ": " + se); 00582 } 00583 00584 return sourceConnected && sinkConnected && channelListSinkConnected; 00585 } 00586 00587 protected void executeCommand(String command) 00588 { 00589 // process and send any command that is done remotly 00590 if (command.equals(Command.GET_METADATA) 00591 || command.equals(Command.RESTART_COMMAND) 00592 || command.equals(Command.ADD_LINK) 00593 || command.equals(Command.REMOVE_LINK) 00594 || command.equals(Command.FOCUS_TO_LINK) 00595 || command.equals(Command.CREATE_INSTANCE) 00596 || command.equals(Command.DELETE_INSTANCE) 00597 || command.equals(Command.START_INSTANCE) 00598 || command.equals(Command.STOP_INSTANCE) 00599 || command.equals(Command.SET_PARAMETER) 00600 || command.equals(Command.RESET_PARAMETER) 00601 ) 00602 { 00603 String toSend = makeCommand(command); 00604 if (toSend != null) 00605 { 00606 synchronized (commands) 00607 { 00608 commands.add(toSend); 00609 sourceCommandThread.interrupt(); 00610 } 00611 } 00612 else log("Problems with command: " + command); 00613 } 00614 else 00615 if (command.equals(Command.TEST_COMMAND)) 00616 { 00617 log("Execute Test Command"); 00618 } 00619 else 00620 if (command.equals(Command.CLEAR_COMMAND)) 00621 { 00622 log("Execute Clear Command"); 00623 displayPane.setText(""); 00624 invalidate(); 00625 } 00626 else 00627 if (command.equals(Command.SWITCH_TO_TOP)) 00628 { 00629 log("Execute Switch to Top Command"); 00630 switchToCard(TOP_CARD); 00631 } 00632 else 00633 if (command.equals(Command.REFRESH_TREE)) 00634 { 00635 // local 00636 log("Unimplemented command = " + command); 00637 } 00638 else 00639 { 00640 log("Request to execurte unrecognized command = " + command); 00641 } 00642 } // ececuteCommand 00643 00648 private String makeCommand(String command) 00649 { 00650 Document doc = Xml.createDocument(); 00651 Node node, root = doc.createElement(NeesControlMetadata.XML_TAG_FOR_COMMAND); 00652 node = doc.createElement(NeesControlMetadata.XML_TAG_FOR_COMMAND); 00653 node.appendChild(doc.createTextNode(command)); 00654 root.appendChild(node); 00655 doc.appendChild(root); 00656 00657 // commands that require no parameters... 00658 // GET_METADATA, RESTART_COMMAND 00659 00660 if (command.equals(Command.ADD_LINK) || command.equals(Command.REMOVE_LINK) 00661 || command.equals(Command.FOCUS_TO_LINK)) 00662 { 00663 // add link name to command 00664 // TODO: fix link name 00665 // node = doc.createElement(NeesControlMetadata.XML_TAG_FOR_LINK); 00666 // node.appendChild(doc.createTextNode("UnknownLink")); 00667 // root.appendChild(node); 00668 return null; 00669 } 00670 else 00671 if (command.equals(Command.CREATE_INSTANCE)) 00672 { 00673 // requires a template 00674 if (templateSelection == null) return null; 00675 node = doc.createElement(NeesControlMetadata.XML_TAG_FOR_TEMPLATE); 00676 node.appendChild(doc.createTextNode(templateSelection)); 00677 root.appendChild(node); 00678 } 00679 else if (command.equals(Command.START_INSTANCE) 00680 || command.equals(Command.STOP_INSTANCE) 00681 || command.equals(Command.DELETE_INSTANCE)) 00682 { 00683 // requires an instance 00684 if (instanceSelection == null) return null; 00685 node = doc.createElement(NeesControlMetadata.XML_TAG_FOR_INSTANCE); 00686 node.appendChild(doc.createTextNode(instanceSelection)); 00687 root.appendChild(node); 00688 } 00689 else 00690 if (command.equals(Command.SET_PARAMETER) 00691 || command.equals(Command.RESET_PARAMETER)) 00692 { 00693 //requires an instance and a parameter 00694 if ((instanceSelection == null) 00695 || (parameterSelection == null)) 00696 return null; 00697 00698 InstanceDescription i = metadata.getInstance(instanceSelection); 00699 if (i == null) return null; 00700 00701 RBNBBaseParameterHolder p = i.getParameter(parameterSelection); 00702 if (p == null) return null; 00703 00704 if (command.equals(Command.SET_PARAMETER)) 00705 { 00706 String s = (String)JOptionPane.showInputDialog( 00707 (Component)this, 00708 "Enter New Value for Parameter\n" 00709 + p.getName() + " of \n" 00710 + i.getName(), 00711 "" + ((p.getValue() != null)?p.getValue().toString():"")); 00712 if (s == null) return null; 00713 p.setValueFromString(s); 00714 } 00715 else // command is reset use default value 00716 { 00717 p.setValue(p.getDefaultValue()); 00718 } 00719 00720 node = doc.createElement(NeesControlMetadata.XML_TAG_FOR_INSTANCE); 00721 node.appendChild(doc.createTextNode(instanceSelection)); 00722 root.appendChild(node); 00723 root.appendChild(p.createXmlNode(doc)); 00724 } 00725 try { 00726 return Xml.writeDocumentToString(doc); 00727 } catch (IOException e) { 00728 // Failed to generate Command 00729 log ("Failed to generate Command (" + command + "): " + e.toString()); 00730 return null; 00731 } 00732 } // makeCommand 00733 00734 protected void start() 00735 { 00736 startSourceThread(); 00737 startSinkThread(); 00738 startChannelListSinkThread(); 00739 } 00740 00741 protected void startSourceThread() 00742 { 00743 00744 if (!sourceConnected) return; 00745 00746 // Use this inner class to hide the public run method 00747 Runnable r = new Runnable() { 00748 public void run() { 00749 runSourceWork(); 00750 } 00751 }; 00752 sourceCommandRunning = true; 00753 sourceCommandThread = new Thread(r, "Command"); 00754 sourceCommandThread.start(); 00755 log("Client: Started Command Send thread."); 00756 } 00757 00758 protected void stopSourceThread() 00759 { 00760 sourceCommandRunning = false; 00761 sourceCommandThread.interrupt(); 00762 log("Client: Stopped Command Send thread."); 00763 } 00764 00765 protected void runSourceWork () 00766 { 00767 BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 00768 try { 00769 while(sourceIsRunning()) 00770 { 00771 String commandString = null; 00772 synchronized (commands) 00773 { 00774 if (commands.size() > 0) 00775 { 00776 commandString = (String)commands.firstElement(); 00777 commands.removeElementAt(0); 00778 } 00779 } 00780 00781 if (commandString != null) 00782 { 00783 log("Posting command: " + commandString); 00784 00785 // send command 00786 sourceMap.PutDataAsString(sourceChannelIndex,commandString); 00787 source.Flush(sourceMap); 00788 } 00789 try{Thread.sleep(1000);} catch (Exception ignore){} 00790 } 00791 } catch (SAPIException se) { 00792 se.printStackTrace(); 00793 // } catch (IOException e) { 00794 // e.printStackTrace(); 00795 } 00796 source.CloseRBNBConnection(); 00797 sourceCommandThread = null; 00798 } 00799 00800 protected boolean sourceIsRunning() 00801 { 00802 return (sourceConnected && sourceCommandRunning); 00803 } 00804 00805 protected void startSinkThread() 00806 { 00807 00808 if (!sinkConnected) return; 00809 00810 // Use this inner class to hide the public run method 00811 Runnable r = new Runnable() { 00812 public void run() { 00813 runSinkWork(); 00814 } 00815 }; 00816 sinkCommandRunning = true; 00817 sinkCommandThread = new Thread(r, "Listen"); 00818 sinkCommandThread.start(); 00819 log("Client: Started Command Listen thread."); 00820 } 00821 00822 protected void stopSinkThread() 00823 { 00824 sinkCommandRunning = false; 00825 sinkCommandThread.interrupt(); 00826 log("Client: Stopped Command Listen thread."); 00827 } 00828 00829 protected void runSinkWork() 00830 { 00831 try { 00832 while(sinkIsRunning()) 00833 { 00834 ChannelMap m = sink.Fetch(-1); 00835 String[] st = m.GetDataAsString(0); 00836 for (int i = 0; i < st.length; i++) 00837 { 00838 processCommandFeedback(st[i]); 00839 } 00840 } 00841 } catch (SAPIException se) { 00842 se.printStackTrace(); 00843 } 00844 sinkCommandThread = null; 00845 } 00846 00847 protected boolean sinkIsRunning() 00848 { 00849 return (sinkConnected && sinkCommandRunning); 00850 } 00851 00852 protected void startChannelListSinkThread() 00853 { 00854 00855 if (!channelListSinkConnected) return; 00856 00857 // Use this inner class to hide the public run method 00858 Runnable r = new Runnable() { 00859 public void run() { 00860 runChannelListSinkWork(); 00861 } 00862 }; 00863 channelListSinkRunning = true; 00864 channelListSinkThread = new Thread(r, "Listen"); 00865 channelListSinkThread.start(); 00866 log("Client: Started Channel List Listen thread."); 00867 } 00868 00869 protected void stopChannelListSinkThread() 00870 { 00871 channelListSinkRunning = false; 00872 channelListSinkThread.interrupt(); 00873 log("Client: Stopped Channel List Listen thread."); 00874 } 00875 00876 protected void runChannelListSinkWork() 00877 { 00878 try { 00879 while(channelListSinkIsRunning()) 00880 { 00881 channelListSink.RequestRegistration(); 00882 ChannelMap m = channelListSink.Fetch(-1); 00883 latestChannelTree = ChannelTree.createFromChannelMap(m); 00884 try {Thread.sleep(1000);} catch (Exception ignore) {} 00885 } 00886 } catch (SAPIException se) { 00887 se.printStackTrace(); 00888 } 00889 sinkCommandThread = null; 00890 } 00891 00892 protected boolean channelListSinkIsRunning() 00893 { 00894 return (channelListSinkConnected && channelListSinkRunning); 00895 } 00896 00897 protected void processCommandFeedback(String feedback) 00898 { 00899 Document doc = null; 00900 try{ 00901 doc = Xml.readDocumentFromString(feedback); 00902 } catch (Throwable ignore) {} 00903 if (doc == null) 00904 log("Message Unrecognized (not XML): " + feedback); 00905 else 00906 { 00907 Element root = doc.getDocumentElement(); 00908 log("Feedback Message type = " + root.getTagName()); 00909 if (root.getTagName().equals( 00910 NeesControlMetadata.XML_TAG_FOR_METADATA)) 00911 { 00912 // this is a metadata xml bundle 00913 try 00914 { 00915 setMetatdataFromXML(feedback); 00916 } catch (Throwable t) 00917 { 00918 t.printStackTrace(); 00919 log("Message parse failed on metadata: " + t); 00920 } 00921 } 00922 else 00923 if (root.getTagName().equals(Xml.XML_TAG_FOR_STATUS_MESSAGE)) 00924 { 00925 logStatusMessage(doc); 00926 } 00927 else 00928 if (root.getTagName().equals(Xml.XML_TAG_FOR_ERROR_MESSAGE)) 00929 { 00930 logErrorMessage(doc); 00931 } 00932 else 00933 log("Message type unrecognized " + root.getTagName()); 00934 } 00935 } 00936 00940 private void logStatusMessage(Document doc) { 00941 // TODO Auto-generated method stub 00942 try { 00943 log(Xml.writeDocumentToString(doc)); 00944 } catch (IOException ignore) { 00945 } 00946 } 00947 00951 private void logErrorMessage(Document doc) { 00952 // TODO Auto-generated method stub 00953 try { 00954 log(Xml.writeDocumentToString(doc)); 00955 } catch (IOException ignore) { 00956 } 00957 } 00958 00962 private void setMetatdataFromXML(String feedback) 00963 throws Throwable 00964 { 00965 metadata.setFromXml(feedback); 00966 updateTree(); 00967 } 00968 00972 private void updateTree() { 00973 DefaultMutableTreeNode top = initObjectTree(); 00974 DefaultTreeModel model = new DefaultTreeModel(top); 00975 tree.setModel(model); 00976 invalidate(); 00977 } 00978 00979 protected void handleObjectSelection(TreeSelectionEvent e) 00980 { 00981 // TODO: handleObjectSelection(e); 00982 DefaultMutableTreeNode node = (DefaultMutableTreeNode) 00983 tree.getLastSelectedPathComponent(); 00984 00985 if (node == null) return; 00986 Object nodeInfo = node.getUserObject(); 00987 String type = nodeInfo.getClass().getName(); 00988 log("Tree Selection is: " + nodeInfo.toString() + " (" + type + ")"); 00989 00990 instanceSelection = null; 00991 templateSelection = null; 00992 parameterSelection = null; 00993 00994 String label = ""; 00995 if (type.equals("java.lang.String")) 00996 { 00997 label = (String)nodeInfo; 00998 } 00999 01000 // determine if a template or instance parameter is selected 01001 if (type.endsWith("TemplateDescription")) 01002 templateSelection = 01003 ((TemplateDescription)nodeInfo).getName(); 01004 if (type.endsWith("InstanceDescription")) 01005 instanceSelection = 01006 ((InstanceDescription)nodeInfo).getName(); 01007 if (type.endsWith("ParameterHolder")) 01008 { 01009 parameterSelection = ((RBNBBaseParameterHolder)nodeInfo).getName(); 01010 DefaultMutableTreeNode probe = 01011 (DefaultMutableTreeNode)(node.getParent().getParent()); 01012 Object probeInfo = probe.getUserObject(); 01013 String probeType = probeInfo.getClass().getName(); 01014 if (probeType.endsWith("TemplateDescription")) 01015 { 01016 templateSelection = 01017 ((TemplateDescription)probeInfo).getName(); 01018 } 01019 else 01020 if (probeType.endsWith("InstanceDescription")) 01021 { 01022 instanceSelection = 01023 ((InstanceDescription)probeInfo).getName(); 01024 } 01025 } 01026 /* 01027 // determine which card 01028 if (label.startsWith("Connected") || label.startsWith("NeesPOP")) 01029 switchToCard(TOP_CARD); 01030 else 01031 if (label.equals("Channels") || type.endsWith("ChannelNodeCover")) 01032 switchToCard(CHANNEL_CARD); 01033 else 01034 if (label.equals("Links")) 01035 switchToCard(LINK_CARD); 01036 else 01037 if (parameterSelection != null) 01038 switchToCard(PARAMETER_CARD); 01039 else 01040 if (label.equals("Templates") || (templateSelection != null)) 01041 switchToCard(TEMPLATE_CARD); 01042 else 01043 if (label.equals("Instances") || (instanceSelection != null)) 01044 switchToCard(INSTANCE_CARD); 01045 else 01046 switchToCard(TOP_CARD); 01047 */ 01048 // determine which buttons to enable 01049 disableAllButtons(); 01050 01051 enableButton(Command.CLEAR_COMMAND); 01052 enableButton(Command.GET_METADATA); 01053 enableButton(Command.RESTART_COMMAND); 01054 enableButton(Command.TEST_COMMAND); 01055 enableButton(Command.REFRESH_TREE); 01056 01057 if (label.equals("Links")) 01058 { 01059 enableButton(Command.ADD_LINK); 01060 enableButton(Command.REMOVE_LINK); 01061 enableButton(Command.FOCUS_TO_LINK); 01062 } 01063 else 01064 if ((instanceSelection != null) && (parameterSelection != null)) 01065 { 01066 enableButton(Command.RESET_PARAMETER); 01067 enableButton(Command.SET_PARAMETER); 01068 } 01069 else 01070 if (templateSelection != null) 01071 { 01072 enableButton(Command.CREATE_INSTANCE); 01073 } 01074 else 01075 if (instanceSelection != null) 01076 { 01077 enableButton(Command.START_INSTANCE); 01078 enableButton(Command.STOP_INSTANCE); 01079 enableButton(Command.DELETE_INSTANCE); 01080 } 01081 01082 // set text fileds 01083 if (instanceSelection == null) 01084 instanceSelectionLabel.setText("No Instance Selected"); 01085 else 01086 instanceSelectionLabel.setText(instanceSelection); 01087 01088 if (templateSelection == null) 01089 templateSelectionLabel.setText("No Template Selected"); 01090 else 01091 templateSelectionLabel.setText(templateSelection); 01092 01093 if (parameterSelection == null) 01094 parameterSelectionLabel.setText("No Parameter Selected"); 01095 else 01096 { 01097 if (instanceSelection != null) 01098 parameterSelectionLabel.setText(parameterSelection + " of " 01099 + instanceSelection); 01100 else 01101 if (templateSelection != null) 01102 parameterSelectionLabel.setText("Template Parameter Selected."); 01103 } 01104 } 01105 01106 protected void log(String message) 01107 { 01108 System.out.println("Log: " + message); 01109 final String m = new String(message); 01110 if (displayPane != null) 01111 { 01112 displayPane.setText(displayPane.getText() + "\n" + m); 01113 } 01114 } 01115 01116 private DefaultMutableTreeNode initObjectTree() { 01117 DefaultMutableTreeNode top; 01118 top = new DefaultMutableTreeNode("Connected to " + server); 01119 if (!sinkConnected) 01120 top = new DefaultMutableTreeNode("Not connected to " + server); 01121 top.add(createChannelNodes()); 01122 if (metadata != null) 01123 top = metadata.insertIntoSwingTree(top); 01124 return top; 01125 } 01126 01127 private DefaultMutableTreeNode createChannelNodes() { 01128 DefaultMutableTreeNode root 01129 = new DefaultMutableTreeNode(CHANNELS_TREE_LABEL); 01130 ChannelTree ct = latestChannelTree; 01131 if (ct == null) 01132 { 01133 root = new DefaultMutableTreeNode("No Channel Tree (refresh?)"); 01134 } 01135 else 01136 { 01137 Iterator i = ct.rootIterator(); 01138 while (i.hasNext()) 01139 { 01140 ChannelTree.Node n = (ChannelTree.Node)i.next(); 01141 if (n.getName().startsWith("_")) 01142 { 01143 if (showHidden) 01144 root.add(makeChannelNodes(n)); 01145 } 01146 else 01147 root.add(makeChannelNodes(n)); 01148 } 01149 } 01150 01151 return root; 01152 } 01153 01154 private DefaultMutableTreeNode makeChannelNodes(ChannelTree.Node node) 01155 { 01156 DefaultMutableTreeNode root = new DefaultMutableTreeNode(new ChannelNodeCover(node)); 01157 List l = node.getChildren(); 01158 Iterator i = l.iterator(); 01159 while (i.hasNext()) 01160 { 01161 root.add(makeChannelNodes((ChannelTree.Node)i.next())); 01162 } 01163 01164 return root; 01165 } 01166 01167 private class ChannelNodeCover 01168 { 01169 ChannelTree.Node node; 01170 01171 ChannelNodeCover(ChannelTree.Node node) 01172 { 01173 this.node = node; 01174 } 01175 01176 public String toString() 01177 { 01178 return node.getName(); 01179 } 01180 } 01181 01182 } // Client 01183 /* 01184 * $Header: /disks/cvs/neesgrid/turbine/src/org/nees/rbnb/Client.java,v 1.10 2004/07/22 18:50:27 hubbard Exp $ 01185 */

Generated on Tue Aug 24 11:12:24 2004 for Data turbine for NEESGrid by doxygen 1.3.7