diff --git a/ch.psi.fda/pom.xml b/ch.psi.fda/pom.xml index 2af4d1d..d3ce916 100644 --- a/ch.psi.fda/pom.xml +++ b/ch.psi.fda/pom.xml @@ -6,6 +6,25 @@ 1.1.41 + + + org.glassfish.jersey.containers + jersey-container-grizzly2-http + 2.3.1 + + + + org.glassfish.jersey.media + jersey-media-sse + 2.3.1 + + + + org.glassfish.jersey.media + jersey-media-json-jackson + 2.3.1 + + com.google.guava guava diff --git a/ch.psi.fda/src/main/java/ch/psi/fda/aq/Acquisition.java b/ch.psi.fda/src/main/java/ch/psi/fda/aq/Acquisition.java index a15a3e5..3222c58 100644 --- a/ch.psi.fda/src/main/java/ch/psi/fda/aq/Acquisition.java +++ b/ch.psi.fda/src/main/java/ch/psi/fda/aq/Acquisition.java @@ -341,9 +341,9 @@ public class Acquisition { // Clear global variables Jython jVariableDictionary.clear(); - // Destroy the CA context - cservice.destroy(); - logger.fine("ChannelService destroyed"); +// // Destroy the CA context +// cservice.destroy(); +// logger.fine("ChannelService destroyed"); // Remove log handler if(logHandler!=null){ diff --git a/ch.psi.fda/src/main/java/ch/psi/fda/rest/AcquisitionEngine.java b/ch.psi.fda/src/main/java/ch/psi/fda/rest/AcquisitionEngine.java new file mode 100644 index 0000000..89c2b66 --- /dev/null +++ b/ch.psi.fda/src/main/java/ch/psi/fda/rest/AcquisitionEngine.java @@ -0,0 +1,74 @@ +/** + * + * 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; + +import java.util.logging.Logger; + +import com.google.common.eventbus.EventBus; + +import ch.psi.fda.aq.Acquisition; +import ch.psi.fda.model.v1.Configuration; +import ch.psi.jcae.ChannelService; + +/** + * @author ebner + * + */ +public class AcquisitionEngine { + + private static final Logger logger = Logger.getLogger(AcquisitionEngine.class.getName()); + + private Acquisition acquisition; + private ChannelService cservice; + + public AcquisitionEngine(ChannelService cservice){ + this.cservice=cservice; + } + + public void scan(Configuration configuration){ + if(acquisition!=null){ + throw new RuntimeException("There is already and acquisition running"); + } + + try{ + EventBus ebus = new EventBus(); + acquisition = new Acquisition(cservice); + acquisition.initalize(ebus, configuration); + acquisition.execute(); + } + catch(InterruptedException e){ + throw new RuntimeException(e); + } + finally{ + acquisition.destroy(); + acquisition=null; + } + } + + + public void stop(){ + if(acquisition==null){ + return; + } + + logger.info("Stop acquisition"); + acquisition.abort(); +// acquisition.destroy(); + } +} 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 new file mode 100644 index 0000000..a67216b --- /dev/null +++ b/ch.psi.fda/src/main/java/ch/psi/fda/rest/ResourceBinder.java @@ -0,0 +1,14 @@ +package ch.psi.fda.rest; + +import org.glassfish.hk2.utilities.binding.AbstractBinder; + +import ch.psi.jcae.impl.DefaultChannelService; + +public class ResourceBinder extends AbstractBinder { + + @Override + protected void configure() { + bind(new AcquisitionEngine(new DefaultChannelService())).to(AcquisitionEngine.class); + } + +} \ No newline at end of file diff --git a/ch.psi.fda/src/main/java/ch/psi/fda/rest/RestServer.java b/ch.psi.fda/src/main/java/ch/psi/fda/rest/RestServer.java new file mode 100644 index 0000000..4a3a358 --- /dev/null +++ b/ch.psi.fda/src/main/java/ch/psi/fda/rest/RestServer.java @@ -0,0 +1,98 @@ +package ch.psi.fda.rest; + +import java.io.IOException; +import java.net.InetAddress; +import java.net.URI; +import java.util.concurrent.CountDownLatch; +import java.util.logging.Logger; + +import javax.ws.rs.core.UriBuilder; + +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.GnuParser; +import org.apache.commons.cli.HelpFormatter; +import org.apache.commons.cli.Options; +import org.apache.commons.cli.ParseException; +import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory; +import org.glassfish.jersey.jackson.JacksonFeature; +import org.glassfish.jersey.server.ResourceConfig; +import org.glassfish.grizzly.http.server.HttpServer; + +import sun.misc.Signal; +import sun.misc.SignalHandler; + +@SuppressWarnings("restriction") +public class RestServer { + + private static final Logger logger = Logger.getLogger(RestServer.class.getName()); + + public static void main(String[] args) throws IOException, ParseException { + + // Option handling + int port = 8080; + + Options options = new Options(); + options.addOption("h", false, "Help"); + options.addOption("p", true, "Server port (default: "+port+")"); + + GnuParser parser = new GnuParser(); + CommandLine line = parser.parse(options, args); + + if (line.hasOption("p")) { + port = Integer.parseInt(line.getOptionValue("p")); + } + if (line.hasOption("h")) { + HelpFormatter f = new HelpFormatter(); + f.printHelp("broker", options); + return; + } + + URI baseUri = UriBuilder.fromUri("http://" + InetAddress.getLocalHost().getHostName() + "/").port(port).build(); + + +// Broker broker = createBroker(config); + + + ResourceBinder binder = new ResourceBinder(); + + ResourceConfig resourceConfig = new ResourceConfig(JacksonFeature.class); + resourceConfig.packages(RestServer.class.getPackage().getName()+".services"); // Services are located in services package + resourceConfig.register(binder); + HttpServer server = GrizzlyHttpServerFactory.createHttpServer(baseUri, resourceConfig); + + // Static content +// String home = System.getenv("BROKER_BASE"); +// if (home == null) { +// home = "src/main/assembly"; +// } +// home = home + "/www"; +// server.getServerConfiguration().addHttpHandler(new StaticHttpHandler(home), "/static"); + + logger.info("Broker started"); +// logger.info(String.format("Management interface available at %sstatic/", baseUri)); + logger.info("Use ctrl+c to stop ..."); + + // Signal handling + final CountDownLatch latch = new CountDownLatch(1); + Signal.handle(new Signal("INT"), new SignalHandler() { + public void handle(Signal sig) { + if(latch.getCount()==0){ + logger.info("Terminate broker by System.exit()"); + System.exit(1); // Terminate program after 2 ctrl+c + } + latch.countDown(); + } + }); + + // Wait for termination, i.e. wait for ctrl+c + try { + latch.await(); + } catch (InterruptedException e) { + } + + server.stop(); + +// broker.terminate(); + + } +} \ No newline at end of file diff --git a/ch.psi.fda/src/main/java/ch/psi/fda/rest/services/ScanService.java b/ch.psi.fda/src/main/java/ch/psi/fda/rest/services/ScanService.java new file mode 100644 index 0000000..9bcb493 --- /dev/null +++ b/ch.psi.fda/src/main/java/ch/psi/fda/rest/services/ScanService.java @@ -0,0 +1,49 @@ +/** + * + * 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.POST; +import javax.ws.rs.Path; +import javax.ws.rs.core.MediaType; + +import ch.psi.fda.model.v1.Configuration; +import ch.psi.fda.rest.AcquisitionEngine; + +@Path("fda") +public class ScanService { + + @Inject + private AcquisitionEngine aengine; + + @POST + @Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) +// @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) + public void execute(Configuration configuration) throws InterruptedException{ + aengine.scan(configuration); + } + + @DELETE + public void stop(){ + aengine.stop(); + } +}