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

NeesControl.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 java.util.Properties; 00013 00014 import java.util.Iterator; 00015 import java.util.List; 00016 import java.util.Vector; 00017 import java.util.Hashtable; 00018 import java.util.Enumeration; 00019 import java.util.Date; 00020 import java.util.TimeZone; 00021 import java.text.SimpleDateFormat; 00022 00023 import org.w3c.dom.Document; 00024 import org.w3c.dom.Element; 00025 import org.w3c.dom.NodeList; 00026 import org.w3c.dom.Node; 00027 00028 import com.rbnb.sapi.*; 00029 00030 //import COM.Creare.Utility.ArgHandler; //for argument parsing 00031 import com.rbnb.utility.ArgHandler; //for argument parsing 00032 00041 public class NeesControl 00042 { 00043 00044 private static final String DEFAULT_PARAMETER_FILE_PATH = "./NeesControl.properties"; 00045 private static final String SERVER_NAME = "localhost"; 00046 private static final String SERVER_PORT = "3333"; 00047 private static final String SOURCE_NAME = "neesPop"; 00048 private static final String SOURCE_CHANNEL_NAME = "commandFeedback"; 00049 private static final String SINK_NAME = "neesPop_CommandInput"; 00050 private static final String SINK_SOURCE_PATH = "neesPopClient/command"; 00051 private static final long CONNECT_IDLE_TIME = 1000*60*10; // ten minutes in milliseconds 00052 private static final long NON_CONNECT_IDLE_TIME = 1000*30; // 30 seconds 00053 private static final long LONG_THREAD_IDLE_TIME = 1000*60*60; // on hour; interupted 00054 00055 private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("MMM d, yyyy h:mm aa"); 00056 private static final SimpleDateFormat LOG_FORMAT = new SimpleDateFormat("yyyy.MM.dd-HH:mm:ss z"); 00057 private static final TimeZone TZ = TimeZone.getTimeZone("GMT"); 00058 00059 static 00060 { 00061 DATE_FORMAT.setTimeZone(TZ); 00062 } 00063 00064 private String server = SERVER_NAME + ":" + SERVER_PORT; 00065 00066 private Thread backgroundThread; 00067 private boolean backgroundRunning = false; 00068 00069 private Source source = null; 00070 private String sourceName = SOURCE_NAME; 00071 private String sourceChannelName = SOURCE_CHANNEL_NAME; 00072 private int sourceChannelIndex; 00073 private ChannelMap sourceMap; 00074 private Thread sourceCommandThread; 00075 private boolean sourceConnected = false; 00076 private boolean sourceCommandRunning = false; 00077 00078 private Sink sink = null; 00079 private String sinkName = SINK_NAME; 00080 private ChannelMap sinkMap; 00081 private int sinkIndex; 00082 private String sinkRequestPath = SINK_SOURCE_PATH; 00083 private Thread sinkCommandThread; 00084 private boolean sinkConnected = false; 00085 private boolean sinkCommandRunning = false; 00086 00087 private Sink metadataSink = null; 00088 private ChannelMap metadataSinkMap; 00089 private Thread metadataSinkThread; 00090 private boolean metadataSinkConnected = false; 00091 private boolean metadataSinkRunning = false; 00092 00093 private Thread commandThread; 00094 private boolean commandRunning = false; 00095 00096 private MetaDataPropertyList pList = new MetaDataPropertyList(); 00097 private Vector commands = new Vector(); 00098 private Vector replies = new Vector(); 00099 private NeesControlMetadata metadata = new NeesControlMetadata(); 00100 00101 private String propertiesFilePath = DEFAULT_PARAMETER_FILE_PATH; 00102 private Properties prop = new Properties(); 00103 00104 private RBNBBase[] widget = 00105 { (new WalkerSource()), (new PictureSource()), (new AxisSource()), 00106 (new NumberSubSample()), (new DataVideoGather()), (new DaqToRbnb()), 00107 (new JPEGThumbnailer()) }; 00108 private String[] widgetType = { 00109 NeesControlMetadata.DATA_TYPE, 00110 NeesControlMetadata.IMAGE_TYPE, 00111 NeesControlMetadata.IMAGE_TYPE, 00112 NeesControlMetadata.DATA_TYPE, 00113 NeesControlMetadata.MOVIE_TYPE, 00114 NeesControlMetadata.DATA_TYPE, 00115 NeesControlMetadata.IMAGE_TYPE 00116 }; 00117 00118 public static void main(String[] args) { 00119 NeesControl n = new NeesControl(); 00120 n.exec(args); 00121 } 00122 00123 private void exec(String[] args) 00124 { 00125 if (!setArgs(args)) return; 00126 00127 // set up template list 00128 for (int i = 0; i < widget.length; i++) 00129 { 00130 addTemplate(widget[i], widgetType[i]); 00131 } 00132 00133 // Use this inner class to hide the public run method 00134 Runnable r = new Runnable() { 00135 public void run() { 00136 runBackground(); 00137 } 00138 }; 00139 backgroundRunning = true; 00140 backgroundThread = new Thread(r, "Background"); 00141 log("NeesControl: Starting Background Thread."); 00142 backgroundThread.start(); 00143 } 00144 00148 private void addTemplate(RBNBBase base, String type) { 00149 // put template into metaData and on local list 00150 String name = base.getTemplateName(); 00151 metadata.addToTemplateList(name,type); 00152 // get paprameters from base and add to template 00153 metadata.insertParametersToTempateFromBase(name, base); 00154 } 00155 00156 protected void runBackground() 00157 { 00158 while(true) 00159 { 00160 try{ 00161 log("Starting... "); 00162 00163 // attempt to set up the initial conditions and then test if they worked 00164 if (connectSource()) startSourceThread(); 00165 if (connectSink()) startSinkThread(); 00166 00167 // this runs until the program is killed from the outside 00168 // all clean up is done by closing the sockets and timing out 00169 while (true) 00170 { 00171 log("Background Thread Loop"); 00172 00173 if (!commandIsRunning()) startCommandThread(); 00174 00175 if (sinkIsRunning() && sourceIsRunning()) 00176 { 00177 // everything is a-ok... go idle for a long time 00178 try { 00179 log("Background idle"); 00180 Thread.sleep(CONNECT_IDLE_TIME); 00181 } catch (InterruptedException e) { 00182 // log interupt and retry 00183 log("Background thread interupted (ok condition)..."); 00184 } 00185 } 00186 else 00187 { 00188 if (!sourceConnected) connectSource(); 00189 if (!sourceIsRunning()) 00190 { 00191 stopSourceThread(); 00192 startSourceThread(); 00193 } 00194 if (!sinkConnected) connectSink(); 00195 if (!sinkIsRunning()) 00196 { 00197 stopSinkThread(); 00198 startSinkThread(); 00199 } 00200 if (!sinkIsRunning() || !sourceIsRunning()) 00201 { 00202 // still problems ... go idle for a short time and retry 00203 log("problems with connections..."); 00204 try { 00205 Thread.sleep(NON_CONNECT_IDLE_TIME); 00206 } catch (InterruptedException e) { 00207 // log interupt and retry 00208 log("Background thread interupted (problem condition)..."); 00209 } 00210 if (!sinkIsRunning()) log (" command sink is not running"); 00211 if (!sourceIsRunning()) log(" response source is not running"); 00212 log("...retyring"); 00213 } 00214 } 00215 } // while (true) 00216 } catch (Throwable t) 00217 { 00218 log("Error in background thread = " + t); 00219 t.printStackTrace(); 00220 } // try 00221 log("Restarting...."); 00222 } // (outer) while (true) 00223 } // runBackground 00224 00225 private void printUsage() { 00226 System.out.println("NeesControl: usage is..."); 00227 System.out.println("NeesControl "); 00228 System.out.println("[-f (PorpertiesFilePat) file path run time properties *" 00229 + propertiesFilePath +"]"); 00230 } // printUsage 00231 00232 private boolean setArgs(String[] args) 00233 { 00234 try { 00235 ArgHandler ah=new ArgHandler(args); 00236 if (ah.checkFlag('h')) { 00237 printUsage(); 00238 return false; 00239 } 00240 if (ah.checkFlag('f')) { 00241 String a=ah.getOption('f'); 00242 if (a!=null) propertiesFilePath=a; 00243 } 00244 } catch (Exception e) { 00245 log("Failed to process command line args: " + e); 00246 log("(use NeesControl -h to see command line options)"); 00247 return false; 00248 } 00249 00250 return 00251 processPropertiesFile(propertiesFilePath) 00252 && setPropertiesFromArgs(args) 00253 && setVarablesFromProperties(); 00254 } // setArgs 00255 00256 private boolean processPropertiesFile(String propertiesFilePath) 00257 { 00258 if (propertiesFilePath == null) return true; 00259 if (propertiesFilePath.length() == 0) return true; 00260 File f = new File(propertiesFilePath); 00261 if (!f.exists()) 00262 { 00263 log("Properties file does not exist: " + propertiesFilePath); 00264 return true; 00265 } 00266 00267 try 00268 { 00269 prop.load(new FileInputStream(f)); 00270 log("Sucessfully loaded properties file: " + propertiesFilePath); 00271 } 00272 catch (IOException e) 00273 { 00274 log("Can not process properties file, " + propertiesFilePath 00275 + ", exception = " + e); 00276 return false; 00277 } 00278 return true; 00279 } // processPropertiesFile 00280 00281 private boolean setPropertiesFromArgs(String[] args) { 00282 // TODO Auto-generated method stub 00283 return true; 00284 } // setPropertiesFromArgs 00285 00286 private boolean setVarablesFromProperties() { 00287 // TODO Auto-generated method stub 00288 return true; 00289 } // setVarablesFromProperties 00290 00291 protected boolean connectSource() 00292 { 00293 try { 00294 // Create a source and connect: 00295 source=new Source(); 00296 source.OpenRBNBConnection(server,sourceName); 00297 sourceMap = new ChannelMap(); 00298 sourceConnected = true; 00299 log("NeesControl: Source connection made to server = " 00300 + server + " as " + sourceName + "."); 00301 } 00302 catch (SAPIException se) 00303 { 00304 log("NeesControl: Cannot connect source to " 00305 + server + " as " + sourceName + ": " + se); 00306 } 00307 return sourceConnected; 00308 } // connectSource 00309 00310 protected boolean connectSink() 00311 { 00312 try { 00313 // Create a sink and connect: 00314 sink=new Sink(); 00315 sink.OpenRBNBConnection(server,sinkName); 00316 sinkMap = new ChannelMap(); 00317 sinkIndex = sinkMap.Add(sinkRequestPath); 00318 sink.Subscribe(sinkMap,"newest"); 00319 sinkConnected = true; 00320 log("NeesControl: Sink connection made to server = " 00321 + server + " as " + sinkName + "."); 00322 } 00323 catch (SAPIException se) 00324 { 00325 log("NeesControl: Cannot connect sink to " 00326 + server + " as " + sinkName + ": " + se); 00327 } 00328 return sinkConnected; 00329 } // connectSink 00330 00331 protected void startSourceThread() 00332 { 00333 00334 if (!sourceConnected) return; 00335 00336 // Use this inner class to hide the public run method 00337 Runnable r = new Runnable() { 00338 public void run() { 00339 runSourceWork(); 00340 } 00341 }; 00342 sourceCommandRunning = true; 00343 sourceCommandThread = new Thread(r, "Command"); 00344 sourceCommandThread.start(); 00345 log("NeesControl: Started Command Respond thread."); 00346 } 00347 00348 protected void stopSourceThread() 00349 { 00350 sourceCommandRunning = false; 00351 if (sourceCommandThread != null) 00352 sourceCommandThread.interrupt(); 00353 log("NeesControl: Stopped Command Respond thread."); 00354 } 00355 00356 protected void runSourceWork () 00357 { 00358 try { 00359 while(sourceIsRunning()) 00360 { 00361 log("Source thread loop"); 00362 String replyString = null; 00363 synchronized (replies) 00364 { 00365 if (replies.size() > 0) 00366 { 00367 replyString = (String)replies.firstElement(); 00368 replies.removeElementAt(0); 00369 } 00370 } 00371 00372 if (replyString != null) 00373 { 00374 //log("Reply sent is: " + replyString); 00375 log ("Reply send"); 00376 // send command 00377 sourceMap.Clear(); 00378 sourceChannelIndex = sourceMap.Add(sourceChannelName); 00379 sourceMap.PutTimeAuto("timeofday"); 00380 sourceMap.PutDataAsString(sourceChannelIndex,replyString); 00381 source.Flush(sourceMap); 00382 } 00383 try { 00384 Thread.sleep(LONG_THREAD_IDLE_TIME); 00385 } catch (InterruptedException ignore) {} 00386 } 00387 } catch (SAPIException se) { 00388 log("Source Connection failed " + se); 00389 } 00390 source.CloseRBNBConnection(); 00391 sourceConnected = false; 00392 sourceCommandThread = null; 00393 backgroundThread.interrupt(); 00394 } 00395 00396 protected boolean sourceIsRunning() 00397 { 00398 return ( (sourceCommandThread != null) && 00399 sourceConnected && sourceCommandRunning); 00400 } 00401 00402 protected void startSinkThread() 00403 { 00404 00405 if (!sinkConnected) return; 00406 00407 // Use this inner class to hide the public run method 00408 Runnable r = new Runnable() { 00409 public void run() { 00410 runSinkWork(); 00411 } 00412 }; 00413 sinkCommandRunning = true; 00414 sinkCommandThread = new Thread(r, "Listen"); 00415 sinkCommandThread.start(); 00416 log("NeesControl: Started Command Listen thread."); 00417 } 00418 00419 protected void stopSinkThread() 00420 { 00421 sinkCommandRunning = false; 00422 if (sinkCommandThread != null) 00423 sinkCommandThread.interrupt(); 00424 log("NeesControl: Stopped Command Listen thread."); 00425 } 00426 00427 protected void runSinkWork() 00428 { 00429 try { 00430 while(sinkIsRunning()) 00431 { 00432 log("Sink thread loop"); 00433 ChannelMap m = sink.Fetch(-1); 00434 if (m == null) 00435 { 00436 log("Data fetch failed."); 00437 continue; 00438 } 00439 String[] st = m.GetDataAsString(sinkIndex); 00440 log("MCP: Command(s) Received(count = "+ st.length + "): "); 00441 for (int i = 0; i < st.length; i++) 00442 { 00443 log(st[i]); 00444 synchronized (commands) 00445 { 00446 commands.add(st[i]); 00447 } 00448 } 00449 log("end command list - starting command thread"); 00450 if ((commandThread != null) 00451 && commandIsRunning()) 00452 commandThread.interrupt(); 00453 } 00454 } catch (SAPIException se) { 00455 log("Sink Connection failed " + se); 00456 } 00457 sink.CloseRBNBConnection(); 00458 sinkConnected = false; 00459 sinkCommandThread = null; 00460 backgroundThread.interrupt(); 00461 } 00462 00463 protected boolean sinkIsRunning() 00464 { 00465 return ( (sinkCommandThread != null) && 00466 sinkConnected && sinkCommandRunning); 00467 } 00468 00469 protected void startCommandThread() 00470 { 00471 00472 // Use this inner class to hide the public run method 00473 Runnable r = new Runnable() { 00474 public void run() { 00475 runCommandWork(); 00476 } 00477 }; 00478 commandRunning = true; 00479 commandThread = new Thread(r, "Command"); 00480 commandThread.start(); 00481 log("NeesControl: Started Command Processing thread."); 00482 } 00483 00484 protected void stopCommandThread() 00485 { 00486 commandRunning = false; 00487 if (commandThread != null) 00488 commandThread.interrupt(); 00489 log("NeesControl: Stopped Command Processing thread."); 00490 } 00491 00492 protected void runCommandWork() 00493 { 00494 String command = ""; 00495 00496 while(commandIsRunning()) 00497 { 00498 try { 00499 Thread.sleep(LONG_THREAD_IDLE_TIME); 00500 } catch (InterruptedException ignore) {} 00501 log("Command thread loop"); 00502 // decant commands and then sleep 00503 while (commands.size() > 0) 00504 { 00505 command = "unrecognized"; 00506 try { 00507 synchronized (commands) 00508 { 00509 command = (String)commands.elementAt(0); 00510 commands.removeElementAt(0); 00511 } 00512 processCommand(command); 00513 } 00514 catch (Throwable t) 00515 { 00516 log("Problems processing command (" + command + ") = " + t); 00517 } 00518 } 00519 } 00520 commandThread = null; 00521 } 00522 00523 protected boolean commandIsRunning() 00524 { 00525 return commandRunning; 00526 } 00527 00528 protected void processCommand(String command) 00529 { 00530 Document doc = null; 00531 try{ 00532 doc = Xml.readDocumentFromString(command); 00533 } catch (Throwable ignore) {} 00534 if (doc == null) 00535 log("Unrecognized Command (not XML): " + command); 00536 else 00537 { 00538 Element root = doc.getDocumentElement(); 00539 if (root.getTagName().equals( 00540 NeesControlMetadata.XML_TAG_FOR_COMMAND)) 00541 { 00542 try 00543 { 00544 processCommand(doc); 00545 } catch (Throwable t) 00546 { 00547 log("Message parse failed on command: " + t); 00548 } 00549 } 00550 else 00551 log("Not Command XML? " + root.getTagName()); 00552 } 00553 } 00554 00555 private void processCommand(Document doc) 00556 { 00557 String command = null; 00558 String template = null; 00559 String instance = null; 00560 RBNBBaseParameterHolder parameter = null; 00561 00562 boolean stateChanged = false; 00563 00564 Element root = doc.getDocumentElement(); 00565 NodeList children = root.getChildNodes(); 00566 final int length = children.getLength(); 00567 for(int i = 0; i < length; i++) 00568 { 00569 Node child = children.item(i); 00570 if (child.getNodeType() == Node.ELEMENT_NODE) 00571 { 00572 Element element = (Element)child; 00573 String name = element.getTagName(); 00574 if (name.equals(NeesControlMetadata.XML_TAG_FOR_COMMAND)) 00575 command = child.getChildNodes().item(0).getNodeValue(); 00576 else 00577 if (name.equals(NeesControlMetadata.XML_TAG_FOR_TEMPLATE)) 00578 template = child.getChildNodes().item(0).getNodeValue(); 00579 else 00580 if (name.equals(NeesControlMetadata.XML_TAG_FOR_INSTANCE)) 00581 instance = child.getChildNodes().item(0).getNodeValue(); 00582 else 00583 if (name.equals(NeesControlMetadata.XML_TAG_FOR_PARAMETER)) 00584 { 00585 parameter = new RBNBBaseParameterHolder(child); 00586 } 00587 } 00588 } 00589 if (command == null) 00590 { 00591 reply(Xml.errorXml("Unprocessed command","command is null")); 00592 } 00593 else 00594 if (command.equals(Command.GET_METADATA)) 00595 { 00596 stateChanged = true; 00597 } 00598 else if (command.equals(Command.RESTART_COMMAND)) 00599 { 00600 reply(Xml.statusXml("Processed restart command")); 00601 stateChanged = true; 00602 } 00603 else if (command.equals(Command.ADD_LINK)) 00604 reply(Xml.statusXml("Unimplemented Command " + command)); 00605 else if (command.equals(Command.REMOVE_LINK)) 00606 reply(Xml.statusXml("Unimplemented Command " + command)); 00607 else if (command.equals(Command.FOCUS_TO_LINK)) 00608 reply(Xml.statusXml("Unimplemented Command " + command)); 00609 else if (command.equals(Command.CREATE_INSTANCE)) 00610 { 00611 if (template == null) 00612 { 00613 reply(Xml.errorXml("Create instance command failed","no template name")); 00614 } 00615 else 00616 { 00617 createInstanceFromTempate(template); 00618 reply(Xml.statusXml("New instance created for " + template)); 00619 stateChanged = true; 00620 } 00621 } 00622 else if (command.equals(Command.START_INSTANCE)) 00623 { 00624 stateChanged = commandStartInstance(instance); 00625 } 00626 else if (command.equals(Command.STOP_INSTANCE)) 00627 { 00628 stateChanged = commandStopInstance(instance); 00629 } 00630 else if (command.equals(Command.DELETE_INSTANCE)) 00631 { 00632 stateChanged = commandDeleteInstance(instance); 00633 } 00634 else if (command.equals(Command.SET_PARAMETER)) 00635 { 00636 if (instance == null) 00637 { 00638 reply(Xml.errorXml("Set parameter command failed","no instance name")); 00639 } 00640 else 00641 if (parameter == null) 00642 { 00643 reply(Xml.errorXml("Set parameter command for " 00644 + instance + "failed","no parameter name")); 00645 } 00646 else 00647 { 00648 if (commandSetParameter(instance,parameter)) 00649 stateChanged = true; 00650 else 00651 reply(Xml.errorXml("Start instance command failed","can not stop instance")); 00652 } 00653 } 00654 else if (command.equals(Command.RESET_PARAMETER)) 00655 { 00656 if (instance == null) 00657 { 00658 reply(Xml.errorXml("Reset parameter command failed","no instance name")); 00659 } 00660 else 00661 if (parameter == null) 00662 { 00663 reply(Xml.errorXml("Reset parameter command for " 00664 + instance + "failed","no parameter name")); 00665 } 00666 else 00667 { 00668 if (commandResetParameter(instance,parameter)) 00669 stateChanged = true; 00670 else 00671 reply(Xml.errorXml("Start instance command failed","can not stop instance")); 00672 } 00673 } 00674 else 00675 { 00676 reply(Xml.errorXml("Unprocessed command",command)); 00677 } 00678 if (stateChanged) postMetadata(); 00679 } 00680 00681 /* (non-javadoc) 00682 * Note: assumes that instance is set to the name of the instance 00683 * and that the instance is an actual instance and not just a empty 00684 * instacne description 00685 */ 00686 private boolean commandStartInstance(String instance) { 00687 boolean started = false; 00688 try 00689 { 00690 if (instance == null) 00691 { 00692 reply(Xml.errorXml("Problem Starting instance", "Null String for instance name.")); 00693 return false; 00694 } 00695 InstanceDescription i = metadata.getInstance(instance); 00696 if (i == null) 00697 { 00698 reply(Xml.errorXml("Problem Starting " + instance, "Null description.")); 00699 return false; 00700 } 00701 started = i.startInstance(); 00702 if (!started) 00703 reply(Xml.errorXml("Problem Starting " + instance, "Start failed.")); 00704 } 00705 catch (Throwable t) 00706 { 00707 reply(Xml.errorXml("Exception when starting " + instance, t.toString())); 00708 started = false; 00709 } 00710 if (started) 00711 reply(Xml.statusXml("Instance " + instance + " started.")); 00712 00713 return started; 00714 } 00715 00716 /* (non-javadoc) 00717 * Note: assumes that instance is set to the name of the instance 00718 * and that the instance is an actual instance and not just a empty 00719 * instacne description 00720 */ 00721 private boolean commandStopInstance(String instance) 00722 { 00723 boolean stopped = false; 00724 try 00725 { 00726 if (instance == null) 00727 { 00728 reply(Xml.errorXml("Problem Stopping instance", "Null String for instance name.")); 00729 return false; 00730 } 00731 InstanceDescription i = metadata.getInstance(instance); 00732 if (i == null) 00733 { 00734 reply(Xml.errorXml("Problem Stopping " + instance, "Null description.")); 00735 return false; 00736 } 00737 stopped = i.stopInstance(); 00738 if (!stopped) 00739 reply(Xml.errorXml("Problem Stopping " + instance, "Start failed.")); 00740 } 00741 catch (Throwable t) 00742 { 00743 reply(Xml.errorXml("Problem stopping " + instance, t.toString())); 00744 stopped = false; 00745 } 00746 if (stopped) 00747 reply(Xml.statusXml("Instance " + instance + " stopped.")); 00748 00749 return stopped; 00750 } 00751 00752 /* (non-javadoc) 00753 * Note: assumes that instance is set to the name of the instance 00754 * and that the instance is an actual instance and not just a empty 00755 * instacne description 00756 */ 00757 private boolean commandDeleteInstance(String instance) 00758 { 00759 boolean deleted = false; 00760 try 00761 { 00762 if (instance == null) 00763 { 00764 reply(Xml.errorXml("Problem Deleting instance", "Null String for instance name.")); 00765 return false; 00766 } 00767 InstanceDescription i = metadata.getInstance(instance); 00768 if (i == null) 00769 { 00770 reply(Xml.errorXml("Problem Deleting " + instance, "Null description.")); 00771 return false; 00772 } 00773 if (i.isRunning()) 00774 { 00775 reply(Xml.errorXml("Problem Deleting " + instance, "Instance is running; stop first.")); 00776 return false; 00777 } 00778 00779 deleted = metadata.deleteInstance(instance); // may fail to actually delete! 00780 } 00781 catch (Throwable t) 00782 { 00783 reply(Xml.errorXml("Problem Deleting " + instance, t.toString())); 00784 deleted = false; 00785 } 00786 00787 if (deleted) 00788 reply(Xml.statusXml("Instance " + instance + " deleted.")); 00789 00790 return deleted; 00791 } 00792 00793 private boolean commandSetParameter(String instance, RBNBBaseParameterHolder parameter) 00794 { 00795 boolean ret = false; 00796 try 00797 { 00798 if (instance == null) return false; 00799 InstanceDescription i = metadata.getInstance(instance); 00800 if (i == null) return false; 00801 i.setParameter(parameter.getName(),parameter.getValue()); 00802 ret = true; 00803 } 00804 catch (Throwable ignore) {} 00805 return ret; 00806 } 00807 00808 private boolean commandResetParameter(String instance, RBNBBaseParameterHolder parameter) 00809 { 00810 boolean ret = false; 00811 try 00812 { 00813 if (instance == null) return false; 00814 InstanceDescription i = metadata.getInstance(instance); 00815 if (i == null) return false; 00816 i.setParameter(parameter.getName(),parameter.getDefaultValue()); 00817 ret = true; 00818 } 00819 catch (Throwable ignore) {} 00820 return ret; 00821 } 00822 00826 private void createInstanceFromTempate(String templateName) { 00827 String name = templateName + "_01"; 00828 int index = 2; 00829 Hashtable h = metadata.getInstancesHashtable(); 00830 while (h.get(name) != null) 00831 { 00832 name = templateName 00833 + ((index < 9)?("_0" + index):("_" + index)); 00834 index++; 00835 } 00836 if (templateName.equals("WalkerSource")) 00837 { 00838 WalkerSource w = new WalkerSource(); 00839 metadata.addToInstanceList(name,templateName,(RBNBBase)w); 00840 } 00841 else if (templateName.equals("PictureSource")) 00842 { 00843 PictureSource p = new PictureSource(); 00844 metadata.addToInstanceList(name,templateName,(RBNBBase)p); 00845 } 00846 else if (templateName.equals("NumberSubSample")) 00847 { 00848 NumberSubSample n = new NumberSubSample(); 00849 metadata.addToInstanceList(name,templateName,(RBNBBase)n); 00850 } 00851 else if (templateName.equals("DataVideoGather")) 00852 { 00853 DataVideoGather n = new DataVideoGather(); 00854 metadata.addToInstanceList(name,templateName,(RBNBBase)n); 00855 } 00856 else if (templateName.equals("DaqToRbnb")) 00857 { 00858 DaqToRbnb n = new DaqToRbnb(); 00859 metadata.addToInstanceList(name,templateName,(RBNBBase)n); 00860 } 00861 else if (templateName.equals("AxisSource")) 00862 { 00863 AxisSource n = new AxisSource(); 00864 metadata.addToInstanceList(name,templateName,(RBNBBase)n); 00865 } 00866 else if (templateName.equals("JPEGThumbnailer")) 00867 { 00868 JPEGThumbnailer n = new JPEGThumbnailer(); 00869 metadata.addToInstanceList(name,templateName,(RBNBBase)n); 00870 } 00871 else 00872 { 00873 log("Creating instance for unknow template type"); 00874 metadata.addToInstanceList(name,templateName); 00875 } 00876 } 00877 00878 protected void reply(String r) 00879 { 00880 synchronized (replies) 00881 { 00882 replies.add(r); 00883 } 00884 if ((sourceCommandThread != null) && sourceIsRunning()) 00885 sourceCommandThread.interrupt(); 00886 } 00887 00888 protected void postMetadata() 00889 { 00890 reply(metadata.toXmlString()); 00891 } 00892 00893 protected void log(String message) 00894 { 00895 String time = LOG_FORMAT.format(new Date()); 00896 System.out.println("Log (" + time + "): " + message); 00897 } 00898 00899 } // NeesControl 00900 /* 00901 * $Header: /disks/cvs/neesgrid/turbine/src/org/nees/rbnb/NeesControl.java,v 1.15 2004/07/22 18:50:27 hubbard Exp $ 00902 */

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