v0.4 stabilization

This commit is contained in:
Dmitry Isaenko 2019-01-26 00:33:52 +03:00
parent 9de9f8d36f
commit cd1e70838c
4 changed files with 43 additions and 22 deletions

View file

@ -25,7 +25,6 @@ public class PrivateMsgCommander { // T
if (cmd.length > 1) if (cmd.length > 1)
cmd[1] = cmd[1].trim(); cmd[1] = cmd[1].trim();
switch (cmd[0]){ switch (cmd[0]){
case "tell": case "tell":
if ((cmd.length == 2) && (cmd[1].split("(\\s)|(\t)+?",2).length == 2)) { if ((cmd.length == 2) && (cmd[1].split("(\\s)|(\t)+?",2).length == 2)) {

View file

@ -35,20 +35,27 @@ public class BotDriver {
case "zero": case "zero":
return new BotZeroWorker(); return new BotZeroWorker();
default: default:
System.out.println("Configuration issue: BotDriver->getWorker() can't find required driver \"" System.out.println("BotDriver->getWorker(): Configuration issue: can't find required driver \""
+serverDriver.get(serverName)[0][0] +serverDriver.get(serverName)[0][0]
+"\".Using \"ZeroWorker\"."); +"\".Using \"ZeroWorker\".");
return new BotZeroWorker(); return new BotZeroWorker();
} }
} }
return null; System.out.println("BotDriver->getWorker(): Unknown issue: Channel exists for non-existing server.\n\tUsing ZeroWorker.");
return new BotZeroWorker();
}
// If channel found that it's impossible to use defined worker from user settings and asking for use ZeroWorker
public static synchronized Worker getZeroWorker(){
return new BotZeroWorker();
} }
private static Worker validateConstancy(Worker worker, String srv, String chan){ // synchronized? private static Worker validateConstancy(Worker worker, String srv, String chan){ // synchronized?
if (worker.isConsistent()){ if (worker.isConsistent()){
return worker; return worker;
} }
else { else {
System.out.println("BotDriver: Unable to use "+worker.getClass().getSimpleName()+" for "+srv+"/"+chan+". Using ZeroWorker instead."); System.out.println("BotDriver->validateConstancy(): Unable to use "
+worker.getClass().getSimpleName()+" for "+srv+"/"+chan
+". Using ZeroWorker instead.");
return new BotZeroWorker(); return new BotZeroWorker();
} }
} }

View file

@ -54,13 +54,16 @@ public class ChanConsumer implements Runnable {
if (!trackUsers(dataStrings[0], dataStrings[1], dataStrings[2])) if (!trackUsers(dataStrings[0], dataStrings[1], dataStrings[2]))
continue; continue;
writerWorker.logAdd(dataStrings[0], dataStrings[1], dataStrings[2]);
// Send to chanel commander thread // Send to chanel commander thread
chanelCommanderPipe.println(data); chanelCommanderPipe.println(data);
chanelCommanderPipe.flush(); chanelCommanderPipe.flush();
//System.out.println("|"+dataStrings[0]+"|"+dataStrings[1]+"|"+dataStrings[2]+"|"); //System.out.println("|"+dataStrings[0]+"|"+dataStrings[1]+"|"+dataStrings[2]+"|");
//System.out.println("Thread: "+this.channelName +"\n\tArray:"+ userList); //System.out.println("Thread: "+this.channelName +"\n\tArray:"+ userList);
if (!writerWorker.logAdd(dataStrings[0], dataStrings[1], dataStrings[2])){ // Write logs, check if LogDriver consistent. If not:
this.fixLogDriverIssues(dataStrings[0], dataStrings[1], dataStrings[2]);
}
if (endThread) { if (endThread) {
reader.close(); reader.close();
chanList.get(channelName).close(); chanList.get(channelName).close();
@ -69,7 +72,7 @@ public class ChanConsumer implements Runnable {
} }
} }
} catch (java.io.IOException e){ } catch (java.io.IOException e){
System.out.println("Internal issue: thread ChanConsumer->run() caused I/O exception:\n\t"+e); // TODO: reconnect System.out.println("ChanConsumer (@"+serverName+"/"+channelName+")->run(): Internal issue in thread: caused I/O exception:\n\t"+e); // TODO: reconnect
} }
writerWorker.close(); writerWorker.close();
//Chanel commander thread's pipe should be closed //Chanel commander thread's pipe should be closed
@ -82,25 +85,25 @@ public class ChanConsumer implements Runnable {
case "PRIVMSG": // most common, we don't have to handle anything else case "PRIVMSG": // most common, we don't have to handle anything else
return true; return true;
case "JOIN": case "JOIN":
addUsers(initiatorArg); addUsers(simplifyNick(initiatorArg));
return true; return true;
case "PART": case "PART":
delUsers(initiatorArg); delUsers(simplifyNick(initiatorArg)); // nick non-simple
return true; return true;
case "QUIT": // TODO fix: use regex case "QUIT":
if (userList.contains(initiatorArg.replaceAll("!.+$", ""))) { if (userList.contains(simplifyNick(initiatorArg))) {
delUsers(initiatorArg); delUsers(simplifyNick(initiatorArg)); // nick non-simple
return true; return true;
} else } else
return false; // user quit, but he/she is not in this channel return false; // user quit, but he/she is not in this channel
case "KICK": case "KICK":
if (rejoin && nick.equals(subjectArg.substring(subjectArg.indexOf(" ") + 1, subjectArg.indexOf(" :")))) if (rejoin && nick.equals(subjectArg.replaceAll("(^.+?\\s)|(\\s.+$)", ""))) // if it's me and I have rejoin policy 'Auto-Rejoin on kick'.
StreamProvider.writeToStream(serverName, "JOIN " + channelName); StreamProvider.writeToStream(serverName, "JOIN " + channelName);
delUsers(subjectArg.substring(subjectArg.indexOf(" ") + 1, subjectArg.indexOf(" :"))); delUsers(subjectArg.replaceAll("(^.+?\\s)|(\\s.+$)", "")); // nick already simplified
return true; return true;
case "NICK": case "NICK":
if (userList.contains(initiatorArg.replaceAll("!.+$", ""))) { if (userList.contains(simplifyNick(initiatorArg))) {
swapUsers(initiatorArg, subjectArg); swapUsers(simplifyNick(initiatorArg), subjectArg);
return true; return true;
} else { } else {
return false; // user changed nick, but he/she is not in this channel return false; // user changed nick, but he/she is not in this channel
@ -111,19 +114,19 @@ public class ChanConsumer implements Runnable {
} }
private void addUsers(String user){ private void addUsers(String user){
if (!userList.contains(user.replaceAll("!.+$", ""))) if (!userList.contains(user))
userList.add(user.replaceAll("!.+$", "")); userList.add(user);
} }
private void delUsers(String user){ private void delUsers(String user){
if (user.replaceAll("!.+$", "").equals(nick)) { if (user.equals(nick)) {
endThread = true; endThread = true;
} }
userList.remove(user.replaceAll("!.+$", "")); userList.remove(user);
} }
private void swapUsers(String userNickOld, String userNickNew){ private void swapUsers(String userNickOld, String userNickNew){
userList.remove(userNickOld.replaceAll("!.+$", "")); userList.remove(userNickOld);
userList.add(userNickNew); userList.add(userNickNew);
if (userNickOld.replaceAll("!.+$", "").equals(nick)) if (userNickOld.equals(nick))
this.nick = userNickNew; this.nick = userNickNew;
} }
// Create ChanelCommander // Create ChanelCommander
@ -142,9 +145,19 @@ public class ChanConsumer implements Runnable {
return new PrintWriter(streamOut); return new PrintWriter(streamOut);
} catch (IOException e) { } catch (IOException e) {
System.out.println("Internal issue: ChanConsumer->getChanelCommander() I/O exception while initialized child objects."); // caused by Socket System.out.println("ChanConsumer (@"+serverName+"/"+channelName+")->getChanelCommander(): Internal issue: I/O exception while initialized child objects:\n\t"+e); // caused by Socket
endThread = true; endThread = true;
return null; return null;
} }
} }
private String simplifyNick(String nick){ return nick.replaceAll("!.*$",""); }
private void fixLogDriverIssues(String a, String b, String c){
System.out.println("ChanConsumer (@"+serverName+"/"+channelName+")->fixLogDriverIssues(): Some issues detected. Trying to fix...");
this.writerWorker = BotDriver.getWorker(serverName, channelName); // Reset logDriver and try using the same one
if (!writerWorker.logAdd(a,b,c)){ // Write to it what was not written (most likely) and if it's still not consistent:
this.writerWorker = BotDriver.getZeroWorker();
System.out.println("ChanConsumer (@"+serverName+"/"+channelName+")->fixLogDriverIssues(): failed to use defined LogDriver. Using ZeroWorker instead.");
}
}
} }

View file

@ -29,7 +29,9 @@ public class SystemConsumer implements Runnable{
private PrivateMsgCommander commander; private PrivateMsgCommander commander;
SystemConsumer(BufferedReader streamReader, String userNick, Map<String, PrintWriter> map, StorageFile storage) { SystemConsumer(BufferedReader streamReader, String userNick, Map<String, PrintWriter> map, StorageFile storage) {
this.writerWorker = BotDriver.getWorker(storage.getServerName(), "system"); this.writerWorker = BotDriver.getWorker(storage.getServerName(), "system");
this.nick = userNick; this.nick = userNick;
this.serverName = storage.getServerName(); this.serverName = storage.getServerName();
this.channelsMap = map; this.channelsMap = map;