00001
00010
package org.nees.daq;
00011
00012
import java.net.*;
00013
import java.io.*;
00014
import java.lang.String.*;
00015
import java.util.StringTokenizer;
00016
import java.util.Vector;
00017
00028 public class ControlPort {
00029
private BufferedReader rd;
00030
private BufferedWriter wr;
00031
private boolean connected;
00032
00038 public ControlPort(Socket socket)
00039
throws UnknownHostException, IOException
00040 {
00041 rd =
new BufferedReader(
new InputStreamReader(socket.getInputStream()));
00042 wr =
new BufferedWriter(
new OutputStreamWriter(socket.getOutputStream()));
00043 connected =
true;
00044
00045 System.out.println(
"Created DAQ Control Port");
00046 }
00047
00053 public String[]
getChannels()
00054 throws IOException
00055 {
00056 String list_command =
"list-channels";
00057 String delimiter =
",";
00058 String raw_list =
"";
00059 String[] result =
new String[0];
00060 Vector channels =
new Vector();
00061
00062
00063
if(!connected) {
00064
throw new IOException(
"DAQ Not connected.");
00065 }
00066
00067 System.out.println(
"Requesting DAQ channel list");
00068 wr.write(list_command +
'\n');
00069 wr.flush();
00070
00071 raw_list = rd.readLine();
00072
00073 System.out.println(
"Got: " + raw_list);
00074
00075
00076 StringTokenizer tokens =
new StringTokenizer(raw_list, delimiter);
00077
00078 String tok;
00079
while(tokens.hasMoreTokens()) {
00080 tok = tokens.nextToken();
00081
00082 channels.addElement(tok);
00083 }
00084
00085 result =
new String[channels.size()];
00086
00087
for (
int i = 0; i < channels.size(); i++)
00088 result[i] = (String) channels.elementAt(i);
00089
00090
return(result);
00091 }
00092
00100
private void subUnsub(String channel,
boolean subscribe)
00101
throws IOException
00102 {
00103 String[] commands = {
"open-port",
"close-port"};
00104 String[] responses = {
"Streaming",
"Stopping"};
00105 String[] stdout = {
"Subscribing to",
"Unsubscribing from"};
00106
int cmd_idx;
00107
00108
if(subscribe)
00109 cmd_idx = 0;
00110
else
00111 cmd_idx = 1;
00112
00113
if(!connected) {
00114
throw new IOException(
"DAQ not connected");
00115 }
00116
00117
00118
00119 wr.write(commands[cmd_idx] +
" " + channel +
"\n");
00120 wr.flush();
00121
00122
00123
00124 String response = rd.readLine();
00125
00126
00127
00128
00129
if(response.startsWith(responses[cmd_idx])) {
00130
return;
00131 }
else {
00132
throw new IOException(
"Response Error on channel " + channel +
00133
" :" + response);
00134 }
00135 }
00136
00149 public void subscribe(String channel)
00150
throws IOException
00151 {
00152 subUnsub(channel,
true);
00153 }
00154
00166 public void unsubscribe(String channel)
00167
throws IOException
00168 {
00169 subUnsub(channel,
false);
00170 }
00171 }