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

DataThread.java

Go to the documentation of this file.
00001 
00007 package org.nees.daq;
00008 
00009 import java.net.*;
00010 import java.io.*;
00011 import java.util.*;
00012 import com.rbnb.sapi.*;
00013 
00015 public class DataThread implements Runnable{
00016     private Socket         socket;
00017     private BufferedReader rd;
00018     private Thread theThread;
00019     private boolean running = false;
00020     
00021     private Hashtable channelTable = new Hashtable();
00022     
00028     DataThread(Socket socket)
00029         throws IOException
00030     {
00031         rd = new BufferedReader(new InputStreamReader(socket.getInputStream()));
00032     }
00033     
00037     public void start() 
00038     {
00039         if (theThread != null) return;
00040         theThread = new Thread(this);
00041         running = true;
00042         theThread.start();
00043     }
00044     
00046     public void stop()
00047     {
00048         running = false;        
00049     }
00050 
00060     public void run () {
00061 
00062         String fromDAQ;
00063         StringTokenizer st;
00064         ISOtoRbnbTime timestamp;
00065         double time;
00066         String channelName;
00067         double data;
00068         DaqListener channel;
00069 
00070         // System.out.println("Running thread for DAQ data");
00071         
00072         try {
00073             while(((fromDAQ = rd.readLine()) != null) && running) {
00074                 // System.out.println("Line from DAQ: " + fromDAQ);
00075                 
00077                 if(fromDAQ.startsWith("driverid")) {
00078                     System.out.println("Skipping driverid");
00079                 }else {
00080                     // Want to split out the timestamp, all up to tab char
00081                     st = new StringTokenizer(fromDAQ, "\t\n ");
00082                     timestamp = new ISOtoRbnbTime(st.nextToken());
00083                     
00084                     time = timestamp.getValue();
00085                     
00087                     while (st.hasMoreTokens())
00088                     {
00089                         channelName = st.nextToken();
00090                         data = Double.parseDouble(st.nextToken());
00091                         channel = (DaqListener)channelTable.get(channelName);
00092                         if (channel != null)
00093                             channel.postData(channelName,time,data);
00094                     }
00095                 }
00096             }
00097         }
00098         catch (Exception e) {
00099             System.out.println("Socket IO Error: " + e);
00100         }
00101         finally
00102         {
00103             theThread = null;
00104             running = false;
00105         }
00106     }
00107     
00114     public void addListener(String name, DaqListener l)
00115     {
00116         channelTable.put(name,l);
00117     }
00118     
00125     public void removeListener(String name)
00126     {
00127         channelTable.remove(name);
00128     }
00129 }

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