-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatServer.java
More file actions
72 lines (61 loc) · 2.34 KB
/
Copy pathChatServer.java
File metadata and controls
72 lines (61 loc) · 2.34 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
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;
import java.util.List;
public class ChatServer extends UnicastRemoteObject implements ChatServerInterface {
private ArrayList<ChatClientInterface> clients;
public ChatServer() throws RemoteException {
clients = new ArrayList<>();
}
public synchronized void registerClient(ChatClientInterface client) throws RemoteException {
clients.add(client);
updateAvailableUsers();
}
public synchronized void unregisterClient(ChatClientInterface client) throws RemoteException {
clients.remove(client);
updateAvailableUsers();
}
@Override
public synchronized void sendMessageToClient(String sender, String recipient, String message) throws RemoteException {
for (ChatClientInterface client : clients) {
if (client.getName().equals(recipient)) {
client.receiveMessage(message);
return; // Exit loop after sending message to the recipient
}
}
}
@Override
public synchronized void broadcastMessage(String message) throws RemoteException {
for (ChatClientInterface client : clients) {
client.receiveMessage(message);
}
}
private synchronized void updateAvailableUsers() throws RemoteException {
List<String> availableUsers = getAvailableUsers();
for (ChatClientInterface client : clients) {
client.updateAvailableUsers(availableUsers); // Notify each client about the updated list
}
}
public static void main(String[] args) {
try {
ChatServer server = new ChatServer();
Naming.rebind("rmi://192.168.10.9/ChatServer", server);
System.out.println("Server running...");
} catch (Exception e) {
System.err.println("Server exception: " + e.getMessage());
e.printStackTrace();
}
}
public synchronized List<String> getAvailableUsers() {
List<String> userList = new ArrayList<>();
for (ChatClientInterface client : clients) {
try {
userList.add(client.getName());
} catch (RemoteException e) {
e.printStackTrace();
}
}
return userList;
}
}