-
Notifications
You must be signed in to change notification settings - Fork 4
/
Program.java
71 lines (60 loc) · 2.63 KB
/
Program.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
package com.gmail.inverseconduit.bot;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.gmail.inverseconduit.AppContext;
import com.gmail.inverseconduit.BotConfig;
import com.gmail.inverseconduit.SESite;
import com.gmail.inverseconduit.chat.ChatInterface;
import com.gmail.inverseconduit.chat.ChatWorker;
import com.gmail.inverseconduit.datatype.ChatMessage;
import com.gmail.inverseconduit.datatype.SeChatDescriptor;
/**
* Class to contain the program, to be started from main. This class is
* responsible for glueing all the components together.
*
* @author vogel612<<a href="vogel612@gmx.de">vogel612@gmx.de</a>>
*/
public class Program {
private static final Logger LOGGER = Logger.getLogger(Program.class.getName());
private static final BotConfig config = AppContext.INSTANCE.get(BotConfig.class);
private final Set<ChatWorker> bots = new HashSet<>();
private final ChatInterface chatInterface;
public static final ChatMessage POISON_PILL = new ChatMessage(null, -1, "", "", -1, "", -1);
/**
* @param chatInterface
* The ChatInterface to use as main interface to wire bots to. It is
* assumed that the ChatInterface's
* {@link ChatInterface#login(com.gmail.inverseconduit.datatype.ProviderDescriptor, com.gmail.inverseconduit.datatype.CredentialsProvider)
* login()} has been called already
* @throws IOException
* if there's a problem loading the Javadocs
*/
public Program(ChatInterface chatInterface) throws IOException {
LOGGER.log(Level.FINEST, "Instantiating Program");
this.chatInterface = chatInterface;
bots.add(DefaultBot.create(chatInterface));
bots.add(new InteractionBot(chatInterface));
bots.forEach(chatInterface::subscribe);
LOGGER.log(Level.INFO, "Basic component setup complete");
}
/**
* Here the injected chatInterface is used to join the rooms specified in
* bot.properties.
* Additionally all bots that were created on startup are started.
*/
public void startup() {
LOGGER.log(Level.INFO, "Beginning startup process");
for (Integer room : config.getRooms()) {
// FIXME: isn't always Stackoverflow
chatInterface.joinChat(new SeChatDescriptor.DescriptorBuilder(SESite.STACK_OVERFLOW).setRoom(() -> room).build());
}
bots.forEach(ChatWorker::start);
LOGGER.log(Level.FINER, "Startup completed.");
}
public Set<ChatWorker> getBots() {
return bots;
}
}