Files
ch.psi.fda.fdaq/ch.psi.fdaq/src/main/java/ch/psi/fdaq/FdaqService.java
T
2013-04-16 09:25:47 +02:00

167 lines
4.7 KiB
Java

/**
*
* 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.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() {
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());
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
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
}
}
}
});
t.start();
}
public void stopAcquisition() {
Thread t = new Thread(new Runnable() {
@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();
out.close();
echoSocket.close();
} catch (IOException e) {
logger.log(Level.SEVERE, "", e);
}
}
});
t.start();
}
}