From 7ba589febaac5183d3f882651318f35a365e24cb Mon Sep 17 00:00:00 2001 From: Simon Ebner Date: Wed, 22 Jan 2014 13:29:07 +0100 Subject: [PATCH] FDA-105 FDAQ is now configurable --- ch.psi.fda.fdaq/Readme.md | 11 ++++ .../ch/psi/fda/fdaq/FdaqConfiguration.java | 62 +++++++++++++++++++ .../main/java/ch/psi/fda/fdaq/FdaqMain.java | 11 +++- .../java/ch/psi/fda/fdaq/FdaqService.java | 21 ++----- 4 files changed, 89 insertions(+), 16 deletions(-) create mode 100644 ch.psi.fda.fdaq/src/main/java/ch/psi/fda/fdaq/FdaqConfiguration.java diff --git a/ch.psi.fda.fdaq/Readme.md b/ch.psi.fda.fdaq/Readme.md index 9a64911..6e9a881 100644 --- a/ch.psi.fda.fdaq/Readme.md +++ b/ch.psi.fda.fdaq/Readme.md @@ -25,6 +25,17 @@ This will take the default connections settings: * Kill Port: 2234 * Number Of Elements: Integer.MAX_VALUE/2 +If you need to specify different settings create a property files with following content: + +``` +ch.psi.fda.fdaq.hostname=myhost +ch.psi.fda.fdaq.port=1234 +ch.psi.fda.fdaq.killPort=4321 +``` + +Use `-Dch.psi.fda.fdaq.config=` to use this property file as base configuration. + + # Development A standalone jar can be build via `mvn clean compile assembly:single`. diff --git a/ch.psi.fda.fdaq/src/main/java/ch/psi/fda/fdaq/FdaqConfiguration.java b/ch.psi.fda.fdaq/src/main/java/ch/psi/fda/fdaq/FdaqConfiguration.java new file mode 100644 index 0000000..7d3f5e3 --- /dev/null +++ b/ch.psi.fda.fdaq/src/main/java/ch/psi/fda/fdaq/FdaqConfiguration.java @@ -0,0 +1,62 @@ +/** + * + * Copyright 2014 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.fda.fdaq; + +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.util.Properties; + +public class FdaqConfiguration { + private String hostname = "mchip015.psi.ch"; + private int port = 2233; + private int killPort = 2234; + + public void loadFile(File file) { + Properties properties = new Properties(); + try { + properties.load(new FileReader(file)); + } catch (IOException e) { + throw new RuntimeException("Cannot read file "+file, e); + } + + hostname = properties.getProperty(FdaqConfiguration.class.getPackage().getName()+".hostname", hostname); + port = Integer.parseInt(properties.getProperty(FdaqConfiguration.class.getPackage().getName()+".port", port+"")); + killPort = Integer.parseInt(properties.getProperty(FdaqConfiguration.class.getPackage().getName()+".killPort", killPort+"")); + } + + 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; + } +} diff --git a/ch.psi.fda.fdaq/src/main/java/ch/psi/fda/fdaq/FdaqMain.java b/ch.psi.fda.fdaq/src/main/java/ch/psi/fda/fdaq/FdaqMain.java index d4ae074..694e624 100644 --- a/ch.psi.fda.fdaq/src/main/java/ch/psi/fda/fdaq/FdaqMain.java +++ b/ch.psi.fda.fdaq/src/main/java/ch/psi/fda/fdaq/FdaqMain.java @@ -31,6 +31,8 @@ import com.google.common.eventbus.EventBus; @SuppressWarnings("restriction") public class FdaqMain { + public final static String FDAQ_CONFIG = "ch.psi.fda.fdaq.config"; + public static void main(String[] args) { if(args.length != 1){ @@ -40,9 +42,16 @@ public class FdaqMain { File file = new File(args[1]); + FdaqConfiguration configuration = new FdaqConfiguration(); + String config = System.getProperty(FDAQ_CONFIG); + + if(config != null){ + configuration.loadFile(new File(config)); + } + EventBus bus = new AsyncEventBus(Executors.newSingleThreadExecutor()); - final FdaqService fdaq = new FdaqService(bus); + final FdaqService fdaq = new FdaqService(bus, configuration); Signal.handle(new Signal("INT"), new SignalHandler() { int count = 0; diff --git a/ch.psi.fda.fdaq/src/main/java/ch/psi/fda/fdaq/FdaqService.java b/ch.psi.fda.fdaq/src/main/java/ch/psi/fda/fdaq/FdaqService.java index 5538ebd..81394b4 100644 --- a/ch.psi.fda.fdaq/src/main/java/ch/psi/fda/fdaq/FdaqService.java +++ b/ch.psi.fda.fdaq/src/main/java/ch/psi/fda/fdaq/FdaqService.java @@ -48,21 +48,12 @@ public class FdaqService { private final EventBus bus; - private String hostname = "mchip015.psi.ch"; - private int port = 2233; - private int killPort = 2234; - private int numberOfElements = Integer.MAX_VALUE; + private FdaqConfiguration configuration; + private final int numberOfElements = Integer.MAX_VALUE/2; - public FdaqService(EventBus bus){ + public FdaqService(EventBus bus, FdaqConfiguration configuration){ 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; + this.configuration = configuration; } /** @@ -84,7 +75,7 @@ public class FdaqService { stopAcquisition = false; - echoSocket = new Socket(hostname, port); + echoSocket = new Socket(configuration.getHostname(), configuration.getPort()); out = new DataOutputStream(echoSocket.getOutputStream()); in = new DataInputStream(echoSocket.getInputStream()); @@ -178,7 +169,7 @@ public class FdaqService { try { running=false; stopAcquisition = true; - Socket echoSocket = new Socket(hostname, killPort); + Socket echoSocket = new Socket(configuration.getHostname(), configuration.getKillPort()); DataOutputStream out = new DataOutputStream(echoSocket.getOutputStream()); ByteBuffer bytebuffer = ByteBuffer.allocate(1 * 4); // 2