-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.java
More file actions
107 lines (92 loc) · 3.59 KB
/
Server.java
File metadata and controls
107 lines (92 loc) · 3.59 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
import java.io.*;
import java.net.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.ReentrantLock;
public class Server {
private static final int PORT = 1234;
private ServerSocket serverSocket;
private final ReentrantLock lock = new ReentrantLock();
private ConcurrentMap<Integer, Socket> clients = new ConcurrentHashMap<>();
private AtomicInteger clientIdCounter = new AtomicInteger(0);
private volatile int currentLeaderId = -1;
public static void main(String[] args) throws IOException {
Server server = new Server();
server.startServer();
}
public void startServer() throws IOException {
serverSocket = new ServerSocket(PORT);
System.out.println("Server started. Listening on Port " + PORT);
while (true) {
Socket clientSocket = serverSocket.accept();
int clientId = clientIdCounter.getAndIncrement();
new Thread(new ClientHandler(clientSocket, clientId)).start();
}
}
private class ClientHandler implements Runnable {
private Socket clientSocket;
private int clientId;
public ClientHandler(Socket clientSocket, int clientId) {
this.clientSocket = clientSocket;
this.clientId = clientId;
}
public void run() {
try {
clients.put(clientId, clientSocket);
System.out.println("Client " + clientId + " connected.");
DataOutputStream output = new DataOutputStream(clientSocket.getOutputStream());
output.writeInt(clientId);
DataInputStream input = new DataInputStream(clientSocket.getInputStream());
try {
while (true) {
String message = input.readUTF();
System.out.println(message);
if (message.startsWith("I am alive")) {
electLeader(clientId);
}
}
} catch (EOFException | SocketException e) {
System.out.println("Client " + clientId + " has disconnected.");
clients.remove(clientId);
if(currentLeaderId == clientId){
currentLeaderId = -1;
}
if(clients.isEmpty()){
clientIdCounter = new AtomicInteger(0);
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
clientSocket.close();
System.out.println("Client " + clientId + " disconnected.");
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
private void electLeader(int newClientId) {
lock.lock();
try {
if (newClientId > currentLeaderId) {
currentLeaderId = newClientId;
notifyAllClients("New leader is client " + currentLeaderId);
}
} finally {
lock.unlock();
}
}
private void notifyAllClients(String message) {
for (Socket socket : clients.values()) {
try {
DataOutputStream output = new DataOutputStream(socket.getOutputStream());
output.writeUTF(message);
output.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}