KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > zirc > threads > IdentListenerThread


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 //zIrc, irc client.
11
// Copyright (C) 2004 CoolBytes(Stephane claret, Andre Aymon, Alban Zumofen) coolbytes@hotmail.com
12
//
13
// This program is free software; you can redistribute it and/or
14
// modify it under the terms of the GNU General Public License
15
// as published by the Free Software Foundation; either version 2
16
// of the License, or (at your option) any later version.
17
//
18
// This program is distributed in the hope that it will be useful,
19
// but WITHOUT ANY WARRANTY; without even the implied warranty of
20
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21
// GNU General Public License for more details.
22

23 /**
24  * <p>Title: IdentListenerThread</p>
25  * <p>Description: thread du ident</p>
26  * <p>Copyright: Copyright (c) 2004</p>
27  * <p>Company: CoolBytes(Stephane Claret, Andre Aymon, Alban Zumofen) coolbytes@hotmail.com</p>
28  * @version 1.0
29  */

30
31
32 public class IdentListenerThread extends Thread JavaDoc
33 {
34   private boolean loop = true ; //pour arreter le thread
35
private String JavaDoc 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     //préparer le socket
46
try
47     {
48       //placer une ecoute sur le port IDENT (defaut 113)
49
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         //Attente d'une connexion et lecture du message reçu
69

70           clientSock = sockServ.accept() ;
71
72         //mise en place des flux
73
inStream = new BufferedReader(new InputStreamReader(clientSock.getInputStream())) ;
74         outStream = new PrintWriter(clientSock.getOutputStream()) ;
75
76         //obtenir l objet IRCconnexion concerné
77
IRCconnexion chatIRC = getConnecConcernee(clientSock);
78
79         //info
80
(new MSGident(chatIRC, chatIRC.GetStatusFrm(), "Reçu requete IDENTD de la part de " + clientSock.getInetAddress().getHostAddress())).
81             affiche() ;
82
83         //lecture
84
while (loop && (line = inStream.readLine()) != null)
85         {
86           repondre(chatIRC, line) ;
87         }
88       }
89       catch(NullPointerException JavaDoc ex)//ajout par alban, si ident refuse c boucle infinie sans ca
90
{}
91       catch (Exception JavaDoc ex)
92       {
93         ex.printStackTrace() ;
94       }
95     }
96   }
97
98   private void repondre(IRCconnexion _chatIRC, String JavaDoc _line)
99   {
100     //message info
101

102     (new MSGident(_chatIRC, _chatIRC.GetStatusFrm(), "Envoi réponse à IdentD.")).affiche() ;
103
104     //formate et balance la reponse a IDENT
105
String JavaDoc 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       //ex.printStackTrace() ;
122
}
123
124   }
125
126   //ajouter une connexion
127
public void addConnection(IRCconnexion _ircChat)
128   {
129     ArrIRCconnexion.add(_ircChat) ;
130   }
131
132   //enlever une connexion
133
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   //obtenir la connexion concernée par une requete
143
public IRCconnexion getConnecConcernee(Socket _s)
144   {
145       boolean trouve = false;
146       int i = 0;
147
148       while(!trouve && (i < ArrIRCconnexion.size()) )
149       {
150         //System.out.println(clientSock.getInetAddress().getHostAddress());
151
// System.out.println((((IRCconnexion)(ArrIRCconnexion.get(i))).getMainSock().getInetAddress().getHostAddress()));
152
//comparer les hosts des sockets
153
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