-
Notifications
You must be signed in to change notification settings - Fork 0
/
cinema_client.c
224 lines (177 loc) · 5.47 KB
/
cinema_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
#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <pthread.h>
#include <string.h>
#include <signal.h>
int socket_descriptor;
struct seat{
unsigned int row;
unsigned int col;
};
/*
* functions declarations
*/
void action_chooser();
void show_seatsmap(char * option);
void print_map(char * mbuffer, unsigned int cinema_raws, unsigned int cinema_clmn);
void seats_reservation(char * option);
int connect_function();
void seats_reservation(char * option) {
int res;
char line[100];
char res_key[11];
unsigned int seats_num,r,c;
//loop until sscanf return 0;
do{
printf("Insert the number of seats you want to reserve: \n");
fgets(line,100,stdin);
res = sscanf(line,"%u\n",&seats_num);
} while(res < 1);
if (seats_num == 0) exit(0);
struct seat seats[seats_num];
int i = 0;
//loop until sscanf returns one or less value
while (i < seats_num) {
do {
printf("Insert rows and columns for seats[%d]: ",i);
fflush(stdout);
fgets(line,100,stdin);
res = sscanf(line,"%u %u",&seats[i].row,&seats[i].col);
} while(res<2);
i++;
}
connect_function();
res = write(socket_descriptor,option,10);
if(res < 10) { perror("reservation send error, option"); exit(1); }
//sends the number of seats you want to book
res = write(socket_descriptor,&seats_num,sizeof(seats_num));
if(res < sizeof(seats_num)) { perror("reservation send error, seats num"); exit(1); }
//sends the coordinates of the seats you have chosen
res = write(socket_descriptor,seats,sizeof(seats));
if(res < sizeof(seats)) { perror("reservation send error, seats data"); exit(1); }
res = read(socket_descriptor,line,11);
if (res < 11 ) { perror("error reading response"); exit(1);}
if (strcmp(line,"RES_ERR") == 0) {
printf("---------------------------------------\n");
printf("| There has been some errors |\n");
printf("| please try again |\n");
printf("---------------------------------------\n");
close(socket_descriptor);
exit(1);
} else {
printf(" -----------------------------------------------------\n");
printf("|Your reservation code is %s, don't forget it |\n",line);
printf(" -----------------------------------------------------\n");
close(socket_descriptor);
exit(1);
}
}
void show_seatsmap(char * option) {
int i,j;
unsigned int cinema_raws;
unsigned int cinema_clmn;
char temp_read[1];
char temp[10];
char * endptr;
char * mbuffer;
connect_function();
write(socket_descriptor,option,10);
read(socket_descriptor,temp,3);
cinema_raws = strtol(temp,&endptr,10);
read(socket_descriptor,temp,3);
cinema_clmn = strtol(temp,&endptr,10);
size_t size = (cinema_raws*cinema_clmn);
mbuffer = (char *)malloc(size*sizeof(char));
memset(mbuffer,0,size*sizeof(char));
char (*matrix)[cinema_clmn] = (char (*) [cinema_clmn])mbuffer;
for(i = 0; i < cinema_raws; i++) {
for(j = 0; j < cinema_clmn; j++) {
read(socket_descriptor,temp_read,1);
sscanf(temp_read," %c",&matrix[i][j]);
}
}
printf("---------------------------------\n");
printf("There are overall %d seats\n",(int)size);
printf("---------------------------------\n");
print_map(mbuffer,cinema_raws,cinema_clmn);
printf("---------------------------------\n");
close(socket_descriptor);
action_chooser();
}
void delete_reservation(char * option) {
char key[11];
printf("Insert your reservation code\n");
fgets(key,11,stdin);
key[11] = '\0';
connect_function();
write(socket_descriptor,option,10);
if(write(socket_descriptor,key,20) == -1) { perror("Write error in delete_reservation"); }
if(read(socket_descriptor,key,20) == -1) { perror("Read error in delete_reservation"); }
if(strcmp(key,"DEL_CONFIRMED") == 0 ) {
printf("Your reservation has been deleted succesfully!\n");
} else {
printf("Something has gone wrong, please try again!\n");
}
close(socket_descriptor);
exit(0);
}
void print_map(char * mbuffer,unsigned int cinema_raws,unsigned int cinema_clmn) {
int i,j;
char (*matrix)[cinema_clmn] = (char (*) [cinema_clmn])mbuffer;
for(i = 0; i < cinema_raws; i++) {
for(j = 0; j < cinema_clmn;j++) {
printf("[%d - %d : %c]",i,j,matrix[i][j]);
}
printf("\n");
}
}
void action_chooser() {
char option[10];
printf("=========================================\n");
printf("What to do?\n\n");
printf("-S show the seats map.\n");
printf("-R reserve one or more seats.\n");
printf("-D cancel reservation.\n");
printf("-E terminate the session.\n");
printf("=========================================\n");
fgets(option,10,stdin);
do {
if (strcmp(option,"-S\n")==0) {
show_seatsmap(option);
}
else if (strcmp(option,"-R\n")==0) {
seats_reservation(option);
}
else if (strcmp(option,"-D\n")==0) {
delete_reservation(option);
}
else if (strcmp(option,"-E\n")==0) {
exit(1);
}
else {
printf("Error: use -R -S -D -E\n");
}
fgets(option,10,stdin);
} while(1);
}
int connect_function() {
int ds_sock;
int length_addr;
int port = 4446;
char * ip = "127.0.0.1"; //assuming that the servers is running locally
struct sockaddr_in addr;
socket_descriptor = socket(AF_INET,SOCK_STREAM,0);
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
inet_aton(ip,&addr.sin_addr);
length_addr = sizeof(addr);
if(connect(socket_descriptor,(struct sockaddr *)&addr,length_addr)==-1) { perror("Connection Error"); exit(1); }
printf("--Estabilished connection with the server--\n");
}
int main() {
action_chooser();
}