From ef1b318017016b8d844db4e950ff98b8e7a685fd Mon Sep 17 00:00:00 2001 From: Simon Ebner Date: Wed, 31 Jul 2013 13:03:37 +0200 Subject: [PATCH] Latest ideas and tests for FDA NG --- ch.psi.fda/pom.xml | 26 ++++ .../aq/ng/AcquisitionEngineNGResource.java | 73 +++++++++++ .../main/java/ch/psi/fda/aq/ng/LManager.java | 36 ++++++ .../main/java/ch/psi/fda/aq/ng/NGServer.java | 119 ++++++++++++++++++ .../java/ch/psi/fda/aq/ng/ResourceBinder.java | 31 +++++ .../fda/aq/ng/AcquisitionEngineNGTest.java | 8 +- 6 files changed, 289 insertions(+), 4 deletions(-) create mode 100644 ch.psi.fda/src/main/java/ch/psi/fda/aq/ng/AcquisitionEngineNGResource.java create mode 100644 ch.psi.fda/src/main/java/ch/psi/fda/aq/ng/LManager.java create mode 100644 ch.psi.fda/src/main/java/ch/psi/fda/aq/ng/NGServer.java create mode 100644 ch.psi.fda/src/main/java/ch/psi/fda/aq/ng/ResourceBinder.java diff --git a/ch.psi.fda/pom.xml b/ch.psi.fda/pom.xml index 11b2e01..b7a7982 100644 --- a/ch.psi.fda/pom.xml +++ b/ch.psi.fda/pom.xml @@ -78,6 +78,32 @@ junit 4.11 + + + + org.glassfish.jersey.containers + jersey-container-grizzly2-http + 2.0 + + + + org.glassfish.jersey.media + jersey-media-sse + 2.0 + + + + org.glassfish.jersey.media + jersey-media-json-jackson + 2.0 + + + + commons-cli + commons-cli + 1.2 + + diff --git a/ch.psi.fda/src/main/java/ch/psi/fda/aq/ng/AcquisitionEngineNGResource.java b/ch.psi.fda/src/main/java/ch/psi/fda/aq/ng/AcquisitionEngineNGResource.java new file mode 100644 index 0000000..cbe503b --- /dev/null +++ b/ch.psi.fda/src/main/java/ch/psi/fda/aq/ng/AcquisitionEngineNGResource.java @@ -0,0 +1,73 @@ +/** + * + * 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.aq.ng; + +import java.util.HashMap; +import java.util.Map; + +import javax.inject.Inject; +import javax.ws.rs.GET; +import javax.ws.rs.NotFoundException; +import javax.ws.rs.POST; +import javax.ws.rs.PUT; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; + +/** + * @author ebner + * + */ +@Path("/") +public class AcquisitionEngineNGResource { + + @Inject + private AcquisitionEngineNG engine; + + @Inject + private LManager lmanager; + + @PUT + @Path("logic") + public void execute(String logic){ + Map map = new HashMap<>(); + engine.execute(map, logic); + } + + @PUT + @Path("logic/{id}") + public void setLogic(@PathParam("id") String id, String logic){ + lmanager.getLogics().put(id, logic); + } + + @GET + @Path("logic/{id}") + public String getLogic(@PathParam("id") String id){ + String logic = lmanager.getLogics().get(id); + if(logic==null){ + throw new NotFoundException(); + } + return logic; + } + + @POST + @Path("logic/{id}") + public void executeLogic(@PathParam("id") String id, String logic){ + lmanager.getLogics().put(id, logic); + } +} diff --git a/ch.psi.fda/src/main/java/ch/psi/fda/aq/ng/LManager.java b/ch.psi.fda/src/main/java/ch/psi/fda/aq/ng/LManager.java new file mode 100644 index 0000000..f2dc445 --- /dev/null +++ b/ch.psi.fda/src/main/java/ch/psi/fda/aq/ng/LManager.java @@ -0,0 +1,36 @@ +/** + * + * 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.aq.ng; + +import java.util.HashMap; +import java.util.Map; + +/** + * Logic Manager + * Basically a map holding id->logic pairs + * + * also holds default resource profiles for logics (rprofiles) + */ +public class LManager { + private final Map logics = new HashMap<>(); + + public Map getLogics(){ + return logics; + } +} diff --git a/ch.psi.fda/src/main/java/ch/psi/fda/aq/ng/NGServer.java b/ch.psi.fda/src/main/java/ch/psi/fda/aq/ng/NGServer.java new file mode 100644 index 0000000..84329bf --- /dev/null +++ b/ch.psi.fda/src/main/java/ch/psi/fda/aq/ng/NGServer.java @@ -0,0 +1,119 @@ +/** + * + * 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.aq.ng; + +import java.net.InetAddress; +import java.net.URI; +import java.net.UnknownHostException; +import java.util.concurrent.CountDownLatch; +import java.util.logging.Logger; + +import javax.ws.rs.core.UriBuilder; +import javax.ws.rs.core.UriBuilderException; + +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.grizzly.http.server.HttpServer; +import org.glassfish.grizzly.http.server.StaticHttpHandler; +import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory; +import org.glassfish.jersey.jackson.JacksonFeature; +import org.glassfish.jersey.media.sse.SseFeature; +import org.glassfish.jersey.server.ResourceConfig; + +import sun.misc.Signal; +import sun.misc.SignalHandler; + +/** + * @author ebner + * + */ +@SuppressWarnings("restriction") +public class NGServer { + + private static final Logger logger = Logger.getLogger(NGServer.class.getName()); + + /** + * @param args + * @throws ParseException + * @throws UnknownHostException + * @throws UriBuilderException + * @throws IllegalArgumentException + */ + public static void main(String[] args) throws ParseException, IllegalArgumentException, UriBuilderException, UnknownHostException { + // 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("dae", options); // definition dae - http://www.urbandictionary.com/define.php?term=dae + return; + } + + URI baseUri = UriBuilder.fromUri("http://" + InetAddress.getLocalHost().getHostName() + "/").port(port).build(); + + // final ResourceConfig resourceConfig = new + // ResourceConfig(ServerSentEventsResource.class, SseFeature.class); + ResourceConfig resourceConfig = new ResourceConfig(SseFeature.class, JacksonFeature.class); + resourceConfig.packages(AcquisitionEngineNGResource.class.getPackage().getName()); + resourceConfig.register(new ResourceBinder()); + HttpServer server = GrizzlyHttpServerFactory.createHttpServer(baseUri, resourceConfig); + + // Static content + String home = System.getenv("DAE_BASE"); + if (home == null) { + home = "src/main/assembly"; + } + home = home + "/www"; + server.getServerConfiguration().addHttpHandler(new StaticHttpHandler(home), "/static"); + + logger.info("dae 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) { + latch.countDown(); + } + }); + + // Wait for termination, i.e. wait for ctrl+c + try { + latch.await(); + } catch (InterruptedException e) { + } + + server.stop(); + } + +} diff --git a/ch.psi.fda/src/main/java/ch/psi/fda/aq/ng/ResourceBinder.java b/ch.psi.fda/src/main/java/ch/psi/fda/aq/ng/ResourceBinder.java new file mode 100644 index 0000000..17b849d --- /dev/null +++ b/ch.psi.fda/src/main/java/ch/psi/fda/aq/ng/ResourceBinder.java @@ -0,0 +1,31 @@ +package ch.psi.fda.aq.ng; + +import org.glassfish.hk2.utilities.binding.AbstractBinder; +//import org.glassfish.jersey.media.sse.SseBroadcaster; + +//import ch.psi.da.hub.model.Keystore; +import ch.psi.jcae.impl.DefaultChannelService; + +public class ResourceBinder extends AbstractBinder { + + @Override + protected void configure() { + // request scope binding +// bind(MyInjectablePerRequest.class).to(MyInjectablePerRequest.class).in(RequestScope.class); + // singleton binding +// bind(MyInjectableSingleton.class).in(Singleton.class); + // singleton instance binding + +// KeystoreSerializer serializer = new KeystoreSerializer(); +// Keystore keystore = serializer.deserialize(); + +// bind(new SseBroadcaster()).to(SseBroadcaster.class); +// bind(serializer).to(KeystoreSerializer.class); +// bind(new KeystoreBroadcaster()).to(KeystoreBroadcaster.class); +// bind(keystore).to(Keystore.class); + + bind(new AcquisitionEngineNG(new DefaultChannelService())).to(AcquisitionEngineNG.class); + bind(new LManager()).to(LManager.class); + } + +} \ No newline at end of file diff --git a/ch.psi.fda/src/test/java/ch/psi/fda/aq/ng/AcquisitionEngineNGTest.java b/ch.psi.fda/src/test/java/ch/psi/fda/aq/ng/AcquisitionEngineNGTest.java index deeab3b..3f4fdd5 100644 --- a/ch.psi.fda/src/test/java/ch/psi/fda/aq/ng/AcquisitionEngineNGTest.java +++ b/ch.psi.fda/src/test/java/ch/psi/fda/aq/ng/AcquisitionEngineNGTest.java @@ -184,11 +184,11 @@ public class AcquisitionEngineNGTest { public void testCustomScript() { Map rdescriptors = new HashMap<>(); rdescriptors.put("x", new ChannelDescriptor<>(Double.class, "MTEST-HW3:MOT1", false)); - rdescriptors.put("xr", new ChannelDescriptor<>(Double.class, "MTEST-HW3:MOT2.RBV", false)); + rdescriptors.put("xr", new ChannelDescriptor<>(Double.class, "MTEST-HW3:MOT1.RBV", false)); String script = "for i in range(1,5,1):\n"+ - " x.setValueAsync(float(i))\n"+ -// " print xr.getValue()\n"; - " print 'done'\n"; + " x.setValueNoWait(float(i))\n"+ + " print xr.getValue()\n"; +// " print 'done'\n"; engine.execute(rdescriptors, script); }