247 lines
6.2 KiB
Plaintext
247 lines
6.2 KiB
Plaintext
SEN-103 Programming Multi-Module Applications
|
||
|
||
Lab 4 Practice Assignment
|
||
|
||
Creating a System with Multiple Executables
|
||
|
||
|
||
|
||
|
||
|
||
Overview
|
||
|
||
In this lab exercise, you will create a taxi dispatching system, which will have two cooperating executables, one to make a request for a taxi and one to dispatch taxis (associate a request with a taxi, and track its progress).
|
||
|
||
|
||
|
||
Instructions
|
||
|
||
For this exercise you will create two programs, taxiRequest and taxiDispatcher. You may do this exercise in Python or C. It might be easier in C since the overall structure of the program is quite similar to the auction demo. If you do the lab in C, you may use my filesearch.c source module to search for and return a list of files that match a pattern.
|
||
|
||
|
||
|
||
The taxiRequest program will loop, gathering information about taxi reservation requests and writing this information to a file with a specific name pattern (e.g. request_nnnnn.txt) in a well-defined format. It might have a user interface like the following:
|
||
|
||
|
||
|
||
./taxiRequest
|
||
|
||
Enter your phone number <Return to exit>: 0987736262
|
||
|
||
Enter your destination: Siam Square
|
||
|
||
Enter your current location: CMKL University
|
||
|
||
Creating taxi reservation request…
|
||
|
||
|
||
|
||
Enter your phone number <Return to exit>: 0821219999
|
||
|
||
Enter your destination: Don Muang Airport
|
||
|
||
Enter your current location: Victory Monument
|
||
|
||
Creating taxi reservation request…
|
||
|
||
|
||
|
||
Enter your phone number <Return to exit>:
|
||
|
||
|
||
|
||
|
||
|
||
A request file might look like this:
|
||
|
||
|
||
|
||
0987736262
|
||
|
||
Siam Square
|
||
|
||
CMKL University
|
||
|
||
|
||
|
||
The taxiDispatcher program is more complicated. It will loop forever, simulating the passage of time using the sleep() function. Assume that each second of computer time is equivalent to one minute of real world time.
|
||
|
||
|
||
|
||
Like the auctionControl program, the taxiDispatcher should sleep for ten seconds. Then it should wake up and process requests and on-going trips, as described below.
|
||
|
||
|
||
|
||
The dispatcher has two jobs:
|
||
|
||
|
||
|
||
Process and enqueue requests: Look for files that indicate taxi requests, created by taxiRequest. For each file that is found, read the file and create a structure representing the request, then delete the file so it will not be processed again. The structure should include the phone number, destination and current location from the request. It should also include an integer tripDuration field which will hold the number of minutes the trip will take. The program should set this field by generating a random number between 10 and 60. After creating the request structure, the program should add it to the end of a linked list that represents taxis that are en route to their destinations.
|
||
|
||
|
||
|
||
Update trip durations and remove taxis that have arrived: Start at the beginning of the list of ongoing trips. For each ongoing trip, subtract 10 (10 minutes) from the tripDuration. If the new tripDuration is less than or equal to zero, announce that the taxi has arrived, then remove the item from the linked list.
|
||
|
||
|
||
|
||
Here is an example of what the output from taxiDispatcher might look like:
|
||
|
||
|
||
|
||
./taxiDispatcher
|
||
|
||
Starting dispatcher
|
||
|
||
|
||
|
||
Checking for requests
|
||
|
||
--- No requests found
|
||
|
||
|
||
|
||
Checking for arrivals
|
||
|
||
--- No taxis have arrived at their destination
|
||
|
||
|
||
|
||
Waiting ten minutes
|
||
|
||
|
||
|
||
Checking for requests
|
||
|
||
--- Assigned taxi to customer 0987736262 going from CMKL University to Siam Square
|
||
|
||
Estimated trip duration 55 minutes
|
||
|
||
|
||
|
||
Checking for arrivals
|
||
|
||
--- No taxis have arrived at their destination
|
||
|
||
|
||
|
||
Waiting 10 minutes
|
||
|
||
|
||
|
||
Checking for requests
|
||
|
||
--- Assigned taxi to customer 0821219999 going from Victory Monument to Don Muang Airport
|
||
|
||
Estimated trip duration 20 minutes
|
||
|
||
|
||
|
||
--- Assigned taxi to customer 0971212222 going from Mega Bangna to Rangsit
|
||
|
||
Estimated trip duration 10 minutes.
|
||
|
||
|
||
|
||
Checking for arrivals
|
||
|
||
--- No taxis have arrived at their destination
|
||
|
||
|
||
|
||
Waiting ten minutes
|
||
|
||
|
||
|
||
Checking for requests
|
||
|
||
--- No requests found
|
||
|
||
|
||
|
||
Checking for arrivals
|
||
|
||
--- No taxis have arrived at their destination
|
||
|
||
|
||
|
||
Waiting ten minutes
|
||
|
||
|
||
|
||
Checking for requests
|
||
|
||
--- No requests found
|
||
|
||
|
||
|
||
Checking for arrivals
|
||
|
||
--- Customer 0971212222 going from Mega Bangna to Rangsit has arrived.
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
The taxi dispatcher program will loop forever, until you hit Ctrl-C to exit.
|
||
|
||
|
||
|
||
Additional Challenge
|
||
|
||
|
||
|
||
Like the auction system, the communication between executables in this system goes only in one direction, from the request application to the dispatching application. In a real taxi dispatching system, the dispatcher module would send information back to the request module when a taxi has been assigned.
|
||
|
||
|
||
|
||
Add this capability to your programs. Specifically:
|
||
|
||
|
||
|
||
Modify the dispatcher so it assigns a four character alphanumeric taxi registration code to each queued request. You can generate registration codes randomly, or create an array of strings for registration codes and use a counter to choose the next code in the array. (When you get to the end of the array, use the modulus operator on the counter to start at the beginning again.)
|
||
Inside the dispatcher, add the registration number to the structure in the trip queue.
|
||
In the dispatcher, when a taxi request is accepted, after creating the structure and adding it to the queue, write a file with a known filename pattern and structure, that holds the customer phone number, taxi registration number and estimated duration.
|
||
In the request program, sleep for five seconds before asking for the next request.
|
||
In the request program, before asking for the next request, have the request program check for any reply files. If any are found, read them, print the information, then delete them.
|
||
|
||
|
||
For example:
|
||
|
||
|
||
|
||
./taxiRequest
|
||
|
||
Enter your phone number <Return to exit>: 0987736262
|
||
|
||
Enter your destination: Siam Square
|
||
|
||
Enter your current location: CMKL University
|
||
|
||
Creating taxi reservation request…
|
||
|
||
|
||
|
||
Enter your phone number <Return to exit>: 0821219999
|
||
|
||
Enter your destination: Don Muang Airport
|
||
|
||
Enter your current location: Victory Monument
|
||
|
||
Creating taxi reservation request…
|
||
|
||
|
||
|
||
–-- Dispatcher message: Taxi AB223 assigned to customer 0987736262
|
||
|
||
Estimated trip duration 55 minutes.
|
||
|
||
|
||
|
||
Enter your phone number <Return to exit>:
|
||
|
||
---------------------------------------------------------------------------------------
|
||
|
||
Submit a zip file containing the code files (.c or .py) and screenshots of the code operation (showing that it works)
|