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

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

Coordinator working

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 catch (InterruptedException e) {
75 e.printStackTrace();
76 }
77 }
78 }
79
80 private void receiveCoordinationMessage(CoordinationMessage cpoll)
81 {
82 // Update the utility function
83 u = cpoll.f;
84 System.out.println(this + " received coordination msg " + cpoll);
85 }
86
87 @Override
88 protected void receiveOffer(Offer o)
89 {
90 System.out.println(this + " received " + o);
91
92 }
93
94 @Override
95 protected Offer sendOffer()
96 {
97 Offer sampleOffer = Offer.getSampleOffer(k);
98 k++;
99
100 if (k > 5)
101 try {
102 Thread.sleep(5000);
103 } catch (InterruptedException e) {
104 // TODO Auto-generated catch block
105 e.printStackTrace();
106 }
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.