-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.c
More file actions
330 lines (298 loc) · 11.1 KB
/
client.c
File metadata and controls
330 lines (298 loc) · 11.1 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
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
327
328
329
330
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include "helpers.h"
#include "requests.h"
#include "parson.h"
/* Functia reprezinta executarea unei cereri de register */
void register_cmd() {
char *username = malloc(MAX * sizeof(char));
char *password = malloc(MAX * sizeof(char));
printf("username=");
get_input(&username);
printf("password=");
get_input(&password);
int sockfd = open_connection("34.241.4.235", 8080, AF_INET, SOCK_STREAM, 0);
JSON_Value *root_value = json_value_init_object();
JSON_Object *root_object = json_value_get_object(root_value);
char *serialized_string = NULL;
json_object_set_string(root_object, "username", username);
json_object_set_string(root_object, "password", password);
serialized_string = json_serialize_to_string_pretty(root_value);
char *message = compute_post_request("34.241.4.235", "/api/v1/tema/auth/register", "application/json", &serialized_string, 1, NULL, 1, NULL);
send_to_server(sockfd, message);
char *response = receive_from_server(sockfd);
int code = check_response(response);
if (code == 201) {
printf("Contul a fost creat cu succes!\n");
} else if (code == 400) {
printf("Username-ul %s este deja utilizat!\n", username);
} else {
printf("Eroare la crearea user-ului!\n");
}
json_free_serialized_string(serialized_string);
json_value_free(root_value);
close(sockfd);
free(message);
free(response);
free(username);
free(password);
}
/* Functia reprezinta executarea unei cereri de login */
void login_cmd(char **cookies) {
char *username = malloc(MAX * sizeof(char));
char *password = malloc(MAX * sizeof(char));
printf("username=");
get_input(&username);
printf("password=");
get_input(&password);
int sockfd = open_connection("34.241.4.235", 8080, AF_INET, SOCK_STREAM, 0);
JSON_Value *root_value = json_value_init_object();
JSON_Object *root_object = json_value_get_object(root_value);
char *serialized_string = NULL;
json_object_set_string(root_object, "username", username);
json_object_set_string(root_object, "password", password);
serialized_string = json_serialize_to_string_pretty(root_value);
char *message = compute_post_request("34.241.4.235", "/api/v1/tema/auth/login", "application/json", &serialized_string, 1, NULL, 1, NULL);
send_to_server(sockfd, message);
char *response = receive_from_server(sockfd);
int code = check_response(response);
if (code == 200) {
*cookies = get_cookie(response);
printf("Te-ai logat cu succes!\n");
} else if (code == 400) {
printf("Username sau parola gresita\n");
} else {
printf("Eroare la logare!\n");
}
json_free_serialized_string(serialized_string);
json_value_free(root_value);
close(sockfd);
free(message);
free(response);
free(username);
free(password);
}
/* Functia reprezinta executarea unei cereri de acces in librarie */
void enter_library(char *cookies, char **token) {
int sockfd = open_connection("34.241.4.235", 8080, AF_INET, SOCK_STREAM, 0);
char *message = compute_get_request("34.241.4.235", "/api/v1/tema/library/access", NULL, &cookies, 1, NULL);
send_to_server(sockfd, message);
char *response = receive_from_server(sockfd);
*token = get_token(response);
close(sockfd);
free(message);
free(response);
}
/* Functia reprezinta executarea unei cereri de afisare a tuturor cartilor */
void get_books(char *cookies, char *token) {
int sockfd = open_connection("34.241.4.235", 8080, AF_INET, SOCK_STREAM, 0);
char *message = compute_get_request("34.241.4.235", "/api/v1/tema/library/books", NULL, NULL, 1, token);
send_to_server(sockfd, message);
char *response = receive_from_server(sockfd);
int code = check_response(response);
if (code != 403) {
char *booksList = compute_books(response);
JSON_Value *json_value = json_parse_string(booksList);
free(booksList);
booksList = json_serialize_to_string_pretty(json_value);
json_value_free(json_value);
printf("%s\n", booksList);
free(booksList);
} else {
printf("Nu aveti acces la biblioteca!\n");
}
close(sockfd);
free(message);
free(response);
}
/* Functia reprezinta executarea unei cereri de afisare a unei carti specifice */
void get_book(char *cookies, char *token) {
char *id = malloc(MAX * sizeof(char));
printf("id=");
get_input(&id);
char *address = malloc(MAX * sizeof(char));
strcpy(address, "/api/v1/tema/library/books/");
strcat(address, id);
int sockfd = open_connection("34.241.4.235", 8080, AF_INET, SOCK_STREAM, 0);
char *message = compute_get_request("34.241.4.235", address, NULL, NULL, 1, token);
send_to_server(sockfd, message);
char *response = receive_from_server(sockfd);
int code = check_response(response);
if (code == 200) {
char *bookInfo = compute_books(response);
JSON_Value *json_value = json_parse_string(bookInfo);
free(bookInfo);
bookInfo = json_serialize_to_string_pretty(json_value);
printf("%s\n", bookInfo);
free(bookInfo);
json_value_free(json_value);
} else if (code == 404) {
printf("Cartea cu id-ul %s nu exista!\n", id);
} else if (code == 403) {
printf("Nu aveti acces la biblioteca!\n");
} else {
printf("A intervenit o eroare!\n");
}
close(sockfd);
free(message);
free(response);
free(address);
free(id);
}
/* Functia reprezinta executarea unei cereri de adaugare a unei carti */
void add_book(char *cookies, char *token) {
char **in = (char **)malloc(5 * sizeof(char *));
for (int i = 0; i < 5; i++) {
in[i] = (char *)malloc(MAX * sizeof(char));
}
printf("title=");
get_input_book(&in[0]);
printf("author=");
get_input_book(&in[1]);
printf("genre=");
get_input_book(&in[2]);
printf("publisher=");
get_input_book(&in[3]);
printf("page_count=");
get_input_book(&in[4]);
int sockfd = open_connection("34.241.4.235", 8080, AF_INET, SOCK_STREAM, 0);
JSON_Value *root_value = json_value_init_object();
JSON_Object *root_object = json_value_get_object(root_value);
char *serialized_string = NULL;
json_object_set_string(root_object, "title", in[0]);
json_object_set_string(root_object, "author", in[1]);
json_object_set_string(root_object, "genre", in[2]);
json_object_set_string(root_object, "publisher", in[3]);
json_object_set_number(root_object, "page_count", atoi(in[4]));
serialized_string = json_serialize_to_string_pretty(root_value);
char *message = compute_post_request("34.241.4.235", "/api/v1/tema/library/books", "application/json", &serialized_string, 1, NULL, 1, token);
send_to_server(sockfd, message);
char *response = receive_from_server(sockfd);
int code = check_response(response);
if (code == 200)
printf("Cartea %s s-a adaugat cu succes!\n", in[0]);
else if (code == 403)
printf("Nu aveti acces la biblioteca!\n");
else
printf("A intervenit o eroare in adaugarea cartii!\n");
json_free_serialized_string(serialized_string);
json_value_free(root_value);
close(sockfd);
for (int i = 0; i < 5; i++) {
free(in[i]);
}
free(in);
free(message);
free(response);
}
/* Functia reprezinta executarea unei cereri de steregere a unei carti */
void delete_book(char *cookies, char *token) {
char *id = malloc(MAX * sizeof(char));
printf("id=");
get_input(&id);
char *address = malloc(MAX * sizeof(char));
strcpy(address, "/api/v1/tema/library/books/");
strcat(address, id);
int sockfd = open_connection("34.241.4.235", 8080, AF_INET, SOCK_STREAM, 0);
char *message = compute_delete_request("34.241.4.235", address, NULL, NULL, 1, token);
send_to_server(sockfd, message);
char *response = receive_from_server(sockfd);
int code = check_response(response);
if (code == 200)
printf("Cartea cu id-ul %s s-a sters cu succes!\n", id);
else if(code == 403)
printf("Nu aveti acces la biblioteca!\n");
else
printf("Cartea nu exista sau a intervenit o eroare!\n");
close(sockfd);
free(message);
free(response);
free(address);
free(id);
}
/* Functia reprezinta executarea unei cereri de delogare*/
void logout_cmd(char **cookies, char **token) {
if (cookies != NULL) {
printf("Te-ai delogat cu succes!\n");
free(*cookies);
*cookies = NULL;
if (*token != NULL) {
free(*token);
*token = NULL;
}
} else {
printf("Nu esti logat!\n");
}
}
int main() {
char *cookies = NULL; // Cookie-ul primit la login
char *token = NULL; // Token-ul de acces pentru biblioteca
int running = 1;
while (running) {
char *command = malloc(MAX * sizeof(char));
fgets(command, MAX, stdin);
command = strtok(command, " \n");
if (!strcmp(command, "register")) {
if (cookies == NULL) {
register_cmd();
} else {
printf("Pentru a crea un cont nou trebuie sa fii delogat!\n");
}
} else if (!strcmp(command, "login")) {
if (cookies == NULL) {
login_cmd(&cookies);
} else {
printf("Esti deja logat!\n");
}
} else if (!strcmp(command, "enter_library")) {
if (cookies != NULL) {
enter_library(cookies, &token);
printf("Acces verificat in librarie!\n");
} else {
printf("Pentru a accesa biblioteca trebuie sa fii logat!\n");
}
} else if (!strcmp(command, "get_books")) {
if (cookies != NULL)
get_books(cookies, token);
else
printf("Nu sunteti logat!\n");
} else if (!strcmp(command, "get_book")) {
if (cookies != NULL)
get_book(cookies, token);
else
printf("Nu sunteti logat!\n");
} else if (!strcmp(command, "add_book")) {
if (cookies != NULL)
add_book(cookies, token);
else
printf("Nu sunteti logat!\n");
} else if (!strcmp(command, "delete_book")) {
if (cookies != NULL)
delete_book(cookies, token);
else
printf("Nu sunteti logat!\n");
} else if (!strcmp(command, "logout")) {
if (cookies != NULL)
logout_cmd(&cookies, &token);
else
printf("Pentru a te deloga trebuie sa fii mai intai logat!\n");
} else if (!strcmp(command, "exit")) {
printf("Se inchide clientul...\n");
running = 0;
} else {
printf("Comanda nu exista!\n");
}
free(command);
}
if (cookies != NULL)
free(cookies);
if (token != NULL)
free(token);
return 0;
}