-
Notifications
You must be signed in to change notification settings - Fork 3
/
Main.java
134 lines (112 loc) · 4.13 KB
/
Main.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
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 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}.
*/
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();
Policy.setPolicy(ScriptSecurityPolicy.getInstance());
System.setSecurityManager(ScriptSecurityManager.getInstance());
BotConfig config = loadConfig();
AppContext.INSTANCE.add(config);
launch(Main.class);
}
private static BotConfig loadConfig() {
Properties properties = new Properties();
properties.setProperty("LOGIN-EMAIL", "");
properties.setProperty("TRIGGER", "**");
properties.setProperty("PASSWORD", "");
properties.setProperty("ROOMS", "1");
return new BotConfig(properties);
}
private static void setupLogging() {
Filter filter = new Filter() {
private final String packageName = Main.class.getPackage().getName();
@Override
public boolean isLoggable(final LogRecord record) {
// only log messages from this app
String name = record.getLoggerName();
return name != null && name.startsWith(packageName);
}
};
Logger global = Logger.getLogger("");
for (Handler handler : global.getHandlers()) {
handler.setFilter(filter);
}
}
@Override
public void start(Stage primaryStage) throws Exception {
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);
}
}
}