The purpose of this project is to make sure that you understand and are familiar with the concepts covered in the lectures, including distributed computing, multithreading, thread definition, creation, management, synchronization, cooperation, event-driven programming, client and server architecture, service execution model of creating a new thread for each request, the performance of parallel computing, and the impact of multi-core processors to multithreading programs with complex coordination and cooperation. Furthermore, you are able to apply these concepts in a programming project.
Section II Project Tasks (Submission required)
Warning: This is a long programming project and it is challenging at both conceptual level and implementation level.
Purpose of this project is to exercise the concepts learned in this chapter. It is not the purpose of this project to create realistic services and applications. We will create more realistic services and applications in the subsequent projects. In this project, you can use a console application or a simple GUI application to implement the user interface to your program.
Description: Consider that you are creating a simplified e-commerce system. The system consists of multiple chicken retailers (clients) and a single chicken farm (server). The system consists of the major components shown in the diagram below.
Figure 1 Architecture of an e-commerce system
An Operation Scenario of the e-commerce system is outlined below:
- The ChickenFarm uses a pricing model to calculate the chicken price. If the new price is lower than the previous price, it emits an event and calls the event handlers in the retailers that have subscribed to the event.
- A Retailer evaluates the price, generates an OrderObject (consisting of multiple values), and sends the order to the Encoder to convert the order object into a plain string.
- The Encoder converts the object into a string.
- The Encoder sends the encoded string back to the caller.
- The Retailer sends the encoded string to one of the free cells in the MultiCellBuffer.
- The ChickenFarm receives the encoded string from the MultiCellBuffer and sends the string to the Decoder for decoding.
- The Decoder sends the OrderObject to the ChickenFarm. The decoded object must contain the same values generated by the Retailer.
- The ChickenFarm creates a new thread to process the order;
- The OrderProcessingThread processes the order, e.g., checks the credit card number and calculates the amount.
- The OrderProcessingThread sends a confirmation to the retailer and prints the order.
Components and assignment tasks in the diagram are explained in details as follows, with their grades allocation:
Assignment 3 tasks [50 points]:
- ChickenFarm is a class on the server side: It will be started as a thread by the Main method and will perform a number of service functions. It uses a PricingModel to determine the chicken prices. It defines a price-cut event that can emit an event and call the event handlers in the Retailers if there is a price-cut according to the PricingModel. It receives the orders (in string) from the MultiCellBuffer. It calls the Decoder to convert the string into the order object. For each order, it starts a new thread (resulting in multiple threads for processing multiple orders*) from OrderProcessing class (or method) to process the
order based on the current price. There is a counter p in the ChickenFarm. After p (e.g., p = 10) price cuts
have been made, the ChickenFarm thread will terminate. [20 points]
- PricingModel: It can be a class or a method in ChickenFarm class. It decides the price of chickens. It can increase price or cut the price. You can define a mathematical model (formula) to determine the price based on the order received within a given time period and the number of chickens the farm can produce in the same time period. You can use a random model to generate the prices. However, you must make sure that your model will allow the price goes up some time and goes down some other time. [5 points]
- OrderProcessing is a class or a method in a class on server side. Whenever an order needs to be processed, a new thread is instantiated from this class (or method) to process the order. It will check the validity of the credit card number. You can define your own credit card format, for example, the credit card number from the retailers must be a number registered to the ChickenFarm, or a number between two given numbers (e.g., between 5000 and 7000). Each OrderProcessing thread will calculate the total amount of charge, e.g., unitPrice*NoOfChickens + Tax + shippingHandling. It will send a confirmation to the retailer when an order is completed. The confirmation must be implemented using a callback method.
- Retailer1 through RetailerN, where N = 5, each retailer is a thread instantiated from the same class (or the same method) in a class. The retailers’ actions are event-driven. Each retailer contains a callback method (event handler) for the ChickenFarm to call when a price-cut event occurs. The retailer will calculate the number of chickens to order, for example, based on the need and the difference between the previous price and the current price. The thread will terminate after the ChickenFarm thread has terminated. Each order is an OrderClass object. The object is sent to the Encoder for encoding. The encoded string is sent back to the retailer. Then, the retailer will send the order in String format to the MultiCellBuffer. Before sending the order to the MultiCellBuffer, a time stamp must be saved. When the confirmation of order completion is received, the time of the order will be calculated and saved (or
printed). [15 points]
Assignment 4 tasks [50 points]:
- OrderClass is a class that contains at least the following private data members: senderId: the identity of the sender, you can use thread name or thread id;
- cardNo: an integer that represents a credit card number;
- amount: an integer that represents the number of chickens to order;
You must use public methods to set and get the private data members. You must decide if these methods need to be synchronized. The instances created from this class are of the OrderObject. [10 points]
- MultiCellBuffer class is used for sending the order from the retailers (clients) to the chickenFarm (server). This class has n data cells (you can simply set n = 2). The number of cells is less than the max number N (you can set N = 5 or enter N from keyboard) of retailers in your experiment. A setOneCell and getOneCell methods can be defined to write data into and to read data from one of the available cells. You must use a semaphore of value n to manage the cells and use a lock for each cell to ensure the synchronization.
- Encoder is a class or a method in a class: The Encoder class will convert an OrderObject into a string. You can choose any way to encode the values into a string, as long as you can decode the string to the original order object. You can use a class or a method to implement the Encoder. [10 points]
- Decoder is a class or a method in a class: The Decoder will convert the string back into the
OrderObject. [10 points]
- Main: The Main thread will perform necessary preparation, create the buffer classes, instantiate the
|objects, create threads, and start threads.||[10 points]|
|10. Submit the test input cases and test output results.||[10 points]|
- This project can be done in C# (Visual Studio) OR in Java (NetBeans). You must indicate the environment (VS 2013, VS 2015, or NetBeans) you use, so that the TA can use the same environment to grade the project. We taught multithreading in both Java and C#. However, we did not teach the event-driven programming in Java. If you choose to do the project in Java, you need to study this part on your own. I suggest that you do the project in Java only if you are really good at Java and already know how to program events. The event handling in Java is not as easy as in C# for this project, and you need to take extra effort to implement the required functions.
- You must follow what is defined in the assignment/project document. You have flexibility to choose your implementation details if they are not specified in the document. If you are not sure on any issue, ask the instructor or the TA by posting the question in the discussion board.
- The program and each component of the program must be well commented.
- This assignment, you may not need to have external input, but you must set your internal inputs in such a way that the program is properly tested, for example, each retailer must have completed at least one ordering process before the chickenFarmer terminate.