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