diff --git a/pom.xml b/pom.xml
index b7c59a9..bf4cfd2 100644
--- a/pom.xml
+++ b/pom.xml
@@ -45,6 +45,7 @@
1.3.2
de.vogel612.testclient_javabot.Main
+ false
diff --git a/src/main/java/de/vogel612/testclient_javabot/Main.java b/src/main/java/de/vogel612/testclient_javabot/Main.java
index 1684fae..dad83f7 100644
--- a/src/main/java/de/vogel612/testclient_javabot/Main.java
+++ b/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();
@@ -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);
}
};
@@ -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() {
+
+ @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);
+ }
}
}
diff --git a/src/main/java/de/vogel612/testclient_javabot/TestProgram.java b/src/main/java/de/vogel612/testclient_javabot/TestProgram.java
deleted file mode 100644
index f90365a..0000000
--- a/src/main/java/de/vogel612/testclient_javabot/TestProgram.java
+++ /dev/null
@@ -1,100 +0,0 @@
-package de.vogel612.testclient_javabot;
-
-import java.io.IOException;
-import java.util.concurrent.Executors;
-import java.util.concurrent.ScheduledExecutorService;
-import java.util.concurrent.TimeUnit;
-import java.util.logging.Logger;
-
-import javafx.event.EventHandler;
-import javafx.stage.Stage;
-import javafx.stage.WindowEvent;
-
-import com.gmail.inverseconduit.AppContext;
-import com.gmail.inverseconduit.bot.DefaultBot;
-import com.gmail.inverseconduit.chat.ChatInterface;
-import com.gmail.inverseconduit.commands.CommandHandle;
-import com.gmail.inverseconduit.commands.sets.CoreBotCommands;
-
-import de.vogel612.testclient_javabot.client.ClientGui;
-import de.vogel612.testclient_javabot.core.TestingChatInterface;
-
-/**
- * Class responsible for making things know each other. This class is the core
- * of the testclient. It's strongly oriented towards
- * {@link com.gmail.inverseconduit.bot.Program JavaBot's Program}, and basically
- * accomplishes the same tasks, just adapted to the needs of the testclient.
- * This includes firing up the {@link DefaultBot actual Bot} in JavaBot and
- * wiring it to the {@link TestingChatInterface}. Additionally the
- * {@link ClientGui} is started here and the {@link CommandHandle Commands} are added to the bot.
- *
- * @author Vogel612<vogel612@gmx.de>
- */
-public class TestProgram {
-
- private static final Logger LOGGER = Logger.getLogger(TestProgram.class.getName());
-
- private static final ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
-
- private final DefaultBot bot;
-
- private final ChatInterface chatInterface = new TestingChatInterface();
-
- private final ClientGui gui;
-
- public TestProgram(Stage stage) {
- LOGGER.info("instantiating TestProgram class");
- AppContext.INSTANCE.add(chatInterface);
- bot = new DefaultBot(chatInterface);
- gui = new ClientGui();
- try {
- gui.init(stage);
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
-
- chatInterface.subscribe(bot);
- chatInterface.subscribe(gui);
- LOGGER.info("Basic component setup completed, beginning command glueing.");
- new CoreBotCommands(chatInterface, bot).allCommands().forEach(bot::subscribe);
- LOGGER.info("Glued Core Commands");
-
- // Used to shut down the executor when the window is closed
- stage.setOnCloseRequest(new EventHandler() {
- @Override
- public void handle(WindowEvent event) {
- dispose();
- LOGGER.info("Junior Client - Bye Bye!");
- }
- });
- }
-
- public void startup() {
- // fake the login??
- scheduleQueryingThread();
- bot.start();
- gui.start();
- LOGGER.info("Startup successfully completed");
- }
-
- private void scheduleQueryingThread() {
- executor.scheduleAtFixedRate(() -> {
- try {
- chatInterface.queryMessages();
- } catch(Exception e) {
- Logger.getAnonymousLogger().severe("Exception in querying thread: " + e.getMessage());
- e.printStackTrace();
- }
- }, 3, 1, TimeUnit.SECONDS);
- Logger.getAnonymousLogger().info("querying thread started");
- }
-
- /**
- * Shuts down the executor
- */
- public void dispose() {
- LOGGER.info("Shutting down Executor");
- executor.shutdownNow();
- }
-}
diff --git a/src/main/java/de/vogel612/testclient_javabot/client/ClientGui.java b/src/main/java/de/vogel612/testclient_javabot/client/ClientGui.java
index 95c1286..e40cb01 100644
--- a/src/main/java/de/vogel612/testclient_javabot/client/ClientGui.java
+++ b/src/main/java/de/vogel612/testclient_javabot/client/ClientGui.java
@@ -10,18 +10,19 @@
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<abhinay_agarwal@live.com>
- *
*/
public class ClientGui implements ChatWorker {
@@ -29,10 +30,9 @@ public class ClientGui implements ChatWorker {
private ChatRenderController controller;
private Stage stage;
-
+
/**
- * Loads FXML and css, creates a scene and plugs it into the stage
- *
+ * Loads FXML and css, creates a scene and plugs it into the stage
*
* @throws IOException
*/
@@ -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);
@@ -74,4 +78,4 @@ public boolean enqueueMessage(ChatMessage chatMessage) {
});
return true;
}
-}
\ No newline at end of file
+}