-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathring.c
326 lines (300 loc) · 14.9 KB
/
ring.c
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#include "ring.h"
#include "utils.h"
#include "communicate.h"
#include "node.h"
int BUFFER_SIZE = 256;
int verb = 0;
int disp_info = 1;
void check_call(int argc, char **argv)
{
signal(SIGINT, INThandler);
int UDP_fd, TCP_fd, TCP_pred_fd, max_socket, error, new_fd, sos_counter, sos_flag = 0;
char message[BUFFER_SIZE];
Node_struct *node;
fd_set available_sockets, ready_sockets; // Variáveis para guardar o conjunto de sockets
struct sockaddr_in client_addr, tcp_client_addr, addr;
socklen_t client_addrlen, tcp_client_addrlen;
char **args;
int num_read, mode;
UDP_addr_list addr_list[100];
int seq_number = 1;
check_call(argc, argv);
disp_header();
node = new_node(atoi(argv[1]), argv[2], argv[3]);
// prompt user for entering a command and shows command list
command_list(atoi(argv[1]), argv[2], argv[3]);
args = malloc(6 * sizeof(char *));
for (int i = 0; i < 6; i++)
{
args[i] = malloc(BUFFER_SIZE * sizeof(char));
}
FD_ZERO(&available_sockets); // initialize fd set
FD_SET(STDIN_FILENO, &available_sockets); // add STDIN to fd set
max_socket = STDIN_FILENO + 1;
node->socket_list = &available_sockets;
while (1)
{
write(1, INPUT, sizeof(INPUT));
ready_sockets = available_sockets; // Cópia de lista de sockets devido ao comportamento destrutivo do select()
seq_number = seq_number == 99 ? 1 : seq_number;
error = select(max_socket, &ready_sockets, NULL, NULL, NULL);
if (error < 0)
{ /*ERROR*/
printf(RED START "File descriptor set ERROR.\n");
exit(1);
}
for (int i = 0; i < max_socket; i++)
{
if (FD_ISSET(i, &ready_sockets))
{
if (i == STDIN_FILENO) // General Listen STDIN
{
num_read = get_command(args);
mode = check_command(args, num_read, node);
switch (mode)
{
case 0: // new
setup(&available_sockets, node, &max_socket, &TCP_fd, &UDP_fd);
printf(START "|Waiting for connections or command input\n");
printf(START "└────\n");
break;
case 1: // bentry
setup(&available_sockets, node, &max_socket, &TCP_fd, &UDP_fd);
args = efnd_send(args, node);
TCP_pred_fd = self_send(args, node);
node->fd_pred = TCP_pred_fd;
FD_SET(TCP_pred_fd, &available_sockets);
max_socket = max_socket > TCP_pred_fd ? max_socket : TCP_pred_fd + 1;
break;
case 2: // pentry
setup(&available_sockets, node, &max_socket, &TCP_fd, &UDP_fd);
TCP_pred_fd = self_send(args, node);
node->fd_pred = TCP_pred_fd;
FD_SET(TCP_pred_fd, &available_sockets);
max_socket = max_socket > TCP_pred_fd ? max_socket : TCP_pred_fd + 1;
break;
case 3: // chord
node->chord_i = atoi(args[1]);
strcpy(node->chord_ip, args[2]);
strcpy(node->chord_port, args[3]);
printf(START IST "|Chord sucessfully added!\n" RESET);
printf(START "└────\n");
break;
case 4: // dchord
node->chord_i = -1;
strcpy(node->chord_ip, "-1");
strcpy(node->chord_port, "-1");
printf(START IST "|Chord sucessfully deleted!\n" RESET);
printf(START "└────\n");
break;
case 5: // show
show(node);
break;
case 6: // find
memset(&addr, '\0', sizeof(struct sockaddr_in));
seq_number++;
find(args, node, addr_list, seq_number, addr);
break;
case 7: // leave
leave(node, &available_sockets);
break;
case 8: // exit
if (node->is_online == 1)
{
printf(ORG START "* The node is still part of a ring, please use the command " IST " leave" ORG "first!\n");
}
else
{
exit(0);
}
break;
case 10: // pbentry
strcpy(args[0], "bentry");
strcpy(args[2], "193.136.138.142");
strcpy(args[3], "58107");
printf(START YLW "This command only works inside the sigma network!");
setup(&available_sockets, node, &max_socket, &TCP_fd, &UDP_fd);
args = efnd_send(args, node);
TCP_pred_fd = self_send(args, node);
node->fd_pred = TCP_pred_fd;
FD_SET(TCP_pred_fd, &available_sockets);
max_socket = max_socket > TCP_pred_fd ? max_socket : TCP_pred_fd + 1;
break;
default:
break;
}
write(1, INPUT, sizeof(INPUT));
}
else if (i == UDP_fd) // General Listen UDP
{
memset(message, '\0', BUFFER_SIZE);
memset(&client_addr, '\0', sizeof(struct sockaddr_in));
client_addrlen = sizeof(client_addr); // dont think this line is needed
clean_args(args, BUFFER_SIZE);
recvfrom(UDP_fd, message, BUFFER_SIZE, 0, (struct sockaddr *)&client_addr, &client_addrlen);
// verbose("UDP message received: \n");
log_info(message, "UDPin");
if (node->is_online)
{
if (sendto(UDP_fd, "ACK", 3, 0, (struct sockaddr *)&client_addr, client_addrlen) < 3) //(was 0) n devia ser 3? se mandar 2 bytes fica podre
{
printf(RED START "ACK send failed\n");
exit(1);
};
// verbose("ACK response sent");
log_info("ACK", "UDPout");
sscanf(message, "%s %s %s %s %s %s", args[0], args[1], args[2], args[3], args[4], args[5]);
if (strcmp(args[0], "FND") == 0)
{
verbose("Received FND via UDP");
printf(START "│Received new search: %s\n", message);
find_receive(args, node);
}
else if (strcmp(args[0], "RSP") == 0)
{
/*Received response to find request*/
verbose("Received RSP via UDP");
printf(START "│Received new response: %s\n", message);
response_receive(args, node, addr_list);
}
else if (strcmp(args[0], "EFND") == 0)
{
seq_number++;
find(args, node, addr_list, seq_number, client_addr);
}
printf(START "└────\n");
write(1, INPUT, sizeof(INPUT));
}
}
else if (i == TCP_fd) // General Listen TCP
{
memset(message, '\0', BUFFER_SIZE);
clean_args(args, BUFFER_SIZE);
// accept connection from incoming socket and read it
tcp_client_addrlen = sizeof(tcp_client_addr);
if ((new_fd = accept(TCP_fd, (struct sockaddr *)&tcp_client_addr, &tcp_client_addrlen)) == -1)
{ /*ERROR*/
printf(RED START "TCP connection acceptance error.");
exit(0);
}
read(new_fd, message, BUFFER_SIZE);
if (node->is_online)
{
log_info(message, "TCPin");
sscanf(message, "%s %s %s %s %s %s", args[0], args[1], args[2], args[3], args[4], args[5]);
// verbose("TCP message received");
// printf(START "├─Received: %s", message);
if (strcmp(args[0], "SELF") == 0)
{
self_receive(args, node, new_fd);
verbose("SELF command received.");
if (node->fd_pred == -1 || (node->pred_i == node->self_i && atoi(args[1]) != node->self_i))
{
TCP_pred_fd = self_send(args, node);
verbose("Predecessor was missing, SELF sent");
FD_SET(TCP_pred_fd, &available_sockets);
max_socket = max_socket > TCP_pred_fd ? max_socket : TCP_pred_fd + 1;
}
printf(START YLW "├─New connection established!" RESET "\n| ---%d─>%d─>%d---\n", node->pred_i, node->self_i, node->succ_i);
}
else if (strcmp(args[0], "SOS") == 0)
{
if (sos_flag == 1)
{
FD_CLR(node->fd_pred, &available_sockets);
FD_SET(new_fd, &available_sockets);
max_socket = max_socket > TCP_pred_fd ? max_socket : TCP_pred_fd + 1;
sos_flag = 0;
node->fd_pred = new_fd;
node->pred_i = atoi(args[1]);
strcpy(node->pred_ip, args[2]);
strcpy(node->pred_port, args[3]);
printf(YLW "SOS connection message recieved. Ring integraty achieved!\n");
}
}
printf(START "└────\n");
write(1, INPUT, sizeof(INPUT));
}
}
// Connection with predecessor
else if (i == node->fd_pred) // Listen on Predecessor TCP
{
int n_read;
memset(message, '\0', BUFFER_SIZE);
clean_args(args, BUFFER_SIZE);
n_read = read(node->fd_pred, message, BUFFER_SIZE);
if (n_read == 0)
{
if (sos_flag == 1)
{
sleep(1);
sos_counter++;
if (sos_counter == 20)
{
gracefull_leave();
}
}
else
{
sleep(1);
sos_counter = 0;
sos_flag = 1;
printf(START RED "Connection with predecessor was closed without warning!\n");
sos_send(node, NULL, 1);
}
// printf(RED "The connections with my predecessor was unexpectedly closed!");
}
sscanf(message, "%s %s %s %s %s %s", args[0], args[1], args[2], args[3], args[4], args[5]);
log_info(message, "TCPin");
verbose("TCP message received on connection with predecessor");
if (strcmp(args[0], "PRED") == 0)
{
verbose("PRED command recived.");
close(node->fd_pred);
FD_CLR(node->fd_pred, &available_sockets);
TCP_pred_fd = self_send(args, node);
FD_SET(TCP_pred_fd, &available_sockets);
max_socket = max_socket > TCP_pred_fd ? max_socket : TCP_pred_fd + 1;
node->fd_pred = TCP_pred_fd;
verbose("Self sent to new predecessor and connection saved");
printf(START "├─Received: %s", message);
printf(START YLW "├─New connection established!" RESET "\n| ---%d─>%d─>%d---\n", node->pred_i, node->self_i, node->succ_i);
printf(START "└────\n");
write(1, INPUT, sizeof(INPUT));
}
else if (strcmp(args[0], "FND") == 0)
{
/*Received Find request*/
verbose("Received FND via TCP");
printf(START "├─Received new search: %s", message);
find_receive(args, node);
write(1, INPUT, sizeof(INPUT));
}
else if (strcmp(args[0], "RSP") == 0)
{
/*Received response to find request*/
verbose("Received RSP via TCP");
printf(START "├─Received new response: %s", message);
response_receive(args, node, addr_list);
write(1, INPUT, sizeof(INPUT));
}
else if (strcmp(args[0], "SELF") == 0)
{
node->succ_i = atoi(args[1]);
strcpy(node->succ_ip, args[2]);
strcpy(node->succ_port, args[3]);
node->fd_succ = node->fd_pred;
}
else if (strcmp(args[0], "SOS") == 0)
{
sos_recieve(args, node);
}
}
}
}
}
for (int i = 0; i <= 5; i++)
free(args[i]);
free(args);
free_node(node);
}