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

NumberSinkApplet.java

00001 /* 00002 * Created on Mar 5, 2004 00003 */ 00004 package org.nees.rbnb; 00005 00006 import java.applet.Applet; 00007 import java.awt.*; 00008 import java.awt.event.ActionEvent; 00009 import java.awt.event.ActionListener; 00010 import java.util.Date; 00011 import java.util.TimeZone; 00012 import java.text.SimpleDateFormat; 00013 00014 import com.rbnb.sapi.*; 00015 //import COM.Creare.Utility.ArgHandler; //for argument parsing 00016 import com.rbnb.utility.ArgHandler; //for argument parsing 00017 //import COM.Creare.Utility.RBNBProcess; //alternative to System.exit, so 00018 //don't bring down servlet engine 00019 import com.rbnb.utility.RBNBProcess; //alternative to System.exit, so 00020 //don't bring down servlet engine 00021 00025 public class NumberSinkApplet extends Applet { 00026 00027 private static final String SERVER_NAME = "localhost"; 00028 private static final String SERVER_PORT = "3333"; 00029 private static final String SINK_NAME = "GetNumber"; 00030 private static final String SOURCE_NAME = "RandomWalk"; 00031 private static final String CHANNEL_NAME = "RandomWalkData"; 00032 00033 private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("MMM d, yyyy h:mm aa"); 00034 private static final TimeZone TZ = TimeZone.getTimeZone("GMT"); 00035 00036 static 00037 { 00038 DATE_FORMAT.setTimeZone(TZ); 00039 } 00040 00041 private String serverName = SERVER_NAME; 00042 private String serverPort = SERVER_PORT; 00043 private String server = serverName + ":" + serverPort; 00044 private String sinkName = SINK_NAME; 00045 private String sourceName = SOURCE_NAME; 00046 private String channelName = CHANNEL_NAME; 00047 private String requestPath = sourceName + "/" + channelName; 00048 00049 Sink sink = null; 00050 ChannelMap sMap; 00051 int index; 00052 boolean connected = false; 00053 00054 Thread stringDataThread; 00055 boolean runit = false; 00056 00057 private void setArgs() { 00058 String serverName = SERVER_NAME; 00059 String serverPort = SERVER_PORT; 00060 String sinkName = SINK_NAME; 00061 String sourceName = SOURCE_NAME; 00062 String channelName = CHANNEL_NAME; 00063 String param; 00064 00065 param = getParameter("serverName"); 00066 if (param != null) 00067 serverName = param; 00068 00069 param = getParameter("serverPort"); 00070 if (param != null) 00071 serverPort = param; 00072 00073 param = getParameter("sinkName"); 00074 if (param != null) 00075 sinkName = param; 00076 00077 param = getParameter("sourceName"); 00078 if (param != null) 00079 sourceName = param; 00080 00081 param = getParameter("channelName"); 00082 if (param != null) 00083 channelName = param; 00084 00085 setArgs(serverName,serverPort,sinkName,sourceName,channelName); 00086 } 00087 00088 private void setArgs(String serverName, String serverPort, 00089 String sinkName, String sourceName, String channelName) 00090 { 00091 this.serverName = serverName; 00092 this.serverPort = serverPort; 00093 00094 server = serverName + ":" + serverPort; 00095 00096 setArgs(server,sinkName,sourceName,channelName); 00097 } 00098 00099 private void setArgs(String server, String sinkName, 00100 String sourceName, String channelName) 00101 { 00102 this.server = server; 00103 serverText.setText(server); 00104 00105 this.sinkName = sinkName; 00106 sinkText.setText(sinkName); 00107 00108 this.sourceName = sourceName; 00109 sourceText.setText(sourceName); 00110 00111 this.channelName = channelName; 00112 channelText.setText(channelName); 00113 00114 requestPath = sourceName + "/" + channelName; 00115 00116 } 00117 00118 private void setArgsFromTextFields() 00119 { 00120 setArgs ( 00121 serverText.getText(), 00122 sinkText.getText(), 00123 sourceText.getText(), 00124 channelText.getText() 00125 ); 00126 } 00127 00128 public void openConnection() 00129 { 00130 if (connected) return; 00131 00132 try { 00133 disableStart(); 00134 setArgsFromTextFields(); 00135 messagePanel.message("Attempting connection to server..."); 00136 // Create a sink and connect: 00137 sink=new Sink(); 00138 sink.OpenRBNBConnection(server,sinkName); 00139 sMap = new ChannelMap(); 00140 index = sMap.Add(requestPath); 00141 sink.Subscribe(sMap,"newest"); 00142 connected = true; 00143 messagePanel.message("Connection made to server..."); 00144 messagePanel.message("on " + server + " as " + sinkName); 00145 messagePanel.message(" Requesting " + requestPath); 00146 enableStart(); 00147 start(); 00148 connectButton.setLabel(DISCONNECT); 00149 connectButton.setActionCommand(connectButton.getLabel()); 00150 } 00151 catch (SAPIException se) 00152 { 00153 messagePanel.message("SAPIException = " + se); 00154 } 00155 } 00156 00157 private void enableStart() { 00158 startStopButton.setEnabled(true); 00159 } 00160 00161 private void disableStart() { 00162 startStopButton.setEnabled(false); 00163 } 00164 00165 private void closeConnection() 00166 { 00167 if (!connected) return; 00168 stop(); 00169 disableStart(); 00170 connected = false; 00171 sink.CloseRBNBConnection(); 00172 connectButton.setLabel(CONNECT); 00173 connectButton.setActionCommand(connectButton.getLabel()); 00174 } 00175 00176 public void startThread() 00177 { 00178 00179 if (!connected) return; 00180 00181 // Use this inner class to hide the public run method 00182 Runnable r = new Runnable() { 00183 public void run() { 00184 runWork(); 00185 } 00186 }; 00187 runit = true; 00188 stringDataThread = new Thread(r, "StringData"); 00189 stringDataThread.start(); 00190 messagePanel.message("NumberSink: Started thread."); 00191 } 00192 00193 public void stopThread() 00194 { 00195 runit = false; 00196 stringDataThread.interrupt(); 00197 messagePanel.message("NumberSink: Stopped thread."); 00198 } 00199 00200 private void runWork () 00201 { 00202 try { 00203 while(isRunning()) 00204 { 00205 ChannelMap m = sink.Fetch(-1); 00206 if (m == null) 00207 { 00208 messagePanel.message("Data fetch failed."); 00209 continue; 00210 } 00211 double timeStamp = m.GetTimeStart(index); 00212 double[] data = m.GetDataAsFloat64(index); 00213 long unixTime = (long)(timeStamp * 1000.0); // convert sec to millisec 00214 String time = DATE_FORMAT.format(new Date(unixTime)); 00215 messagePanel.message("Data Received (for " + time + " GMT): "); 00216 for (int i = 0; i < data.length; i++) 00217 { 00218 messagePanel.message(" " + i + ": " + data[i]); 00219 } 00220 } 00221 } catch (SAPIException se) { 00222 se.printStackTrace(); 00223 } 00224 stringDataThread = null; 00225 } 00226 00227 public boolean isRunning() 00228 { 00229 return (connected && runit); 00230 } 00231 00232 public void init() { 00233 setArgs(); 00234 setLayout(); 00235 repaint(); 00236 messagePanel.message("initializing... "); 00237 messagePanel.message("Starting NumberSinkAppler on " + server + " as " + sinkName); 00238 messagePanel.message(" Requesting " + requestPath); 00239 } 00240 00241 public void start() { 00242 messagePanel.message("starting... "); 00243 startThread(); 00244 startStopButton.setLabel(STOP); 00245 startStopButton.setActionCommand(startStopButton.getLabel()); 00246 } 00247 00248 public void stop() { 00249 messagePanel.message("stopping... "); 00250 stopThread(); 00251 startStopButton.setLabel(START); 00252 startStopButton.setActionCommand(startStopButton.getLabel()); 00253 } 00254 00255 public void destroy() { 00256 messagePanel.message("preparing for unloading..."); 00257 closeConnection(); 00258 } 00259 00260 private static final String CONNECT = "Connect"; 00261 private static final String DISCONNECT = "Disconnect"; 00262 private Button connectButton = new Button(CONNECT); 00263 00264 private static final String START = "Start"; 00265 private static final String STOP = "Stop"; 00266 private Button startStopButton = new Button(START); 00267 00268 private TextField serverText = 00269 new TextField(SERVER_NAME + ":" + SERVER_PORT,40); 00270 private TextField sinkText = new TextField(SINK_NAME,40); 00271 private TextField sourceText = new TextField(SOURCE_NAME,40); 00272 private TextField channelText = new TextField(CHANNEL_NAME,40); 00273 00274 MessagePanel messagePanel = new MessagePanel(); 00275 00276 private void setLayout() 00277 { 00278 setLayout(new BorderLayout()); 00279 00280 Panel p = new Panel(); 00281 p.setLayout(new GridLayout(2,4)); 00282 p.add(serverText); 00283 p.add(new Label("Server Host : port")); 00284 p.add(sinkText); 00285 p.add(new Label("Sink Name")); 00286 p.add(sourceText); 00287 p.add(new Label("Source Name")); 00288 p.add(channelText); 00289 p.add(new Label("Channel Name")); 00290 add("North",p); 00291 00292 p = new Panel(); 00293 p.add(connectButton); 00294 p.add(startStopButton); 00295 add("South", p); 00296 00297 p = new Panel(); 00298 p.add(messagePanel); 00299 add("Center", p); 00300 00301 startStopButton.setEnabled(false); 00302 startStopButton.setActionCommand(startStopButton.getLabel()); 00303 startStopButton.addActionListener( 00304 new ActionListener() { 00305 public void actionPerformed(ActionEvent ev) { 00306 startStopAction(ev.getActionCommand()); 00307 } 00308 } 00309 ); 00310 00311 connectButton.setActionCommand(connectButton.getLabel()); 00312 connectButton.addActionListener( 00313 new ActionListener() { 00314 public void actionPerformed(ActionEvent ev) { 00315 connectAction(ev.getActionCommand()); 00316 } 00317 } 00318 ); 00319 00320 } 00321 00322 private void startStopAction(String command) 00323 { 00324 if (command.equals(STOP)) 00325 stop(); 00326 else 00327 start(); 00328 } 00329 00330 private void connectAction(String command) 00331 { 00332 if (command.equals(CONNECT)) 00333 openConnection(); 00334 else 00335 closeConnection(); 00336 } 00337 }

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