Skip to content

Commit 25bf96e

Browse files
committed
Implemented booking for users(w/o cancel booking)
1 parent b75a65a commit 25bf96e

File tree

2 files changed

+208
-41
lines changed

2 files changed

+208
-41
lines changed

client.c

+86-17
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#define PASSWORD_LEN 16
1414
#define USERNAME_LEN 20
1515
#define TRNAME_LEN 30
16+
#define TRAVELLER_LEN 20
1617
#define PORT_NO 5000
1718
#define ADMIN_ACC_TYPE 0
1819
#define NORMAL_ACC_TYPE 1
@@ -38,6 +39,16 @@ typedef struct train{
3839
int status;
3940
}struct_train;
4041

42+
typedef struct booking{
43+
int book_no;
44+
int trn_no;
45+
int acc_no;
46+
char name[TRAVELLER_LEN];
47+
int age;
48+
int seats;
49+
int status;
50+
}struct_booking;
51+
4152
static struct_customer CURRENT_USER;
4253

4354
/* Implementation of login functionality.
@@ -82,14 +93,87 @@ int login(int conn_fd){
8293
return 0;
8394
}
8495

96+
void display_train(struct_train train){
97+
printf(" \t%d \t%s\t \t%d\t\t \t%d\t \t%d\n",train.trn_no,train.trn_name,train.trn_avl_seats,train.trn_book_seats,train.status);
98+
}
99+
100+
void show_all_train(int conn_fd){
101+
struct_train response;int moreflg;
102+
if(read(conn_fd, &moreflg, sizeof(int))==-1) ERR_EXIT("read()");
103+
moreflg=ntohl(moreflg);
104+
printf(" Train No | Train Name\t | Available Seats | Booked Seats | Status \t\n");
105+
while(moreflg){
106+
if(read(conn_fd, &response, sizeof(struct_train))==-1) ERR_EXIT("read()");
107+
display_train(response);
108+
if(read(conn_fd, &moreflg, sizeof(int))==-1) ERR_EXIT("read()");
109+
moreflg=ntohl(moreflg);
110+
}
111+
}
112+
113+
void display_booking(struct_booking booking){
114+
printf("\t%d\t\t%d\t\t%d\t%s\t %d\t\t %d\t %d\n",booking.book_no,booking.acc_no,booking.trn_no,booking.name,booking.age,booking.seats,booking.status);
115+
}
116+
117+
void show_all_booking(int conn_fd){
118+
struct_booking response;int moreflg;
119+
if(write(conn_fd, &CURRENT_USER.acc_no, sizeof(int))==-1) ERR_EXIT("read()");
120+
if(read(conn_fd, &moreflg, sizeof(int))==-1) ERR_EXIT("read()");
121+
moreflg=ntohl(moreflg);
122+
printf("Booking No | Booking Account No | Train No | Traveller Name | Age | Seats Booked | Status\n");
123+
while(moreflg){
124+
if(read(conn_fd, &response, sizeof(struct_booking))==-1) ERR_EXIT("read()");
125+
display_booking(response);
126+
if(read(conn_fd, &moreflg, sizeof(int))==-1) ERR_EXIT("read()");
127+
moreflg=ntohl(moreflg);
128+
}
129+
}
130+
131+
void book_ticket(int conn_fd){
132+
show_all_train(conn_fd);
133+
struct_booking booking;
134+
int possibleflg=0;
135+
char name[TRAVELLER_LEN];
136+
printf("Enter Name: ");
137+
scanf(" %[^\n]",name);
138+
strcpy(booking.name,name);
139+
printf("Enter Age: ");
140+
scanf("%d",&booking.age);
141+
printf("Enter Train No: ");
142+
scanf("%d",&booking.trn_no);
143+
printf("Enter number of seats to book: ");
144+
scanf("%d",&booking.seats);
145+
booking.acc_no=CURRENT_USER.acc_no;
146+
if(write(conn_fd, &booking, sizeof(booking)) == -1) ERR_EXIT("write()");
147+
if(read(conn_fd, &possibleflg, sizeof(int)) == -1) ERR_EXIT("write()");
148+
149+
if(possibleflg){
150+
struct_booking response;
151+
if(read(conn_fd, &response, sizeof(struct_booking)) == -1) ERR_EXIT("write()");
152+
printf("Booking has been completed successfully with following details:\n");
153+
printf("Booking No | Booking Account No | Train No | Traveller Name | Age | Seats Booked | Status\n");
154+
display_booking(response);
155+
return;
156+
}
157+
158+
printf("Booking cannot be completed because of either of following reasons:\n");
159+
printf("a) Train has been cancelled/ Train number is invalid.Please check in the train list(shown above).\n");
160+
printf("b) Requirement of your seats does not meet availability. Please check in the train list(shown above).\n\n");
161+
}
162+
85163
int menu_user(int conn_fd){
86164
printf("1. Book Ticket\n2. View Booking History\n3. Cancel Booking\n4. Exit\n\n");
87165
int choice;int option;
88166
scanf("%d",&choice);
89167
switch(choice){
90168
case 1:
169+
option=htonl(choice);
170+
if(write(conn_fd, &option, sizeof(int)) == -1) ERR_EXIT("write()");
171+
book_ticket(conn_fd);
91172
break;
92173
case 2:
174+
option=htonl(choice);
175+
if(write(conn_fd, &option, sizeof(int)) == -1) ERR_EXIT("write()");
176+
show_all_booking(conn_fd);
93177
break;
94178
case 3:
95179
break;
@@ -104,6 +188,8 @@ int menu_user(int conn_fd){
104188
sleep(2);
105189
break;
106190
}
191+
printf("\nPress enter to get back to main menu\n");
192+
getchar();getchar();
107193
return 1;
108194
}
109195

@@ -199,10 +285,6 @@ struct_train get_train(){
199285
return new;
200286
}
201287

202-
void display_train(struct_train train){
203-
printf(" \t%d \t%s\t \t%d\t\t \t%d\t \t%d\n",train.trn_no,train.trn_name,train.trn_avl_seats,train.trn_book_seats,train.status);
204-
}
205-
206288
void add_train(int conn_fd){
207289
//printf("Inside add_customer\n"); //DELETE
208290
struct_train new=get_train();struct_train response;
@@ -213,19 +295,6 @@ void add_train(int conn_fd){
213295
display_train(response);
214296
}
215297

216-
void show_all_train(int conn_fd){
217-
struct_train response;int moreflg;
218-
if(read(conn_fd, &moreflg, sizeof(int))==-1) ERR_EXIT("read()");
219-
moreflg=ntohl(moreflg);
220-
printf(" Train No | Train Name\t | Available Seats | Booked Seats | Status \t\n");
221-
while(moreflg){
222-
if(read(conn_fd, &response, sizeof(struct_train))==-1) ERR_EXIT("read()");
223-
display_train(response);
224-
if(read(conn_fd, &moreflg, sizeof(int))==-1) ERR_EXIT("read()");
225-
moreflg=ntohl(moreflg);
226-
}
227-
}
228-
229298
void search_train(int conn_fd){
230299
int trn_no;struct_train response;int presentflg=0;
231300

server.c

+122-24
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#define PASSWORD_LEN 16
1818
#define USERNAME_LEN 20
1919
#define TRNAME_LEN 30
20+
#define TRAVELLER_LEN 20
2021
#define PORT_NO 5000
2122
#define MAX_CUSTOMER 1000
2223
#define ADMIN_ACC_TYPE 0
@@ -28,6 +29,8 @@
2829
#define S_DEL_CUSTOMER 1
2930
#define S_ADD_TRAIN 2
3031
#define S_DEL_TRAIN 3
32+
#define S_MOD_TRAIN 4
33+
#define S_BOOK_TICKET 5
3134

3235

3336
void ERR_EXIT(const char * msg) {perror(msg);exit(EXIT_FAILURE);}
@@ -48,6 +51,16 @@ typedef struct train{
4851
int status;
4952
}struct_train;
5053

54+
typedef struct booking{
55+
int book_no;
56+
int trn_no;
57+
int acc_no;
58+
char name[TRAVELLER_LEN];
59+
int age;
60+
int seats;
61+
int status;
62+
}struct_booking;
63+
5164
typedef union semun
5265
{
5366
int val;
@@ -59,15 +72,6 @@ static int NO_OF_SEMAPHORE=10;static int G_SEMID=0;
5972

6073
static int LOGGED_IN_USER[MAX_CUSTOMER];static int LOGGED_IN_COUNT=0;
6174

62-
/*struct flock get_writelock_customer(int start){
63-
struct flock lock;
64-
lock.l_start = start*sizeof(struct_customer);
65-
lock.l_len = sizeof(struct_customer);
66-
lock.l_pid =
67-
lock.l_type = F_WRLCK;
68-
lock.l_whence = SEEK_SET;
69-
return lock;
70-
}*/
7175

7276
/* Prepares the semaphore set and sets the semaphore setid in a global variable
7377
*/
@@ -87,6 +91,8 @@ void init_semaphore_set(){
8791
if(semctl(G_SEMID, S_DEL_CUSTOMER, SETVAL, arg) == -1) ERR_EXIT("semctl()");
8892
if(semctl(G_SEMID, S_ADD_TRAIN, SETVAL, arg) == -1) ERR_EXIT("semctl()");
8993
if(semctl(G_SEMID, S_DEL_TRAIN, SETVAL, arg) == -1) ERR_EXIT("semctl()");
94+
if(semctl(G_SEMID, S_MOD_TRAIN, SETVAL, arg) == -1) ERR_EXIT("semctl()");
95+
if(semctl(G_SEMID, S_BOOK_TICKET, SETVAL, arg) == -1) ERR_EXIT("semctl()");
9096
}
9197

9298
/* Holding the semaphore*/
@@ -175,6 +181,107 @@ void release_logged_in_user(int conn_fd){
175181
}
176182
}
177183

184+
void modify_train(int trn_no,int booked_seats){
185+
wait(S_MOD_TRAIN);
186+
int fd;
187+
if((fd = open("train", O_RDWR))==-1) ERR_EXIT("open()");
188+
struct_train actual_trn;
189+
lseek(fd,(trn_no-1)*(sizeof(struct_train)),SEEK_SET);
190+
read(fd,&actual_trn,1*sizeof(struct_train));
191+
lseek(fd,-1*sizeof(struct_train),SEEK_CUR);
192+
193+
actual_trn.trn_avl_seats-=booked_seats;
194+
actual_trn.trn_book_seats+=booked_seats;
195+
196+
write(fd,&actual_trn,1*sizeof(struct_train));
197+
release(S_MOD_TRAIN);
198+
}
199+
200+
void show_all_train(int conn_fd){
201+
int moreflg=htonl(1);int fd;
202+
203+
if((fd = open("train", O_RDONLY))==-1) ERR_EXIT("open()");
204+
struct_train actual_trn;
205+
while(read(fd,&actual_trn,1*sizeof(struct_train))){
206+
if(write(conn_fd, &moreflg, sizeof(int)) == -1) ERR_EXIT("write()");
207+
if(write(conn_fd, &actual_trn, sizeof(struct_train)) == -1) ERR_EXIT("write()");
208+
}
209+
moreflg=htonl(0);
210+
if(write(conn_fd, &moreflg, sizeof(int)) == -1) ERR_EXIT("write()");
211+
212+
if(close(fd)==-1) ERR_EXIT("close()");//closing file
213+
}
214+
215+
void show_all_booking(int conn_fd){
216+
int moreflg=htonl(1);int fd;int acc_no;
217+
218+
if(read(conn_fd, &acc_no, 1*sizeof(int)) == -1) ERR_EXIT("read()");
219+
char filename[10], accno[4];
220+
sprintf(accno, "%d", acc_no);
221+
strcpy(filename,accno);strcat(filename,"bh");
222+
223+
if((fd = open(filename, O_RDONLY))==-1) ERR_EXIT("open()");
224+
struct_booking actual_book;
225+
while(read(fd,&actual_book,1*sizeof(struct_booking))){
226+
if(write(conn_fd, &moreflg, sizeof(int)) == -1) ERR_EXIT("write()");
227+
if(write(conn_fd, &actual_book, sizeof(struct_booking)) == -1) ERR_EXIT("write()");
228+
}
229+
moreflg=htonl(0);
230+
if(write(conn_fd, &moreflg, sizeof(int)) == -1) ERR_EXIT("write()");
231+
232+
if(close(fd)==-1) ERR_EXIT("close()");//closing file
233+
}
234+
235+
void book_ticket(int conn_fd){
236+
show_all_train(conn_fd);
237+
struct_booking new;
238+
int presentflg=0;int fd;
239+
if(read(conn_fd, &new, 1*sizeof(struct_booking)) == -1) ERR_EXIT("read()");
240+
241+
if((fd = open("train", O_RDONLY))==-1) ERR_EXIT("open()");
242+
243+
struct_train actual_trn;
244+
lseek(fd,(new.trn_no-1)*(sizeof(struct_train)),SEEK_SET);
245+
read(fd,&actual_trn,1*sizeof(struct_train));
246+
if(actual_trn.trn_no==new.trn_no && actual_trn.status==NORMAL && actual_trn.trn_avl_seats>=new.seats){
247+
presentflg=1;
248+
presentflg=htonl(presentflg);
249+
modify_train(actual_trn.trn_no,new.seats);
250+
}
251+
252+
if(close(fd)==-1) ERR_EXIT("close()");//closing file
253+
254+
presentflg=htonl(presentflg);
255+
if(write(conn_fd, &presentflg, sizeof(int)) == -1) ERR_EXIT("write()");
256+
257+
if(presentflg){
258+
wait(S_BOOK_TICKET);
259+
260+
char filename[10], accno[4];int bookfd;
261+
sprintf(accno, "%d", new.acc_no);
262+
strcpy(filename,accno);strcat(filename,"bh");
263+
if((bookfd = open(filename, O_RDWR | O_CREAT, 0744))==-1) ERR_EXIT("open()");
264+
265+
int size=lseek(bookfd,0,SEEK_END);
266+
if(size==0){
267+
new.book_no=1;
268+
}else{
269+
struct_booking last;
270+
lseek(bookfd,-1*sizeof(struct_booking),SEEK_END);
271+
read(bookfd,&last,sizeof(struct_booking));
272+
new.book_no=last.book_no+1;
273+
}
274+
new.status=NORMAL;
275+
276+
lseek(bookfd,0,SEEK_END);
277+
write(bookfd,&new,sizeof(struct_booking));
278+
279+
if(write(conn_fd, &new, sizeof(struct_booking)) == -1) ERR_EXIT("write()");
280+
release(S_BOOK_TICKET);
281+
}
282+
}
283+
284+
178285
struct_customer add_customer(int conn_fd){
179286
struct_customer new;int fd;
180287
if(read(conn_fd, &new, 1*sizeof(struct_customer)) == -1) ERR_EXIT("read()");
@@ -285,21 +392,6 @@ struct_train add_train(int conn_fd){
285392
return new;
286393
}
287394

288-
void show_all_train(int conn_fd){
289-
int moreflg=htonl(1);int fd;
290-
291-
if((fd = open("train", O_RDONLY))==-1) ERR_EXIT("open()");
292-
struct_train actual_trn;
293-
while(read(fd,&actual_trn,1*sizeof(struct_train))){
294-
if(write(conn_fd, &moreflg, sizeof(int)) == -1) ERR_EXIT("write()");
295-
if(write(conn_fd, &actual_trn, sizeof(struct_train)) == -1) ERR_EXIT("write()");
296-
}
297-
moreflg=htonl(0);
298-
if(write(conn_fd, &moreflg, sizeof(int)) == -1) ERR_EXIT("write()");
299-
300-
if(close(fd)==-1) ERR_EXIT("close()");//closing file
301-
}
302-
303395
void search_train(int conn_fd){
304396
int trn_no;int presentflg=0;int fd;
305397
if(read(conn_fd, &trn_no, sizeof(int)) == -1) ERR_EXIT("write()");
@@ -364,6 +456,12 @@ void * service(void * fd)
364456
int ret=authenticate(conn_fd);
365457
printf("ret: %d\n",ret);
366458
if(write(conn_fd, &ret, sizeof(ret)) == -1) ERR_EXIT("write()");
459+
}else if(option==1){
460+
printf("Inside option 1\n");
461+
book_ticket(conn_fd);
462+
}else if(option==2){
463+
printf("Inside option 2\n");
464+
show_all_booking(conn_fd);
367465
}else if(option==5){
368466
printf("Inside option 5\n");
369467
struct_customer new=add_customer(conn_fd);

0 commit comments

Comments
 (0)