All pastes #612924 Raw Edit

PircBot reconnect script

public java v1 · immutable
·published 2007-07-10 18:22 UTC
rendered paste body
class ReconnectHandler extends TimerTask {	// This is how many times we try to reconnect before giving up.	private final int totalTries = 20;	private int currentTries = 0;	// This is the bot we are trying to reconnect.	private iKitty bot;	// Attempt a reconnect every 30 seconds.	Timer timer = new Timer();	public ReconnectHandler(iKitty bot) {		this.bot = bot;		this.timer.schedule(this, 0, 30 * 1000);	}	// Close the timer, so this can be garbage collected.	public void close() {		this.timer.cancel();	}	// What do we do?	public void run() {		try {			System.out.println("Attempting to connect again.");			// Join the server we were disconnected from.			this.bot.reconnect();			// Try to rejoin all of our previous channels. 			for(int i = 0; i < bot.channels.size(); i++) {				bot.joinChannel(bot.channels.get(i));			}		} catch(IrcException e) {			// Not untill all tries have been exhausted.			if(this.currentTries == this.totalTries) {				// Allow us to be garbage collected.				this.timer.cancel();				// Report to user, and exit.				JOptionPane.showMessageDialog(null, "We could not reconnect in " + this.totalTries + ".\nThe bot will now close.");				System.exit(0);							}		} catch(IOException E) {			// Not sure what to do, so ...			this.timer.cancel();			System.out.println(E);		} finally {			// Try again.			this.currentTries++;		}	}}