-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.c
More file actions
111 lines (101 loc) · 3.06 KB
/
client.c
File metadata and controls
111 lines (101 loc) · 3.06 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include "client.h"
#include "commands.h"
//global stuff
int socket_http;
char* session_token = NULL;
const char* library_access_jwt = NULL;
int connection_socket(char* ip, uint16_t port) {
int sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0) {
perror("socket creation failed");
return -1;
}
struct sockaddr_in server_addr;
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(port);
inet_pton(AF_INET, ip, &server_addr.sin_addr);
if (connect(sock, (struct sockaddr*)&server_addr, sizeof(server_addr)) < 0) {
perror("connection failed");
return -1;
}
return sock;
}
int set_global_token(char** token2change, char* value, int len) {
*token2change = calloc(1, len + 1);
memcpy(*token2change, value, len);
(*token2change)[len] = '\0';
return 0;
}
int unset_global_token(char** token2unset) {
free(*token2unset);
*token2unset = NULL;
return 0;
}
//HOST: 63.32.125.183 PORT: 8081
int execute(char* command) {
if (strcmp(command, "login_admin") == 0) {
login_admin();
} else if (strcmp(command, "add_user") == 0) {
add_user();
} else if (strcmp(command, "get_users") == 0) {
get_users();
} else if (strcmp(command, "delete_user") == 0) {
delete_user();
} else if (strcmp(command, "logout_admin") == 0) {
logout_admin();
} else if (strcmp(command, "login") == 0) {
login();
} else if (strcmp(command, "get_access") == 0) {
get_access();
} else if (strcmp(command, "get_movies") == 0) {
get_movies();
} else if (strcmp(command, "get_movie") == 0) {
get_movie();
} else if (strcmp(command, "add_movie") == 0) {
add_movie();
} else if (strcmp(command, "delete_movie") == 0) {
delete_movie();
} else if (strcmp(command, "update_movie") == 0) {
update_movie();
} else if (strcmp(command, "get_collections") == 0) {
get_collections();
} else if (strcmp(command, "get_collection") == 0) {
get_collection();
} else if (strcmp(command, "add_collection") == 0) {
add_collection();
} else if (strcmp(command, "delete_collection") == 0) {
delete_collection();
} else if (strcmp(command, "add_movie_to_collection") == 0) {
add_movie_to_collection();
} else if (strcmp(command, "delete_movie_from_collection") == 0) {
delete_movie_from_collection();
} else if (strcmp(command, "logout") == 0) {
logout();
} else if (strcmp(command, "exit") == 0) {
return 1;
} else {
puts("Error: Wrong command!");
}
return 0;
}
int main() {
socket_http = connection_socket(IP, PORT);
char* command = calloc(128, 1);
puts("Begin:");
while(1){
scanf("%s", command);
if (execute(command) != 0) {
puts("Exiting...");
break;
}
}
close(socket_http);
return 0;
}