1 | package bargainingchips.protocol;
|
---|
2 |
|
---|
3 | import java.util.List;
|
---|
4 |
|
---|
5 | import java.util.ArrayList;
|
---|
6 | import java.util.concurrent.BlockingQueue;
|
---|
7 |
|
---|
8 | import bargainingchips.actions.Offer;
|
---|
9 | import bargainingchips.actions.OfferBy;
|
---|
10 |
|
---|
11 | /**
|
---|
12 | * A (possibly asynchronous) protocol between two agents A and B.
|
---|
13 | * The protocol acts as the postman of delivering messages between the agents and validates their messages.
|
---|
14 | * The protocol keeps the one official (synchronized) log of what has happened.
|
---|
15 | */
|
---|
16 | public abstract class BilateralProtocol implements Runnable
|
---|
17 | {
|
---|
18 | // Messaging to and from A and B
|
---|
19 | protected BlockingQueue<Offer> fromA;
|
---|
20 | protected BlockingQueue<Offer> toA;
|
---|
21 | protected BlockingQueue<Offer> fromB;
|
---|
22 | protected BlockingQueue<Offer> toB;
|
---|
23 |
|
---|
24 | protected List<OfferBy> log;
|
---|
25 |
|
---|
26 | public BilateralProtocol(BlockingQueue<Offer> fromA,
|
---|
27 | BlockingQueue<Offer> toA, BlockingQueue<Offer> fromB,
|
---|
28 | BlockingQueue<Offer> toB) {
|
---|
29 | super();
|
---|
30 | this.fromA = fromA;
|
---|
31 | this.toA = toA;
|
---|
32 | this.fromB = fromB;
|
---|
33 | this.toB = toB;
|
---|
34 | this.log = new ArrayList<OfferBy>();
|
---|
35 | }
|
---|
36 |
|
---|
37 | protected abstract ValidationResult validate(OfferBy o);
|
---|
38 |
|
---|
39 | @Override
|
---|
40 | public void run()
|
---|
41 | {
|
---|
42 | String sender = "A";
|
---|
43 |
|
---|
44 | // Wait for a message from A or B
|
---|
45 | try {
|
---|
46 | while (true)
|
---|
47 | {
|
---|
48 | BlockingQueue<Offer> from = from(sender); // starts with A
|
---|
49 | BlockingQueue<Offer> to = to(sender); // starts with B
|
---|
50 |
|
---|
51 | Offer o = from.poll();
|
---|
52 |
|
---|
53 | if (o != null)
|
---|
54 | {
|
---|
55 | OfferBy ob = new OfferBy(sender, o);
|
---|
56 | ValidationResult validationResult = validate(ob);
|
---|
57 | if (validationResult.isValid()) // if the offer was valid
|
---|
58 | {
|
---|
59 | log(ob);
|
---|
60 | to.put(o);
|
---|
61 | }
|
---|
62 | else // drop!
|
---|
63 | System.err.println("Warning: " + ob + " was not a valid offer and was dropped. Log: " + log.toString());
|
---|
64 |
|
---|
65 | // The validation could result in a state where the negotiation ended.
|
---|
66 | // We have an outcome, we let the agents know and clean up
|
---|
67 | if (validationResult.hasEnded())
|
---|
68 | {
|
---|
69 | Offer finalOutcome = validationResult.getOutcome();
|
---|
70 | toA.put(finalOutcome);
|
---|
71 | toB.put(finalOutcome);
|
---|
72 | System.out.println("Protocol detected final outcome: " + finalOutcome +
|
---|
73 | ". Notified agents and quitting.");
|
---|
74 | return;
|
---|
75 | }
|
---|
76 | }
|
---|
77 |
|
---|
78 | // Swap the queue to check: A <-> B
|
---|
79 | sender = swapRole(sender, "A", "B");
|
---|
80 | }
|
---|
81 | } catch (InterruptedException e) {
|
---|
82 | // TODO Auto-generated catch block
|
---|
83 | e.printStackTrace();
|
---|
84 | }
|
---|
85 | }
|
---|
86 |
|
---|
87 | private String swapRole(String x, String y, String z)
|
---|
88 | {
|
---|
89 | return x.equals(y) ? z : y;
|
---|
90 | }
|
---|
91 |
|
---|
92 | private BlockingQueue<Offer> from(String sender)
|
---|
93 | {
|
---|
94 | return "A".equals(sender) ? fromA : fromB;
|
---|
95 | }
|
---|
96 |
|
---|
97 | private BlockingQueue<Offer> to(String sender)
|
---|
98 | {
|
---|
99 | return "A".equals(sender) ? toB : toA;
|
---|
100 | }
|
---|
101 |
|
---|
102 | private void log(OfferBy ob)
|
---|
103 | {
|
---|
104 | log.add(ob);
|
---|
105 | System.out.println("Logged: " + ob);
|
---|
106 | }
|
---|
107 |
|
---|
108 | protected OfferBy getLastOffer()
|
---|
109 | {
|
---|
110 | return log.get(log.size() - 1);
|
---|
111 | }
|
---|
112 |
|
---|
113 | }
|
---|