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

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