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

Plot.java

00001 /* 00002 * Created on Mar 5, 2004 00003 * Cloned into Plot.java on 7/30/04 by Paul, trying to see if JFreeChart will work for us. 00004 */ 00005 package org.nees.rbnb; 00006 00007 import java.io.IOException; 00008 import java.util.Date; 00009 import java.util.TimeZone; 00010 import java.text.SimpleDateFormat; 00011 00012 import com.rbnb.sapi.*; 00013 import com.rbnb.utility.ArgHandler; //for argument parsing 00014 import com.rbnb.utility.RBNBProcess; //alternative to System.exit, so 00015 //don't bring down servlet engine 00016 00017 00018 import java.awt.Color; 00019 import java.text.DateFormat; 00020 00021 import org.jfree.chart.*; 00022 import org.jfree.data.time.Day; 00023 00028 public class Plot { 00029 00030 private static final String SERVER_NAME = "localhost"; 00031 private static final String SERVER_PORT = "3333"; 00032 private static final String SINK_NAME = "Plot"; 00033 private static final String SOURCE_NAME = "RandomWalk"; 00034 private static final String CHANNEL_NAME = "RandomWalkData"; 00035 00036 private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("MMM d, yyyy h:mm aa"); 00037 private static final TimeZone TZ = TimeZone.getTimeZone("GMT"); 00038 00039 static 00040 { 00041 DATE_FORMAT.setTimeZone(TZ); 00042 } 00043 00044 private String serverName = SERVER_NAME; 00045 private String serverPort = SERVER_PORT; 00046 private String server = serverName + ":" + serverPort; 00047 private String sinkName = SINK_NAME; 00048 private String sourceName = SOURCE_NAME; 00049 private String channelName = CHANNEL_NAME; 00050 private String requestPath = sourceName + "/" + channelName; 00051 00052 Sink sink = null; 00053 ChannelMap sMap; 00054 int index; 00055 boolean connected = false; 00056 00057 Thread stringDataThread; 00058 boolean runit = false; 00059 00060 public static void main(String[] args) { 00061 NumberSink s = new NumberSink(args); 00062 s.exec(); 00063 s.startThread(); 00064 } 00065 00066 private void printUsage() { 00067 System.out.println("Plot: usage is..."); 00068 System.out.println("Plot "); 00069 System.out.println("[-s Server Hostname *" + SERVER_NAME + "] "); 00070 System.out.println("[-o Server Port Number *" + SERVER_PORT + "] "); 00071 System.out.println("[-k Sink Name *" + SINK_NAME + " ]"); 00072 System.out.println("[-n Source Name *" + SOURCE_NAME + "] "); 00073 System.out.println("[-c Source Channel Name *" + CHANNEL_NAME + "] "); 00074 } 00075 00076 public Plot(String[] args) { 00077 //parse args 00078 try { 00079 ArgHandler ah=new ArgHandler(args); 00080 if (ah.checkFlag('h')) { 00081 printUsage(); 00082 RBNBProcess.exit(0); 00083 } 00084 if (ah.checkFlag('s')) { 00085 String a=ah.getOption('s'); 00086 if (a!=null) serverName=a; 00087 } 00088 if (ah.checkFlag('p')) { 00089 String a=ah.getOption('p'); 00090 if (a!=null) serverPort=a; 00091 } 00092 if (ah.checkFlag('n')) { 00093 String a=ah.getOption('n'); 00094 if (a!=null) sourceName=a; 00095 } 00096 if (ah.checkFlag('c')) { 00097 String a=ah.getOption('c'); 00098 if (a!=null) channelName=a; 00099 } 00100 if (ah.checkFlag('k')) { 00101 String a=ah.getOption('k'); 00102 if (a!=null) sinkName=a; 00103 } 00104 } catch (Exception e) { 00105 System.err.println("NumberSink argument exception "+e.getMessage()); 00106 e.printStackTrace(); 00107 RBNBProcess.exit(0); 00108 } 00109 00110 requestPath = sourceName + "/" + channelName; 00111 server = serverName + ":" + serverPort; 00112 00113 System.out.println("Starting Plot on " + server + " as " + sinkName); 00114 System.out.println(" Requesting " + requestPath); 00115 System.out.println(" Use Plot -h to see optional parameters"); 00116 } 00117 00118 public void exec() 00119 { 00120 try { 00121 // Create a sink and connect: 00122 sink=new Sink(); 00123 sink.OpenRBNBConnection(server,sinkName); 00124 sMap = new ChannelMap(); 00125 index = sMap.Add(requestPath); 00126 sink.Subscribe(sMap,"newest"); 00127 connected = true; 00128 System.out.println("Plot: Connection made to server = " 00129 + server + " as " + sinkName 00130 + " requesting " + requestPath + "."); 00131 } catch (SAPIException se) { se.printStackTrace(); } 00132 } 00133 00134 public void startThread() 00135 { 00136 00137 if (!connected) return; 00138 00139 // Use this inner class to hide the public run method 00140 Runnable r = new Runnable() { 00141 public void run() { 00142 runWork(); 00143 } 00144 }; 00145 runit = true; 00146 stringDataThread = new Thread(r, "StringData"); 00147 stringDataThread.start(); 00148 System.out.println("Plot: Started thread."); 00149 } 00150 00151 public void stopThread() 00152 { 00153 runit = false; 00154 stringDataThread.interrupt(); 00155 System.out.println("Plot: Stopped thread."); 00156 } 00157 00158 private void runWork () 00159 { 00160 try { 00161 while(isRunning()) 00162 { 00163 ChannelMap m = sink.Fetch(-1); 00164 if (m == null) 00165 { 00166 System.out.println("Data fetch failed."); 00167 continue; 00168 } 00169 double timeStamp = m.GetTimeStart(index); 00170 double[] data = m.GetDataAsFloat64(index); 00171 long unixTime = (long)(timeStamp * 1000.0); // convert sec to millisec 00172 String time = DATE_FORMAT.format(new Date(unixTime)); 00173 System.out.println("Data Received (for " + time + " GMT): "); 00174 for (int i = 0; i < data.length; i++) 00175 { 00176 System.out.println(" " + i + ": " + data[i]); 00177 } 00178 } 00179 } catch (SAPIException se) { 00180 se.printStackTrace(); 00181 } 00182 stringDataThread = null; 00183 } 00184 00185 public boolean isRunning() 00186 { 00187 return (connected && runit); 00188 } 00189 00190 00191 }

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