Created first version of Cdump REST service
This commit is contained in:
2014-01-09 09:10:25 +01:00
parent 3e841c7596
commit fafea6dc69
4 changed files with 214 additions and 0 deletions
@@ -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 <http://www.gnu.org/licenses/>.
*
*/
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;
}
}
@@ -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);
}
}
@@ -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 <http://www.gnu.org/licenses/>.
*
*/
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;
}
}
@@ -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 <http://www.gnu.org/licenses/>.
*
*/
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();
}
}