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

WalkerSourceApplet.java

00001 /* 00002 * Created on Feb 5, 2004 00003 * 00004 * A RBNB source that generates numbers in a bounded random walk. 00005 */ 00006 00007 package org.nees.rbnb; 00008 00009 import java.applet.Applet; 00010 import java.awt.*; 00011 import java.awt.event.ActionEvent; 00012 import java.awt.event.ActionListener; 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 00030 public class WalkerSourceApplet extends Applet 00031 implements WalkerCommandActionListener 00032 { 00033 00034 SimpleRandomWalk base = new SimpleRandomWalk(); 00035 00036 private static final String SERVER_NAME = "localhost"; 00037 private static final String SERVER_PORT = "3333"; 00038 private static final String SOURCE_NAME = "RandomWalk"; 00039 private static final String CHANNEL_NAME = "RandomWalkData"; 00040 private static final String COMMAND_NAME = "Command"; 00041 private static final String COMMAND_CHANNEL = "CommandData"; 00042 private static final long TIMER_INTERVAL=1000; 00043 00044 private String serverName = SERVER_NAME; 00045 private String serverPort = SERVER_PORT; 00046 private String server = serverName + ":" + serverPort; 00047 private String sourceName = SOURCE_NAME; 00048 private String channelName = CHANNEL_NAME; 00049 private String commandName = COMMAND_NAME; 00050 private String commandChannel = COMMAND_CHANNEL; 00051 private String commandPath = commandName + "/" + commandChannel; 00052 private long timerInterval = TIMER_INTERVAL; 00053 00054 Source source = null; 00055 ChannelMap sMap; 00056 int index; 00057 boolean connected = false; 00058 00059 Thread timerThread; 00060 boolean runit = false; 00061 00062 CommandThread commandThread = null; 00063 00064 private void setArgs() { 00065 String serverName = SERVER_NAME; 00066 String serverPort = SERVER_PORT; 00067 String sourceName = SOURCE_NAME; 00068 String channelName = CHANNEL_NAME; 00069 String timerString = "" + TIMER_INTERVAL; 00070 String commandName = COMMAND_NAME; 00071 String commandChannel = COMMAND_CHANNEL; 00072 String param; 00073 00074 param = getParameter("serverName"); 00075 if (param != null) 00076 serverName = param; 00077 00078 param = getParameter("serverPort"); 00079 if (param != null) 00080 serverPort = param; 00081 00082 param = getParameter("source"); 00083 if (param != null) 00084 sourceName = param; 00085 00086 param = getParameter("channel"); 00087 if (param != null) 00088 channelName = param; 00089 00090 param = getParameter("timer"); 00091 if (param != null) 00092 timerString = param; 00093 00094 param = getParameter("commandName"); 00095 if (param != null) 00096 commandName = param; 00097 00098 param = getParameter("commandChannel"); 00099 if (param != null) 00100 commandChannel = param; 00101 00102 setArgs(serverName,serverPort,sourceName,channelName,timerString, 00103 commandName,commandChannel); 00104 } 00105 00106 private void setArgs(String serverName, String serverPort, 00107 String sourceName, String channelName, String timerString, 00108 String commandName, String commandChannel) 00109 { 00110 this.serverName = serverName; 00111 this.serverPort = serverPort; 00112 00113 server = serverName + ":" + serverPort; 00114 00115 this.commandName = commandName; 00116 this.commandChannel = commandChannel; 00117 00118 commandPath = commandName + "/" + commandChannel; 00119 00120 setArgs(server,sourceName,channelName,timerString); 00121 } 00122 00123 private void setArgs(String server, String sourceName, 00124 String ChannelName, String timerString) 00125 { 00126 long timerInterval = TIMER_INTERVAL; 00127 00128 try { 00129 timerInterval = Long.parseLong(timerString); 00130 } catch (Throwable ignore){} 00131 00132 setArgs(server,sourceName,channelName,timerInterval); 00133 } 00134 00135 private void setArgs(String server, String sourceName, 00136 String channelName, long timerInterval) 00137 { 00138 this.server = server; 00139 serverText.setText(server); 00140 00141 this.sourceName = sourceName; 00142 sourceText.setText(sourceName); 00143 00144 this.channelName = channelName; 00145 channelText.setText(channelName); 00146 00147 this.timerInterval = timerInterval; 00148 timerText.setText(""+timerInterval); 00149 } 00150 00151 private void setArgsFromTextFields() 00152 { 00153 setArgs ( 00154 serverText.getText(), 00155 sourceText.getText(), 00156 channelText.getText(), 00157 timerText.getText() 00158 ); 00159 } 00160 00161 private void openConnection() 00162 { 00163 if (connected) return; 00164 00165 try { 00166 disableStart(); 00167 setArgsFromTextFields(); 00168 messagePanel.message("Attempting connection to server..."); 00169 source=new Source(); 00170 source.OpenRBNBConnection(server,sourceName); 00171 sMap = new ChannelMap(); 00172 int index = sMap.Add(channelName); 00173 connected = true; 00174 messagePanel.message("Connection made to server..."); 00175 enableStart(); 00176 start(); 00177 connectButton.setLabel(DISCONNECT); 00178 connectButton.setActionCommand(connectButton.getLabel()); 00179 } catch (SAPIException se) 00180 { 00181 messagePanel.message("SAPIException = " + se); 00182 } 00183 } 00184 00185 private void enableStart() { 00186 startStopButton.setEnabled(true); 00187 } 00188 00189 private void disableStart() { 00190 startStopButton.setEnabled(false); 00191 } 00192 00193 private void closeConnection() 00194 { 00195 if (!connected) return; 00196 stop(); 00197 disableStart(); 00198 connected = false; 00199 source.CloseRBNBConnection(); 00200 connectButton.setLabel(CONNECT); 00201 connectButton.setActionCommand(connectButton.getLabel()); 00202 } 00203 00204 private void startThread() 00205 { 00206 00207 if (!connected) return; 00208 00209 if (runit) return; 00210 00211 // Use this inner class to hide the public run method 00212 Runnable r = new Runnable() { 00213 public void run() { 00214 runWork(); 00215 } 00216 }; 00217 runit = true; 00218 timerThread = new Thread(r, "Timer"); 00219 timerThread.start(); 00220 messagePanel.message("Start: server = " + server); 00221 messagePanel.message(" source = " + sourceName + " with " + channelName); 00222 messagePanel.message(" timer interval = " + timerInterval); 00223 messagePanel.message(" command path = " + commandPath); 00224 } 00225 00226 private void stopThread() 00227 { 00228 if (!connected) return; 00229 00230 if (runit) 00231 { 00232 runit = false; 00233 timerThread.interrupt(); 00234 messagePanel.message("Stopped thread."); 00235 } 00236 } 00237 00238 private void runWork () 00239 { 00240 try { 00241 while(connected && runit) 00242 { 00243 // Push data onto the server: 00244 // System.out.print("Put new data to server: "); 00245 sMap.PutTimeAuto("timeofday"); 00246 double data[] = new double[1]; 00247 data[0] = base.next(); 00248 sMap.PutDataAsFloat64(index,data); 00249 messagePanel.message("" + data[0]); 00250 source.Flush(sMap); 00251 Thread.sleep(timerInterval); 00252 } 00253 } catch (SAPIException se) { 00254 // se.printStackTrace(); 00255 messagePanel.message("SAPIException " + se + "; "); 00256 } catch (InterruptedException e) { 00257 //e.printStackTrace(); 00258 messagePanel.message("InterrupedExcetion; "); 00259 } 00260 timerThread = null; 00261 } 00262 00263 private boolean isRunning() 00264 { 00265 return (connected && runit); 00266 } 00267 00268 public void init() { 00269 setArgs(); 00270 setLayout(); 00271 repaint(); 00272 messagePanel.message("initializing... "); 00273 } 00274 00275 public void start() { 00276 messagePanel.message("starting... "); 00277 commandThread = new CommandThread(commandPath, messagePanel, this); 00278 commandThread.connect(); 00279 commandThread.startThread(); 00280 startThread(); 00281 startStopButton.setLabel(STOP); 00282 startStopButton.setActionCommand(startStopButton.getLabel()); 00283 } 00284 00285 public void stop() { 00286 messagePanel.message("stopping... "); 00287 stopThread(); 00288 commandThread.stopThread(); 00289 commandThread.disconnect(); 00290 commandThread = null; 00291 startStopButton.setLabel(START); 00292 startStopButton.setActionCommand(startStopButton.getLabel()); 00293 } 00294 00295 public void destroy() { 00296 messagePanel.message("preparing for unloading..."); 00297 closeConnection(); 00298 } 00299 00300 private static final String CONNECT = "Connect"; 00301 private static final String DISCONNECT = "Disconnect"; 00302 private Button connectButton = new Button(CONNECT); 00303 00304 private static final String START = "Start"; 00305 private static final String STOP = "Stop"; 00306 private Button startStopButton = new Button(START); 00307 00308 private TextField serverText = 00309 new TextField(SERVER_NAME + ":" + SERVER_PORT,40); 00310 private TextField sourceText = new TextField(SOURCE_NAME,40); 00311 private TextField channelText = new TextField(CHANNEL_NAME,40); 00312 private TextField timerText = new TextField("" + TIMER_INTERVAL,40); 00313 00314 MessagePanel messagePanel = new MessagePanel(); 00315 00316 private void setLayout() 00317 { 00318 setLayout(new BorderLayout()); 00319 00320 Panel p = new Panel(); 00321 p.setLayout(new GridLayout(2,4)); 00322 p.add(serverText); 00323 p.add(new Label("Server Host : port")); 00324 p.add(sourceText); 00325 p.add(new Label("Source Name")); 00326 p.add(channelText); 00327 p.add(new Label("Channel Name")); 00328 p.add(timerText); 00329 p.add(new Label("Timer (millisec.)")); 00330 add("North",p); 00331 00332 p = new Panel(); 00333 p.add(connectButton); 00334 p.add(startStopButton); 00335 add("South", p); 00336 00337 p = new Panel(); 00338 p.add(messagePanel); 00339 add("Center", p); 00340 00341 startStopButton.setEnabled(false); 00342 startStopButton.setActionCommand(startStopButton.getLabel()); 00343 startStopButton.addActionListener( 00344 new ActionListener() { 00345 public void actionPerformed(ActionEvent ev) { 00346 startStopAction(ev.getActionCommand()); 00347 } 00348 } 00349 ); 00350 00351 connectButton.setActionCommand(connectButton.getLabel()); 00352 connectButton.addActionListener( 00353 new ActionListener() { 00354 public void actionPerformed(ActionEvent ev) { 00355 connectAction(ev.getActionCommand()); 00356 } 00357 } 00358 ); 00359 00360 } 00361 00362 private void startStopAction(String command) 00363 { 00364 if (command.equals(STOP)) 00365 stop(); 00366 else 00367 start(); 00368 } 00369 00370 private void connectAction(String command) 00371 { 00372 if (command.equals(CONNECT)) 00373 openConnection(); 00374 else 00375 closeConnection(); 00376 } 00377 00378 public void processCommand(String command) { 00379 messagePanel.message("Command received: " + command); 00380 if (command.equals("stop")) 00381 { 00382 stop(); 00383 } 00384 else if (command.equals("start")) 00385 { 00386 start(); 00387 } 00388 } 00389 00390 private class CommandThread 00391 { 00392 00393 Thread commandThread = null; 00394 Sink sink = null; 00395 String sinkName = "WalkerCommandSink"; 00396 String requestPath; 00397 MessagePanel messagePanel; 00398 boolean connected; 00399 boolean runit; 00400 WalkerCommandActionListener w = null; 00401 00402 public CommandThread(String path, MessagePanel message, WalkerCommandActionListener x) 00403 { 00404 w = x; 00405 requestPath = path; 00406 messagePanel = message; 00407 } 00408 00412 public void connect() { 00413 try { 00414 // Create a sink and connect: 00415 sink=new Sink(); 00416 sink.OpenRBNBConnection(server,sinkName); 00417 sMap = new ChannelMap(); 00418 index = sMap.Add(requestPath); 00419 sink.Subscribe(sMap,"newest"); 00420 connected = true; 00421 messagePanel.message("CommandThread: Connection made to server = " 00422 + server); 00423 messagePanel.message(" as " + sinkName 00424 + " requesting " + requestPath + "."); 00425 } catch (SAPIException se) 00426 { 00427 messagePanel.message("CommandThread: creation exception = " + se); 00428 } 00429 } 00430 00434 public void disconnect() { 00435 sink.CloseRBNBConnection(); 00436 sink = null; 00437 } 00438 00439 public void startThread() 00440 { 00441 00442 if (!connected) return; 00443 00444 // Use this inner class to hide the public run method 00445 Runnable r = new Runnable() { 00446 public void run() { 00447 runWork(); 00448 } 00449 }; 00450 runit = true; 00451 commandThread = new Thread(r, "CommandThread"); 00452 commandThread.start(); 00453 messagePanel.message("CommandThread: Started thread."); 00454 } 00455 00456 public void stopThread() 00457 { 00458 runit = false; 00459 commandThread.interrupt(); 00460 messagePanel.message("CommandThread: Stopped thread."); 00461 } 00462 00463 private void runWork () 00464 { 00465 try { 00466 while(isRunning()) 00467 { 00468 ChannelMap m = sink.Fetch(-1); 00469 if (m == null) 00470 { 00471 messagePanel.message("CommandThread: fetch failed."); 00472 continue; 00473 } 00474 String[] st = m.GetDataAsString(index); 00475 messagePanel.message("CommandThread: Command(s) Received: "); 00476 for (int i = 0; i < st.length; i++) 00477 { 00478 System.out.println(st[i]); 00479 w.processCommand(st[i]); 00480 } 00481 } 00482 } catch (SAPIException se) { 00483 messagePanel.message("CommandThread: exception in fetch = " + se); 00484 stopThread(); 00485 } 00486 commandThread = null; 00487 } 00488 00489 public boolean isRunning() 00490 { 00491 return (connected && runit); 00492 } 00493 00494 } 00495 00496 }

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