00001
00009 package org.nees.daq;
00010
00011 import java.net.*;
00012 import java.io.*;
00013 import java.lang.String.*;
00014 import java.util.StringTokenizer;
00015 import java.util.Vector;
00016
00020 public class ControlPort {
00021 private BufferedReader rd;
00022 private BufferedWriter wr;
00023 private boolean connected;
00024
00030 public ControlPort(Socket socket)
00031 throws UnknownHostException, IOException
00032 {
00033 rd = new BufferedReader(new InputStreamReader(socket.getInputStream()));
00034 wr = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
00035 connected = true;
00036
00037 System.out.println("Created CP ok");
00038 }
00039
00045 public String[] getChannels()
00046 throws IOException
00047 {
00048 String list_command = "list-channels";
00049 String delimiter = ",";
00050 String raw_list = "";
00051 String[] result = new String[0];
00052 Vector channels = new Vector();
00053
00054
00055 if(!connected) {
00056 throw new IOException("DAQ Not connected.");
00057 }
00058
00059 System.out.println("Requesting DAQ channel list");
00060 wr.write(list_command + '\n');
00061 wr.flush();
00062
00063 raw_list = rd.readLine();
00064
00065 System.out.println("Got: " + raw_list);
00066
00067
00068 StringTokenizer tokens = new StringTokenizer(raw_list, delimiter);
00069
00070 String tok;
00071 while(tokens.hasMoreTokens()) {
00072 tok = tokens.nextToken();
00073
00074 channels.addElement(tok);
00075 }
00076
00077 result = new String[channels.size()];
00078
00079 for (int i = 0; i < channels.size(); i++)
00080 result[i] = (String) channels.elementAt(i);
00081
00082 return(result);
00083 }
00084
00092 private void subUnsub(String channel, boolean subscribe)
00093 throws IOException
00094 {
00095 String[] commands = {"open-port", "close-port"};
00096 String[] responses = {"Streaming", "Stopping"};
00097 String[] stdout = {"Subscribing to", "Unsubscribing from"};
00098 int cmd_idx;
00099
00100 if(subscribe)
00101 cmd_idx = 0;
00102 else
00103 cmd_idx = 1;
00104
00105 if(!connected) {
00106 throw new IOException("DAQ not connected");
00107 }
00108
00109 System.out.println(stdout[cmd_idx] + " channel " + channel);
00110
00111 wr.write(commands[cmd_idx] + " " + channel + '\n');
00112 wr.flush();
00113
00114 String response = rd.readLine();
00115
00116
00117 if(response.startsWith(responses[cmd_idx])) {
00118 return;
00119 }else {
00120 throw new IOException("Response Error on channel " + channel +
00121 " :" + response);
00122 }
00123 }
00124
00129 public void subscribe(String channel)
00130 throws IOException
00131 {
00132 subUnsub(channel, true);
00133 }
00134
00139 public void unsubscribe(String channel)
00140 throws IOException
00141 {
00142 subUnsub(channel, false);
00143 }
00144 }