From 9256d6231a2f6b2d005de50c22eb344a583e9787 Mon Sep 17 00:00:00 2001 From: Rudi Schlatte Date: Sun, 21 Jan 2024 18:31:12 +0100 Subject: [PATCH] For local processing, send out AMPL file ... only if connected to ActiveMQ, of course. Change-Id: Id886f440f8fbe0dda1c620c5aabeeb82b17b5604 --- .../optimiser/controller/LocalExecution.java | 20 +++++++++++++++---- .../optimiser/controller/Main.java | 19 +++++++++--------- .../optimiser/controller/NebulousApp.java | 5 +++++ 3 files changed, 31 insertions(+), 13 deletions(-) diff --git a/optimiser-controller/src/main/java/eu/nebulouscloud/optimiser/controller/LocalExecution.java b/optimiser-controller/src/main/java/eu/nebulouscloud/optimiser/controller/LocalExecution.java index 1bc1b3e..9db0061 100644 --- a/optimiser-controller/src/main/java/eu/nebulouscloud/optimiser/controller/LocalExecution.java +++ b/optimiser-controller/src/main/java/eu/nebulouscloud/optimiser/controller/LocalExecution.java @@ -5,10 +5,12 @@ import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.util.concurrent.Callable; +import java.util.concurrent.CountDownLatch; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; +import eu.nebulouscloud.exn.core.Publisher; import lombok.extern.slf4j.Slf4j; import picocli.CommandLine.Command; import picocli.CommandLine.Parameters; @@ -31,17 +33,27 @@ public class LocalExecution implements Callable { @Override public Integer call() { ObjectMapper mapper = new ObjectMapper(); + CountDownLatch exn_synchronizer = new CountDownLatch(1); + ExnConnector connector = main.getActiveMQConnector(); + Publisher publisher = null; + if (connector != null) { + publisher = connector.getAmplMessagePublisher(); + connector.start(exn_synchronizer); + } JsonNode msg; try { msg = mapper.readTree(Files.readString(app_creation_msg, StandardCharsets.UTF_8)); } catch (IOException e) { log.error("Could not read an input file: ", e); return 1; - } - NebulousApp app = NebulousApp.newFromAppMessage(msg, - main.getActiveMQConnector() == null ? null : main.getActiveMQConnector().getAmplMessagePublisher()); + } + NebulousApp app = NebulousApp.newFromAppMessage(msg, publisher); + if (connector != null) { + log.info("Sending AMPL to channel {}", publisher); + app.sendAMPL(); + } System.out.println(app.generateAMPL()); - + // TODO: wait for solver reply here? return 0; } } diff --git a/optimiser-controller/src/main/java/eu/nebulouscloud/optimiser/controller/Main.java b/optimiser-controller/src/main/java/eu/nebulouscloud/optimiser/controller/Main.java index 19614ff..a9c0f2d 100644 --- a/optimiser-controller/src/main/java/eu/nebulouscloud/optimiser/controller/Main.java +++ b/optimiser-controller/src/main/java/eu/nebulouscloud/optimiser/controller/Main.java @@ -112,7 +112,10 @@ public class Main implements Callable { } /** - * Initialization code shared between main and subcommands. + * Initialization code shared between main and subcommands. Note that + * here we connect to SAL if possible, but (for now) do not start the EXN + * ActiveMQ middleware. Each main method needs to call + * `activeMQConnector.start`. */ private void init() { log.info("Beginning common startup of optimiser-controller"); @@ -123,7 +126,6 @@ public class Main implements Callable { log.error("Connection to SAL unsuccessful"); } else { log.info("Established connection to SAL"); - // FIXME: remove this once we have the exn connector NebulousApp.setSalConnector(salConnector); } } else { @@ -134,14 +136,13 @@ public class Main implements Callable { log.info("Preparing ActiveMQ connection: host={} port={}", activemq_host, activemq_port); activeMQConnector - = new ExnConnector(activemq_host, activemq_port, - activemq_user, activemq_password, - new ConnectorHandler() { - public void onReady(AtomicReference context) { - log.info("Optimiser-controller connected to ActiveMQ"); - } + = new ExnConnector(activemq_host, activemq_port, + activemq_user, activemq_password, + new ConnectorHandler() { + public void onReady(AtomicReference context) { + log.info("Optimiser-controller connected to ActiveMQ"); } - ); + }); } else { log.info("ActiveMQ login info not set, only operating locally."); } diff --git a/optimiser-controller/src/main/java/eu/nebulouscloud/optimiser/controller/NebulousApp.java b/optimiser-controller/src/main/java/eu/nebulouscloud/optimiser/controller/NebulousApp.java index 3fcffa8..9c83e4f 100644 --- a/optimiser-controller/src/main/java/eu/nebulouscloud/optimiser/controller/NebulousApp.java +++ b/optimiser-controller/src/main/java/eu/nebulouscloud/optimiser/controller/NebulousApp.java @@ -162,6 +162,7 @@ public class NebulousApp { } this.UUID = app_message.at(uuid_path).textValue(); this.app_name = app_message.at(name_path).textValue(); + log.info("New App instantiated: Name='{}', UUID='{}'", app_name, UUID); } /** @@ -306,6 +307,10 @@ public class NebulousApp { * Calculate AMPL file and send it off to the right channel. */ public void sendAMPL() { + if (ampl_message_channel == null) { + log.error("AMPL publisher not set, cannot send AMPL file"); + return; + } String ampl = generateAMPL(); ObjectNode msg = mapper.createObjectNode(); msg.put(getUUID() + ".ampl", ampl);