From fafea6dc69019ed59e8ee1dfa029784ad4757120 Mon Sep 17 00:00:00 2001 From: Simon Ebner Date: Thu, 9 Jan 2014 09:10:25 +0100 Subject: [PATCH] FDA-103 Created first version of Cdump REST service --- .../java/ch/psi/fda/rest/CdumpEngine.java | 98 +++++++++++++++++++ .../java/ch/psi/fda/rest/ResourceBinder.java | 5 + .../ch/psi/fda/rest/model/CdumpRequest.java | 48 +++++++++ .../psi/fda/rest/services/CDUMPService.java | 63 ++++++++++++ 4 files changed, 214 insertions(+) create mode 100644 ch.psi.fda/src/main/java/ch/psi/fda/rest/CdumpEngine.java create mode 100644 ch.psi.fda/src/main/java/ch/psi/fda/rest/model/CdumpRequest.java create mode 100644 ch.psi.fda/src/main/java/ch/psi/fda/rest/services/CDUMPService.java diff --git a/ch.psi.fda/src/main/java/ch/psi/fda/rest/CdumpEngine.java b/ch.psi.fda/src/main/java/ch/psi/fda/rest/CdumpEngine.java new file mode 100644 index 0000000..6c76970 --- /dev/null +++ b/ch.psi.fda/src/main/java/ch/psi/fda/rest/CdumpEngine.java @@ -0,0 +1,98 @@ +/** + * + * 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.rest; + +import java.io.File; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + +import javax.inject.Inject; + +import ch.psi.fda.cdump.Cdump; +import ch.psi.fda.cdump.CdumpConfiguration; +import ch.psi.fda.rest.model.CdumpRequest; +import ch.psi.fda.serializer.SerializerTXT; +import ch.psi.jcae.ChannelService; + +import com.google.common.eventbus.AsyncEventBus; +import com.google.common.eventbus.EventBus; + +public class CdumpEngine { + + private Cdump cdump; + private EventBus bus; + + private ZMQDataService zmqService; + private ChannelService cservice; + private CdumpConfiguration configuration; + + private boolean stream = false; + + private ExecutorService eservice; + + + @Inject + public CdumpEngine(ChannelService cservice, ZMQDataService zmqService, CdumpConfiguration configuration){ + this.cservice = cservice; + this.zmqService = zmqService; + this.configuration = configuration; + + eservice = Executors.newSingleThreadExecutor(); + } + + + public void acquire(String trackingId, final CdumpRequest request){ + if(cdump!=null){ + throw new IllegalStateException("Cdump is already running"); + } + + bus = new AsyncEventBus(Executors.newSingleThreadExecutor()); + cdump = new Cdump(cservice,bus,configuration); + + SerializerTXT serializer = new SerializerTXT(new File(request.getFilename())); + serializer.setShowDimensionHeader(false); + bus.register(serializer); + + stream = (request.getStream()!=null && request.getStream().getIds().length>0) ; + if(stream){ + // Stream data via ZMQ + zmqService.setTrackingId(trackingId); + bus.register(zmqService); + } + + eservice.execute(new Runnable() { + @Override + public void run() { + cdump.acquire(request.getSamplingRate()); + } + }); + } + + + public void stop(){ + cdump.stop(); + cdump = null; + bus.unregister(zmqService); + } + + + public boolean isActive(){ + return cdump!=null; + } +} diff --git a/ch.psi.fda/src/main/java/ch/psi/fda/rest/ResourceBinder.java b/ch.psi.fda/src/main/java/ch/psi/fda/rest/ResourceBinder.java index 095f0ce..298d979 100644 --- a/ch.psi.fda/src/main/java/ch/psi/fda/rest/ResourceBinder.java +++ b/ch.psi.fda/src/main/java/ch/psi/fda/rest/ResourceBinder.java @@ -7,6 +7,7 @@ import javax.inject.Singleton; import org.glassfish.hk2.utilities.binding.AbstractBinder; import ch.psi.fda.aq.AcquisitionConfiguration; +import ch.psi.fda.cdump.CdumpConfiguration; import ch.psi.jcae.ChannelService; import ch.psi.jcae.impl.DefaultChannelService; @@ -16,9 +17,13 @@ public class ResourceBinder extends AbstractBinder { protected void configure() { bind(DefaultChannelService.class).to(ChannelService.class).in(Singleton.class); bind(AcquisitionConfiguration.class).to(AcquisitionConfiguration.class).in(Singleton.class); + bind(AcquisitionEngine.class).to(AcquisitionEngine.class).in(Singleton.class); bind(FdaqEngine.class).to(FdaqEngine.class).in(Singleton.class); bind(ZMQDataService.class).to(ZMQDataService.class).in(Singleton.class); + + bind(CdumpEngine.class).to(CdumpEngine.class).in(Singleton.class); + bind(CdumpConfiguration.class).to(CdumpConfiguration.class).in(Singleton.class); } } \ No newline at end of file diff --git a/ch.psi.fda/src/main/java/ch/psi/fda/rest/model/CdumpRequest.java b/ch.psi.fda/src/main/java/ch/psi/fda/rest/model/CdumpRequest.java new file mode 100644 index 0000000..a483dca --- /dev/null +++ b/ch.psi.fda/src/main/java/ch/psi/fda/rest/model/CdumpRequest.java @@ -0,0 +1,48 @@ +/** + * + * 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.rest.model; + +import javax.xml.bind.annotation.XmlRootElement; + +@XmlRootElement +public class CdumpRequest { + private String filename; + private Stream stream; + + private String samplingRate; + + public String getFilename() { + return filename; + } + public void setFilename(String filename) { + this.filename = filename; + } + public Stream getStream() { + return stream; + } + public void setStream(Stream stream) { + this.stream = stream; + } + public String getSamplingRate() { + return samplingRate; + } + public void setSamplingRate(String samplingRate) { + this.samplingRate = samplingRate; + } +} diff --git a/ch.psi.fda/src/main/java/ch/psi/fda/rest/services/CDUMPService.java b/ch.psi.fda/src/main/java/ch/psi/fda/rest/services/CDUMPService.java new file mode 100644 index 0000000..455dcf1 --- /dev/null +++ b/ch.psi.fda/src/main/java/ch/psi/fda/rest/services/CDUMPService.java @@ -0,0 +1,63 @@ +/** + * + * 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.fda.rest.services; + +import javax.inject.Inject; +import javax.ws.rs.Consumes; +import javax.ws.rs.DELETE; +import javax.ws.rs.GET; +import javax.ws.rs.PUT; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.core.MediaType; + +import ch.psi.fda.rest.CdumpEngine; +import ch.psi.fda.rest.model.CdumpRequest; + +@Path("cdump") +public class CDUMPService { + + @Inject + private CdumpEngine cdumpE; + + @PUT + @Path("{trackingId}") + @Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) + public void execute(@PathParam("trackingId") String trackingId, CdumpRequest request) throws InterruptedException{ + cdumpE.acquire(trackingId, request); + } + + @DELETE + @Path("{trackingId}") + public void stop(@PathParam("trackingId") String trackingId){ + terminateAll(); + } + + @DELETE + public void terminateAll(){ + cdumpE.stop(); + } + + @GET + public boolean isActive(){ + return cdumpE.isActive(); + } + +}