Spring 2019
Programming Assignment 1: Asterix and the Bazaar
Due: 23:55 pm, Monday March 4, 2019
The Gauls love to trade goods in their weekly bazaar. The bazaar contains two types of people: buyers and sellers. Each seller sells one of of the following goods: fish, salt, or boars. Each buyer in the bazaar is looking to buy one of these three items.
While previously buyers met sellers by screaming for what one wished to buy or sell, the village chief Vitalstatix has decreed that henceforth, all bazaar business shall strictly follow the peer-to-peer model. Each buyer shall gently whisper their needs to all her neighbors, who will then propagate the message to their neighbors and so on, until a seller is found or the maximum limit on the number of hops a message can traverse is reached.
If a seller is found, then the seller sends back a response that traverses in the reverse direction back to the buyer. At this point, the buyer and the seller directly enter into a transaction (without using intermediate peers).
Assume that the number of people in the bazaar N is specified beforehand (N should be configurable in your system).
First construct a p2p network such all N peers form a connected network. You can use either a structured or unstructured P2P topology to construct the network. No neighbor discovery is needed; 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. Each seller picks one of three items to sell. Each buyer randomly picks an item and attempts to purchase it; it then waits a random amount of time, then picks another item to buy and so on. Each seller starts with n items (e.g., n boars) to sell; upon selling all n items, the seller picks another item at random and becomes a seller of that item.
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. For instance, a seller should be able to process multiple concurrent buy requests and decrementing the number of items in stock should be done using synchronization. As part of your design, consider whether to use a thread per request model (where you spawn a new thread for each new request) or a pre-spawned thread pool model at each peer for concurrent event processing.
We do not expect elaborate use of github or testing frameworks - rather we want you to become familiar with these tools and start using them for your lab work (or your other work/research).