From d76b1d89a781fc7bdbc6e4e0a4f13d281ad83a11 Mon Sep 17 00:00:00 2001 From: Simon Ebner Date: Tue, 16 Apr 2013 08:32:41 +0200 Subject: [PATCH] Fdaq Service --- .../java/ch/psi/fdaq/FdaqConfiguration.java | 143 ++++++++++++++++++ .../main/java/ch/psi/fdaq/FdaqService.java | 136 +++++++++++++++++ 2 files changed, 279 insertions(+) create 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/FdaqService.java 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 new file mode 100644 index 0000000..ad0802f --- /dev/null +++ b/ch.psi.fdaq/src/main/java/ch/psi/fdaq/FdaqConfiguration.java @@ -0,0 +1,143 @@ +/** + * + * 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.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; + 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+"=..."); + } + + Properties properties = new Properties(); + try { + properties.load(new FileReader(config+"/config/fdaq.properties")); + } 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")); + 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/FdaqService.java b/ch.psi.fdaq/src/main/java/ch/psi/fdaq/FdaqService.java new file mode 100644 index 0000000..a2dc72e --- /dev/null +++ b/ch.psi.fdaq/src/main/java/ch/psi/fdaq/FdaqService.java @@ -0,0 +1,136 @@ +/** + * + * 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.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.logging.Level; +import java.util.logging.Logger; + +/** + * Fdaq service + * + */ +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; + + /** + * Start the acquisition + */ + public void startAcquisition(final File file) { + Thread t = new Thread(new Runnable() { + + @Override + public void run() { + try { + FdaqConfiguration config = FdaqConfiguration.getInstance(); + stopAcquisition = false; + Socket echoSocket = new Socket(config.getHostname(), config.getPort()); + DataOutputStream out = new DataOutputStream(echoSocket.getOutputStream()); + DataInputStream in = new DataInputStream(echoSocket.getInputStream()); + + int nMessages = config.getNelements(); + + // 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(); + + + // open file and write + try(PrintWriter 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(); + +// System.out.println(a + " " + b1 + " " + b2 + " " + c1 + " " + c2 + " " + d); + writer.println(a + "\t" + b1 + "\t" + b2 + "\t" + c1 + "\t" + c2 + "\t" + d); + } + } + + 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); + } + } + } + }); + t.start(); + } + + public void stopAcquistion() { + 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(); + + out.close(); + echoSocket.close(); + } catch (IOException e) { + logger.log(Level.SEVERE, "", e); + } + } + +}