package bargainingchips.players; import java.util.List; import java.util.ArrayList; import java.util.concurrent.BlockingQueue; import java.util.concurrent.LinkedBlockingQueue; import bargainingchips.NegotiationContext; import bargainingchips.actions.Offer; import bargainingchips.actions.OfferBy; import bargainingchips.messaging.coordination.CoordinationMessage; import bargainingchips.messaging.status.StatusMessage; import bargainingchips.protocol.AsynchronousOffersProtocol; import bargainingchips.utilityfunctions.UF_CloseToQuantity; import bargainingchips.utilityfunctions.UtilityFunction; import bargainingchips.wishlist.WishList; import bargainingchips.wishlist.WishListBuilder; public class Buyer { private Coordinator c; private BlockingQueue cin; private BlockingQueue cout; private List threads; public Buyer(WishList overallWishlist) { cin = new LinkedBlockingQueue(); cout = new LinkedBlockingQueue(); // Coordinator c = new Coordinator(overallWishlist, cout, cin); threads = new ArrayList(); } public void connectSeller(String sellerName, WishList wishlistSam) { // Set up the protocol BlockingQueue from = new LinkedBlockingQueue(); BlockingQueue toBuyer = new LinkedBlockingQueue(); BlockingQueue toSeller = new LinkedBlockingQueue(); String bobiName = "Bob" + " " + getThreadNumber(); AsynchronousOffersProtocol aop = new AsynchronousOffersProtocol(from, bobiName, toBuyer, sellerName, toSeller); NegotiationContext context = new NegotiationContext(); // Make a new subnegotiator WishList wishlist = new WishListBuilder().addWish("Green", 2).build(); UtilityFunction u = new UF_CloseToQuantity(wishlist); Agent bobi = new BoulwareAgent(bobiName, u, context, toBuyer, from, cin, cout); // Seller UtilityFunction uSam = new UF_CloseToQuantity(wishlistSam); Agent sam = new BasicAgent(sellerName, uSam, context, toSeller, from); // The thread NegotiationThread thread = new NegotiationThread(); thread.protocol = aop; thread.subbuyer = bobi; thread.seller = sam; threads.add(thread); } public void startThreads() { // Start the coordinator once Thread threadCoordinator = new Thread(c); threadCoordinator.start(); // Start all threads: subnegotiator + protocol + seller for (NegotiationThread t : threads) { System.out.println(t.subbuyer.toDescription()); System.out.println("playing vs"); System.out.println(t.seller.toDescription()); Thread aopThread = new Thread(t.protocol); aopThread.start(); Thread threadBuyer = new Thread(t.subbuyer); threadBuyer.start(); Thread threadSeller = new Thread(t.seller); threadSeller.start(); } } private int getThreadNumber() { return threads.size(); } }