|
//*****************************************************************
// This thread class used to monitor the connection to the
// DataServer. It periodically requests data from the server and
// then has DataClient update its display.
class DataReader
extends Thread
{
String net_line = "";
boolean keepRunning = true;
DataClient c;
//-------------------------------------------------------------
public DataReader(DataClient c) {
this.c = c;
}
//--------------------------------------------------------------
public void run() {
String dataString="";
String histDataString;
// This loops until either the connection is broken or the
// stop button or stop key is hit
while (keepRunning) {
// Ask the server to send data.
try{
c.write_net_output_line("send data");
}catch (IOException e){
break;
}
// First number sent from server is an integer that gives
// the number of data
values to be sent.
try{
c.numData
= c.read_net_input_int();
}catch (IOException e){break;}
dataString = "Number data pts= " + c.numData + "\n";
System.out.println(dataString);
histDataString = "";
// Create an array to hold the data and then read in the
// values from the server.
c.data = new float[c.numData];
for( int i=0; i <
c.numData; i++){
try{
c.data[i] = c.read_net_input_float();
// Pass the data for the monitored channel
to the
// histogram.
if(i == c.monitorChan) c.hist.setData(c.data[i]);
}catch (IOException
e){break;}
// BarChart needs data in string form so convert
here.
int j=((int)c.data[i]);
dataString
+= c.data[i] + " ";
histDataString
+= j + " ";
}
// Set the data for the TextArea, the event chart.
c.data_line = dataString
+ "\n";
c.chart.setData(histDataString);
// Now repaint the display.
c.repaint();
// Ask for data every TimeUpdate
try {
Thread.sleep(c.TimeUpdate);
} catch (InterruptedException
e)
{}
}
c.data_line = dataString + "\n";
c.msg_line = "disconnected";
c.repaint();
}
}
|