Browse Source

feat: reconnection en cas de déconnexion

Martin Passard 7 years ago
parent
commit
a0ac24e516
2 changed files with 54 additions and 14 deletions
  1. 50 0
      src/main/Bot.java
  2. 4 14
      src/main/Main.java

+ 50 - 0
src/main/Bot.java

@@ -1,7 +1,14 @@
 package main;
 package main;
 
 
+import java.io.IOException;
+import java.net.UnknownHostException;
+import java.text.SimpleDateFormat;
+import java.util.Date;
 import java.util.List;
 import java.util.List;
+import java.util.Locale;
 
 
+import org.jibble.pircbot.IrcException;
+import org.jibble.pircbot.NickAlreadyInUseException;
 import org.jibble.pircbot.PircBot;
 import org.jibble.pircbot.PircBot;
 
 
 import actions.Action;
 import actions.Action;
@@ -15,6 +22,7 @@ public class Bot extends PircBot {
 	private String[] admins;
 	private String[] admins;
 	private boolean responseOnPrivateChannel = true;
 	private boolean responseOnPrivateChannel = true;
 	private boolean responseOnPrivateMessages = true;
 	private boolean responseOnPrivateMessages = true;
+	private volatile static long WAIT_BEFORE_RECONNECT = 60;
 	
 	
 
 
 	public Bot() {
 	public Bot() {
@@ -29,6 +37,7 @@ public class Bot extends PircBot {
 		}
 		}
 	}
 	}
 	
 	
+	
 	public void onPrivateMessage(String sender, String login, String hostname, String message) {
 	public void onPrivateMessage(String sender, String login, String hostname, String message) {
 		if(responseOnPrivateMessages) {
 		if(responseOnPrivateMessages) {
 			onMessage(sender, sender, login, hostname, message);
 			onMessage(sender, sender, login, hostname, message);
@@ -37,6 +46,47 @@ public class Bot extends PircBot {
 		}
 		}
 	}
 	}
 	
 	
+	
+	public void onDisconnect() {
+		Date d = new Date();
+		System.err.println("Je viens d'être déconnectée!");
+		this.sendMessageToAdmins("Je viens d'être déconnectée!");
+
+		while(!this.isConnected()) {
+		try {
+			Thread.sleep(WAIT_BEFORE_RECONNECT*1000);
+		} catch (InterruptedException e) {
+			e.printStackTrace();
+		}
+			try {
+				this.reconnect();
+			}catch (UnknownHostException e) {
+				
+			
+			} catch (NickAlreadyInUseException e) {
+				this.sendMessageToAdmins("Je viens d'être déconnectée et en tentant de me reconnecter mon nom était déjà utilisé!");
+				e.printStackTrace();
+			} catch (IOException e) {
+				this.sendMessageToAdmins("Je viens d'être déconnectée et en tentant de me reconnecter, j'ai eu une IOException: "+e.toString());
+				e.printStackTrace();
+			} catch (IrcException e) {
+				this.sendMessageToAdmins("Je viens d'être déconnectée et en tentant de me reconnecter, j'ai eu une IrcException: "+e.toString());
+				e.printStackTrace();
+			}
+		}
+		
+		SimpleDateFormat sdf = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss", Locale.FRENCH);
+		this.sendMessageToAdmins("Je viens de me reconnecter, j'était abscente depuis le "+sdf.format(d));
+		
+	}
+	
+	public final void joinChannels() {
+		String[] channels = Main.getCHANNELS();
+		for(int i = 0; i< channels.length; i++) {
+			joinChannel(channels[i]);
+		}
+	}
+	
 	public void onMessage(String channel, String sender,
 	public void onMessage(String channel, String sender,
 			String login, String hostname, String message) {
 			String login, String hostname, String message) {
 		
 		

+ 4 - 14
src/main/Main.java

@@ -4,9 +4,9 @@ import java.net.ConnectException;
 
 
 public class Main {
 public class Main {
 
 
-	private static String SERVER = "irc.geeknode.net";
-	private static int PORT = 6667;
-	private static String[] CHANNELS = { "#marmat" };
+	public volatile static String SERVER = "irc.geeknode.net";
+	public volatile static int PORT = 6667;
+	private volatile static String[] CHANNELS = { "#marmat" };
 	private static long TIMEOUT_BEFORE_RECONNECTING = 360;
 	private static long TIMEOUT_BEFORE_RECONNECTING = 360;
 	private static int failures = 0;
 	private static int failures = 0;
 	private static boolean DEBUG=true;
 	private static boolean DEBUG=true;
@@ -52,7 +52,6 @@ public class Main {
 		}
 		}
 	}
 	}
 
 
-
 	public static boolean isDebug () {
 	public static boolean isDebug () {
 		return Main.DEBUG;
 		return Main.DEBUG;
 	}
 	}
@@ -62,45 +61,36 @@ public class Main {
 		DEBUG=b;
 		DEBUG=b;
 	}
 	}
 
 
-
 	public static final String getSERVER() {
 	public static final String getSERVER() {
 		return SERVER;
 		return SERVER;
 	}
 	}
 
 
-
 	public static final void setSERVER(String sERVER) {
 	public static final void setSERVER(String sERVER) {
 		SERVER = sERVER;
 		SERVER = sERVER;
 	}
 	}
 
 
-
 	public static final int getPORT() {
 	public static final int getPORT() {
 		return PORT;
 		return PORT;
 	}
 	}
 
 
-
 	public static final void setPORT(int pORT) {
 	public static final void setPORT(int pORT) {
 		PORT = pORT;
 		PORT = pORT;
 	}
 	}
 
 
-
 	public static final String[] getCHANNELS() {
 	public static final String[] getCHANNELS() {
 		return CHANNELS;
 		return CHANNELS;
 	}
 	}
 
 
-
 	public static final void setCHANNELS(String[] cHANNELS) {
 	public static final void setCHANNELS(String[] cHANNELS) {
 		CHANNELS = cHANNELS;
 		CHANNELS = cHANNELS;
 	}
 	}
 
 
-
 	public static final long getTIMEOUT_BEFORE_RECONNECTING() {
 	public static final long getTIMEOUT_BEFORE_RECONNECTING() {
 		return TIMEOUT_BEFORE_RECONNECTING;
 		return TIMEOUT_BEFORE_RECONNECTING;
 	}
 	}
 
 
-
 	public static final void setTIMEOUT_BEFORE_RECONNECTING(long tIMEOUT_BEFORE_RECONNECTING) {
 	public static final void setTIMEOUT_BEFORE_RECONNECTING(long tIMEOUT_BEFORE_RECONNECTING) {
 		TIMEOUT_BEFORE_RECONNECTING = tIMEOUT_BEFORE_RECONNECTING;
 		TIMEOUT_BEFORE_RECONNECTING = tIMEOUT_BEFORE_RECONNECTING;
 	}
 	}
-	
-	
+
 }
 }