From c078fb93c2287819a42ed86f7fc0e3879d8300f0 Mon Sep 17 00:00:00 2001 From: Simon Ebner Date: Tue, 17 Dec 2013 16:27:56 +0100 Subject: [PATCH] Cleaned up code and simplified --- ch.psi.fdaq/.classpath | 6 +- .../org.eclipse.core.resources.prefs | 3 + .../.settings/org.eclipse.jdt.core.prefs | 3 + ch.psi.fdaq/Readme.md | 22 +- ch.psi.fdaq/pom.xml | 35 ++- .../java/ch/psi/fdaq/FdaqConfiguration.java | 149 ----------- .../src/main/java/ch/psi/fdaq/FdaqMain.java | 51 ++++ .../main/java/ch/psi/fdaq/FdaqService.java | 234 +++++++++--------- .../java/ch/psi/fdaq/SocketClient.java | 0 .../java/ch/psi/fdaq/SocketClientStop.java | 0 10 files changed, 217 insertions(+), 286 deletions(-) create mode 100644 ch.psi.fdaq/.settings/org.eclipse.core.resources.prefs delete mode 100644 ch.psi.fdaq/src/main/java/ch/psi/fdaq/FdaqConfiguration.java create mode 100644 ch.psi.fdaq/src/main/java/ch/psi/fdaq/FdaqMain.java rename ch.psi.fdaq/src/{main => test}/java/ch/psi/fdaq/SocketClient.java (100%) rename ch.psi.fdaq/src/{main => test}/java/ch/psi/fdaq/SocketClientStop.java (100%) diff --git a/ch.psi.fdaq/.classpath b/ch.psi.fdaq/.classpath index 8dee4f3..953de0b 100644 --- a/ch.psi.fdaq/.classpath +++ b/ch.psi.fdaq/.classpath @@ -27,6 +27,10 @@ - + + + + + diff --git a/ch.psi.fdaq/.settings/org.eclipse.core.resources.prefs b/ch.psi.fdaq/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000..5b781ec --- /dev/null +++ b/ch.psi.fdaq/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,3 @@ +eclipse.preferences.version=1 +encoding//src/main/java=UTF-8 +encoding//src/test/java=UTF-8 diff --git a/ch.psi.fdaq/.settings/org.eclipse.jdt.core.prefs b/ch.psi.fdaq/.settings/org.eclipse.jdt.core.prefs index 4ede96d..ec4300d 100644 --- a/ch.psi.fdaq/.settings/org.eclipse.jdt.core.prefs +++ b/ch.psi.fdaq/.settings/org.eclipse.jdt.core.prefs @@ -1,2 +1,5 @@ eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 +org.eclipse.jdt.core.compiler.compliance=1.7 org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning +org.eclipse.jdt.core.compiler.source=1.7 diff --git a/ch.psi.fdaq/Readme.md b/ch.psi.fdaq/Readme.md index 4dec26a..efd6376 100644 --- a/ch.psi.fdaq/Readme.md +++ b/ch.psi.fdaq/Readme.md @@ -6,14 +6,16 @@ server for data retrieval. The fdaq code provides exactly 2 to functionalities, it can query on the data socket for data (specifying how many data point) and can send a reset request on the reset channel. -# Configuration -The software is configured by a properties file that is specified via `-Dch.psi.fdaq.home=fdaq.properties` . +# Usage -Following properties can be specified: - - - - - - -
Property Default
ch.psi.fdaq.hostname mchip015.psi.ch
port 2233
killPort 2234
filePrefix ${yyyy_MM}/${yyyyMMdd}/${yyyyMMddHHmmss}_${name}/${yyyyMMddHHmm}_
+To acquire data into a file use: + +``` +fdaq +``` + +This will take the default connections settings: + * Hostname: mchip015.psi.ch + * Port: 2233 + * Kill Port: 2234 + * Number Of Elements: Integer.MAX_VALUE/2 diff --git a/ch.psi.fdaq/pom.xml b/ch.psi.fdaq/pom.xml index d0b58c1..c7b4a76 100644 --- a/ch.psi.fdaq/pom.xml +++ b/ch.psi.fdaq/pom.xml @@ -2,19 +2,28 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 ch.psi - ch.psi.fdaq + ch.psi.fda.fdaq 0.0.1-SNAPSHOT - - - i.snapshots - Artifactory Snapshots - http://yoke.psi.ch/artifactory/libs-snapshots-local - - - i.releases - Atrifactory Releases - http://yoke.psi.ch/artifactory/libs-releases-local - - + + + ch.psi + ch.psi.fda.core + 0.0.1-SNAPSHOT + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + UTF-8 + 1.7 + 1.7 + + + + \ No newline at end of file diff --git a/ch.psi.fdaq/src/main/java/ch/psi/fdaq/FdaqConfiguration.java b/ch.psi.fdaq/src/main/java/ch/psi/fdaq/FdaqConfiguration.java deleted file mode 100644 index 0da28a2..0000000 --- a/ch.psi.fdaq/src/main/java/ch/psi/fdaq/FdaqConfiguration.java +++ /dev/null @@ -1,149 +0,0 @@ -/** - * - * Copyright 2010 Paul Scherrer Institute. All rights reserved. - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This code is distributed in the hope that it will be useful, - * but without any warranty; without even the implied warranty of - * merchantability or fitness for a particular purpose. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this code. If not, see . - * - */ - -package ch.psi.fdaq; - -import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileReader; -import java.io.IOException; -import java.text.SimpleDateFormat; -import java.util.Date; -import java.util.Properties; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -/** - * Fdaq configuration file - */ -public class FdaqConfiguration { - - private static final FdaqConfiguration instance = new FdaqConfiguration(); - - private String hostname = "mchip015.psi.ch"; - private int port = 2233; - private int killPort = 2234; - private int nelements = Integer.MAX_VALUE/2; - private String filePrefix = "${yyyy_MM}/${yyyyMMdd}/${yyyyMMddHHmmss}_${name}/${yyyyMMddHHmm}_"; - private String dataDirectory; - - private FdaqConfiguration(){ - loadConfiguration(); - } - - public static FdaqConfiguration getInstance(){ - return instance; - } - - private void loadConfiguration() { - String config = System.getProperty(FdaqService.APP_HOME); - - if(config == null){ - throw new RuntimeException("No configuration file specified via -D"+FdaqService.APP_HOME+"=..."); - } - - File cfile = new File(config+"/config/fdaq.properties"); - if(cfile.exists()){ - Properties properties = new Properties(); - try { - properties.load(new FileReader(cfile)); - } catch (FileNotFoundException e) { - throw new RuntimeException("Configuration file "+config+" not found", e); - - } catch (IOException e) { - throw new RuntimeException("Cannot read configuration file "+config, e); - } - - hostname = properties.getProperty(FdaqConfiguration.class.getPackage().getName()+".hostname", "mchip015.psi.ch"); - port = Integer.parseInt(properties.getProperty(FdaqConfiguration.class.getPackage().getName()+".port", "2233")); - killPort = Integer.parseInt(properties.getProperty(FdaqConfiguration.class.getPackage().getName()+".killPort", "2234")); - filePrefix = properties.getProperty(FdaqConfiguration.class.getPackage().getName()+".filePrefix","${yyyy_MM}/${yyyyMMdd}/${yyyyMMddHHmmss}_${name}/${yyyyMMddHHmm}_"); - } - dataDirectory = config+"/data"; - } - - public String getHostname() { - return hostname; - } - - public void setHostname(String hostname) { - this.hostname = hostname; - } - - public int getPort() { - return port; - } - - public void setPort(int port) { - this.port = port; - } - - public int getKillPort() { - return killPort; - } - - public void setKillPort(int killPort) { - this.killPort = killPort; - } - - public int getNelements() { - return nelements; - } - - public void setNelements(int nelements) { - this.nelements = nelements; - } - - public String getFilePrefix() { - return filePrefix; - } - - public void setFilePrefix(String filePrefix) { - this.filePrefix = filePrefix; - } - - public String getDataDirectory() { - return dataDirectory; - } - - public void setDataDirectory(String dataDirectory) { - this.dataDirectory = dataDirectory; - } - - public String replaceMacros(String string, Date date, String name){ - String newString = string; - - // Replace scan name macros - newString = newString.replaceAll("\\$\\{name\\}", name); - - - // Replace date macros - Pattern pattern = Pattern.compile("\\$\\{[a-z,A-Z,-,_,:]*\\}"); - Matcher matcher = pattern.matcher(newString); - while(matcher.find()){ - String datePattern = matcher.group(); - datePattern = datePattern.replaceAll("\\$\\{", ""); - datePattern = datePattern.replaceAll("\\}", ""); - SimpleDateFormat datef = new SimpleDateFormat(datePattern); - newString = matcher.replaceFirst(datef.format(date)); - matcher = pattern.matcher(newString); - } - return newString; - } -} diff --git a/ch.psi.fdaq/src/main/java/ch/psi/fdaq/FdaqMain.java b/ch.psi.fdaq/src/main/java/ch/psi/fdaq/FdaqMain.java new file mode 100644 index 0000000..24368bb --- /dev/null +++ b/ch.psi.fdaq/src/main/java/ch/psi/fdaq/FdaqMain.java @@ -0,0 +1,51 @@ +/** + * + * Copyright 2013 Paul Scherrer Institute. All rights reserved. + * + * This code is free software: you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation, either version 3 of the License, or (at your option) any + * later version. + * + * This code is distributed in the hope that it will be useful, but without any + * warranty; without even the implied warranty of merchantability or fitness for + * a particular purpose. See the GNU Lesser General Public License for more + * details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this code. If not, see . + * + */ +package ch.psi.fdaq; + +import java.io.File; +import java.util.concurrent.Executors; + +import ch.psi.fda.serializer.SerializerTXT; + +import com.google.common.eventbus.AsyncEventBus; +import com.google.common.eventbus.EventBus; + +/** + * + */ +public class FdaqMain { + + public static void main(String[] args) { + + if(args.length != 1){ + System.err.println("Usage: fdaq "); + } + + File file = new File(args[0]); + + EventBus bus = new AsyncEventBus(Executors.newSingleThreadExecutor()); + FdaqService fdaq = new FdaqService(bus); + + SerializerTXT serializer = new SerializerTXT(file); + bus.register(serializer); + + fdaq.acquire(); + } + +} diff --git a/ch.psi.fdaq/src/main/java/ch/psi/fdaq/FdaqService.java b/ch.psi.fdaq/src/main/java/ch/psi/fdaq/FdaqService.java index 7faf81f..5d884fc 100644 --- a/ch.psi.fdaq/src/main/java/ch/psi/fdaq/FdaqService.java +++ b/ch.psi.fdaq/src/main/java/ch/psi/fdaq/FdaqService.java @@ -18,19 +18,22 @@ */ package ch.psi.fdaq; -import java.io.BufferedWriter; import java.io.DataInputStream; import java.io.DataOutputStream; -import java.io.File; -import java.io.FileWriter; import java.io.IOException; -import java.io.PrintWriter; import java.net.Socket; import java.nio.ByteBuffer; import java.nio.ByteOrder; +import java.util.ArrayList; +import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; +import com.google.common.eventbus.EventBus; + +import ch.psi.fda.messages.DataMessage; +import ch.psi.fda.messages.Metadata; + /** * Fdaq service * @@ -39,128 +42,133 @@ public class FdaqService { private static final Logger logger = Logger.getLogger(FdaqService.class.getName()); - public final static String APP_HOME = "ch.psi.fdaq.home"; private volatile boolean stopAcquisition = false; + private final EventBus bus; + + private String hostname = "mchip015.psi.ch"; + private int port = 2233; + private int killPort = 2234; + private int numberOfElements = Integer.MAX_VALUE/2; + + public FdaqService(EventBus bus){ + this.bus = bus; + } + + public FdaqService(EventBus bus, String hostname, int port, int killPort, int numberOfElements){ + this.bus = bus; + this.hostname = hostname; + this.port = port; + this.killPort = killPort; + this.numberOfElements = numberOfElements; + } + /** - * Start the acquisition + * Acquire data from the fdaq box + * @param hostname + * @param port + * @param numberOfElements */ - public void startAcquisition(final File file) { - Thread t = new Thread(new Runnable() { - - @Override - public void run() { - - PrintWriter writer = null; - Socket echoSocket = null; - DataOutputStream out = null; - DataInputStream in = null; - - try { - FdaqConfiguration config = FdaqConfiguration.getInstance(); - stopAcquisition = false; - echoSocket = new Socket(config.getHostname(), config.getPort()); - out = new DataOutputStream(echoSocket.getOutputStream()); - in = new DataInputStream(echoSocket.getInputStream()); + public void acquire() { - int nMessages = config.getNelements(); + Socket echoSocket = null; + DataOutputStream out = null; + DataInputStream in = null; - // struct fdaqbloc_in {int fnum;int nsample;}; - ByteBuffer bytebuffer = ByteBuffer.allocate(2 * 4); // 2 times - // Integers - bytebuffer.order(ByteOrder.LITTLE_ENDIAN); - bytebuffer.putInt(26); - bytebuffer.putInt(nMessages); - out.write(bytebuffer.array()); - out.flush(); + try { + stopAcquisition = false; + echoSocket = new Socket(hostname, port); + out = new DataOutputStream(echoSocket.getOutputStream()); + in = new DataInputStream(echoSocket.getInputStream()); - - // open file and write - logger.info("Open file: "+file.getAbsolutePath()); - writer = new PrintWriter(new BufferedWriter(new FileWriter(file))); - - writer.println("# a" + "\t" + "b1" + "\t" + "b2" + "\t" + "c1" + "\t" + "c2" + "\t" + "d"); - - for (int t = 0; t < nMessages; t++) { - // struct fdaqbloc_out {int trigindex;int adc1reg;int - // adc2reg;int encoder;}; - ByteBuffer buffer = ByteBuffer.allocate(4 * 4); // 4 times - // Integers - buffer.order(ByteOrder.LITTLE_ENDIAN); - int r = in.read(buffer.array()); - if (r == -1) { - break; - } - - int a = buffer.getInt(); - int b = buffer.getInt(); - int b1 = b & 0xffff; - int b2 = (b >> 16) & 0xffff; - int c = buffer.getInt(); - int c1 = c & 0xffff; - int c2 = (c >> 16) & 0xffff; - int d = buffer.getInt(); - -// logger.info(a + " " + b1 + " " + b2 + " " + c1 + " " + c2 + " " + d); - writer.println(a + "\t" + b1 + "\t" + b2 + "\t" + c1 + "\t" + c2 + "\t" + d); - - if(t%100==0){ // flush at least every hundered messages - writer.flush(); - } - } - - writer.close(); - out.close(); - in.close(); - echoSocket.close(); - } catch (IOException e) { - if(!stopAcquisition){ // Ignore potential exceptions if stop acquisition was triggered before - logger.log(Level.SEVERE, "", e); - } - } - finally{ - try{ - writer.close(); - out.close(); - in.close(); - echoSocket.close(); - } catch (IOException e) { - // Ignore because not relevant at this stage - } + // struct fdaqbloc_in {int fnum;int nsample;}; + ByteBuffer bytebuffer = ByteBuffer.allocate(2 * 4); // 2 times + // Integers + bytebuffer.order(ByteOrder.LITTLE_ENDIAN); + bytebuffer.putInt(26); + bytebuffer.putInt(numberOfElements); + out.write(bytebuffer.array()); + out.flush(); + + final List metadata = new ArrayList<>(); + metadata.add(new Metadata("a")); + metadata.add(new Metadata("b1")); + metadata.add(new Metadata("b2")); + metadata.add(new Metadata("c1")); + metadata.add(new Metadata("c2")); + metadata.add(new Metadata("d")); + + for (int t = 0; t < numberOfElements; t++) { + // struct fdaqbloc_out {int trigindex;int adc1reg;int + // adc2reg;int encoder;}; + ByteBuffer buffer = ByteBuffer.allocate(4 * 4); // 4 times + // Integers + buffer.order(ByteOrder.LITTLE_ENDIAN); + int r = in.read(buffer.array()); + if (r == -1) { + break; } + + int a = buffer.getInt(); + int b = buffer.getInt(); + int b1 = b & 0xffff; + int b2 = (b >> 16) & 0xffff; + int c = buffer.getInt(); + int c1 = c & 0xffff; + int c2 = (c >> 16) & 0xffff; + int d = buffer.getInt(); + + DataMessage message = new DataMessage(metadata); + message.getData().add(a); + message.getData().add(b1); + message.getData().add(b2); + message.getData().add(c1); + message.getData().add(c2); + message.getData().add(d); + bus.post(message); } - }); - t.start(); + + out.close(); + in.close(); + echoSocket.close(); + } catch (IOException e) { + // Ignore potential exceptions if stop was triggered before all messages were retrieved + if (!stopAcquisition) { + throw new RuntimeException(e); + } + } finally { + try { + out.close(); + in.close(); + echoSocket.close(); + } catch (IOException e) { + // Ignore because not relevant at this stage + } + } } - public void stopAcquisition() { - Thread t = new Thread(new Runnable() { + /** + * Sending termination command to fdaq box + */ + public void stop() { + try { + stopAcquisition = true; + Socket echoSocket = new Socket(hostname, killPort); + DataOutputStream out = new DataOutputStream(echoSocket.getOutputStream()); - @Override - public void run() { - try { - stopAcquisition = true; - FdaqConfiguration c = FdaqConfiguration.getInstance(); - Socket echoSocket = new Socket(c.getHostname(), c.getKillPort()); - DataOutputStream out = new DataOutputStream(echoSocket.getOutputStream()); + ByteBuffer bytebuffer = ByteBuffer.allocate(1 * 4); // 2 + // times + // Integers + bytebuffer.order(ByteOrder.LITTLE_ENDIAN); + bytebuffer.putInt(666); + out.write(bytebuffer.array()); + out.flush(); - ByteBuffer bytebuffer = ByteBuffer.allocate(1 * 4); // 2 - // times - // Integers - bytebuffer.order(ByteOrder.LITTLE_ENDIAN); - bytebuffer.putInt(666); - out.write(bytebuffer.array()); - out.flush(); - - out.close(); - echoSocket.close(); - } catch (IOException e) { - logger.log(Level.SEVERE, "", e); - } - } - }); - t.start(); + out.close(); + echoSocket.close(); + } catch (IOException e) { + logger.log(Level.SEVERE, "", e); + } } - } diff --git a/ch.psi.fdaq/src/main/java/ch/psi/fdaq/SocketClient.java b/ch.psi.fdaq/src/test/java/ch/psi/fdaq/SocketClient.java similarity index 100% rename from ch.psi.fdaq/src/main/java/ch/psi/fdaq/SocketClient.java rename to ch.psi.fdaq/src/test/java/ch/psi/fdaq/SocketClient.java diff --git a/ch.psi.fdaq/src/main/java/ch/psi/fdaq/SocketClientStop.java b/ch.psi.fdaq/src/test/java/ch/psi/fdaq/SocketClientStop.java similarity index 100% rename from ch.psi.fdaq/src/main/java/ch/psi/fdaq/SocketClientStop.java rename to ch.psi.fdaq/src/test/java/ch/psi/fdaq/SocketClientStop.java