Skip to content

Commit

Permalink
use an executor instead
Browse files Browse the repository at this point in the history
  • Loading branch information
samypr100 committed Sep 1, 2024
1 parent 9b9fd14 commit 6f52c96
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 54 deletions.
2 changes: 1 addition & 1 deletion src/main/java/edu/jhuapl/trinity/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ public void start(Stage stage) throws IOException {
if(enableHttp) {
reh.startHttpService();
}

setupMissionTimer(scene);
//add helper tools and overlays
circleSpinner = new CircleProgressIndicator();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,58 +25,76 @@
import edu.jhuapl.trinity.messages.TrinityHttpServer;
import javafx.application.Platform;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;

import javafx.scene.Scene;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.logging.Logger;

/**
* @author Sean Phillips
*/
public class RestEventHandler implements EventHandler<RestEvent> {
Scene scene;
TrinityHttpServer trinityHttpServer = null;
Thread serverThread = null;


private static final Logger LOGGER = Logger.getLogger(RestEventHandler.class.getName());

private final Scene scene;
private ExecutorService executor;
private Future<?> serverFuture;


public RestEventHandler(Scene scene) {
this.scene = scene;
this.executor = Executors.newSingleThreadExecutor();
}

public void startHttpService() {
if(null == trinityHttpServer && null == serverThread) {
System.out.println("Creating Trinity HTTP Server...");
trinityHttpServer = new TrinityHttpServer(scene);
serverThread = new Thread(trinityHttpServer, "Trinity HTTP Server");
serverThread.setDaemon(true);
serverThread.start();
Platform.runLater(() -> {
scene.getRoot().fireEvent(
new CommandTerminalEvent("Creating Trinity HTTP Server...",
new Font("Consolas", 20), Color.GREEN));
});
if (serverFuture != null && !serverFuture.isDone()) {
LOGGER.warning("Trinity HTTP Server is already running.");
return;
}

LOGGER.info("Creating Trinity HTTP Server...");
serverFuture = executor.submit(new TrinityHttpServer(scene));

Platform.runLater(() -> {
scene.getRoot().fireEvent(
new CommandTerminalEvent("Creating Trinity HTTP Server...",
new Font("Consolas", 20), Color.GREEN));
});
}

public void stopHttpService() {
if(null != trinityHttpServer && null != serverThread) {
Platform.runLater(() -> {
scene.getRoot().fireEvent(
new CommandTerminalEvent("Stopping Trinity HTTP Server...",
new Font("Consolas", 20), Color.YELLOW));
});
trinityHttpServer.stop(); //will kill future inside the runnable.
trinityHttpServer = null;
serverThread = null;
if (serverFuture == null || serverFuture.isDone()) {
LOGGER.warning("Trinity HTTP Server is not running.");
return;
}
Platform.runLater(() -> {
scene.getRoot().fireEvent(
new CommandTerminalEvent("Stopping Trinity HTTP Server...",
new Font("Consolas", 20), Color.YELLOW));
});

// Cancel the running task and interrupt if running
serverFuture.cancel(true);
// Disable new tasks from being submitted
executor.shutdown();
// Cancel currently executing tasks (if any)
executor.shutdownNow();

// Reset the executor service for future use
executor = Executors.newSingleThreadExecutor();
}

@Override
public void handle(RestEvent t) {
if(t.getEventType() == RestEvent.START_RESTSERVER_THREAD) {
if(null == trinityHttpServer && null == serverThread)
startHttpService();
} else if(t.getEventType() == RestEvent.TERMINATE_RESTSERVER_THREAD) {
if(null != trinityHttpServer && null != serverThread)
stopHttpService();
if (t.getEventType() == RestEvent.START_RESTSERVER_THREAD) {
startHttpService();
} else if (t.getEventType() == RestEvent.TERMINATE_RESTSERVER_THREAD) {
stopHttpService();
}
}
}
32 changes: 12 additions & 20 deletions src/main/java/edu/jhuapl/trinity/messages/TrinityHttpServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,28 +32,20 @@
public class TrinityHttpServer implements Runnable {

private static final Logger LOGGER = Logger.getLogger(TrinityHttpServer.class.getName());
private static final String HTTP_HOST = "0.0.0.0";
private static final int HTTP_PORT = 8080;
private static final String DEFAULT_HTTP_HOST = "0.0.0.0";
private static final int DEFAULT_HTTP_PORT = 8080;
private final Scene scene;
ChannelFuture future;
EventLoopGroup parentGroup;
EventLoopGroup childGroup;
ServerBootstrap bootstrap;


public TrinityHttpServer(Scene scene) {
this.scene = scene;
}
public void stop() {
future.cancel(true);
childGroup.shutdownGracefully();
parentGroup.shutdownGracefully();
}

@Override
public void run() {
parentGroup = new NioEventLoopGroup();
childGroup = new NioEventLoopGroup();
EventLoopGroup parentGroup = new NioEventLoopGroup();
EventLoopGroup childGroup = new NioEventLoopGroup();
try {
bootstrap = new ServerBootstrap()
ServerBootstrap bootstrap = new ServerBootstrap()
.group(parentGroup, childGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<SocketChannel>() {
Expand All @@ -63,11 +55,11 @@ protected void initChannel(SocketChannel ch) {
ch.pipeline().addLast(new HttpObjectAggregator(Integer.MAX_VALUE));
ch.pipeline().addLast(new TrinityServerHandler(scene));
}
});
future = bootstrap.bind(HTTP_HOST, HTTP_PORT).sync();
});
ChannelFuture future = bootstrap.bind(DEFAULT_HTTP_HOST, DEFAULT_HTTP_PORT).sync();
future.channel().closeFuture().sync();
} catch (InterruptedException ex) {
LOGGER.log(Level.SEVERE, null, ex);
LOGGER.log(Level.WARNING, "Trinity HTTP Server Stopped.");
} finally {
childGroup.shutdownGracefully();
parentGroup.shutdownGracefully();
Expand All @@ -79,7 +71,7 @@ public static class TrinityServerHandler extends SimpleChannelInboundHandler<Ful
private static final Logger LOGGER = Logger.getLogger(TrinityServerHandler.class.getName());

private final MessageProcessor processor;

public TrinityServerHandler(Scene scene) {
this.processor = new MessageProcessor(scene);
}
Expand Down Expand Up @@ -142,4 +134,4 @@ public boolean processMessage(String messageBody) {
}
}
}
}
}
2 changes: 1 addition & 1 deletion src/main/resources/edu/jhuapl/trinity/fxml/Data.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
</HBox>
<HBox alignment="CENTER_LEFT" layoutX="10.0" layoutY="169.0" spacing="10.0">
<children>
<Label prefWidth="150.0" text="REST Injection Service" />
<Label prefWidth="150.0" text="HTTP Service" />
<ToggleButton fx:id="restInjectToggleButton" minWidth="100.0" mnemonicParsing="false" onAction="#toggleRestInject" text="Enable" />
<ProgressIndicator fx:id="restProgressIndicator" prefHeight="32.0" prefWidth="32.0" style="-fx-background-color: #00000000;" />
</children>
Expand Down

0 comments on commit 6f52c96

Please sign in to comment.