17
17
#define PASSWORD_LEN 16
18
18
#define USERNAME_LEN 20
19
19
#define TRNAME_LEN 30
20
+ #define TRAVELLER_LEN 20
20
21
#define PORT_NO 5000
21
22
#define MAX_CUSTOMER 1000
22
23
#define ADMIN_ACC_TYPE 0
28
29
#define S_DEL_CUSTOMER 1
29
30
#define S_ADD_TRAIN 2
30
31
#define S_DEL_TRAIN 3
32
+ #define S_MOD_TRAIN 4
33
+ #define S_BOOK_TICKET 5
31
34
32
35
33
36
void ERR_EXIT (const char * msg ) {perror (msg );exit (EXIT_FAILURE );}
@@ -48,6 +51,16 @@ typedef struct train{
48
51
int status ;
49
52
}struct_train ;
50
53
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
+
51
64
typedef union semun
52
65
{
53
66
int val ;
@@ -59,15 +72,6 @@ static int NO_OF_SEMAPHORE=10;static int G_SEMID=0;
59
72
60
73
static int LOGGED_IN_USER [MAX_CUSTOMER ];static int LOGGED_IN_COUNT = 0 ;
61
74
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
- }*/
71
75
72
76
/* Prepares the semaphore set and sets the semaphore setid in a global variable
73
77
*/
@@ -87,6 +91,8 @@ void init_semaphore_set(){
87
91
if (semctl (G_SEMID , S_DEL_CUSTOMER , SETVAL , arg ) == -1 ) ERR_EXIT ("semctl()" );
88
92
if (semctl (G_SEMID , S_ADD_TRAIN , SETVAL , arg ) == -1 ) ERR_EXIT ("semctl()" );
89
93
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()" );
90
96
}
91
97
92
98
/* Holding the semaphore*/
@@ -175,6 +181,107 @@ void release_logged_in_user(int conn_fd){
175
181
}
176
182
}
177
183
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
+
178
285
struct_customer add_customer (int conn_fd ){
179
286
struct_customer new ;int fd ;
180
287
if (read (conn_fd , & new , 1 * sizeof (struct_customer )) == -1 ) ERR_EXIT ("read()" );
@@ -285,21 +392,6 @@ struct_train add_train(int conn_fd){
285
392
return new ;
286
393
}
287
394
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
-
303
395
void search_train (int conn_fd ){
304
396
int trn_no ;int presentflg = 0 ;int fd ;
305
397
if (read (conn_fd , & trn_no , sizeof (int )) == -1 ) ERR_EXIT ("write()" );
@@ -364,6 +456,12 @@ void * service(void * fd)
364
456
int ret = authenticate (conn_fd );
365
457
printf ("ret: %d\n" ,ret );
366
458
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 );
367
465
}else if (option == 5 ){
368
466
printf ("Inside option 5\n" );
369
467
struct_customer new = add_customer (conn_fd );
0 commit comments