-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcpClient.c
369 lines (233 loc) · 10.4 KB
/
tcpClient.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#include "header.h"
#include "support.c"
struct sockaddr_in server_address;
int socket_descriptor;
char status[16] = "not connected.";
/* This is the SIGINT handler. It is used to ensure the correct
application exit, by informing server side about the closure
of the channel. */
void sigHandler() {
sigset_t set;
sigemptyset( &set );
sigaddset( &set, SIGINT );
sigprocmask( SIG_BLOCK, &set, 0 );
int ret, asw;
putchar('\n');
printf("\n\nDo you really want to exit? (1 for yes) : ");
ret = scanf( "%d", &asw );
if (ret == -1) Error_("error in function: scanf (sigHandler).", 1);
while( getchar() != '\n'){};
if ( asw == 1 ) goto exit;
signal( SIGINT, sigHandler );
sigprocmask( SIG_UNBLOCK, &set, 0 );
goto run;
exit:
unconnect();
exit(EXIT_SUCCESS);
run:
sigprocmask( SIG_UNBLOCK, &set, 0 );
}
int main (int argc, char** argv) {
sigset_t set;
sigemptyset( &set );
sigaddset( &set, SIGINT );
signal( SIGINT, sigHandler);
int ret;
gui:
display();
int operation;
printf("\n\n Insert operation code here : ");
ret = scanf( "%d", &operation);
if (ret == -1) Error_("error in function: scanf.", 1);
while( getchar() != '\n' );
switch( operation) {
case 0 :
if ( strcmp( "connected.", status) == 0 ) {
unconnect();
close( socket_descriptor );
memset( status, 0, strlen(status) );
sprintf( status, "not connected.");
goto gui; }
else try_connection();
goto gui;
case 1 :
sigprocmask( SIG_BLOCK, &set, 0);
request_1();
if( sigpending( &set ) ){
if ( sigismember(&set, SIGINT ) ) {
signal( SIGINT, sigHandler );
sigprocmask( SIG_UNBLOCK, &set, 0 );
}
}
sigemptyset( &set );
sigaddset( &set, SIGINT );
sigprocmask( SIG_UNBLOCK, &set, 0 );
goto gui;
case 2:
sigprocmask( SIG_BLOCK, &set, 0);
request_2();
if( sigpending( &set ) ){
if ( sigismember(&set, SIGINT ) ) {
signal( SIGINT, sigHandler );
sigprocmask( SIG_UNBLOCK, &set, 0 );
}
}
sigemptyset( &set );
sigaddset( &set, SIGINT );
sigprocmask( SIG_UNBLOCK, &set, 0 );
goto gui;
case 3:
sigprocmask( SIG_BLOCK, &set, 0);
request_3();
if( sigpending( &set ) ){
if ( sigismember(&set, SIGINT ) ) {
signal( SIGINT, sigHandler );
sigprocmask( SIG_UNBLOCK, &set, 0 );
}
}
sigemptyset( &set );
sigaddset( &set, SIGINT );
sigprocmask( SIG_UNBLOCK, &set, 0 );
goto gui;
default: goto gui;
}
return EXIT_SUCCESS;
}
/* By this function, the thin-client menu is printed on stdout. */
void display() {
printf("\e[1;1H\e[2J");
printf("....................................................................................\n");
printf("....................................................................................\n");
printf("..........................| CINEMA RESERVATIONS |.........................\n");
printf("....................................................................................\n");
printf("....................................................................................\n\n");
printf(" STATUS : %s \n", status);
printf(" CTRL + C to exit the application. \n\n\n");
printf(" _____ ________ ________ ____What are you looking for ?____ ________ _______ _______\n");
printf("| |\n");
printf("| OP 0 : Connect/Unconnect to CINEMA RESERVATIONS SERVER. |\n");
printf("| OP 1 : View the current state of today's seats reservation. |\n");
printf("| OP 2 : Reserve some seats for today's show. |\n");
printf("| OP 3 : Delete a reservation. |\n");
printf("|____ ________ ________ ________ ________ ________ ________ ________ ________ ______|\n\n");
fflush(stdout);
}
/* Connect to CINEMA RESERVATION Server */
void try_connection() {
int ret;
//open a session on a socket, identified by socket_descriptor.
socket_descriptor = socket( AF_INET, SOCK_STREAM, 0);
if (socket_descriptor == -1) Error:("Error in function: socket.", 1);
//set contents of sockaddr_in structure representing server's address.
memset( &server_address, 0, sizeof( struct sockaddr ));
server_address.sin_family = AF_INET;
server_address.sin_port = htons(PORT);
server_address.sin_addr.s_addr = inet_addr("127.0.0.1");
//try to connect to the server.
ret = connect( socket_descriptor, (struct sockaddr *) &server_address, sizeof( server_address ) );
if (ret == -1 ) {
printf("\n\nUnable to get a connection to Cinema Reservations Server. Please, try later.");
fflush(stdout);
return;
}
memset(status, 0, strlen(status));
strcpy(status, "connected.");
}
/* Close the connection to CINEMA RESERVATION Server */
void unconnect(){
char buff[MAX_LINE]; int ret, bytes = 0;
//set the request-code and send it to server.
sprintf( buff, "0\n" );
ret = Writeline( socket_descriptor, buff, strlen(buff) );
if (ret == -1) Error_("error in function: Writeline.", 1);
}
/* RETRIVE CURRENT CINEMA SEATS VIEW */
int request_1() {
char buff[MAX_LINE]; int ret, bytes = 0;
//set the request-code and send it to server.
sprintf( buff, "1\n" );
ret = Writeline( socket_descriptor, buff, strlen(buff) );
if (ret == -1) Error_("error in function: Writeline.", 1);
memset( buff, 0, strlen(buff) );
//get file size from server.
ret = Readline( socket_descriptor, buff, MAX_LINE);
if (ret == -1) Error_("error in function: Readline.", 1);
bytes = atoi( buff );
memset( buff, 0, strlen(buff) );
//read all file and print it on terminal.
printf("\e[1;1H\e[2J");
printf("\n\n\n *** CINEMA SEATS CURRENT STATE VIEW *** \n\n");
printf("\n\n\e[1;34m |.......................................................................|\e[0m \n");
printf( "\e[1;34m |............................. SCREEN ................................|\e[0m \n");
printf( "\e[1;34m |.......................................................................|\e[0m \n\n\n");
fflush(stdout);
int counter = 0;
while ( counter != bytes ) {
counter += Readline( socket_descriptor, buff, MAX_LINE );
if (counter == -1) Error_("error in function: read.", 1);
printf( "%s", buff);
fflush(stdout);
memset( buff , 0, MAX_LINE );
}
printf("\n Please, press a button to procede..."); fflush(stdout);
while(getchar() != '\n'){}
return 0;
}
/* FORWARD A RESERVATION REQUEST */
int request_2() {
int ret, numSeats, LINE, PLACE; char buff[MAX_LINE];
//set the request-code and send it to server.
sprintf( buff, "2\n" );
ret = Writeline( socket_descriptor, buff, strlen(buff));
if (ret == -1) Error_("error in function: Writeline.", 1);
memset( buff, 0, strlen(buff) );
printf("\n How many seats do you want to reserve?\n N : ");
ret = scanf("%d", &numSeats);
if (ret == -1) Error_("Error in function : scanf (request_2).", 1);
while( getchar() != '\n') {}
printf(" Please provide seats positions :");
for (int i = 0; i < numSeats; i ++) {
printf("\n SEAT RESERVE N°%d :\n LINE ", i );
ret = scanf( "%d", &LINE );
if (ret == -1) Error_("Error in function : scanf (request_2).", 1);
while( getchar() != '\n') {}
printf("\n PLACE ");
ret = scanf( "%d", &PLACE );
if (ret == -1) Error_("Error in function : scanf (request_2).", 1);
while( getchar() != '\n') {}
sprintf( buff + strlen(buff), "%d:%d ", LINE, PLACE );
} sprintf(buff + strlen(buff), "\n");
ret = Writeline( socket_descriptor, buff, strlen(buff));
if (ret == -1) Error_("Error in function : Writeline (request 2)", 1);
memset( buff, 0, sizeof(buff) );
ret = Readline( socket_descriptor, buff, MAX_LINE );
if (ret == -1) Error_("Error in function : Readline (request 2)", 1);
if ( strcmp( buff, "ABORT") == 0 ) {
printf("\n\n ABORT TRANSACTION : Unable to reserve these places for today's show.");
fflush(stdout);
while(getchar() != '\n'){}
return 0;
}
else{
printf(" TRANSACTION COMPLETE : Your reservation code is %s", buff );
fflush(stdout);
while(getchar() != '\n'){}
return 0;
}
}
/* FORWARD A CANCELLATION REQUEST */
int request_3() {
char buff[MAX_LINE]; int ret, bytes = 0;
//set the request-code and send it to server.
sprintf( buff, "3\n" );
ret = Writeline( socket_descriptor, buff, strlen(buff) );
if (ret == -1) Error_("error in function: Writeline.", 1);
memset( buff, 0, strlen(buff) );
//insert reservation code for cancellation.
printf("\n Please, insert your reservation code here : "); fflush(stdout);
ret = scanf("%s", buff);
if (ret == -1) Error_("Error in function : scanf (request 3).", 1);
while( getchar()!= '\n') {}
ret = Writeline( socket_descriptor, buff, strlen(buff) + 1 );
if (ret == -1) Error_("error in function: Writeline.", 1);
}