Browse Source

Feat: Reconnait les noms des FAI

Martin Passard 7 years ago
parent
commit
6dd3c459d6
5 changed files with 209 additions and 52 deletions
  1. 7 0
      src/data/ISP.java
  2. 10 1
      src/data/ISPDAO.java
  3. 68 18
      src/data/ISPdata.java
  4. 58 31
      src/main/Bot.java
  5. 66 2
      src/main/Cache.java

+ 7 - 0
src/data/ISP.java

@@ -24,6 +24,13 @@ public class ISP {
 		this.data = data;
 		this.data = data;
 	}
 	}
 	
 	
+	public String getShortestName() {
+		if(data.hasShortName()) {
+			return data.getShortname();
+		}else {
+			return name;
+		}
+	}
 	
 	
 	public void setName(String name) {
 	public void setName(String name) {
 		this.name = name;
 		this.name = name;

+ 10 - 1
src/data/ISPDAO.java

@@ -1,5 +1,6 @@
 package data;
 package data;
 
 
+import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStream;
 import java.net.Authenticator;
 import java.net.Authenticator;
@@ -79,9 +80,11 @@ public class ISPDAO {
 
 
 	    } catch (MalformedURLException e) {
 	    } catch (MalformedURLException e) {
 	        e.printStackTrace();
 	        e.printStackTrace();
+	    } catch (FileNotFoundException fnfe) { // IF 404
+	    	return null;
 	    } catch (IOException e) {
 	    } catch (IOException e) {
 	        e.printStackTrace();
 	        e.printStackTrace();
-	    }
+	    } 
 	    if(s!=null) {
 	    if(s!=null) {
 	    	s.close();
 	    	s.close();
 	    }
 	    }
@@ -114,6 +117,7 @@ public class ISPDAO {
 
 
 	public ISP getISP(int number) {
 	public ISP getISP(int number) {
 		String json = executeGet(dbAdress+number+'/');
 		String json = executeGet(dbAdress+number+'/');
+		if(json != null) {
 		System.out.println(json);
 		System.out.println(json);
 		JSONObject jsonObj = new JSONObject(json);
 		JSONObject jsonObj = new JSONObject(json);
 		String name = getName(jsonObj);
 		String name = getName(jsonObj);
@@ -124,6 +128,8 @@ public class ISPDAO {
 		String last_update = getDateUpdated(jsonObj);
 		String last_update = getDateUpdated(jsonObj);
 		ISP isp = new ISP(name, id, member, date_added, last_update, ispData);
 		ISP isp = new ISP(name, id, member, date_added, last_update, ispData);
 		return isp;
 		return isp;
+		}
+		return null;
 
 
 	}
 	}
 	/**
 	/**
@@ -206,5 +212,8 @@ public class ISPDAO {
 		Date d = c.getTime();
 		Date d = c.getTime();
 		return d;
 		return d;
 	}
 	}
+	
+	
+	
 
 
 }
 }

+ 68 - 18
src/data/ISPdata.java

@@ -8,6 +8,7 @@ import IRC.Server;
 
 
 public class ISPdata {
 public class ISPdata {
 
 
+	private String shortname;
 	private String website;
 	private String website;
 	private String description;
 	private String description;
 	private Server [] ircChan;
 	private Server [] ircChan;
@@ -17,13 +18,14 @@ public class ISPdata {
 	private String email;
 	private String email;
 	private String creationDate;
 	private String creationDate;
 	private String joinDate;
 	private String joinDate;
-	
-	
-	
 
 
-	public ISPdata(String website, String description, Server[] chatrooms, int progressStatus,
+
+
+
+	public ISPdata(String shortName, String website, String description, Server[] chatrooms, int progressStatus,
 			int membersCount, int subscribersCount, String email, String creationDate, String joinDate) {
 			int membersCount, int subscribersCount, String email, String creationDate, String joinDate) {
 		super();
 		super();
+		this.shortname = shortName;
 		this.website = website;
 		this.website = website;
 		this.description = description;
 		this.description = description;
 		this.ircChan = chatrooms;
 		this.ircChan = chatrooms;
@@ -33,28 +35,67 @@ public class ISPdata {
 		this.email=email;
 		this.email=email;
 		this.creationDate = creationDate;
 		this.creationDate = creationDate;
 		this.joinDate = joinDate;
 		this.joinDate = joinDate;
+		
 	}
 	}
-	
+
 	public ISPdata(JSONObject jo){
 	public ISPdata(JSONObject jo){
-		this.website = jo.getString("website");
-		JSONArray chatroomsJSON = jo.getJSONArray("chatrooms");
-		Server [] chatrooms = new Server[chatroomsJSON.length()];
-		for(int i = 0; i<chatroomsJSON.length(); ++i) {
-			String servaddr = chatroomsJSON.getString(i);
-			chatrooms[i] = new Server(servaddr);
-					
+		this.shortname = getString(jo,"shortname");
+		this.website = getString(jo,"website","?");
+		Server [] chatrooms;
+		try {
+			JSONArray chatroomsJSON = jo.getJSONArray("chatrooms");
+			chatrooms = new Server[chatroomsJSON.length()];
+			for(int i = 0; i<chatroomsJSON.length(); ++i) {
+				String servaddr = chatroomsJSON.getString(i);
+				chatrooms[i] = new Server(servaddr);
+
+			}
+		}catch(JSONException jsonE) {	// Si il n'y a pas de chatroom
+			chatrooms = new Server[0];
 		}
 		}
-		this.progressStatus = jo.getInt("progressStatus");
-		this.membersCount = jo.getInt("memberCount");
-		this.subscribersCount = jo.getInt("subscriberCount");
+		this.progressStatus = getInt(jo,"progressStatus");
+		this.membersCount = getInt(jo,"memberCount",0);
+		this.subscribersCount = getInt(jo,"subscriberCount",0);
 		try {
 		try {
-		this.joinDate = jo.getString("ffdnMemberSince");
+			this.joinDate = getString(jo,"ffdnMemberSince","?");
 		}catch (JSONException joe) {
 		}catch (JSONException joe) {
 			this.joinDate = "?";
 			this.joinDate = "?";
 		}
 		}
-		this.creationDate = jo.getString("creationDate");
+		this.creationDate = getString(jo,"creationDate","?");
+
+	}
+	
+
+	private int getInt(JSONObject jo, String key, int DEFAULT) {
+		int res;
+		try {
+			res = jo.getInt(key);
+		}catch(JSONException jsone) {
+			res = DEFAULT;
+		}
+		return res;
 		
 		
 	}
 	}
+	
+	private int getInt(JSONObject jo, String key) {
+		return getInt(jo,key,-1);
+		
+	}
+	
+	private String getString (JSONObject jo, String key, String DEFAULT) {
+		String res;
+		try {
+			res = jo.getString(key);
+		}catch(JSONException jsoe) {
+			res = DEFAULT;
+		}
+		return res;
+	}
+	
+	private String getString (JSONObject jo, String key) {
+		return getString(jo, key, null);
+	}
+	
 
 
 	public String getWebsite() {
 	public String getWebsite() {
 		return website;
 		return website;
@@ -65,7 +106,7 @@ public class ISPdata {
 		return description;
 		return description;
 	}
 	}
 
 
-	
+
 
 
 
 
 	public Server[] getIrcChan() {
 	public Server[] getIrcChan() {
@@ -96,6 +137,15 @@ public class ISPdata {
 	public String getJoinDate() {
 	public String getJoinDate() {
 		return joinDate;
 		return joinDate;
 	}
 	}
+
+
+	public boolean hasShortName() {
+		return !(shortname==null || shortname.equals(""));
+	}
+
+	public String getShortname() {
+		return shortname;
+	}
 	
 	
 	
 	
 }
 }

+ 58 - 31
src/main/Bot.java

@@ -26,37 +26,7 @@ public class Bot extends PircBot {
 
 
 		if (message.contains("+info")) {
 		if (message.contains("+info")) {
 
 
-			String s = message.substring(message.indexOf(' ')+1);
-			if(!EntierPositifNonVide.verifie(s)) {			// Un mot après +info
-				if(s.equalsIgnoreCase("all")) {	            // +info all
-					Cache c = Cache.getInstance();
-					List<ISP> listeFAI;
-					try {
-						listeFAI = c.getListe();
-					}catch (Exception e) {
-						try {
-							listeFAI = idao.getISPs();
-						} catch (Exception e1) {
-							// TODO Auto-generated catch block
-							e1.printStackTrace();
-						}
-					}
-					for(ISP i : c.getListe()) {
-						if(i.isFFDNMember()) {
-							sendMessage(channel, i.toStringIRC());
-						}
-					}
-
-				}
-
-			}else {											// Un nombre après +info
-
-				int  id = Integer.parseInt(message.substring(message.indexOf(' ')+1));
-				List<String> strings = idao.getISP(id).toStringIRC();
-				for(String response : strings) {
-					sendMessage(channel,response);
-				}
-			}
+			info(channel,sender,login,hostname,message);
 
 
 		}
 		}
 
 
@@ -75,4 +45,61 @@ public class Bot extends PircBot {
 			sendMessage(channel,s);
 			sendMessage(channel,s);
 		}
 		}
 	}
 	}
+	
+	
+	public void info(String channel, String sender,
+	String login, String hostname, String message) {
+		
+		String s = message.substring(message.indexOf(' ')+1);
+		if(!EntierPositifNonVide.verifie(s)) {			// Un mot après +info
+			
+			
+			if(s.equalsIgnoreCase("all")) {	          			  // +info all
+				Cache c = Cache.getInstance();
+				sendMessage(channel, c.toStringIRC());
+				
+				
+				
+				List<ISP> listeFAI;
+				try {
+					listeFAI = c.getListe();
+				}catch (Exception e) {
+					try {
+						listeFAI = idao.getISPs();
+					} catch (Exception e1) {
+						// TODO Auto-generated catch block
+						e1.printStackTrace();
+					}
+				}
+				for(ISP i : c.getListe()) {
+					if(i.isFFDNMember()) {
+						sendMessage(channel, i.toStringIRC());
+					}
+				}
+
+			}else if(s.equalsIgnoreCase("ffdn")) {				//+info ffdn
+				Cache c = Cache.getInstance();
+				sendMessage(channel, c.toStringIRC());
+				
+			}else {
+				Cache c = Cache.getInstance();
+				ISP i = c.getISPWithName(s);
+				if(i == null) {
+					sendMessage(channel, "Le FAI "+s+" est Inconnu, désolé");
+				}else {
+					sendMessage(channel, i.toStringIRC());
+				}
+			}
+
+		}else {											// Un nombre après +info
+
+			int  id = Integer.parseInt(message.substring(message.indexOf(' ')+1));
+			List<String> strings = idao.getISP(id).toStringIRC();
+			for(String response : strings) {
+				sendMessage(channel,response);
+			}
+		}
+		
+	}
+	
 }
 }

+ 66 - 2
src/main/Cache.java

@@ -1,9 +1,8 @@
 package main;
 package main;
 
 
+import java.util.LinkedList;
 import java.util.List;
 import java.util.List;
 
 
-import com.sun.corba.se.impl.orbutil.RepositoryIdUtility;
-
 import data.ISP;
 import data.ISP;
 import data.ISPDAO;
 import data.ISPDAO;
 
 
@@ -68,4 +67,69 @@ public class Cache {
 	public List<ISP> getListe(){
 	public List<ISP> getListe(){
 		return cache;
 		return cache;
 	}
 	}
+	
+	public int getMemberCountInFede() {
+		int i = 0;
+		for(ISP isp : getListe()) {
+			if(isp.isFFDNMember()) {
+				i += isp.getMembersCount();
+			}
+		}
+		return i;
+	}
+	
+	public int getSubscribersCountInFede() {
+		int i = 0;
+		for(ISP isp : getListe()) {
+			if(isp.isFFDNMember()) {
+				i += isp.getSubscribersCount();
+			}
+		}
+		return i;
+	}
+	
+	/**
+	 * Donne le nombre de FAI de la fédé en parcourant le cache.
+	 * @param ffdn_member Si = null alors on s'en fout, si =true, seuls les FAI de la fédé, si = false, seuls les FAI non membres
+	 * @return nombre de FAI
+	 */
+	public int getISPCount(Boolean ffdn_member) {
+		if(ffdn_member == null) {
+			return cache.size();
+		}else{
+			int count = 0;
+			for(ISP fai : cache) {
+				if(ffdn_member == fai.isFFDNMember()) {
+					count++;
+				}
+			}
+			return count;
+			
+		}
+			
+	}
+	
+	public ISP getISPWithName(String s) {
+		for(ISP i : cache) {
+			if(i.getShortestName().equalsIgnoreCase(s)) {
+				return i;
+			}
+		}
+		return null;
+	}
+	
+	public String toString() {
+		return "Cache de "+cache.size()+" FAI";
+	}
+	
+	public List<String> toStringIRC() {
+		List<String> liste = new LinkedList<String>();
+		liste.add("Il y a "+cache.size()+" FAI dont "+getISPCount(Boolean.TRUE)+" dans la fédé");
+		liste.add("Cela représente en tout (dans la fédé) "+getSubscribersCountInFede()+" Abonné.e.s et "+getMemberCountInFede()+" Membres");
+		return liste;
+	}
+	
+	
+	
+	
 }
 }