import java.text.DecimalFormat; import java.util.StringTokenizer; import java.io.*; import com.rbnb.sapi.*; public class TestTimeRefInteractive { Sink sink; Source source; String server = "localhost:3333"; String sourceName = "TestSource"; String sinkName = "TestSink"; String sourceChannel0 = "channel0"; String sourceChannel1 = "channel1"; String sourcePath0 = sourceName + "/" + sourceChannel0; String sourcePath1 = sourceName + "/" + sourceChannel1; double startedAt0, endedAt0; double startedAt1, endedAt1; String[] types = {"Absolute", "Newest", "Oldest", "Aligned", "After", "Modified", "Next", "Previous"}; public final static int OK = 0; public final static int ERROR = -1; public final static int DONE = 1; int inputStatus = OK; String statusReason = null; String inputRequest = null; double inputTime = 0.0; double inputDuration = 0.0; BufferedReader in; public static void main(String[] args){(new TestTimeRefInteractive()).exec();} private void exec() { try{ sink = new Sink(); sink.OpenRBNBConnection(server,sinkName); source = new Source(1000,"none",0); source.OpenRBNBConnection(server,sourceName); System.out.println("Connected"); // set up the ring buffer with data if (!runSupply()) { System.out.println("Failed to set up data: abort run."); return; } System.out.println("Data Ready"); //run sample queries in = new BufferedReader(new InputStreamReader(System.in)); while (true) { System.out.println(); System.out.println("--------------------------------------------"); System.out.print("Channel 0 start time = " + formatValue(startedAt0)); System.out.println(", end time = " + formatValue(endedAt0)); System.out.print("Channel 1 start time = " + formatValue(startedAt1)); System.out.println(", end time = " + formatValue(endedAt1)); System.out.println(); setValuesFromInput(); if (inputStatus == ERROR) { System.out.println("ERROR: " + statusReason); continue; } if (inputStatus == DONE) break; runQuery(inputRequest,inputTime,inputDuration); } System.out.println("Done."); } catch(Throwable t) { t.printStackTrace(); System.out.println("Early abort!"); } source.CloseRBNBConnection(); sink.CloseRBNBConnection(); } // exec /** * */ private void setValuesFromInput() { try{ inputStatus = OK; // get input line System.out.println("Query: enter referenceType, time, duration " + "(e.g. absolute, 100.0, 3.0);"); System.out.println(" types can also be entered as a zero-based index in..."); System.out.print(" " + types[0]); for (int i = 1; (i < types.length); i++) { System.out.print(", " + types[i]); } System.out.println(); System.out.println(" enter an empty line to quit"); String line = in.readLine(); if (line.trim().length() == 0) { inputStatus = DONE; return; } // tokenize StringTokenizer st = new StringTokenizer(line,", "); if (!st.hasMoreTokens()) { inputStatus = ERROR; statusReason = "missing referenceType in input"; return; } String rString = st.nextToken(); if (!st.hasMoreTokens()) { inputStatus = ERROR; statusReason = "missing time in input"; return; } String tString = st.nextToken(); if (!st.hasMoreTokens()) { inputStatus = ERROR; statusReason = "missing duration in input"; return; } String dString = st.nextToken(); // parse inputRequest = rString.trim(); try { int n = Integer.parseInt(inputRequest); if ((n > -1) && (n < types.length)) inputRequest = types[n]; else { inputStatus = ERROR; statusReason = "Time Referance index, " + n + ", " + "must be between 0 and " + (types.length - 1) + ", inclusive"; return; } } catch (Exception ignore){} inputTime = Double.parseDouble(tString.trim()); inputDuration = Double.parseDouble(dString.trim()); // validate if (inputDuration < 0.0) { inputStatus = ERROR; statusReason = "Duration must be zero or positive"; return; } boolean found = false; for (int i = 0; (i < types.length) && !found; i++) { if (inputRequest.compareToIgnoreCase(types[i]) == 0) found = true; } if (!found) { inputStatus = ERROR; statusReason = "'" + inputRequest + "' is an invalid time referance"; return; } } catch (Exception e){ inputStatus = ERROR; statusReason = e.getLocalizedMessage(); e.printStackTrace(); return; } } private boolean runSupply() { System.out.println("Starting runSupply..."); try{ startedAt0 = 100.0; startedAt1 = startedAt0 + 0.25; ChannelMap cMap = new ChannelMap(); int cMapIndex0 = cMap.Add(sourceChannel0); int cMapIndex1 = cMap.Add(sourceChannel1); double time = startedAt0; double value = 0.0, inc = 1.0; // ordinal number of sample // put out points for (int j = 0; j < 60; j++) { for (int i = 0; i < 10; i++) { double data[] = new double[1]; data[0] = value; value += inc; cMap.PutTime(time,0.0); cMap.PutDataAsFloat64(cMapIndex0,data); cMap.PutTime(time+0.25,0.0); cMap.PutDataAsFloat64(cMapIndex1,data); cMap.PutTime(time+0.75,0.0); cMap.PutDataAsFloat64(cMapIndex1,data); // Note: to test the channel order swith the above and // compare results with the out put from this order, i.e ... //cMap.PutTime(time,0.0); //cMap.PutDataAsFloat64(cMapIndex1,data); //cMap.PutTime(time+0.25,0.0); //cMap.PutDataAsFloat64(cMapIndex0,data); time += 1.0; // one fake second } source.Flush(cMap); } endedAt0 = time -= 1.0; endedAt1 = time + 0.75; } catch(Throwable t) { t.printStackTrace(); System.out.println("runSupply dies"); return false; } System.out.println("Done with runSupply."); return true; } // runSupply private void runQuery(String type, double time, double duration) { try{ System.out.println("For: time = " + time + "; duration = " + duration + "; request type = " + inputRequest); ChannelMap cMap = new ChannelMap(); int cMapIndex0 = cMap.Add(sourcePath0); int cMapIndex1 = cMap.Add(sourcePath1); sink.Request(cMap,time,duration,type); ChannelMap m = sink.Fetch(-1,cMap); int channelCount = m.NumberOfChannels(); double[] times0 = null, times1 = null; if (channelCount == 0) { System.out.println(" --> no data returned"); return; } else if (channelCount == 1) { String name = m.GetChannelList()[0]; System.out.println(" --> data on only one channel (" + name + ")"); if (name.equals(sourcePath0)) { times0 = m.GetTimes(0); } else { times1 = m.GetTimes(0); } } else { System.out.println(" --> data on both channels"); times0 = m.GetTimes(cMapIndex0); times1 = m.GetTimes(cMapIndex1); } System.out.print(" Times on Channel 0: "); if (times0 == null) System.out.print(" channel not returned"); else if (times0.length == 0) System.out.print(" no data on channel"); else for (int i = 0; i < times0.length; i++) System.out.print(formatValue(times0[i]) + " "); System.out.println(); System.out.print(" Times on Channel 1: "); if (times1 == null) System.out.print(" channel not returned"); else if (times1.length == 0) System.out.print(" no data on channel"); else for (int i = 0; i < times1.length; i++) System.out.print(formatValue(times1[i]) + " "); System.out.println(); } catch(Throwable t) { t.printStackTrace(); System.out.println("Oops: bad query?"); } } // runQuery private String formatValue(double d) { DecimalFormat myFormatter = new DecimalFormat("##0.00"); String output = myFormatter.format(d); String pad = " "; output = pad.substring(output.length()) + output; return output; } }