00001
00011 package org.nees.daq;
00012
00013 import java.io.IOException;
00014 import java.net.UnknownHostException;
00015 import java.net.Socket;
00016 import java.util.Hashtable;
00017 import com.rbnb.sapi.*;
00018
00019 import COM.Creare.Utility.ArgHandler;
00020 import COM.Creare.Utility.RBNBProcess;
00021
00022
00024 public class DaqToRbnb {
00025
00026 private ControlPort controlPort = null;
00027 private DataThread dataThread = null;
00028 private ChannelEntry[] channels = new ChannelEntry[0];
00029
00030 private static final String DEFAULT_DAQ_SERVER = "localhost";
00031 private static final int DEFAULT_DAQ_CONTROL_PORT = 55055;
00032 private static final int DEFAULT_DAQ_DATA_PORT = 55056;
00033
00034 private String daqServerName = DEFAULT_DAQ_SERVER;
00035 private int daqControlPort = DEFAULT_DAQ_CONTROL_PORT;
00036 private int daqDataPort = DEFAULT_DAQ_DATA_PORT;
00037
00038 private static final String DEFAULT_RBNB_SERVER = "localhost";
00039 private static final int DEFAULT_RBNB_PORT = 3333;
00040 private static final String DEFAULT_RBNB_SOURCE_NAME = "FromDAQ";
00041
00042 private String rbnbServerName = DEFAULT_RBNB_SERVER;
00043 private int rbnbServerPort = DEFAULT_RBNB_PORT;
00044 private String rbnbSourceName = DEFAULT_RBNB_SOURCE_NAME;
00045
00046 private Socket controlSocket = null;
00047 private Socket dataSocket = null;
00048
00049 private Source source;
00050
00055 public static final void main(String[] args)
00056 {
00057 try
00058 {
00059 DaqToRbnb control = new DaqToRbnb(args);
00060 control.startDaqConnections();
00061 control.connectToDaqControl();
00062 control.buildChannels();
00063 control.start();
00064 }
00065 catch (Exception e)
00066 {
00067 e.printStackTrace();
00068 }
00069 }
00070
00074 public DaqToRbnb(String[] args)
00075 {
00076 parseArgs(args);
00077 }
00078
00080 private void printUsage() {
00081 System.out.println("DaqToRbnb: usage is...");
00082 System.out.print("DaqToRbnb ");
00083 System.out.print("[-q DAQ Server *" + DEFAULT_DAQ_SERVER + "] ");
00084 System.out.print("[-c DAQ Control Port *" + DEFAULT_DAQ_CONTROL_PORT + "] ");
00085 System.out.print("[-d DAQ Control Port *" + DEFAULT_DAQ_DATA_PORT + "] ");
00086 System.out.print("[-s RBNB Server *" + DEFAULT_RBNB_SERVER + "] ");
00087 System.out.print("[-p RBNB Port *" + DEFAULT_RBNB_PORT + "] ");
00088 System.out.print("[-n RBNB Source Name *" + DEFAULT_RBNB_SOURCE_NAME + "] ");
00089 System.out.println();
00090 }
00091
00095 private void parseArgs (String[] args)
00096 {
00097 String a;
00098
00099 try {
00100 ArgHandler ah=new ArgHandler(args);
00101 if (ah.checkFlag('h')) {
00102 printUsage();
00103 RBNBProcess.exit(0);
00104 }
00105 if (ah.checkFlag('q')) {
00106 a=ah.getOption('q');
00107 if (a!=null) daqServerName=a;
00108 }
00109 if (ah.checkFlag('c')) {
00110 a=ah.getOption('c');
00111 if (a!=null) daqControlPort=Integer.parseInt(a);
00112 }
00113 if (ah.checkFlag('d')) {
00114 a=ah.getOption('d');
00115 if (a!=null) daqDataPort=Integer.parseInt(a);
00116 }
00117 if (ah.checkFlag('s')) {
00118 a=ah.getOption('s');
00119 if (a!=null) rbnbServerName=a;
00120 }
00121 if (ah.checkFlag('p')) {
00122 a=ah.getOption('p');
00123 if (a!=null) rbnbServerPort=Integer.parseInt(a);
00124 }
00125 if (ah.checkFlag('n')) {
00126 a=ah.getOption('n');
00127 if (a!=null) rbnbSourceName=a;
00128 }
00129 } catch (Exception e) {
00130 System.err.println("DaqToRbnb argument exception "+e.getMessage());
00131 e.printStackTrace();
00132 RBNBProcess.exit(0);
00133 }
00134 System.out.println("Starting DaqToRbnb with");
00135 System.out.println(" DAQ: server = " + daqServerName +
00136 "; control port = "+ daqControlPort + "; data port = " + daqDataPort );
00137 System.out.println(" RBNB: server = " + rbnbServerName +
00138 "; port = "+ rbnbServerPort + "; source name = " + rbnbSourceName );
00139 System.out.println(" Use DaqToRbnb -h to see optional parameters");
00140 }
00141
00142
00147 private void startDaqConnections()
00148 throws UnknownHostException, IOException
00149 {
00151 controlSocket = new Socket(daqServerName,daqControlPort);
00152
00156 try
00157 {
00158 Thread.sleep(1000);
00159 } catch (Exception ignore) {}
00160 System.out.println("...Continued");
00161
00162 dataSocket = new Socket(daqServerName,daqDataPort);
00163 }
00164
00168 private void connectToDaqControl()
00169 throws UnknownHostException, IOException
00170 {
00171 controlPort = null;
00172
00173 ControlPort port = null;
00174
00175
00176 port = new ControlPort(controlSocket);
00177
00178 System.out.println("Pause for one sec after ControlPort...");
00179 try
00180 {
00181 Thread.sleep(1000);
00182 } catch (Exception ignore) {}
00183 System.out.println("...Continued");
00184
00185 controlPort = port;
00186 }
00187
00192 private void buildChannels()
00193 throws IOException, SAPIException
00194 {
00195 if (controlPort == null) return;
00196
00198 dataThread = new DataThread(dataSocket);
00199
00200 source = new Source();
00201 source.OpenRBNBConnection(rbnbServerName + ":" + rbnbServerPort, rbnbSourceName);
00202
00203 String[] channelList = controlPort.getChannels();
00204
00205 channels = new ChannelEntry[channelList.length];
00206
00208 for (int i = 0; i < channelList.length; i++)
00209 {
00210 String name = channelList[i];
00211 channels[i] = new ChannelEntry(name);
00212 dataThread.addListener(name, channels[i]);
00213 }
00214
00215 }
00216
00221 private void start() throws IOException
00222 {
00223 if (controlPort == null) return;
00224
00225 if (dataThread == null) return;
00226
00227 for (int i = 0; i < channels.length; i++)
00228 {
00229 channels[i].subscribe();
00230 }
00231
00232 dataThread.start();
00233
00234 }
00235
00239 private class ChannelEntry implements DaqListener
00240 {
00241
00242 String name;
00243 ChannelMap map;
00244 boolean subscribed = false;
00245 int index;
00246
00250 ChannelEntry(String name) throws SAPIException
00251 {
00252 this.name = name;
00253 map = new ChannelMap();
00254 index = map.Add(name);
00255 System.out.println("RBNB: Connection made to sever = "
00256 + rbnbServerName + ":" + rbnbServerPort +
00257 " as source = " + rbnbSourceName +
00258 " with channel = " + name + ".");
00259 }
00260
00262 void subscribe() throws IOException
00263 {
00264 controlPort.subscribe(name);
00265 subscribed = true;
00266 }
00267
00269 void unsubscribe() throws IOException
00270 {
00271 controlPort.unsubscribe(name);
00272 subscribed = false;
00273 }
00274
00275
00276
00277
00278
00279
00280
00281 public void postData(String name, double time, double data)
00282 throws SAPIException
00283 {
00284 if (!name.equals(this.name)) return;
00285
00286 map.PutTime(time, 0.0);
00287
00288 double dataArray[] = new double[1];
00289 dataArray[0] = data;
00290 map.PutDataAsFloat64(index,dataArray);
00291
00292 source.Flush(map);
00293 }
00294 }
00295 }