Spring 2007
Programming Assignment 2: Asterix and the Trading Post
Due: Tuesday, April 10 2007
(for off-campus students - 14 days after viewing Lecture 15)
At the very beginning, the bazaar served the Gauls well. However, with the growing popularity of the bazaar, there were too many whispers that got lost in the din. After a town meeting, Vitalstatix, the village chief, decreed that henceforth the bazaar will adapt a trading post model. The Guals will use a leader election algorithm to elect a trader, who will be in charge of the trading post. All sellers will deposit their good when the market opens with the trader. Buyers will buy from the trader and the trader will pay the seller after each sale (after deducting a suitable commission).
A trader can resign her post at any time; or may call in sick. In this case, the market should elect a new leader. The trader maintains meticulous accounts, so the current state of the market (number of good available for sale, pending sales) are not lost when a new trader is elected. Use a disk file to maintain these accounts.
While in the bazaar model, concurrent buy requests for the last item on sale were resolved randomly, the trading post has adopted a charter to be fair to all buyers. Accordingly, all buyers and sellers have a logical clock (digital timepieces are yet to be invented, so logical clocks are the only option). Logical clocks are used to order buy events, and if there is only one item remaining, then concurrent buy requests are resolved using clock values. You need to decide whether to equip each Gaul with a Lamport or Vector clock. Sellers can offer new items for sale any time: like before, the item type (salt, fish, boar) and the number of items available is specified to the trader. Buyers need to specify what item and how many they wish to buy. As per the fair trading charter, purchase prices are determined a priori and are not negotiable. Upon each sale, the appropriate seller is credited with the purchase price.Assume that the number of people in the trading post N is specified beforehand (N should be configurable in your system).
First construct an connected network. Assume that a list of all N peers and their addresses/ports are specified in a configuration file.
Once the network is formed, randomly assign a seller or buyer role to each peer following the description of project 1. A peer can be both a buyer or a seller, or only one of the two. Peers will then elect a coordinator using any election method mentioned in the class. The peer does not sell or buy anything once it is elected as the coordinator. If the current coordinator resigns, a new round of election will be triggered. We can assume that the last coordinator will write all the useful information to a file so that the new coordinator can read such information from the file.
Each peer maintains a clock. The clock is used to decide which seller to do business with, once multiple responses are received. In this assignment, you may implement either Lamport logical clocks or vector clocks.
As in Project 1, each peer is both a client and a server.
Each peer should be able to accept multiple requests at the same time. This could be easily done using threads. Be aware of the thread synchronizing issues to avoid inconsistency or deadlock in your system.
No GUIs are required. Simple command line interfaces are fine.