-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.c
294 lines (238 loc) · 6.84 KB
/
client.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
/**
* vim: sts=2 ts=2 sw=2 et
* Server for Chat
*
* Dalton Pinto
*/
#include "common.c"
#include <time.h>
#include <netinet/tcp.h>
// ------- Global Variables --------
server_info servers[SERVER_LIST_SIZE];
// Porta: unsigned short (2 bytes)
// IP: unsigned int (4 bytes)
// ------- Function Prototypes --------
// void error(char *msg);
// string string_create(char *);
// string recv_string(int sock);
// void send_string(int sock, string str);
// void free_string(string str);
// ------- Commands Prototypes --------
void cmd_list_servers(int sock);
void cmd_connect(int sock);
void cmd_send_message(int sock);
void cmd_heartbeat(int sock);
void cmd_list_users(int sock);
void cmd_echo(int sock);
void cmd_quit(int sock);
// ------- Server Response Prototypes --------
void server_sent_message(int sock);
void server_listed_users(int sock);
void server_echoed(int sock);
// ------- Helper Prototypes --------
int handle_user(char, int);
int handle_server(char, int);
void *listening(void *);
void *heart_beating(void *);
void send_string_command(int, char);
int get_socket(char *, int);
void choose_server(char *, int *);
// ------- Very Simple Functions --------
// void sigquit(){ exit(0); }
int server_socket, coordinator_socket;
pthread_t listening_thread, beating_thread;
void sigquit(){
pthread_cancel(listening_thread);
pthread_cancel(beating_thread);
close(server_socket);
exit(0);
}
int main(int argc, char *argv[])
{
// int server_socket, coordinator_socket, port;
int port;
char string_ip[15], code;
// pthread_t listening_thread, beating_thread;
void *arg;
signal(SIGQUIT, sigquit); // Waiting to be killed (kill -QUIT <pid>)
if(argc != 3){
strcpy(string_ip, COORDINATOR_IP);
port = COORDINATOR_PORT;
coordinator_socket = get_socket(string_ip, port);
cmd_list_servers(coordinator_socket);
close(coordinator_socket);
choose_server(string_ip, &port);
}else{
strcpy(string_ip, argv[1]);
port = atoi(argv[2]);
}
server_socket = get_socket(string_ip, port);
cmd_connect(server_socket);
// Started Listening Thread that listens server responses
arg = malloc(sizeof(server_socket));
memcpy(arg, &server_socket, sizeof(server_socket));
pthread_create(&listening_thread, 0, listening, arg);
// Started Heartbeat Thread that keeps beating for each 5 seconds
arg = malloc(sizeof(server_socket));
memcpy(arg, &server_socket, sizeof(server_socket));
pthread_create(&beating_thread, 0, heart_beating, arg);
// Main loop that receives users commands
while(1){
scanf("%c", &code);
while(code == '\n') scanf("%c", &code);
if( handle_user(code, server_socket) ) break;
}
pthread_cancel(listening_thread);
pthread_cancel(beating_thread);
close(server_socket);
return 0;
}
// ------- Commands Functions --------
void cmd_list_servers(int sock){
int bytes_recieved, i;
char send_data[1024], recv_data[1024];
send(sock, "C", sizeof(char), 0);
recv(sock, recv_data, sizeof(char), 0);
if(recv_data[0] != 'C') error("ERROR Invalid Command");
for(i = 0; i < SERVER_LIST_SIZE; i++){
recv(sock, &(servers[i]), sizeof(server_info), 0);
printf("<%d> IP: %d.%d.%d.%d Porta: %d\n", i, servers[i].ip[0], servers[i].ip[1], servers[i].ip[2], servers[i].ip[3], servers[i].port);
}
fflush(stdout);
}
void cmd_connect(int sock){
send_string_command(sock, 'C');
}
void cmd_send_message(int sock){
send_string_command(sock, 'M');
}
void cmd_heartbeat(int sock){
send(sock, "H", sizeof(char), 0);
}
void cmd_list_users(int sock){
send(sock, "L", sizeof(char), 0);
}
void cmd_quit(int sock) {
send(sock, "Q", sizeof(char), 0);
}
void cmd_echo(int sock){
send_string_command(sock, 'E');
}
// ------- Server Responses --------
void server_sent_message(int sock){
string sender, message;
sender = recv_string(sock);
message = recv_string(sock);
printf("Message from %s:\n%s\n", sender.str, message.str);
fflush(stdout);
free_string(sender);
free_string(message);
}
void server_listed_users(int sock){
int size, i;
string str;
recv(sock, &size, sizeof(int), 0);
printf("Clients (%d):\n", size);
for(i = 0; i < size; i++){
str = recv_string(sock);
printf("%s\n", str.str);
free_string(str);
}
fflush(stdout);
}
void server_echoed(int sock){
string str;
str = recv_string(sock);
printf("Echoed:\n%s\n", str.str);
fflush(stdout);
free_string(str);
}
// ------- Helper Functions --------
int handle_user(char code, int sock){
switch(code){
case 'C':
cmd_connect(sock); break;
case 'M':
cmd_send_message(sock); break;
case 'H':
cmd_heartbeat(sock); break;
case 'L':
cmd_list_users(sock); break;
case 'E':
cmd_echo(sock); break;
case 'Q':
cmd_quit(sock);
return 1;
default:
printf("UNKNOWN COMMAND!\n");
fflush(stdout);
}
return 0;
}
int handle_server(char code, int sock){
// printf("CODE: %c, %d\n", code, (int) code);
switch(code){
case 'M':
server_sent_message(sock); break;
case 'L':
server_listed_users(sock); break;
case 'E':
server_echoed(sock); break;
default:
recv(sock, &code, sizeof(char), 0);
handle_server(code, sock);
// return 1;
}
return 0;
}
void *listening(void *arg){
int sock = *((int*)arg);
char code;
free(arg);
while(1){
recv(sock, &code, sizeof(char), 0);
if( handle_server(code, sock) ) error("ERROR Something went wrong...");
}
}
void *heart_beating(void *arg){
int sock = *((int*)arg);
free(arg);
while(1){
cmd_heartbeat(sock);
sleep(5);
}
}
void send_string_command(int sock, char cmd){
char str[100];
string sized_str;
fgets(str, 100, stdin);
while(strcmp(str, "\n") == 0) fgets(str, 100, stdin);
send(sock, &cmd, 1, 0);
sized_str = string_create(str);
send_string(sock, sized_str);
free_string(sized_str);
}
int get_socket(char *ip_address, int port){
int sock;
struct sockaddr_in coordinator_address;
struct hostent *host;
sock = socket(AF_INET, SOCK_STREAM, 0);
if(sock < 0) error("ERROR opening coordinator socket");
host = gethostbyname(ip_address);
coordinator_address.sin_family = AF_INET;
coordinator_address.sin_port = htons(port);
coordinator_address.sin_addr = *((struct in_addr *)host->h_addr);
bzero(&(coordinator_address.sin_zero),8);
if( connect(sock, (struct sockaddr *)&coordinator_address, sizeof(coordinator_address)) < 0 ) error("ERROR connecting");
return sock;
}
void choose_server(char *string_ip, int *port){
int code;
printf("Choose server (0-9):\n");
scanf("%d", &code);
if(code > 9 || code < 0){ printf("\n::%d::\n", code); error("ERROR Type number between 0 or 9"); }
sprintf(string_ip, "%d.%d.%d.%d", servers[code].ip[0], servers[code].ip[1], servers[code].ip[2], servers[code].ip[3]);
*port = servers[code].port;
printf("Server => IP: %s Porta: %d\n", string_ip, *port);
fflush(stdout);
}