source: src/main/java/bargainingchips/players/Buyer.java@ 341

Last change on this file since 341 was 340, checked in by Tim Baarslag, 5 years ago

Change BilateralProtocol loop to avoid busy wait; note that this fixes only a part of the busy wait problem.

Package structure now in line with latest Acumex discussions.

Pinpointed error avoidance in Agent.

File size: 3.0 KB
Line 
1package bargainingchips.players;
2
3import java.util.List;
4
5import java.util.ArrayList;
6import java.util.concurrent.BlockingQueue;
7import java.util.concurrent.LinkedBlockingQueue;
8
9import bargainingchips.NegotiationContext;
10import bargainingchips.actions.Offer;
11import bargainingchips.actions.OfferBy;
12import bargainingchips.messaging.coordination.CoordinationMessage;
13import bargainingchips.messaging.status.StatusMessage;
14import bargainingchips.protocol.AsynchronousOffersProtocol;
15import bargainingchips.utilityfunctions.UF_CloseToQuantity;
16import bargainingchips.utilityfunctions.UtilityFunction;
17import bargainingchips.wishlist.WishList;
18import bargainingchips.wishlist.WishListBuilder;
19
20public 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
100}
Note: See TracBrowser for help on using the repository browser.