GetTimeStamps.java
00001 import java.io.IOException;
00002 import java.util.Date;
00003 import java.util.TimeZone;
00004 import java.text.SimpleDateFormat;
00005
00006 import com.rbnb.sapi.*;
00007 import com.rbnb.utility.ArgHandler;
00008
00009
00010 public class GetTimeStamps {
00011
00012 private static final String SERVER_NAME = "neestpm.mcs.anl.gov";
00013 private static final String SERVER_PORT = "3333";
00014 private static final String SINK_NAME = "GetTimeStamps";
00015
00016 private static final String REQUEST_DEFAULT = "absolute";
00017
00018 private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("MMM d, yyyy h:mm:ss.SSS aa");
00019 private static final TimeZone TZ = TimeZone.getTimeZone("GMT");
00020
00021 private static final SimpleDateFormat INPUT_FORMAT = new SimpleDateFormat("yyyy-MM-dd:hh:mm:ss.SSS");
00022
00023 static
00024 {
00025 DATE_FORMAT.setTimeZone(TZ);
00026 INPUT_FORMAT.setTimeZone(TZ);
00027 }
00028
00029 private String serverName = SERVER_NAME;
00030 private String serverPort = SERVER_PORT;
00031 private String server = serverName + ":" + serverPort;
00032 private String sinkName = SINK_NAME;
00033 private String requestPath;
00034
00035 private double start = -1.0;
00036 private double duration = 0.0;
00037 private String type;
00038
00039 public static void main(String[] args) {
00040 GetTimeStamps g = new GetTimeStamps();
00041 if (!g.setArgs(args)) return;
00042 g.exec();
00043 }
00044
00045 private void printUsage() {
00046 System.out.println("GetTimeStamps: usage is...");
00047 System.out.println("GetTimeStamps ");
00048 System.out.println("[-s Server Hostname *" + SERVER_NAME + "] ");
00049 System.out.println("[-p Server Port Number *" + SERVER_PORT + "] ");
00050 System.out.println("[-n Sink Name *" + SINK_NAME + " ]");
00051 System.out.println("-r Request Path - required");
00052 System.out.println("-a Start Time - required");
00053 System.out.println("[-d duration in seconds defaults to 0.0]");
00054 System.out.println("[-z End Time]");
00055 System.out.println("[-t type of request *" + REQUEST_DEFAULT + " ]");
00056 System.out.println("Note: times can either be yyyy-mm-dd:hh:mm:ss.nnn or");
00057 System.out.println("an arbitraty floating point number");
00058 System.out.println("Start-end takes precidence over Start-duration");
00059 }
00060
00061 public boolean setArgs(String[] args) {
00062
00063 boolean useEnd = false;
00064 double end = 0.0;
00065
00066
00067 try {
00068 ArgHandler ah=new ArgHandler(args);
00069 if (ah.checkFlag('h')) {
00070 printUsage();
00071 return false;
00072 }
00073 if (ah.checkFlag('s')) {
00074 String a=ah.getOption('s');
00075 if (a!=null) serverName=a;
00076 }
00077 if (ah.checkFlag('p')) {
00078 String a=ah.getOption('p');
00079 if (a!=null) serverPort=a;
00080 }
00081 if (ah.checkFlag('n')) {
00082 String a=ah.getOption('n');
00083 if (a!=null) sinkName=a;
00084 }
00085 if (ah.checkFlag('t')) {
00086 String a=ah.getOption('t');
00087 if (a!=null) type=a;
00088 }
00089 if (ah.checkFlag('r')) {
00090 String a=ah.getOption('r');
00091 if (a!=null) requestPath=a;
00092 }
00093 if (ah.checkFlag('d')) {
00094 String a=ah.getOption('d');
00095 if (a!=null)
00096 {
00097 try
00098 {
00099 double value = Double.parseDouble(a);
00100 duration = value;
00101 }
00102 catch (Exception ex)
00103 {
00104 System.out.println("Failed to parse duration (" + a + "): " + ex);
00105 return false;
00106 }
00107 }
00108 }
00109 if (ah.checkFlag('a')) {
00110 String a=ah.getOption('a');
00111 if (a!=null)
00112 {
00113 try
00114 {
00115 double value = getTimeOrDouble(a);
00116 start = value;
00117 }
00118 catch (Exception ex)
00119 {
00120 System.out.println("Failed to parse start time (" + a + "): " + ex);
00121 return false;
00122 }
00123 }
00124 }
00125 if (ah.checkFlag('z')) {
00126 String a=ah.getOption('z');
00127 if (a!=null)
00128 {
00129 try
00130 {
00131 double value = getTimeOrDouble(a);
00132 end = value;
00133 useEnd = true;
00134 }
00135 catch (Exception ex)
00136 {
00137 System.out.println("Failed to parse end time (" + a + "): " + ex);
00138 return false;
00139 }
00140 }
00141 }
00142 } catch (Exception e) {
00143 System.err.println("GetTimeStamps argument exception "+e.getMessage());
00144 e.printStackTrace();
00145 return false;
00146 }
00147
00148 if (requestPath == null)
00149 {
00150 System.out.println("Request path is required");
00151 printUsage();
00152 return false;
00153 }
00154
00155 if (start < 0.0)
00156 {
00157 System.out.println("Start Time is required");
00158 printUsage();
00159 return false;
00160 }
00161
00162 if (useEnd)
00163 {
00164 if (start > end)
00165 {
00166 System.out.println("End time (" + end + ") must come after start ("
00167 + start +")");
00168 return false;
00169 }
00170
00171 duration = end - start;
00172 }
00173
00174 if (! ( type.equals("absolute")
00175 || type.equals("newest")
00176 || type.equals("oldest")
00177 || type.equals("aligned")
00178 || type.equals("after")
00179 || type.equals("modified")
00180 ))
00181 {
00182 System.out.println("Invalid type = " + type);
00183 printUsage();
00184 return false;
00185 }
00186
00187 server = serverName + ":" + serverPort;
00188
00189 System.out.println("GetTimeStamps on " + server + " as " + sinkName);
00190 System.out.println(" Requesting " + requestPath);
00191 System.out.println(" Times: start = " + start + "; duration = " + duration
00192 + "; type = " + type);
00193 System.out.println(" Use GetTimeStamps -h to see optional parameters");
00194 return true;
00195 }
00196
00197 private void exec() {
00198
00199 try
00200 {
00201
00202 Sink sink=new Sink();
00203 sink.OpenRBNBConnection(server,sinkName);
00204 ChannelMap sMap = new ChannelMap();
00205 int index = sMap.Add(requestPath);
00206 sink.Request(sMap, start, duration, type);
00207 ChannelMap rMap = sink.Fetch(-1,sMap);
00208 double[] times = rMap.GetTimes(0);
00209 for (int i = 0; i < times.length; i++)
00210 {
00211 String indexString = (i<10)?"00"+i:((i<100)?"0"+i:""+i);
00212 double dtime = times[i];
00213 long m = (long)(dtime * 1000.0);
00214 String timeString = DATE_FORMAT.format(new Date(m));
00215 System.out.println(indexString + ": " + timeString + " -- " + dtime);
00216 }
00217 sink.CloseRBNBConnection();
00218 }
00219 catch (Exception e)
00220 {
00221 e.printStackTrace();
00222 }
00223 }
00224
00225 public static double getTimeOrDouble(String arg) throws Exception
00226 {
00227 double value = 0.0;
00228 boolean gotit = false;
00229 String reason = null;
00230
00231 try{
00232 Date d = INPUT_FORMAT.parse(arg);
00233 long t = d.getTime();
00234 value = ((double)t)/1000.0;
00235 gotit = true;
00236 } catch (Exception e1)
00237 {
00238 reason = e1.toString();
00239 gotit = false;
00240 }
00241
00242 if (!gotit)
00243 try {
00244 value = Double.parseDouble(arg);
00245 gotit = true;
00246 } catch (Exception e2)
00247 {
00248 reason = reason + "; " + e2.toString();
00249 gotit = false;
00250 }
00251
00252 if (!gotit)
00253 throw(new Exception("Failed to parse time " + arg
00254 + "; exception:" + reason));
00255
00256 return value;
00257
00258 }
00259
00260 }
Generated on Tue Aug 24 11:12:25 2004 for Data turbine for NEESGrid by
1.3.7