スレッドの仕組みはわかりませんが、私は正しいことをしていると思います。
私はいくつかのコンピュータを管理するためにIRC botをコーディングしています。
ボットはコマンド-newを受け取ると、指定されたパラメータで別のボットを実行します。
ボットはネットワークに正しく接続しますが、最後にもう1つのコマンドを実行するコマンドが応答を停止します。ここで
ボットクラスです:
スレッドはJava IRCボットで正しく動作しません
public class IRCBot extends Thread {
private final String NETWORK;
private final int PORT;
private String nickname;
private String defaultChannel;
private String admin;
private Socket connection;
private BufferedReader input;
private BufferedWriter output;
private final String VERSION = "1.00";
public IRCBot(String NETWORK, int PORT, String nickname, String defaultChannel, String admin) {
this.NETWORK = NETWORK;
this.PORT = PORT;
this.nickname = nickname;
this.defaultChannel = defaultChannel;
this.admin = admin;
}
private void connect() throws Exception {
connection = new Socket(NETWORK, PORT);
input = new BufferedReader(new InputStreamReader(connection.getInputStream()));
output = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream()));
}
private void disconnect() throws Exception {
connection.close();
}
private void sendLoginData() throws Exception {
output.write("NICK " + nickname + "\n");
output.write("USER " + nickname + " codemonkey.com CM: " + nickname + "\n");
output.flush();
}
private void pingPong(String[] data) throws Exception {
if (data[0].equals("PING")) {
System.out.println("--> PONG " + data[1]);
output.write("PONG " + data[1] + "\n");
output.flush();
}
}
private void joinChannel(String channel) throws Exception {
output.write("JOIN " + channel + "\n");
output.flush();
}
private void sendMessage(String to, String message) throws Exception {
System.out.println("--> PRIVMSG " + to + " :" + message + "\n");
output.write("PRIVMSG " + to + " :" + message + "\n");
output.flush();
}
private void verifyMOTD(String[] data) throws Exception {
if (data.length >= 2) {
/**
* 376 is the protocol number (end of MOTD)
*/
if (data[1].equals("376")) {
joinChannel(defaultChannel);
sendMessage(defaultChannel, "wazup monkeys!");
}
}
}
private boolean isCommand(String[] data) {
if (data.length >= 4) {
if (data[1].equals("PRIVMSG")) {
String[] split = data[0].split("!");
if (split[0].substring(1).equals(admin)) {
if (data[3].substring(1, 2).equals("-")) {
return true;
}
}
}
}
return false;
}
private void verifyCommand(String[] data) throws Exception {
if (isCommand(data)) {
String from = data[2];
String command = data[3].substring(2);
switch (command) {
case "admin":
sendMessage(from, admin);
break;
case "version":
sendMessage(from, VERSION);
break;
case "avapro":
sendMessage(from, String.valueOf(Runtime.getRuntime().availableProcessors()));
break;
case "freememory":
sendMessage(from, String.valueOf(Runtime.getRuntime().freeMemory()));
break;
case "totalmemory":
sendMessage(from, String.valueOf(Runtime.getRuntime().totalMemory()));
break;
case "new":
if (data.length < 9) {
throw new WrongParametersException(from, "-new (network) (port) (nickname) (defaultChannel) (admin)");
}
IRCBot newBot = new IRCBot(data[4],Integer.parseInt(data[5]),data[6],data[7],data[8]);
Thread threadNewBot = new Thread(newBot);
threadNewBot.run();
break;
default:
sendMessage(from, "exception -> uknown function: \"" + command + "\"");
break;
}
}
}
public void run() {
try {
connect();
sendLoginData();
while (true) {
String data = null;
while ((data = input.readLine()) != null) {
System.out.println("<-- " + data);
String[] dataSplitted = data.split(" ");
pingPong(dataSplitted);
verifyMOTD(dataSplitted);
try {
verifyCommand(dataSplitted);
} catch (WrongParametersException ex) {
sendMessage(ex.from(), ex.toString());
}
}
}
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
}
そしてここメインクラス:
public class Main {
public static void main(String[] args) {
IRCBot bot = new IRCBot("irc.quakenet.org",6667,"cod3monk3y","#codemonkey","codemonkey");
Thread botThread = new Thread(bot);
botThread.run();
}
}
"新しい"コマンドにはどのようなパラメータを使用していますか? – Viruzzo