[330] | 1 | package bargainingchips.players;
|
---|
| 2 |
|
---|
[331] | 3 | import java.util.List;
|
---|
[330] | 4 |
|
---|
[331] | 5 | import java.util.ArrayList;
|
---|
| 6 | import java.util.concurrent.BlockingQueue;
|
---|
| 7 | import java.util.concurrent.LinkedBlockingQueue;
|
---|
| 8 |
|
---|
| 9 | import bargainingchips.NegotiationContext;
|
---|
| 10 | import bargainingchips.actions.Offer;
|
---|
| 11 | import bargainingchips.actions.OfferBy;
|
---|
[340] | 12 | import bargainingchips.messaging.coordination.CoordinationMessage;
|
---|
| 13 | import bargainingchips.messaging.status.StatusMessage;
|
---|
[331] | 14 | import bargainingchips.protocol.AsynchronousOffersProtocol;
|
---|
| 15 | import bargainingchips.utilityfunctions.UF_CloseToQuantity;
|
---|
| 16 | import bargainingchips.utilityfunctions.UtilityFunction;
|
---|
[340] | 17 | import bargainingchips.wishlist.WishList;
|
---|
| 18 | import bargainingchips.wishlist.WishListBuilder;
|
---|
[331] | 19 |
|
---|
| 20 | public class Buyer
|
---|
| 21 | {
|
---|
| 22 | private Coordinator c;
|
---|
| 23 | private BlockingQueue<CoordinationMessage> cin;
|
---|
| 24 | private BlockingQueue<StatusMessage> cout;
|
---|
| 25 |
|
---|
| 26 | private List<NegotiationThread> threads;
|
---|
| 27 |
|
---|
| 28 | public Buyer(WishList overallWishlist)
|
---|
| 29 | {
|
---|
| 30 | cin = new LinkedBlockingQueue<CoordinationMessage>();
|
---|
| 31 | cout = new LinkedBlockingQueue<StatusMessage>();
|
---|
| 32 |
|
---|
| 33 | // Coordinator
|
---|
| 34 | c = new Coordinator(overallWishlist, cout, cin);
|
---|
| 35 |
|
---|
| 36 | threads = new ArrayList<NegotiationThread>();
|
---|
| 37 | }
|
---|
| 38 |
|
---|
| 39 | public void connectSeller(String sellerName, WishList wishlistSam)
|
---|
| 40 | {
|
---|
| 41 | // Set up the protocol
|
---|
| 42 | BlockingQueue<OfferBy> from = new LinkedBlockingQueue<OfferBy>();
|
---|
| 43 | BlockingQueue<Offer> toBuyer = new LinkedBlockingQueue<Offer>();
|
---|
| 44 | BlockingQueue<Offer> toSeller = new LinkedBlockingQueue<Offer>();
|
---|
| 45 |
|
---|
| 46 | String bobiName = "Bob" + " " + getThreadNumber();
|
---|
| 47 |
|
---|
| 48 | AsynchronousOffersProtocol aop = new AsynchronousOffersProtocol(from, bobiName, toBuyer, sellerName, toSeller);
|
---|
| 49 |
|
---|
| 50 | NegotiationContext context = new NegotiationContext();
|
---|
| 51 |
|
---|
| 52 | // Make a new subnegotiator
|
---|
| 53 | WishList wishlist = new WishListBuilder().addWish("Green", 2).build();
|
---|
| 54 | UtilityFunction u = new UF_CloseToQuantity(wishlist);
|
---|
| 55 | Agent bobi = new BoulwareAgent(bobiName, u, context, toBuyer, from, cin, cout);
|
---|
| 56 |
|
---|
| 57 | // Seller
|
---|
| 58 | UtilityFunction uSam = new UF_CloseToQuantity(wishlistSam);
|
---|
| 59 | Agent sam = new BasicAgent(sellerName, uSam, context, toSeller, from);
|
---|
| 60 |
|
---|
| 61 | // The thread
|
---|
| 62 | NegotiationThread thread = new NegotiationThread();
|
---|
| 63 | thread.protocol = aop;
|
---|
| 64 | thread.subbuyer = bobi;
|
---|
| 65 | thread.seller = sam;
|
---|
| 66 |
|
---|
| 67 | threads.add(thread);
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | public void startThreads()
|
---|
| 71 | {
|
---|
| 72 | // Start the coordinator once
|
---|
| 73 | Thread threadCoordinator = new Thread(c);
|
---|
| 74 | threadCoordinator.start();
|
---|
| 75 |
|
---|
| 76 | // Start all threads: subnegotiator + protocol + seller
|
---|
| 77 | for (NegotiationThread t : threads)
|
---|
| 78 | {
|
---|
| 79 | System.out.println(t.subbuyer.toDescription());
|
---|
| 80 | System.out.println("playing vs");
|
---|
| 81 | System.out.println(t.seller.toDescription());
|
---|
| 82 |
|
---|
| 83 | Thread aopThread = new Thread(t.protocol);
|
---|
| 84 | aopThread.start();
|
---|
| 85 |
|
---|
| 86 | Thread threadBuyer = new Thread(t.subbuyer);
|
---|
| 87 | threadBuyer.start();
|
---|
| 88 |
|
---|
| 89 | Thread threadSeller = new Thread(t.seller);
|
---|
| 90 | threadSeller.start();
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | private int getThreadNumber()
|
---|
| 96 | {
|
---|
| 97 | return threads.size();
|
---|
| 98 | }
|
---|
| 99 |
|
---|
[330] | 100 | }
|
---|