-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathConnexion.java
executable file
·210 lines (170 loc) · 5.49 KB
/
Connexion.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import java.io.*;
import java.net.*;
import java.util.List;
import java.util.Vector;
public class Connexion extends Thread{
private Socket socket = null ;
private PrintWriter out;
private static int clientConnectes = 0;
private int idConnexion ;
private String msg = "";
private String addr = "";
private String pseudo = "";
private int port ;
private String[] tokens ;
private String delims = "[|]" ;
private String nom;
private String adresseMulticast;
private int portMulticast;
public static Vector<ProprieteClient> clientConnected = new Vector<ProprieteClient>();;
public static Vector<ProprieteGroup> groupConnected = new Vector<ProprieteGroup>();;
public Connexion(Socket s){
socket = s;
}
public Socket getSocket(){
return socket;
}
/**
* MŽthode qui envoie le message en argument ˆ tous les clients connectŽs
* @param message: le message ˆ envoyer
*/
public void envoiMessage(String message){
PrintWriter out;
for(int i=0; i<Serveur.getAll().size(); i++){
try {
out = new PrintWriter(Serveur.getAll().get(i).getSocket().getOutputStream(),true);
out.println(message);
}catch (IOException e){}
}
}
public void run(){
BufferedReader in;
try {
out = new PrintWriter(socket.getOutputStream());
} catch (IOException e1) {
e1.printStackTrace();
}
while(true){
try {
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
msg = in.readLine();
if(msg.startsWith("NEW_USER")){
msg = msg.substring(8);
System.out.println(" Ce que j'ai recu client :"+msg);
tokens = msg.split(delims);
pseudo = tokens[0];
addr = tokens[1];
port = Integer.parseInt(tokens[2]);
ProprieteClient user = new ProprieteClient(clientConnectes, pseudo,addr,port);
if(!isUserExist(user)){
clientConnected.add(user);
clientConnectes++;
System.out.println("n¡ " + clientConnectes + " : " + clientConnected.get(clientConnectes-1).getPseudo());
}else {
out.println("PseudoUsed");
out.flush();
Thread.sleep(200);
}
}else if(msg.startsWith("NEW_GROUP")){
msg = msg.substring(9);
System.out.println(" Ce que j'ai recu group :"+msg);
tokens = msg.split(delims);
nom = tokens[0];
adresseMulticast = tokens[1];
portMulticast = Integer.parseInt(tokens[2]);
ProprieteGroup group = new ProprieteGroup(nom,adresseMulticast,portMulticast);
if(!isGroupExist(nom)){
groupConnected.add(group);
System.out.println(nom +" Est crŽe");
}else {
out.println("NomUsed");
out.flush();
Thread.sleep(200);
}
}else if(msg.startsWith("XX-FIN")){
msg = msg.substring(6);
tokens = msg.split(delims);
pseudo = tokens[0];
addr = tokens[1];
port = Integer.parseInt(tokens[2]);
for(int i=0;i<clientConnected.size();i++){
ProprieteClient cl = clientConnected.get(i) ;
if(cl.getAddr().equals(addr) && cl.getPseudo().equals(pseudo) && cl.getPort() == port ){
clientConnected.remove(cl);
removeClientDisconected(cl);
}
}
clientConnectes--;
msg = "Un client s'est dŽconnectŽ \t " +
clientConnectes + "Client(s) en ligne en ce moment"
;
System.out.println(msg);
// on peut fermer le serveur si y a pas clientConnectes==0
//System.exit(0);
}else if (msg.startsWith("XX-LISTE")){
String listToClient = "[" ;
for(int i=0;i<clientConnected.size();i++){
idConnexion = clientConnected.get(i).getNum();
pseudo = clientConnected.get(i).getPseudo();
addr = clientConnected.get(i).getAddr() ;
System.out.println("XX-LISTE --> addr:"+addr);
port = clientConnected.get(i).getPort() ;
listToClient += idConnexion+"|"+pseudo+"|"+addr+"|"+port;
if(i<clientConnected.size()-1)
listToClient += "@" ;
}
listToClient += "]";
out.println(listToClient);
out.flush();
}else if (msg.startsWith(("XX-GLIST"))){
String listGroup = "glist";
for(int i=0;i<groupConnected.size();i++){
nom = groupConnected.get(i).getNom();
adresseMulticast = groupConnected.get(i).getAddressMulticast() ;
portMulticast = groupConnected.get(i).getPortMulticast() ;
listGroup +=nom+"|"+adresseMulticast+"|"+portMulticast;
if(i<groupConnected.size()-1)
listGroup += "@" ;
}
out.println(listGroup);
out.flush();
}
} catch (Exception e) {
}
}
}
private boolean isUserExist(ProprieteClient user) {
for(int i=0;i<clientConnected.size();i++){
idConnexion = clientConnected.get(i).getNum();
pseudo = clientConnected.get(i).getPseudo();
addr = clientConnected.get(i).getAddr() ;
port = clientConnected.get(i).getPort() ;
if (pseudo.equals(user.getPseudo())){
System.out.println("Pseudo DŽjˆ utilisŽ ! ");
return true;
}else if(addr.equals(user.getAddr()) && port == user.getPort() ){
System.out.println("Port & adresse DŽjˆ utilisŽ ! ");
return true;
}
}
return false;
}
private boolean isGroupExist(String _nom){
for(int i=0;i<groupConnected.size();i++){
nom = clientConnected.get(i).getPseudo();
if (nom.equals(_nom)){
System.out.println("Nom DŽjˆ utilisŽ ! ");
return true;
}
}
return false;
}
private void removeClientDisconected(ProprieteClient cl) {
int port = cl.getPort() ;
String addr = cl.getAddr();
String toClient = "FIN_OK" ;
toClient += addr+"|"+port;
out.println(toClient);
out.flush();
}
}