1 | package protocol;
|
---|
2 |
|
---|
3 | import java.util.List;
|
---|
4 |
|
---|
5 | import java.util.ArrayList;
|
---|
6 | import java.util.concurrent.BlockingQueue;
|
---|
7 |
|
---|
8 | import onetomany.bargainingchipsgame.interactions.Offer;
|
---|
9 | import onetomany.bargainingchipsgame.interactions.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 boolean 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 | if (validate(ob))
|
---|
57 | {
|
---|
58 | log(ob);
|
---|
59 | to.put(o);
|
---|
60 | }
|
---|
61 | else // drop!
|
---|
62 | System.err.println("Warning: " + ob + " was not a valid offer. Log: " + log.toString());
|
---|
63 | }
|
---|
64 |
|
---|
65 | // Swap the queue to check: A <-> B
|
---|
66 | sender = swapRole(sender, "A", "B");
|
---|
67 | }
|
---|
68 | } catch (InterruptedException e) {
|
---|
69 | // TODO Auto-generated catch block
|
---|
70 | e.printStackTrace();
|
---|
71 | }
|
---|
72 | }
|
---|
73 |
|
---|
74 | private String swapRole(String x, String y, String z)
|
---|
75 | {
|
---|
76 | return x.equals(y) ? z : y;
|
---|
77 | }
|
---|
78 |
|
---|
79 | private BlockingQueue<Offer> from(String sender)
|
---|
80 | {
|
---|
81 | return "A".equals(sender) ? fromA : fromB;
|
---|
82 | }
|
---|
83 |
|
---|
84 | private BlockingQueue<Offer> to(String sender)
|
---|
85 | {
|
---|
86 | return "A".equals(sender) ? toB : toA;
|
---|
87 | }
|
---|
88 |
|
---|
89 | private void log(OfferBy ob)
|
---|
90 | {
|
---|
91 | log.add(ob);
|
---|
92 | System.out.println("Logged: " + ob);
|
---|
93 | }
|
---|
94 |
|
---|
95 | protected OfferBy getLastOffer()
|
---|
96 | {
|
---|
97 | return log.get(log.size() - 1);
|
---|
98 | }
|
---|
99 |
|
---|
100 | }
|
---|