-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClientHandler.java
More file actions
163 lines (144 loc) · 3.57 KB
/
ClientHandler.java
File metadata and controls
163 lines (144 loc) · 3.57 KB
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
import java.io.*;
import java.net.*;
import java.util.*;
public class ClientHandler implements Runnable
{
private Socket socket;
private Server serveur;
private ObjectOutputStream out;
private ObjectInputStream in;
private String idUtilisateur;
public ClientHandler(Socket socket, Server serveur)
{
this.socket = socket;
this.serveur = serveur;
try
{
out = new ObjectOutputStream(socket.getOutputStream());
in = new ObjectInputStream(socket.getInputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
public void run()
{
try
{
while (true)
{
@SuppressWarnings("unchecked")
Map<String, Object> message = (Map<String, Object>) in.readObject();
traiterMessage(message);
}
}
catch (IOException | ClassNotFoundException e)
{
System.out.println("Client déconnecté: " + e.getMessage());
}
finally
{
if (idUtilisateur != null)
serveur.deconnecterClient(idUtilisateur);
}
}
private void traiterMessage(Map<String, Object> message) {
String type = (String) message.get("type");
Object contenu = message.get("contenu");
switch (type)
{
case "NOUVEAU_PROFIL":
Profil nouveauProfil = (Profil) contenu;
serveur.ajouterProfil(nouveauProfil);
idUtilisateur = nouveauProfil.getId();
serveur.enregistrerClient(idUtilisateur, this);
Map<String, Object> reponse = new HashMap<>();
reponse.put("type", "PROFIL_CREE");
reponse.put("profil", nouveauProfil);
envoyerObjet(reponse);
envoyerProfils();
break;
case "CONNEXION":
@SuppressWarnings("unchecked")
Map<String, String> infoConnexion = (Map<String, String>) contenu;
String identifiant = infoConnexion.get("identifiant");
String motDePasse = infoConnexion.get("motDePasse");
Profil profilConnecte = serveur.authentifierUtilisateur(identifiant, motDePasse);
if (profilConnecte != null)
{
idUtilisateur = profilConnecte.getId();
serveur.enregistrerClient(idUtilisateur, this);
List<Message> historiqueMessages = serveur.getHistoriqueMessagesUtilisateur(idUtilisateur);
Map<String, Object> reponseConnexion = new HashMap<>();
reponseConnexion.put("type", "CONNEXION_REUSSIE");
reponseConnexion.put("profil", profilConnecte);
reponseConnexion.put("historique_messages", historiqueMessages);
envoyerObjet(reponseConnexion);
envoyerProfils();
}
else
{
Map<String, Object> reponseErreur = new HashMap<>();
reponseErreur.put("type", "ERREUR_CONNEXION");
reponseErreur.put("message", "Identifiant ou mot de passe incorrect");
envoyerObjet(reponseErreur);
}
break;
case "LIKE":
String[] ids = (String[]) contenu;
serveur.enregistrerLike(ids[0], ids[1]);
break;
case "MESSAGE":
Message messageChat = (Message) contenu;
serveur.envoyerMessage(messageChat);
break;
}
}
public void envoyerProfils()
{
try
{
out.writeObject(serveur.getProfilsPourUtilisateur(idUtilisateur));
out.flush();
}
catch (IOException e)
{
e.printStackTrace();
}
}
public void envoyerMatch(Profil profil)
{
try
{
out.writeObject(profil);
out.flush();
}
catch (IOException e)
{
e.printStackTrace();
}
}
public void envoyerMessage(Message message)
{
try
{
out.writeObject(message);
out.flush();
}
catch (IOException e)
{
e.printStackTrace();
}
}
public void envoyerObjet(Object objet)
{
try
{
out.writeObject(objet);
out.flush();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}