-
Notifications
You must be signed in to change notification settings - Fork 0
/
pa3_client.c
268 lines (254 loc) · 7.55 KB
/
pa3_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
#include "tlv.h"
#include <arpa/inet.h>
#include <netinet/in.h>
#include <readline/history.h>
#include <readline/readline.h>
#include <stdio.h>
#include <sys/socket.h>
int client_fd, port;
bool isLoggedIn = false;
int loggedInUserID = -1;
bool isExit = false;
void handleRecvTLV(int actionID, uint32_t userID, int seatNumber) {
// 서버의 응답 대기 및 수신
tlv_t resTLV;
bool hasBooked = false;
if (recv_tlv(client_fd, &resTLV) == 0) {
// 응답 처리
// printf("Received response - Type: %u, Length: %u\n", resTLV.type,
// resTLV.length);
// 여기서 resTLV.value를 사용하여 응답 처리 로직 추가
Res *res = (Res *)resTLV.value;
// printf("Action ID: %d, Code: %d\n", res->actionID, res->code);
for (int i = 1; i <= 256; i++) {
if (res->seats[i]) {
hasBooked = true;
break;
}
}
char *ERROR_MSG = NULL;
switch (actionID) {
case 0:
if (res->code == 0)
isExit = true;
else if (res->code == 1) {
ERROR_MSG = "arguments are invalid";
}
if (ERROR_MSG)
fprintf(stderr, "Failed to disconnect as %s.\n", ERROR_MSG);
else
fprintf(stdout, "Connection terminated.\n");
break;
case 1:
if (res->code == 0) {
isLoggedIn = true;
loggedInUserID = userID;
} else if (res->code == 1) {
ERROR_MSG = "user is active";
} else if (res->code == 2) {
ERROR_MSG = "client is active";
} else if (res->code == 3) {
ERROR_MSG = "password is incorrect";
}
if (ERROR_MSG)
fprintf(stderr, "Failed to login as %s.\n", ERROR_MSG);
else
fprintf(stdout, "Logged in successfully.\n");
break;
case 2:
if (res->code == 0) {
;
} else if (res->code == 1) {
ERROR_MSG = "user is not logged in";
} else if (res->code == 2) {
ERROR_MSG = "seat is unavailable";
} else if (res->code == 3) {
ERROR_MSG = "seat number is out of range";
}
if (ERROR_MSG)
fprintf(stderr, "Failed to book as %s.\n", ERROR_MSG);
else
fprintf(stdout, "Booked seat %d.\n", seatNumber);
break;
case 3:
if (res->code == 0) {
if (hasBooked) {
fprintf(stdout, "Booked seats ");
bool isFirst = true;
for (int i = 1; i <= 256; i++) {
if (res->seats[i]) {
if (isFirst) {
fprintf(stdout, "%d", i);
isFirst = false;
} else
fprintf(stdout, ", %d", i);
}
}
fprintf(stdout, ".\n");
} else
fprintf(stdout, "Did not book any seats.\n");
} else if (res->code == 1) {
ERROR_MSG = "user is not logged in";
fprintf(stderr, "Failed to confirm booking as %s.\n", ERROR_MSG);
}
break;
case 4:
if (res->code == 0) {
;
} else if (res->code == 1) {
ERROR_MSG = "user is not logged in";
} else if (res->code == 2) {
ERROR_MSG = "user did not book the specified seat";
} else if (res->code == 3) {
ERROR_MSG = "seat number is out of range";
}
if (ERROR_MSG)
fprintf(stderr, "Failed to cancel booking as %s.\n", ERROR_MSG);
else
fprintf(stdout, "Canceled seat %d.\n", seatNumber);
break;
case 5:
if (res->code == 0) {
isLoggedIn = false;
loggedInUserID = -1;
} else if (res->code == 1) {
ERROR_MSG = "user is not logged in";
}
if (ERROR_MSG)
fprintf(stderr, "Failed to logout as %s.\n", ERROR_MSG);
else
fprintf(stdout, "Logged out successfully.\n");
break;
default:
fprintf(stderr, "Action %d is unknown\n", actionID);
break;
}
free(resTLV.value);
} else {
fprintf(stderr, "Failed to receive response from server.\n");
}
}
void processInput(char *line) {
if (strlen(line) > 0) {
add_history(line);
uint32_t userID = 0;
int idx = 0, actionID = -1, seatNumber = -1;
char *password = NULL;
char *ptr = strtok(line, " ");
while (ptr != NULL) {
if (idx == 0) {
userID = atoi(ptr);
} else if (idx == 1) {
actionID = atoi(ptr);
} else if (idx == 2 && (actionID == 0 || actionID == 2 || actionID == 3 ||
actionID == 4 || actionID == 5)) {
seatNumber = atoi(ptr);
} else if (idx == 2 && actionID == 1) {
// make new string and copy
password = strdup(ptr); // 문자열 복사
// printf("password: %s\n", password);
} else {
;
// fprintf(stderr, "Invalid input\n");
}
ptr = strtok(NULL, " "); // 다음 문자열을 잘라서 포인터를 반환
idx++;
}
size_t password_len =
password ? strlen(password) + 1 : 0; // 널 문자 포함 길이
size_t query_len = sizeof(Query) - sizeof(char *) + password_len;
uint8_t *query_buffer = malloc(query_len);
Query query = {
.userID = userID,
.actionID = actionID,
.seatNumber = seatNumber,
.password = NULL,
};
if (query.actionID == 0 && isLoggedIn) {
Query prefixQuery = {
.userID = loggedInUserID,
.actionID = 5,
.seatNumber = 0,
.password = NULL,
};
size_t prefix_password_len = 0;
size_t prefix_query_len = sizeof(Query) - sizeof(char *) + password_len;
uint8_t *prefix_query_buffer = malloc(prefix_query_len);
memcpy(prefix_query_buffer, &prefixQuery, sizeof(Query) - sizeof(char *));
tlv_t tlv = {
.type = 0,
.length = prefix_query_len,
.value = prefix_query_buffer,
};
send_tlv(client_fd, &tlv);
free(prefix_query_buffer);
handleRecvTLV(5, loggedInUserID, 0);
}
memcpy(query_buffer, &query, sizeof(Query) - sizeof(char *));
if (password) {
memcpy(query_buffer + sizeof(Query) - sizeof(char *), password,
password_len);
}
tlv_t tlv = {
.type = 0,
.length = query_len,
.value = query_buffer,
};
send_tlv(client_fd, &tlv);
free(query_buffer);
if (password)
free(password);
handleRecvTLV(actionID, userID, seatNumber);
free(line);
}
}
void processFile(const char *filename) {
FILE *file = fopen(filename, "r");
if (!file) {
perror("fopen");
exit(1);
}
char *line = NULL;
size_t len = 0;
ssize_t read;
while ((read = getline(&line, &len, file)) != -1) {
if (line[read - 1] == '\n') {
line[read - 1] = '\0'; // Remove newline character
}
processInput(strdup(line)); // Duplicate line for processing
}
free(line);
fclose(file);
// Send termination request to server
processInput(strdup("0 0 0"));
}
int main(int argc, char *argv[]) {
if (argc != 3 && argc != 4) {
fprintf(stderr, "Usage: %s ip_address port [file]\n", argv[0]);
exit(1);
}
port = atoi(argv[2]);
struct sockaddr_in server_addr;
client_fd = socket(AF_INET, SOCK_STREAM, 0);
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(port);
server_addr.sin_addr.s_addr = inet_addr(argv[1]); // Use provided IP address
if (connect(client_fd, (struct sockaddr *)&server_addr,
sizeof(server_addr)) == -1) {
perror("connect");
exit(1);
}
if (argc == 4) {
processFile(argv[3]); // Process the provided file
} else {
char *line;
while ((line = readline("")) != NULL) {
processInput(line);
if (isExit)
break;
}
}
close(client_fd);
return 0;
}