diff options
Diffstat (limited to 'src/org/jogamp/jabot/irc/CatOut.java')
-rw-r--r-- | src/org/jogamp/jabot/irc/CatOut.java | 115 |
1 files changed, 75 insertions, 40 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; |