source: src/main/java/onetomany/bargainingchipsgame/players/BuyerSubnegotiator.java@ 280

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

CoordinationMessages are concurrent

File size: 2.7 KB
Line 
1package onetomany.bargainingchipsgame.players;
2
3import java.util.concurrent.BlockingQueue;
4
5import onetomany.bargainingchipsgame.interactions.Offer;
6import onetomany.bargainingchipsgame.players.utilityfunction.UtilityFunction;
7
8public class BuyerSubnegotiator extends BilateralAgent
9{
10 // Messaging from and to the coordinator
11 protected BlockingQueue<CoordinationMessage> cin;
12 protected BlockingQueue<NegotiationStatusMessage> cout;
13 private UtilityFunction u;
14
15 int k;
16
17 public BuyerSubnegotiator(String name, BlockingQueue<Offer> in, BlockingQueue<Offer> out,
18 BlockingQueue<CoordinationMessage> cin, BlockingQueue<NegotiationStatusMessage> cout)
19 {
20 super(name, in, out);
21 this.cin = cin;
22 this.cout = cout;
23 k = 1;
24 }
25
26 @Override
27 public void run()
28 {
29 // The buyer expects a utility function from the Coordinator
30 CoordinationMessage coordinatormsg = null;
31 try {
32 coordinatormsg = cin.take();
33 receiveCoordinationMessage(coordinatormsg);
34 } catch (InterruptedException e2) {
35 e2.printStackTrace();
36 }
37
38 // The buyer may send an opening offer
39 Offer opening = sendOpeningOffer();
40 if (opening != null)
41 try {
42 out.put(opening);
43 } catch (InterruptedException e1) {
44 e1.printStackTrace();
45 }
46
47 while (true)
48 {
49 Offer o;
50 try
51 {
52 // Wait for either a message from coordinator or an incoming offer from seller
53 while (true)
54 {
55 CoordinationMessage cpoll = cin.poll();
56 if (cpoll != null)
57 {
58 receiveCoordinationMessage(cpoll);
59 break;
60 }
61
62 Offer poll = in.poll();
63 if (poll != null)
64 {
65 receiveOffer(poll);
66 break;
67 }
68 }
69
70 // A sync happened, so we can send out a new offer
71 Offer sendOffer = sendOffer();
72 out.put(sendOffer);
73
74 if (k >= 0)
75 try {
76 Thread.sleep(1000);
77 } catch (InterruptedException e) {
78 // TODO Auto-generated catch block
79 e.printStackTrace();
80 }
81 }
82 catch (InterruptedException e) {
83 e.printStackTrace();
84 }
85 }
86 }
87
88 private void receiveCoordinationMessage(CoordinationMessage cpoll)
89 {
90 // Update the utility function
91 u = cpoll.f;
92 System.out.println(this + " received coordination msg " + cpoll);
93 }
94
95 @Override
96 protected void receiveOffer(Offer o)
97 {
98 System.out.println(this + " received " + o);
99
100 }
101
102 @Override
103 protected Offer sendOffer()
104 {
105 Offer sampleOffer = Offer.getSampleOffer(k);
106 k++;
107
108 System.out.println(this + " sends " + sampleOffer);
109 return sampleOffer;
110 }
111
112 @Override
113 protected Offer sendOpeningOffer()
114 {
115 Offer sampleOffer = Offer.getSampleOffer(0);
116 System.out.println(this + " sends opening offer " + sampleOffer);
117 return sampleOffer;
118 }
119
120}
Note: See TracBrowser for help on using the repository browser.