hps-java/src/main/java/org/lcsim/hps/evio
diff -N EvioFileProducer.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ EvioFileProducer.java 24 Feb 2012 06:38:24 -0000 1.1
@@ -0,0 +1,282 @@
+package org.lcsim.hps.evio;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.jlab.coda.et.EtAttachment;
+import org.jlab.coda.et.EtConstants;
+import org.jlab.coda.et.EtEvent;
+import org.jlab.coda.et.EtStation;
+import org.jlab.coda.et.EtSystem;
+import org.jlab.coda.et.EtSystemOpenConfig;
+import org.jlab.coda.et.enums.Mode;
+import org.jlab.coda.jevio.EvioEvent;
+import org.jlab.coda.jevio.EvioException;
+import org.jlab.coda.jevio.EvioFile;
+
+// This is copied and modified from Carl Timmer's EvioProducer class in et 12 org.jlab.coda.et.apps package.
+public class EvioFileProducer {
+
+ //File file = null;
+ EvioFile evioFile = null;
+ ByteBuffer byteBuffer = null;
+
+ // These are all used by the wrapper method doMain.
+ List<String> evioFilePaths = new ArrayList<String>();
+ String etName = null, host = null;
+ int port = EtConstants.serverPort;
+ int group = 1;
+ int delay = 0;
+ int size = 32;
+
+ EvioFileProducer()
+ {}
+
+ /*
+ EvioFileProducer(File file) {
+ this.file = file;
+ try {
+ this.evioFile = new EvioFile(file);
+ } catch (IOException x) {
+ throw new RuntimeException(x);
+ }
+ }
+
+ public void setFile(File file)
+ {
+ this.file = file;
+ try {
+ this.evioFile = new EvioFile(file);
+ } catch (IOException x) {
+ throw new RuntimeException(x);
+ }
+ }
+ */
+
+ private EvioEvent nextEvent() {
+ try {
+ return evioFile.parseNextEvent();
+ } catch (EvioException x) {
+ throw new RuntimeException(x);
+ }
+ }
+
+ private ByteBuffer nextByteBuffer() {
+ EvioEvent nextEvent = nextEvent();
+ byteBuffer = ByteBuffer.allocate(nextEvent.getTotalBytes());
+ return byteBuffer;
+ }
+
+ private static void usage() {
+ System.out.println("\nUsage: java Producer -f <et name> -e <evio file> [-p <server port>] [-host <host>]" +
+ " [-d <delay in millisec>] [-g <group #>]\n\n" +
+ " -f ET system's name\n" +
+ " -s size in bytes for requested events\n" +
+ " -p port number for a udp broadcast\n" +
+ " -d delay in millisec between getting and putting events\n" +
+ " -g group number of new events to get\n" +
+ " -host host the ET system resides on (defaults to anywhere)\n\n" +
+ " This consumer works by making a connection to the\n" +
+ " ET system's tcp server port.\n");
+ // Kill the program after this is called.
+ System.exit(1);
+ }
+
+ public void copyToEtEvent(EtEvent event) {
+ event.getDataBuffer().put(byteBuffer);
+ }
+
+ public static void main(String[] args) {
+ (new EvioFileProducer()).doMain(args); // call wrapper method
+ }
+
+ public void doMain(String[] args) {
+ try {
+ for (int i = 0; i < args.length; i++) {
+ if (args[i].equalsIgnoreCase("-e")) {
+ evioFilePaths.add(new String(args[++i]));
+ }
+ else if (args[i].equalsIgnoreCase("-f")) {
+ etName = args[++i];
+ }
+ else if (args[i].equalsIgnoreCase("-host")) {
+ host = args[++i];
+ }
+ else if (args[i].equalsIgnoreCase("-p")) {
+ try {
+ port = Integer.parseInt(args[++i]);
+ if ((port < 1024) || (port > 65535)) {
+ System.out.println("Port number must be between 1024 and 65535.");
+ usage();
+ return;
+ }
+ }
+ catch (NumberFormatException ex) {
+ System.out.println("Did not specify a proper port number.");
+ usage();
+ return;
+ }
+ }
+ else if (args[i].equalsIgnoreCase("-s")) {
+ try {
+ size = Integer.parseInt(args[++i]);
+ if (size < 1) {
+ System.out.println("Size needs to be positive int.");
+ usage();
+ return;
+ }
+ }
+ catch (NumberFormatException ex) {
+ System.out.println("Did not specify a proper size.");
+ usage();
+ return;
+ }
+ }
+ else if (args[i].equalsIgnoreCase("-g")) {
+ try {
+ group = Integer.parseInt(args[++i]);
+ if ((group < 1) || (group > 10)) {
+ System.out.println("Group number must be between 0 and 10.");
+ usage();
+ return;
+ }
+ }
+ catch (NumberFormatException ex) {
+ System.out.println("Did not specify a proper group number.");
+ usage();
+ return;
+ }
+ }
+ else if (args[i].equalsIgnoreCase("-d")) {
+ try {
+ delay = Integer.parseInt(args[++i]);
+ if (delay < 1) {
+ System.out.println("delay must be > 0.");
+ usage();
+ return;
+ }
+ }
+ catch (NumberFormatException ex) {
+ System.out.println("Did not specify a proper delay.");
+ usage();
+ return;
+ }
+ }
+ else {
+ usage();
+ return;
+ }
+ }
+
+ List<File> evioFiles = new ArrayList<File>();
+ System.out.println("Got EVIO input files ...");
+ for (String evioFilePath : evioFilePaths) {
+ System.out.println(evioFilePath);
+ File evioFile = new File(evioFilePath);
+ if (!evioFile.exists()) {
+ System.out.println("The EVIO input file " + evioFilePath + " does not exist!");
+ throw new RuntimeException("EVIO file " + evioFilePath + " was not found.");
+ }
+ else {
+ evioFiles.add(evioFile);
+ }
+ }
+ // TODO Handle more than one file.
+ if (evioFiles.size() > 1) {
+ System.out.println("Can't handle more than one input EVIO file right now!");
+ throw new RuntimeException();
+ }
+
+ // Set EVIO input file.
+ this.evioFile = new EvioFile(evioFiles.get(0));
+
+ if (host == null) {
+ host = EtConstants.hostAnywhere;
+ /*
+ try {
+ host = InetAddress.getLocalHost().getHostName();
+ }
+ catch (UnknownHostException ex) {
+ System.out.println("Host not specified and cannot find local host name.");
+ usage();
+ return;
+ }
+ */
+ }
+
+ if (etName == null) {
+ usage();
+ return;
+ }
+
+ // make a direct connection to ET system's tcp server
+ EtSystemOpenConfig config = new EtSystemOpenConfig(etName, host, port);
+
+ // create ET system object with verbose debugging output
+ EtSystem sys = new EtSystem(config, EtConstants.debugInfo);
+ sys.open();
+
+ // get GRAND_CENTRAL station object
+ EtStation gc = sys.stationNameToObject("GRAND_CENTRAL");
+
+ // attach to grandcentral
+ EtAttachment att = sys.attach(gc);
+
+ // array of events
+ EtEvent[] mevs;
+
+ int chunk = 1, count = 0, startingVal = 0;
+ long t1, t2, totalT = 0, totalCount = 0;
+ double rate, avgRate;
+ ByteBuffer buf;
+
+ // keep track of time for event rate calculations
+ t1 = System.currentTimeMillis();
+
+ for (int i = 0; i < 50; i++) {
+ while (count < 30000L) {
+ // get array of new events
+ mevs = sys.newEvents(att, Mode.SLEEP, false, 0, chunk, size, group);
+
+ if (delay > 0) Thread.sleep(delay);
+
+ for (int j = 0; j < mevs.length; j++) {
+ buf = this.nextByteBuffer();
+ //mevs[j].getDataBuffer().put(buf);
+ this.copyToEtEvent(mevs[j]);
+ int len = buf.position();
+ mevs[j].setLength(len);
+ }
+ startingVal++;
+
+ // put events back into ET system
+ sys.putEvents(att, mevs);
+ count += mevs.length;
+
+ }
+
+ // calculate the event rate
+ t2 = System.currentTimeMillis();
+ rate = 1000.0 * ((double) count) / ((double) (t2 - t1));
+ totalCount += count;
+ totalT += t2 - t1;
+ avgRate = 1000.0 * ((double) totalCount) / totalT;
+ System.out.println("rate = " + String.format("%.3g", rate) +
+ " Hz, avg = " + String.format("%.3g", avgRate));
+ count = 0;
+ t1 = System.currentTimeMillis();
+ }
+ System.out.println("End of producing events, now close");
+ sys.close();
+ System.out.println("Closing EVIO file ...");
+ evioFile.close();
+ }
+ catch (Exception ex) {
+ System.out.println("Error using ET system as producer");
+ ex.printStackTrace();
+ }
+ }
+}