Cleaned up code and simplified

This commit is contained in:
2013-12-17 16:27:56 +01:00
parent e5e2698d7e
commit c078fb93c2
10 changed files with 217 additions and 286 deletions
+5 -1
View File
@@ -27,6 +27,10 @@
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
@@ -0,0 +1,3 @@
eclipse.preferences.version=1
encoding//src/main/java=UTF-8
encoding//src/test/java=UTF-8
@@ -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
+12 -10
View File
@@ -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:
<table>
<tr><th>Property </th><th> Default</th></tr>
<tr><td>ch.psi.fdaq.hostname </td><td> mchip015.psi.ch</td></tr>
<tr><td>port </td><td> 2233</td></tr>
<tr><td>killPort </td><td> 2234</td></tr>
<tr><td>filePrefix </td><td> ${yyyy_MM}/${yyyyMMdd}/${yyyyMMddHHmmss}_${name}/${yyyyMMddHHmm}_ </td></tr>
</table>
To acquire data into a file use:
```
fdaq <file>
```
This will take the default connections settings:
* Hostname: mchip015.psi.ch
* Port: 2233
* Kill Port: 2234
* Number Of Elements: Integer.MAX_VALUE/2
+22 -13
View File
@@ -2,19 +2,28 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>ch.psi</groupId>
<artifactId>ch.psi.fdaq</artifactId>
<artifactId>ch.psi.fda.fdaq</artifactId>
<version>0.0.1-SNAPSHOT</version>
<distributionManagement>
<snapshotRepository>
<id>i.snapshots</id>
<name>Artifactory Snapshots</name>
<url>http://yoke.psi.ch/artifactory/libs-snapshots-local</url>
</snapshotRepository>
<repository>
<id>i.releases</id>
<name>Atrifactory Releases</name>
<url>http://yoke.psi.ch/artifactory/libs-releases-local</url>
</repository>
</distributionManagement>
<dependencies>
<dependency>
<groupId>ch.psi</groupId>
<artifactId>ch.psi.fda.core</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<encoding>UTF-8</encoding>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
@@ -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 <http://www.gnu.org/licenses/>.
*
*/
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;
}
}
@@ -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 <http://www.gnu.org/licenses/>.
*
*/
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 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();
}
}
@@ -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> 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);
}
}
}