1 package zirc.threads ; 2 3 import java.io.* ; 4 import java.net.* ; 5 import java.util.* ; 6 import zirc.base.* ; 7 import zirc.msg.* ; 8 9 10 23 30 31 32 public class IdentListenerThread extends Thread 33 { 34 private boolean loop = true ; private String line ; 36 private ArrayList ArrIRCconnexion = new ArrayList() ; 37 private ServerSocket sockServ ; 38 private Socket clientSock ; 39 private PrintWriter outStream ; 40 private BufferedReader inStream ; 41 42 public IdentListenerThread() throws IOException 43 { 44 45 try 47 { 48 sockServ = new ServerSocket(IRCconnexion.getIdentPort()) ; 50 } 51 catch (UnknownHostException ex) 52 { 53 ex.printStackTrace() ; 54 } 55 catch (IOException ex) 56 { 57 System.out.println("Ident tourne deja") ; 58 } 59 } 60 61 public void run() 62 { 63 while (loop) 64 { 65 66 try 67 { 68 70 clientSock = sockServ.accept() ; 71 72 inStream = new BufferedReader(new InputStreamReader(clientSock.getInputStream())) ; 74 outStream = new PrintWriter(clientSock.getOutputStream()) ; 75 76 IRCconnexion chatIRC = getConnecConcernee(clientSock); 78 79 (new MSGident(chatIRC, chatIRC.GetStatusFrm(), "Reçu requete IDENTD de la part de " + clientSock.getInetAddress().getHostAddress())). 81 affiche() ; 82 83 while (loop && (line = inStream.readLine()) != null) 85 { 86 repondre(chatIRC, line) ; 87 } 88 } 89 catch(NullPointerException ex) {} 91 catch (Exception ex) 92 { 93 ex.printStackTrace() ; 94 } 95 } 96 } 97 98 private void repondre(IRCconnexion _chatIRC, String _line) 99 { 100 102 (new MSGident(_chatIRC, _chatIRC.GetStatusFrm(), "Envoi réponse à IdentD.")).affiche() ; 103 104 String rep = _line + " : " + "USERID" + " : " + _chatIRC.GetUser_operatingSys() + " : " + _chatIRC.getUser_nomUser() ; 106 outStream.println(rep) ; 107 outStream.flush() ; 108 109 } 110 111 public void stopThread() 112 { 113 loop = false ; 114 115 try 116 { 117 sockServ.close() ; 118 } 119 catch (IOException ex) 120 { 121 } 123 124 } 125 126 public void addConnection(IRCconnexion _ircChat) 128 { 129 ArrIRCconnexion.add(_ircChat) ; 130 } 131 132 public void removeConnection(IRCconnexion _ircChat) 134 { 135 int ind = ArrIRCconnexion.indexOf(_ircChat) ; 136 if(ind > -1) 137 { 138 ArrIRCconnexion.remove(ind); 139 } 140 } 141 142 public IRCconnexion getConnecConcernee(Socket _s) 144 { 145 boolean trouve = false; 146 int i = 0; 147 148 while(!trouve && (i < ArrIRCconnexion.size()) ) 149 { 150 if(clientSock.getInetAddress().getHostAddress().equals (((IRCconnexion)(ArrIRCconnexion.get(i))).getMainSock().getInetAddress().getHostAddress())) 154 { 155 trouve = true; 156 } 157 else 158 { 159 i++; 160 } 161 162 } 163 164 if(trouve) 165 { 166 return (IRCconnexion)(ArrIRCconnexion.get(i)); 167 } 168 else 169 { 170 return null; 171 } 172 173 } 174 175 } 176 | Popular Tags |