Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Objective: Implement a client/server database in a Linux or Unix environment pro

ID: 3777336 • Letter: O

Question

Objective: Implement a client/server database in a Linux or Unix environment programming in C.

Procedure:

A central computer holds the database for airline reservations for Adirondack Air which runs four flights numbered 1 through 4. The plane capacities for these flights are 5, 2, 10, and 10, respectively. Travel agents from all over the world can access the reservation system by running a local application which calls up the central computer and runs an interactive session that supports the following commands:

                Format of Command:

                                Query for number of seats available on flight x: Q x

                                Reserve seats I, j, etc. user name nm:                   R I j .. nm

                                Cancel seats for flight x under name n:                C x n

                The query command will first display the seat numbers of all the available seats on flight x if there are any. If none are available, the query ends with the message “none available.” Otherwise, the travel agent may book any number of those seats for a name (say Jones) with the command R 1 6 2 9 Jones. Here 1 6 2 9 is the list of seats that are to be reserved for Jones. You should check that the requested seats are in the list of available seats. Any number of agents may access the reservation system concurrently. You will need to lock the flight record for the duration of this transaction so no other agent can take an available seat in the interim. If there is already a lock on the data, the server should send an explanatory message to the travel agent, because the agent will have to wait. If the travel agent responds with a C (cancel), terminate the query without allocating any seats.

Cancel should make available all seats under the given name on the specified flight, and then display the current number of seats now available.

On the server’s monitor, display the current state of the database in real time. Indicate which seats are reserved and by whom. As soon as a change occurs, the screen should be updated. On the same screen show the clients who are currently active.

The system should be robust in the sense that any erroneous command elicits a reasonable error message. Communication over the network will use sockets. The database will be in one file. The data on the file will be locked at the record level by the Unix lock system call. The server will fork a child to handle each new interaction session from a travel agent. Use TCP/IP sockets to communicate between server and clients. Comment your source code.

Test your program by having the agents read a series of commands from a file and record the output on the server to see that it is consistent with the order of messages from the agent clients.

Lab Report: Write the specifications for each of the modules involved in the system included the format for data communicated over sockets. Submit the source code for the server and for the clients along with the input files used for testing. The source code files MUST be named airline server.c and agentx.c, where x is the agent number. The input files should be named agent1.dat, agent2.dat, etc. The server should print the results for each flight in terms of the number of passengers and the seat numbers after the server has completed the work of a given client. You can refer to the output files as agent1.outdata or agent2.outdata, etc

Explanation / Answer

#include<stdio.h>
#include <stdlib.h>
#include<string.h>

//STRUCTURE
typedef struct{
char flightNo[5];
char date[12];
char time[6];
char gate[3];
}Flight;
Flight flight={"YZ22","10-12-2008","20:30","RT"};

typedef struct{
char name[30];
char booking_ID[3];
int seats;
}Seat;
Seat choice[4][5];

void displaymenu();
void booking();
void seat();
void ticket();
void records();
void looping();
void exit_();

//Variables
int selection;
int i;
int j;
int seats_num[20]={0};
int booking_ID=100;
int seatsAvailable=20;
int password;

int main(void)
{
displaymenu();
while(selection!=4)
{
looping();
}
return 0;
}

void displaymenu()
{
printf(" ");
printf(" Airline System "
" ======================= "
" MENU "
" ======================= "
" 1.BOOKING "
" 2.SEAT "
" 3.RECORDS "
" 4.EXIT ");

printf(" Enter your selection : ");
scanf("%d",&selection);
looping();
return;
}

//looping()
void looping()
{
switch(selection)
{
case 1:
booking();
break;
case 2:
seat();
break;
case 3:
records();
break;
case 4:
exit_();
break;
default:
printf(" Invalid selecion.Try again ");
}
return;
}

//booking
void booking()
{
for(i=0;i<4;i++)
for(j=0;j<5;j++)
{
printf(" Please enter seats number : ");
scanf("%d",&choice[i][j].seats);
fflush(stdin);

if(choice[i][j].seats<=seatsAvailable)
{
printf(" Please enter passenger name : ");
scanf("%[^ ]",choice[i][j].name);
fflush(stdin);
ticket();
booking_ID++;
}
seatsAvailable=seatsAvailable-choice[i][j].seats;

system("pause");
system("cls");
displaymenu();
}

if (seatsAvailable<0)
{
printf(" ");
printf(" SORRY, the flight is fully booked ");
printf(" =================END================= ");
displaymenu();
}
if(choice[i][j].seats>seatsAvailable)
{
printf(" ");
printf(" The flight leave %d seats ",seatsAvailable);
displaymenu();
}
return;
}

void ticket()
{
printf(" ");

printf(" -----------------AIRLINE BOOKING TICKET---------------- ");
printf(" ============================================================ ");
printf(" Booking ID : %d Flight No :
%s ",booking_ID,flight.flightNo);
printf(" Passenger : %s ",choice[i][j].name);
printf(" Date : %s ",flight.date);
printf(" Time : %s ",flight.time);
printf(" Gate : %s ",flight.gate);
printf(" Seats No. : %d%c ",i+1,j+65);
printf(" ============================================================ ");
return;}


//seat
void seat()
{
printf(" A B C D E ");
for(j=0;j<5;j++)
{
printf("%d ",booking_ID);
}
for(i=0;i<4;i++)
{
printf(" ");
printf("%d ",i+1);
}
system("pause");
system("cls");
displaymenu();
return;
}

void records() //For Staff to View the flight's records
{
printf(" Please enter password: ");
scanf("%d", &password); //111

if (password==111)
{
system("cls");

printf(" ==================================== ");
printf(" ALL FLIGHT RECORDS ");
printf(" ==================================== ");
printf(" Seats Available left : %d ",seatsAvailable);

ticket();
system("pause");
system("cls");
displaymenu();
}
else
{
printf(" Invalid password ");
system("pause");
system("cls");
displaymenu();
}
return;
}

void exit_()
{
printf(" Thank you for using this system ");
exit(1);
return;
}

                           OR

#include <iostream>

#include <cctype>

#include <string>

#include <cstring>

using namespace std;

//Declare function proto type

int assignSeat(int seat_num, int row_num, intpass_num);

int num1(char*);

int num2(char*);

int NumSeats = 12;

void InitializeSeats();

void Reserve();

void Cancel();

void ChangeSeat();

void Display();

struct Seat

  {

      char pass_name[80];

      int Available;

  };

   

  struct Seat SeatArray[6][2];

   

int main()

{     

   char seatchioce = 0;

   int row_num = 6;

   char a = 0;

   char w = 0;

   

  int total_passenger = 12;

   

  char arr1[][6] = {"1A","2A","3A","4A","5A","6A"};

  char arr2[][6] = {"1B","2B","3B","4B","5B","6B"};

     

  int MenuChoice;

  InitializeSeats();

  while(1)

  {

      cout << " 1. Reserve" << endl;

      cout << " 2. Cancel" << endl;

      cout << " 3. Change Seat" << endl;

      cout << " 4. Display" << endl;

      cout << " 5. Exit" << endl;

      cout << "Enter your choice: ";

      cin >> MenuChoice;

    if((MenuChoice < 0) && (MenuChoice > 5))

    {

      cout << "Invalid Choice" << endl;

      system("Pause Try Again");

    }

    else

    {

        switch(MenuChoice)

        {

            case 1: Reserve();

                break;

            case 2: Cancel();

                break;

            case 3: ChangeSeat();

                break;

            case 4: Display();

                break;

            case 5:

                exit(1);

        }

    }

    cin.get();

  }

   

return 0;

}

void Reserve() //This function is for reserv the seats

{

    int pass_num = 0;

    cout << "Wellcome to CheapSkate Airline Passenger seat assignment" << endl;

         

     cout << "All " << NumSeats << " seats are available " << endl;

     

     for(int i = 0; i < 6; i++)

     {

         for(int j = 0; j < 2; j++)

         {

             if(SeatArray[i][j].Available == 1)

             {

                 cout << "Please enter the passenger name: ";

                 cin >> SeatArray[i][j].pass_name;

                 SeatArray[i][j].Available = 0;

                 NumSeats--;

                 return;

             }

         }

         

     }

}

void Cancel()// This function is for Cancel the seat

{

    char CancelPassengerName[80];

    cout << "Enter the Passenger to be cancelled: ";

    cin >> CancelPassengerName;

    for(int i =0; i <6; i++)

    {

        for(int j=0; j<2; j++)

        {

             

            if(strcmp(SeatArray[i][j].pass_name, CancelPassengerName) == 0)

            {

                NumSeats++;

                SeatArray[i][j].Available = 1;

                return;

            }

        }

    }

    cout << " Passenger not in the list" <<endl;

}

void ChangeSeat()//This function is for Change the seat

{

    char MovePassenger[80];

    int SeatRow, SeatColumn;

    cout << "Enter the passenger name to be moved: ";

    cin >> MovePassenger;

    for(int i = 0; i < 6; i++)

    {    for(int j = 0; j < 2; j++)

        {

            if(strcmp(SeatArray[i][j].pass_name, MovePassenger) == 0)

            {

                SeatRow = i;

                SeatColumn = j;

            }

        }

    }

    if(NumSeats <= 0)

    {

        cout << "No seat available there for you cannot change seat" << endl;

        return;

    }

    else{

        for(int i = 0; i < 6; i++)

        {

            for(int j = 0; j < 2; j++)

            {

                if(SeatArray[i][j].Available == 1)

                {

                    strcpy_s(SeatArray[i][j].pass_name, MovePassenger);

                    SeatArray[SeatRow][SeatColumn].Available = 1;

                    SeatArray[i][j].Available = 0;

                    return;

                }

            }

        }

    }

}

void Display()//Display the seat assingment for the all reservation

{

    for(int i = 0; i < 6; i++)

    {

        for(int j = 0; j < 2; j++)

        {

            if(SeatArray[i][j].Available == 0)

            {

                cout << SeatArray[i][j].pass_name << " = " << i+1;

                if (j == 0) { cout << "A" <<endl; }

                else { cout << "B" << endl; }

            }

            else

            {

                if(j == 1)

                    cout << i+1 << "B" << endl;

                else

                    cout << i+1 << "A" << endl;

            }

        }

    }

}

void InitializeSeats()//Initialy all seats are available

{

    for(int i = 0; i < 12; i++)

    {

        for(int j = 0; j < 2; j++)

            SeatArray[i][j].Available = 1;

    }

}