diff options
author | Sven Gothel <[email protected]> | 2013-07-10 04:16:21 +0200 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2013-07-10 04:16:21 +0200 |
commit | 1e6aa2a522df366dea13b6dd117eaea33c048595 (patch) | |
tree | 6d5708913e43da03af3998cf41178fdf71740b5a /src | |
parent | 23f54e6cf4dac04df8158591684fec46cfd3967c (diff) |
CatOut/LogBot: Test server connection from main loop: Send PING each 1min, and act when last-timestamp is > PONG_TIMEOUT (3min)HEADmaster
- Do not reconnect automatically, since we use main-loop connection test incl. reconnect
Diffstat (limited to 'src')
-rw-r--r-- | src/org/jogamp/jabot/irc/CatOut.java | 115 | ||||
-rw-r--r-- | src/org/jogamp/jabot/irc/LogBot.java | 45 |
2 files changed, 108 insertions, 52 deletions
diff --git a/src/org/jogamp/jabot/irc/CatOut.java b/src/org/jogamp/jabot/irc/CatOut.java index 07e4ae5..5e4c9de 100644 --- a/src/org/jogamp/jabot/irc/CatOut.java +++ b/src/org/jogamp/jabot/irc/CatOut.java @@ -19,10 +19,9 @@ import java.util.Calendar; import java.util.GregorianCalendar; import java.util.Locale; -import org.jibble.pircbot.PircBot; import org.jogamp.jabot.util.TimeTool; -public class CatOut extends PircBot { +public class CatOut { private static final String HASH = "#"; @@ -43,7 +42,8 @@ public class CatOut extends PircBot { final long logrotate, logrotateStart; final String logprefix, urlprefix; final boolean htmlOut; - final File htmlHeader, htmlFooter; + final File htmlHeader, htmlFooter; + final boolean testPongTO; { String _joinMessage = "This channel is logged"; String _login=null, _nick=null, _nickPwd=null, _server=null, _channelNoHash=null; @@ -52,6 +52,7 @@ public class CatOut extends PircBot { String _logprefix="", _urlprefix=""; boolean _htmlOut = false; String _htmlHeader = null, _htmlFooter=null; + boolean _testPongTO = false; for(int i=0; i<args.length; i++) { if(args[i].equals("-joinMessage")) { i++; @@ -106,6 +107,8 @@ public class CatOut extends PircBot { cal.set(Calendar.MINUTE, mm); _logrotateStart = cal.getTimeInMillis(); } + } else if(args[i].equals("-testPongTO")) { + _testPongTO = true; } } if( null == _login || @@ -127,6 +130,7 @@ public class CatOut extends PircBot { htmlOut = _htmlOut; htmlHeader = new File(_htmlHeader); htmlFooter= new File(_htmlFooter); + testPongTO = _testPongTO; } long nextLogrotate = logrotateStart + logrotate; final Calendar logCal = new GregorianCalendar(TimeTool.getNearZuluTimeZone(), Locale.getDefault()); @@ -134,6 +138,8 @@ public class CatOut extends PircBot { if( System.currentTimeMillis() > nextLogrotate ) { nextLogrotate += logrotate; } + System.err.println("verbose : "+verbose); + System.err.println("testPongTO : "+testPongTO); System.err.println("Now : "+logCal.getTime()); logCal.setTimeInMillis(logrotateStart); System.err.println("Logrotate Start : "+logCal.getTime()); @@ -145,44 +151,69 @@ public class CatOut extends PircBot { System.err.println("Next Logrotate-2: "+logCal.getTime()); } - final LogBot bot = new LogBot(showHostname, joinMessage, htmlOut); - bot.setVerbose(verbose); - bot.setLoginAndNick(login, nick); - - LogStream logOut; - if ( 0 < logrotate ) { - logOut = new LogStream(bot, logprefix, urlprefix, server, channelNoHash, htmlHeader); - bot.setOut(logOut.out, true); - bot.setJoinMessage(joinMessage+" @ "+logOut.urlString); - bot.logNotice(bot.getJoinMessage()); - } else { - logOut = null; - } - if( null != nickPwd && nickPwd.length() > 0 ) { - bot.setPostConnectAction(new Runnable() { - public void run() { - bot.identify(nickPwd); - } - }); - } - bot.connect(server); - bot.joinChannel(HASH+channelNoHash); - if( 0 < logrotate ) { - while(true) { // forever ! - final long now = System.currentTimeMillis(); + LogBot bot = null; + LogStream logOut = null; + final long t0 = System.currentTimeMillis(); + boolean testPongTODone = false; - if( now >= nextLogrotate ) { + while(true) { // forever ! + final long now = System.currentTimeMillis(); + final long lastTS = null != bot ? bot.getLastTS() : now; + final boolean pongTO = ( now - lastTS ) >= PONG_TIMEOUT || + ( testPongTO && !testPongTODone && ( now - t0 ) >= PONG_TIMEOUT ) ; + final boolean newBOT = null == bot || pongTO; + + if( pongTO ) { + testPongTODone = testPongTO; + System.err.println("Probably lost connection, lastTS is "+(now - lastTS)+" ms old, now: "+logCal.getTime()+" / "+now+" ms [testTS2Late "+testPongTO+", done "+testPongTODone+"]"); + bot.disconnect(); + bot.dispose(); + bot = null; + } + if( null == bot ) { + bot = new LogBot(showHostname, joinMessage, htmlOut, false /* reconnectOnDisconnect */); + bot.setVerbose(verbose); + bot.setLoginAndNick(login, nick); + if( null != nickPwd && nickPwd.length() > 0 ) { + final LogBot fbot = bot; + bot.setPostConnectAction(new Runnable() { + public void run() { + fbot.identify(nickPwd); + } + }); + } + try { + bot.connect(server); + bot.joinChannel(HASH+channelNoHash); + } catch (Exception e) { + System.err.println("Catched "+e.getClass().getName()+": "+e.getMessage()); + e.printStackTrace(); + try { + bot.disconnect(); + bot.dispose(); + } catch (Exception e2) {} + bot = null; + } + } + if( null != bot ) { + if( newBOT || ( 0 < logrotate && now >= nextLogrotate ) ) { // Swap Logfiles: Open and set new logfile, then close old one. final LogStream _logOut = new LogStream(bot, logprefix, urlprefix, server, channelNoHash, htmlHeader); bot.logNotice("Continue @ "+_logOut.urlString); bot.setOut(_logOut.out, true); - bot.logNotice("Previous @ "+ logOut.urlString); + if( null != logOut ) { + bot.logNotice("Previous @ "+ logOut.urlString); + } bot.setJoinMessage(joinMessage+" @ "+_logOut.urlString); - bot.sendLoggedNotice(HASH+channelNoHash, bot.getJoinMessage()); - if(htmlOut) { - logOut.cat(htmlFooter); + if( !newBOT ) { + bot.sendLoggedNotice(HASH+channelNoHash, bot.getJoinMessage()); + } + if( null != logOut ) { + if( htmlOut ) { + logOut.cat(htmlFooter); + } + logOut.close(); // implies flush } - logOut.close(); // implies flush logOut = _logOut; nextLogrotate += logrotate; logCal.setTimeInMillis(now); @@ -190,16 +221,20 @@ public class CatOut extends PircBot { logCal.setTimeInMillis(nextLogrotate); System.err.println("Next Logrotate: "+logCal.getTime()); } - try { - Thread.sleep(FLUSH_INTERVAL); - } catch (Exception e) { } - if(null != logOut) { - logOut.flush(); + } + try { + Thread.sleep(FLUSH_INTERVAL); + if( null != bot ) { + bot.sendPing(); } + } catch (Exception e) { } + if(null != logOut) { + logOut.flush(); } } } - public static final long FLUSH_INTERVAL = 60*1000; // flush every minute + public static final long FLUSH_INTERVAL = 1*60*1000; // flush every minute + public static final long PONG_TIMEOUT = 3*60*1000; // server pong timeout (we send one ping each FLUSH_TIMEOUT) static class LogStream { public final PrintStream out; diff --git a/src/org/jogamp/jabot/irc/LogBot.java b/src/org/jogamp/jabot/irc/LogBot.java index 3f76f61..80a6591 100644 --- a/src/org/jogamp/jabot/irc/LogBot.java +++ b/src/org/jogamp/jabot/irc/LogBot.java @@ -40,18 +40,20 @@ public class LogBot extends PircBot { private final Calendar calendar;
private final Object timeSync = new Object();
+ private final boolean reconnectOnDisconnect;
private final boolean xmlOut;
private final Object outSync = new Object();
private PrintStream out;
private int lineCount;
+ private long lastTS;
private Runnable postConnectAction;
- public LogBot(boolean showHostname, String joinMessage, boolean xmlOut) {
- this(showHostname, joinMessage, TimeTool.getNearZuluTimeZone(), Locale.getDefault(), System.out, xmlOut);
+ public LogBot(boolean showHostname, String joinMessage, boolean xmlOut, boolean reconnectOnDisconnect) {
+ this(showHostname, joinMessage, TimeTool.getNearZuluTimeZone(), Locale.getDefault(), System.out, xmlOut, reconnectOnDisconnect);
}
- public LogBot(boolean showHostname, String joinMessage, TimeZone timeZone, Locale locale, PrintStream out, boolean xmlOut) {
+ public LogBot(boolean showHostname, String joinMessage, TimeZone timeZone, Locale locale, PrintStream out, boolean xmlOut, boolean reconnectOnDisconnect) {
this.showHostname = showHostname;
this.calendar = new GregorianCalendar(timeZone, locale);
this.xmlOut = xmlOut;
@@ -59,6 +61,23 @@ public class LogBot extends PircBot { this.joinMessage = joinMessage;
this.lineCount = 0;
this.postConnectAction = null;
+ this.lastTS = System.currentTimeMillis();
+ this.reconnectOnDisconnect = reconnectOnDisconnect;
+ }
+
+ @Override
+ protected void handleLine(String line) {
+ lastTS = System.currentTimeMillis();
+ super.handleLine(line);
+ }
+
+ public void sendPing() {
+ this.sendRawLine("PING :" + getNick());
+ }
+
+ /** Returns the last timestamp as set by ctor or receiving a line from server. */
+ public final long getLastTS() {
+ return lastTS;
}
public final void setJoinMessage(String joinMessage) {
@@ -67,7 +86,7 @@ public class LogBot extends PircBot { public final String getJoinMessage() {
return joinMessage;
}
-
+
/**
* <p>
* Sets the internal login of the Bot. This should be set before joining
@@ -272,16 +291,18 @@ public class LogBot extends PircBot { @Override
public void onDisconnect() {
append(NAVY, "* Disconnected.");
- while (!isConnected()) {
- try {
- reconnect();
- }
- catch (Exception e) {
+ if( reconnectOnDisconnect ) {
+ while (!isConnected()) {
try {
- Thread.sleep(10000);
+ reconnect();
}
- catch (Exception anye) {
- // Do nothing.
+ catch (Exception e) {
+ try {
+ Thread.sleep(10000);
+ }
+ catch (Exception anye) {
+ // Do nothing.
+ }
}
}
}
|