forked from mRafaelSilva/Servidor-Cliente
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.java
More file actions
284 lines (226 loc) · 10.9 KB
/
Copy pathServer.java
File metadata and controls
284 lines (226 loc) · 10.9 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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import java.awt.SystemTray;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import org.json.JSONArray;
import org.json.JSONObject;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.io.*;
import java.net.*;
// 1 É TIPO REGISTO
// 2 É TIPO PEDIDO
// 3 É TIPO ACK
// 4 É TIPO ENVIO DE TAREFA
// 5 É TIPO RESULTADO
public class Server {
private static final int TCP_PORT = 54321; // Porta para conexões TCP
private static final int PORT = 12345;
private DatagramSocket socket;
private ServerSocket tcpSocket;
private List<Integer> clients = new ArrayList<>();
private AtomicInteger clientIdCounter = new AtomicInteger(1);
private DatagramUtils utils;
private AckHandle ackHandle;
private InetAddress ipServidorIperf = null;
public Server() throws IOException {
socket = new DatagramSocket(PORT);
tcpSocket = new ServerSocket(TCP_PORT);
this.utils = new DatagramUtils();
ackHandle = new AckHandle(socket,utils);
System.out.println("Servidor iniciado na porta UDP: " + PORT + " e na porta TCP " + TCP_PORT);
}
public void listen() {
while (true) {
try {
byte[] buffer = new byte[1024];
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
socket.receive(packet);
new Thread(() -> handleClient(packet)).start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void handleClient(DatagramPacket packet) {
try {
String message = new String(packet.getData(), 0, packet.getLength());
String[] parts = message.split("\\|");
int type = Integer.parseInt(parts[0]);
int sequenceNumber = Integer.parseInt(parts[1]);
InetAddress clientAddress = packet.getAddress();
int clientPort = packet.getPort();
if (type == 1) {
// gera um novo id para o cliente
int newClientId = clientIdCounter.getAndIncrement();
clients.add(newClientId);
String fileName = "Resultados/Cliente" + newClientId + ".txt";
Files.write(Paths.get(fileName), ("Cliente " + newClientId + " registado.\n").getBytes());
ackHandle.sendEGuardaAck(sequenceNumber, newClientId, clientAddress, clientPort, socket);
return;
}
if (parts.length < 3 || parts[2] == null || parts[2].isEmpty()) {
System.out.println("A mensagem recebida é inválida");
return;
}
int cliente = Integer.parseInt(parts[2]);
if (!clients.contains(cliente)) {
System.out.println("Cliente desconhecido: " + cliente);
return;
}
if (type == 2) {
System.out.println("O cliente com o id " + cliente + " está a pedir tarefas.");
// envia um pacote com a tarefa
List<String> taskCommands = readTasksFromJSON(cliente, clientAddress);
if (taskCommands == null || taskCommands.isEmpty()) {
System.out.println("Nenhuma tarefa disponível para o cliente " + cliente);
return;
}
int tamanho = taskCommands.size();
String str = String.valueOf(tamanho);
String mensagemNTarefas = utils.criaDatagramaTarefaResultado(4, sequenceNumber, cliente, str);
DatagramPacket nTarefasPacket = new DatagramPacket(
mensagemNTarefas.getBytes(), mensagemNTarefas.length(), clientAddress, clientPort
);
socket.send(nTarefasPacket);
ackHandle.criaAckPendente(sequenceNumber, cliente, clientAddress, PORT, nTarefasPacket);
// Itera pela lista de comandos das tarefas
for (int i = 0; i < taskCommands.size(); i++) {
String comandoTarefa = taskCommands.get(i); // Acede ao comando no índice atual
int taskSequenceNumber = sequenceNumber + i + 1; // Número de sequência único para cada tarefa
// Cria a mensagem de tarefa específica
String mensagemTarefa = utils.criaDatagramaTarefaResultado(4, taskSequenceNumber, cliente, comandoTarefa);
// Cria e envia o pacote
DatagramPacket taskPacket = new DatagramPacket(
mensagemTarefa.getBytes(), mensagemTarefa.length(), clientAddress, clientPort
);
socket.send(taskPacket);
System.out.println("Pacote enviado para a tarefa: " + comandoTarefa);
// Cria um ACK pendente para a tarefa enviada
ackHandle.criaAckPendente(taskSequenceNumber, cliente, clientAddress, clientPort, taskPacket);
}
return;
}
if (type == 3) {
System.out.println("Recebi um ack");
ackHandle.processAck(sequenceNumber, cliente);
return;
}
if (type == 5) {
String result = parts[3];
System.out.println("Resultado recebido do cliente " + cliente);
ackHandle.sendAck(sequenceNumber, cliente, clientAddress, clientPort, socket);
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String timestamp = now.format(formatter);
String fileName = "Resultados/Cliente" + cliente + ".txt";
String resultEntry = "[" + timestamp + "] " + result + "\n";
Files.write(Paths.get(fileName), resultEntry.getBytes(), StandardOpenOption.APPEND);
return;
}
} catch (Exception e) {
e.printStackTrace();
}
}
private List<String> readTasksFromJSON(int clientId, InetAddress clientAddress) {
List<String> taskList = new ArrayList<>();
try {
String conteudo = new String(Files.readAllBytes(Paths.get("tasks.json")));
JSONObject jsonObject = new JSONObject(conteudo);
JSONArray tasks = jsonObject.getJSONArray("tasks");
for (int i = 0; i < tasks.length(); i++) {
JSONObject task = tasks.getJSONObject(i);
int taskClientId = task.getInt("client_id");
if (taskClientId == clientId) {
String taskId = task.getString("task_id");
int tipo_tarefa = task.getInt("tipo_tarefa");
if (tipo_tarefa == 2) {
ipServidorIperf = clientAddress;
}
String command = task.getString("command");
if (tipo_tarefa == 3) {
command = command.replace("<server_ip>", ipServidorIperf.getHostAddress());
if (ipServidorIperf == null) {
System.out.println("Erro: Nenhum servidor iperf configurado para o cliente " + clientId);
return null;
}
}
int frequency = task.getInt("frequency");
long limit = task.getLong("limit");
String formattedTask = "task_id=" + taskId +",client_id=" + taskClientId + ",tipo_tarefa=" + tipo_tarefa +
",command=" + command + ",frequency=" + frequency + ",limit=" + limit;
taskList.add(formattedTask);
}
}
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Tarefas para o cliente " + clientId + ": " + taskList);
return taskList;
}
public void listenForAlerts() {
new Thread(() -> {
while (true) {
try (Socket clientSocket = tcpSocket.accept()) {
InputStream inputStream = clientSocket.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String alertMessage = reader.readLine();
System.out.println("Alerta recebido: " + alertMessage);
// Tratamento do alerta
processAlert(alertMessage);
// Envia o ACK de volta ao cliente
OutputStream outputStream = clientSocket.getOutputStream();
PrintWriter writer = new PrintWriter(outputStream, true);
writer.println("ACK");
} catch (IOException e) {
System.err.println("Erro ao lidar com conexão TCP: " + e.getMessage());
}
}
}).start();
}
private void processAlert(String alertMessage) {
// Exemplo de parsing e log do alerta
String[] parts = alertMessage.split("\\|");
if (parts.length < 6) {
System.err.println("Alerta inválido recebido: " + alertMessage);
return;
}
try {
double value = Double.parseDouble(parts[3]);
String parameter = parts[2];
int clientId = Integer.parseInt(parts[1]);
double limit = Double.parseDouble(parts[5]);
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String timestamp = now.format(formatter);
// Constrói a mensagem formatada
String logEntry = String.format("[%s] Alerta no cliente %d: o parâmetro %s tem o valor %.1f e ultrapassou o limite de %.1f.%n",
timestamp, clientId, parameter, value, limit);
// Constrói o nome do ficheiro com base no ID do cliente
String fileName = "Resultados/Cliente" + clientId + ".txt";
// Regista a mensagem formatada no arquivo do cliente
Files.write(Paths.get(fileName), logEntry.getBytes(), StandardOpenOption.APPEND, StandardOpenOption.CREATE);
System.out.println("Alerta registado no ficheiro: " + fileName);
} catch (IOException e) {
System.err.println("Erro ao processar alerta: " + e.getMessage());
}
}
public static void main(String[] args) {
Server server = null;
try {
server = new Server();
Server finalServer = server;
new Thread(() -> finalServer.listenForAlerts()).start();
server.listen();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (server != null && server.ackHandle != null) {
server.ackHandle.stopRetransmissionTask();
}
}
}
}