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

CommandSource.java

00001 /*
00002  * Created on Mar 5, 2004
00003  */
00004 package org.nees.rbnb;
00005 
00006 import java.io.IOException;
00007 import java.io.InputStreamReader;
00008 import java.io.BufferedReader;
00009 
00010 import com.rbnb.sapi.*;
00011 import COM.Creare.Utility.ArgHandler; //for argument parsing
00012 import COM.Creare.Utility.RBNBProcess; //alternative to System.exit, so
00013                                        //don't bring down servlet engine
00014 
00018 public class CommandSource {
00019 
00020     private static final String SERVER = "localhost:3333";
00021     private static final String SOURCE_NAME = "Command";
00022     private static final String CHANNEL_NAME = "CommandData";
00023     
00024     private String server = SERVER;
00025     private String sourceName = SOURCE_NAME;
00026     private String channelName = CHANNEL_NAME;
00027      
00028     Source source = null;
00029     ChannelMap sMap;
00030     int index;
00031     boolean connected = false;
00032     
00033     Thread commandThread;
00034     boolean runit = false;
00035     
00036     public static void main(String[] args) {
00037         CommandSource w = new CommandSource(args);
00038         w.exec();
00039         w.startThread();
00040     }
00041     
00042     private void printUsage() {
00043         System.out.println("CommandSource: usage is...");       
00044         System.out.print("CommandSource ");
00045         System.out.print("[-s server_hostname *" + SERVER + "] ");
00046         System.out.print("[-n source_name *" + SOURCE_NAME + "] ");
00047         System.out.print("[-c channel_name *" + CHANNEL_NAME + "] ");
00048         System.out.println();
00049     }
00050     
00051     public CommandSource(String[] args) {
00052         //parse args
00053         try {
00054             ArgHandler ah=new ArgHandler(args);
00055             if (ah.checkFlag('h')) {
00056                 printUsage();
00057                 RBNBProcess.exit(0);                
00058             }
00059             if (ah.checkFlag('s')) {
00060                 String a=ah.getOption('s');
00061                 if (a!=null) server=a;
00062             }
00063             if (ah.checkFlag('n')) {
00064                 String a=ah.getOption('n');
00065                 if (a!=null) sourceName=a;
00066             }
00067             if (ah.checkFlag('c')) {
00068                 String a=ah.getOption('c');
00069                 if (a!=null) channelName=a;
00070             }
00071         } catch (Exception e) {
00072             System.err.println("CommandSource argument exception "+e.getMessage());
00073             e.printStackTrace();
00074             RBNBProcess.exit(0);
00075         }
00076         
00077         System.out.println("Starting CommandSource on " + server + " as " + sourceName);
00078         System.out.println("  Channel name = " + channelName);
00079         System.out.println("  Use CommandSource -h to see optional parameters");
00080     }
00081     
00082     public CommandSource(String server_host, String source_name, 
00083             String channel_name)
00084     {
00085         server = server_host;
00086         sourceName = source_name;
00087         channelName = channel_name;
00088 
00089         System.out.println("Starting CommandSource on " + server + " as " + sourceName);
00090         System.out.println("  Channel name = " + channelName);
00091         System.out.println("  Use CommandSource -h to see optional parameters");
00092     }
00093     
00094     public CommandSource()
00095     {
00096         this(SERVER,SOURCE_NAME,CHANNEL_NAME);
00097     }
00098     
00099     public void exec()
00100     {
00101         try {
00102             // Create a source and connect:
00103             source=new Source();
00104             source.OpenRBNBConnection(server,sourceName);
00105             sMap = new ChannelMap();
00106             index = sMap.Add(channelName);
00107             sMap.PutTimeAuto("timeofday");
00108             connected = true;
00109             System.out.println("CommandSource: Connection made to sever = "
00110                 + server + " as " + sourceName + " with " + channelName + ".");
00111         } catch (SAPIException se) { se.printStackTrace(); }
00112     }
00113     
00114     public void startThread()
00115     {
00116         
00117         if (!connected) return;
00118         
00119         // Use this inner class to hide the public run method
00120         Runnable r = new Runnable() {
00121             public void run() {
00122               runWork();
00123             }
00124         };
00125         runit = true;
00126         commandThread = new Thread(r, "Command");
00127         commandThread.start();
00128         System.out.println("CommandSource: Started thread.");
00129     }
00130 
00131     public void stopThread()
00132     {
00133         runit = false;
00134         commandThread.interrupt();
00135         System.out.println("CommandSource: Stopped thread.");
00136     }
00137     
00138     private void runWork ()
00139     {
00140         BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
00141         try {
00142             while(isRunning())
00143             {
00144                 // get command
00145                 System.out.println("ENTER COMMAND:");
00146                 String commandString = in.readLine();
00147                 
00148                 System.out.println("Command is: " + commandString);
00149                 
00150                 // send command
00151                 sMap.PutDataAsString(index,commandString);
00152                 
00153                 source.Flush(sMap);
00154             }
00155         } catch (SAPIException se) {
00156             se.printStackTrace(); 
00157         } catch (IOException e) {
00158             e.printStackTrace();
00159         }
00160         commandThread = null;
00161     }
00162     
00163     public boolean isRunning()
00164     {
00165         return (connected && runit);
00166     }
00167         
00168 }

Generated on Tue Mar 23 11:54:24 2004 for Data turbine for NEESGrid by doxygen 1.3.6