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.Creare.Utility.RBNBProcess; //alternative to System.exit, so
00017                                        //don't bring down servlet engine
00018 
00027 public class WalkerSourceApplet extends Applet {
00028 
00029     SimpleRandomWalk base = new SimpleRandomWalk();
00030     
00031     private static final String SERVER_NAME = "localhost";
00032     private static final String SERVER_PORT = "3333";
00033     private static final String SOURCE_NAME = "RandomWalk";
00034     private static final String CHANNEL_NAME = "RandomWalkData";
00035     private static final long TIMER_INTERVAL=1000;
00036     
00037     private String serverName = SERVER_NAME;
00038     private String serverPort = SERVER_PORT;
00039     private String server = serverName + ":" + serverPort;
00040     private String sourceName = SOURCE_NAME;
00041     private String channelName = CHANNEL_NAME;
00042     private long timerInterval = TIMER_INTERVAL;
00043          
00044     Source source = null;
00045     ChannelMap sMap;
00046     int index;
00047     boolean connected = false;
00048     
00049     Thread timerThread;
00050     boolean runit = false;
00051     
00052     private void setArgs() {
00053         String serverName = SERVER_NAME;
00054         String serverPort = SERVER_PORT;
00055         String sourceName = SOURCE_NAME;
00056         String channelName = CHANNEL_NAME;
00057         String timerString = "" + TIMER_INTERVAL;
00058         String param;
00059 
00060         param = getParameter("serverName");
00061         if (param != null)
00062             serverName = param;
00063         
00064         param = getParameter("serverPort");
00065         if (param != null)
00066             serverPort = param;
00067         
00068         param = getParameter("source");
00069         if (param != null)
00070             sourceName = param;
00071 
00072         param = getParameter("channel");
00073         if (param != null)
00074             channelName = param;
00075 
00076         param = getParameter("timer");
00077         if (param != null)
00078             timerString = param;
00079 
00080         setArgs(serverName,serverPort,sourceName,channelName,timerString);      
00081     }
00082 
00083     private void setArgs(String serverName, String serverPort,
00084              String sourceName, String channelName, String timerString)
00085     {
00086         this.serverName = serverName;
00087         this.serverPort = serverPort;
00088         
00089         server = serverName + ":" + serverPort;
00090     
00091         setArgs(server,sourceName,channelName,timerString);
00092     }
00093 
00094     private void setArgs(String server, String sourceName, 
00095             String ChannelName, String timerString)
00096     {
00097         long timerInterval = TIMER_INTERVAL;
00098 
00099         try {
00100             timerInterval = Long.parseLong(timerString);
00101         }  catch (Throwable ignore){}
00102 
00103         setArgs(server,sourceName,channelName,timerInterval);       
00104     }
00105 
00106     private void setArgs(String server, String sourceName,
00107         String channelName, long timerInterval)
00108     {
00109         this.server = server;
00110         serverText.setText(server);
00111         
00112         this.sourceName = sourceName;
00113         sourceText.setText(sourceName);
00114         
00115         this.channelName = channelName;
00116         channelText.setText(channelName);
00117         
00118         this.timerInterval = timerInterval;
00119         timerText.setText(""+timerInterval);
00120     }
00121 
00122     private void setArgsFromTextFields()
00123     {
00124         setArgs (   
00125             serverText.getText(),
00126             sourceText.getText(),
00127             channelText.getText(),
00128             timerText.getText()
00129         );
00130     }
00131     
00132     private void openConnection()
00133     {
00134         if (connected) return;
00135         
00136         try {
00137             disableStart();
00138             setArgsFromTextFields();
00139             messagePanel.message("Attempting connection to sever...");
00140             source=new Source();
00141             source.OpenRBNBConnection(server,sourceName);
00142             sMap = new ChannelMap();
00143             int index = sMap.Add(channelName);
00144             connected = true;
00145             messagePanel.message("Connection made to sever...");
00146             enableStart();
00147             start();
00148             connectButton.setLabel(DISCONNECT);
00149             connectButton.setActionCommand(connectButton.getLabel());
00150         } catch (SAPIException se)
00151         {
00152             messagePanel.message("SAPIException = " + se);
00153         }
00154     }
00155     
00156     private void enableStart() {
00157         startStopButton.setEnabled(true);
00158     }
00159 
00160     private void disableStart() {
00161         startStopButton.setEnabled(false);
00162     }
00163 
00164     private void closeConnection()
00165     {
00166         if (!connected) return;
00167         stop();
00168         disableStart();
00169         connected = false;
00170         source.CloseRBNBConnection();
00171         connectButton.setLabel(CONNECT);
00172         connectButton.setActionCommand(connectButton.getLabel());
00173     }
00174 
00175     private void startThread()
00176     {
00177         
00178         if (!connected) return;
00179 
00180         if (runit) return;
00181         
00182         // Use this inner class to hide the public run method
00183         Runnable r = new Runnable() {
00184             public void run() {
00185               runWork();
00186             }
00187         };
00188         runit = true;
00189         timerThread = new Thread(r, "Timer");
00190         timerThread.start();
00191         messagePanel.message("Start: sever = " + server);
00192         messagePanel.message("   source = " + sourceName + " with " + channelName);
00193         messagePanel.message("   timer interval = " + timerInterval);
00194     }
00195 
00196     private void stopThread()
00197     {
00198         if (!connected) return;
00199         
00200         if (runit)
00201         {
00202             runit = false;
00203             timerThread.interrupt();
00204             messagePanel.message("Stopped thread.");
00205         }
00206     }
00207     
00208     private void runWork ()
00209     {
00210         try {
00211             while(connected && runit)
00212             {
00213                 // Push data onto the server:
00214                 // System.out.print("Put new data to server: ");
00215                 sMap.PutTimeAuto("timeofday");
00216                 double data[] = new double[1];
00217                 data[0] = base.next();
00218                 sMap.PutDataAsFloat64(index,data);
00219                 messagePanel.message("" + data[0]);
00220                 source.Flush(sMap);
00221                 Thread.sleep(timerInterval);
00222             }
00223         } catch (SAPIException se) {
00224             // se.printStackTrace();
00225             messagePanel.message("SAPIException " + se + "; "); 
00226         } catch (InterruptedException e) {
00227             //e.printStackTrace();
00228             messagePanel.message("InterrupedExcetion; ");
00229         }
00230         timerThread = null;
00231     }
00232     
00233     private boolean isRunning()
00234     {
00235         return (connected && runit);
00236     }
00237     
00238     public void init() {
00239         setArgs();
00240         setLayout();
00241         repaint();
00242         messagePanel.message("initializing... ");
00243     }
00244 
00245     public void start() {
00246         messagePanel.message("starting... ");
00247         startThread();
00248         startStopButton.setLabel(STOP);
00249         startStopButton.setActionCommand(startStopButton.getLabel());
00250     }
00251 
00252     public void stop() {
00253         messagePanel.message("stopping... ");
00254         stopThread();
00255         startStopButton.setLabel(START);
00256         startStopButton.setActionCommand(startStopButton.getLabel());
00257     }
00258 
00259     public void destroy() {
00260         messagePanel.message("preparing for unloading...");
00261         closeConnection();
00262     }
00263 
00264     private static final String CONNECT = "Connect";
00265     private static final String DISCONNECT = "Disconnect";
00266     private Button connectButton = new Button(CONNECT);
00267     
00268     private static final String START = "Start";
00269     private static final String STOP = "Stop";
00270     private Button startStopButton = new Button(START);
00271     
00272     private TextField serverText =
00273         new TextField(SERVER_NAME + ":" + SERVER_PORT,40);
00274     private TextField sourceText = new TextField(SOURCE_NAME,40);
00275     private TextField channelText = new TextField(CHANNEL_NAME,40);
00276     private TextField timerText = new TextField("" + TIMER_INTERVAL,40);
00277 
00278     MessagePanel messagePanel = new MessagePanel();
00279 
00280     private void setLayout()
00281     {
00282         setLayout(new BorderLayout());      
00283 
00284         Panel p = new Panel();
00285         p.setLayout(new GridLayout(2,4));
00286         p.add(serverText);
00287         p.add(new Label("Server Host : port"));
00288         p.add(sourceText);
00289         p.add(new Label("Source Name"));
00290         p.add(channelText);
00291         p.add(new Label("Channel Name"));
00292         p.add(timerText);
00293         p.add(new Label("Timer (millisec.)"));
00294         add("North",p);
00295             
00296         p = new Panel();
00297         p.add(connectButton);
00298         p.add(startStopButton);     
00299         add("South", p);
00300         
00301         p = new Panel();
00302         p.add(messagePanel);
00303         add("Center", p);
00304     
00305         startStopButton.setEnabled(false);
00306         startStopButton.setActionCommand(startStopButton.getLabel());
00307         startStopButton.addActionListener(
00308             new ActionListener() {
00309                 public void actionPerformed(ActionEvent ev) {
00310                     startStopAction(ev.getActionCommand());
00311                 }
00312             }
00313         );
00314         
00315         connectButton.setActionCommand(connectButton.getLabel());
00316         connectButton.addActionListener(
00317             new ActionListener() {
00318                 public void actionPerformed(ActionEvent ev) {
00319                     connectAction(ev.getActionCommand());
00320                 }
00321             }
00322         );
00323     
00324     }
00325     
00326     private void startStopAction(String command)
00327     {
00328         if (command.equals(STOP))
00329             stop();
00330         else
00331             start();
00332     }
00333 
00334     private void connectAction(String command)
00335     {
00336         if (command.equals(CONNECT))
00337             openConnection();
00338         else
00339             closeConnection();
00340     }
00341 }

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