-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.java
More file actions
154 lines (118 loc) · 4.31 KB
/
Server.java
File metadata and controls
154 lines (118 loc) · 4.31 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
import java.io.*;
import java.net.*;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
public class Server
{
private ServerSocket serverSocket;
private Map<String, ClientHandler> clientsConnectes = new ConcurrentHashMap<>();
private List<Profil> profils = new ArrayList<>();
private Map<String, List<String>> likes = new HashMap<>();
private List<Message> historiqueMessages = new ArrayList<>();
private Map<String, String> identifiantsParId = new HashMap<>();
private static final int PORT = 5000;
public void demarrer()
{
try
{
serverSocket = new ServerSocket(PORT);
System.out.println("Serveur démarré sur le port " + PORT);
while (true)
{
Socket clientSocket = serverSocket.accept();
ClientHandler handler = new ClientHandler(clientSocket, this);
new Thread(handler).start();
}
}
catch (IOException e)
{
System.err.println("Erreur serveur: " + e.getMessage());
}
}
public synchronized void ajouterProfil(Profil profil)
{
if (profil.getId() == null || profil.getId().isEmpty())
profil.setId(UUID.randomUUID().toString());
profils.add(profil);
identifiantsParId.put(profil.getId(), profil.getNom());
System.out.println("Nouveau profil ajouté: " + profil.getNom() + " avec ID: " + profil.getId());
diffuserProfilsATous();
}
public synchronized void enregistrerClient(String idUtilisateur, ClientHandler handler)
{
clientsConnectes.put(idUtilisateur, handler);
}
public synchronized void enregistrerLike(String idUtilisateur, String idProfilLike)
{
likes.computeIfAbsent(idUtilisateur, k -> new ArrayList<>()).add(idProfilLike);
List<String> likesAutreProfil = likes.getOrDefault(idProfilLike, new ArrayList<>());
if (likesAutreProfil.contains(idUtilisateur)) {
System.out.println("Match entre " + idUtilisateur + " et " + idProfilLike);
Profil profilMatch1 = getProfilParId(idUtilisateur);
Profil profilMatch2 = getProfilParId(idProfilLike);
if (profilMatch1 != null && profilMatch2 != null)
{
ClientHandler handler1 = clientsConnectes.get(idUtilisateur);
ClientHandler handler2 = clientsConnectes.get(idProfilLike);
if (handler1 != null) handler1.envoyerMatch(profilMatch2);
if (handler2 != null) handler2.envoyerMatch(profilMatch1);
}
}
}
public synchronized void envoyerMessage(Message message)
{
historiqueMessages.add(message);
ClientHandler destinataire = clientsConnectes.get(message.getDestinataire());
if (destinataire != null)
destinataire.envoyerMessage(message);
}
public synchronized Profil authentifierUtilisateur(String identifiant, String motDePasse)
{
for (Profil profil : profils)
if ((profil.getNom().equals(identifiant) ||
(identifiantsParId.containsKey(profil.getId()) && identifiantsParId.get(profil.getId()).equals(identifiant))) &&
profil.getMotDePasse() != null && profil.getMotDePasse().equals(motDePasse))
return profil;
return null;
}
public synchronized List<Message> getHistoriqueMessagesUtilisateur(String idUtilisateur) {
return historiqueMessages.stream()
.filter(msg -> msg.getExpediteur().equals(idUtilisateur) || msg.getDestinataire().equals(idUtilisateur))
.collect(Collectors.toList());
}
public void deconnecterClient(String idUtilisateur)
{
clientsConnectes.remove(idUtilisateur);
System.out.println("Client déconnecté: " + idUtilisateur);
}
public synchronized List<Profil> getProfilsPourUtilisateur(String idUtilisateur)
{
List<Profil> profilsFiltres = new ArrayList<>();
for (Profil profil : profils)
if (!profil.getId().equals(idUtilisateur))
profilsFiltres.add(profil);
return profilsFiltres;
}
// Trouver un profil par son ID
private Profil getProfilParId(String id)
{
for (Profil profil : profils)
if (profil.getId().equals(id))
return profil;
return null;
}
public synchronized void diffuserProfilsATous() {
for (Map.Entry<String, ClientHandler> entry : clientsConnectes.entrySet())
{
String idUtilisateur = entry.getKey();
ClientHandler handler = entry.getValue();
handler.envoyerProfils();
}
}
public static void main(String[] args)
{
Server serveur = new Server();
serveur.demarrer();
}
}