Skip to content

Commit

Permalink
Injected TestingChatInterface to JavaBot's Program, fixes #14
Browse files Browse the repository at this point in the history
- TestProgram is no more needed
- Main now has the responsibilities, that used to be excess in TestProgram
- Reconfigured Maven to not try and clean daemon threads to allow
proper execution over maven exec-plugin
- Made Gui shut down the JavaFX-thread when getting the PoisonPill
  • Loading branch information
Vogel612 committed Jan 31, 2015
1 parent a8c9d24 commit d27d206
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 116 deletions.
1 change: 1 addition & 0 deletions pom.xml
Expand Up @@ -45,6 +45,7 @@
<version>1.3.2</version>
<configuration>
<mainClass>de.vogel612.testclient_javabot.Main</mainClass>
<cleanupDaemonThreads>false</cleanupDaemonThreads>
</configuration>
</plugin>
</plugins>
Expand Down
83 changes: 73 additions & 10 deletions src/main/java/de/vogel612/testclient_javabot/Main.java
@@ -1,25 +1,43 @@
package de.vogel612.testclient_javabot;

import java.io.IOException;
import java.security.Policy;
import java.util.Properties;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.logging.Filter;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;

import com.gmail.inverseconduit.AppContext;
import com.gmail.inverseconduit.BotConfig;
import com.gmail.inverseconduit.bot.Program;
import com.gmail.inverseconduit.chat.ChatInterface;
import com.gmail.inverseconduit.commands.sets.CoreBotCommands;
import com.gmail.inverseconduit.security.ScriptSecurityManager;
import com.gmail.inverseconduit.security.ScriptSecurityPolicy;
import javafx.application.Application;
import javafx.stage.Stage;
import com.google.common.util.concurrent.ThreadFactoryBuilder;

import de.vogel612.testclient_javabot.client.ClientGui;
import de.vogel612.testclient_javabot.core.TestingChatInterface;

/**
* Entry point of Application. Class responsible for creating an JavaFX Application thread
* This class extends {@link Application}.
* Entry point of Application. Class responsible for creating an JavaFX
* Application thread
* This class extends {@link Application}.
*/
public final class Main extends Application {

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

public static final void main(final String[] args) {
setupLogging();

Expand Down Expand Up @@ -50,9 +68,7 @@ private static void setupLogging() {
public boolean isLoggable(final LogRecord record) {
// only log messages from this app
String name = record.getLoggerName();
return (name == null)
? false
: name.startsWith(packageName);
return name != null && name.startsWith(packageName);
}
};

Expand All @@ -64,8 +80,55 @@ public boolean isLoggable(final LogRecord record) {

@Override
public void start(Stage primaryStage) throws Exception {
TestProgram p = new TestProgram(primaryStage);
AppContext.INSTANCE.add(p);
p.startup();
ChatInterface chatInterface = new TestingChatInterface();
ClientGui gui = new ClientGui();
try {
gui.init(primaryStage);
} catch(IOException e) {
chatInterface.close();
throw new RuntimeException(e);
}
chatInterface.subscribe(gui);
gui.start();

Program program = new Program(chatInterface);

//FIXME: Remove after merge of #41, because it's no more needed
new CoreBotCommands(chatInterface, program.getBot()).allCommands().forEach(program.getBot()::subscribe);
// Used to shut down the executor when the window is closed
primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {

@Override
public void handle(WindowEvent event) {
chatInterface.getSubscriptions().forEach(worker -> {
try {
worker.enqueueMessage(Program.POISON_PILL);
} catch(InterruptedException ignore) {}
});
LOGGER.info("Junior Client - Bye Bye!");
}
});

scheduleQueryingThread(chatInterface);
program.startup();
}

private void scheduleQueryingThread(ChatInterface chatInterface) {
ThreadFactory factory =
new ThreadFactoryBuilder().setDaemon(true).setNameFormat("message-query-thread-%d").build();
Executors.newSingleThreadScheduledExecutor(factory).scheduleAtFixedRate(() -> queryMessagesFor(chatInterface),
1000, 500, TimeUnit.MILLISECONDS);
Logger.getAnonymousLogger().info("querying thread started");
}

private static void queryMessagesFor(ChatInterface chatInterface) {
try {
chatInterface.queryMessages();
} catch(RuntimeException | Error e) {
LOGGER.log(Level.SEVERE, "Runtime Exception or Error occurred:", e);
throw e;
} catch(Exception e) {
LOGGER.log(Level.WARNING, "Exception occured:", e);
}
}
}
100 changes: 0 additions & 100 deletions src/main/java/de/vogel612/testclient_javabot/TestProgram.java

This file was deleted.

16 changes: 10 additions & 6 deletions src/main/java/de/vogel612/testclient_javabot/client/ClientGui.java
Expand Up @@ -10,29 +10,29 @@
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

import com.gmail.inverseconduit.bot.Program;
import com.gmail.inverseconduit.chat.ChatWorker;
import com.gmail.inverseconduit.datatype.ChatMessage;

import de.vogel612.testclient_javabot.client.controller.ChatRenderController;

/**
* Class responsible for loading the FXML and creating an JavaFX test client scene
* Class responsible for loading the FXML and creating an JavaFX test client
* scene
* for the JavaBot.
*
* @author itachi<<a href="mailto:abhinay_agarwal@live.com"
* >abhinay_agarwal@live.com</a>>
*
*/
public class ClientGui implements ChatWorker {

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

private ChatRenderController controller;
private Stage stage;

/**
* Loads FXML and css, creates a scene and plugs it into the stage
* <br/>
* Loads FXML and css, creates a scene and plugs it into the stage <br/>
*
* @throws IOException
*/
Expand Down Expand Up @@ -65,6 +65,10 @@ public void start() {
*/
@Override
public boolean enqueueMessage(ChatMessage chatMessage) {
if (chatMessage == Program.POISON_PILL) {
Platform.exit();
}

Platform.runLater(() -> {
try {
controller.addMessage(chatMessage);
Expand All @@ -74,4 +78,4 @@ public boolean enqueueMessage(ChatMessage chatMessage) {
});
return true;
}
}
}

0 comments on commit d27d206

Please sign in to comment.