[307] | 1 | package protocol;
|
---|
| 2 |
|
---|
| 3 | import java.util.List;
|
---|
| 4 |
|
---|
[308] | 5 | import java.util.ArrayList;
|
---|
[307] | 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 An 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;
|
---|
[308] | 23 |
|
---|
[307] | 24 | protected List<OfferBy> log;
|
---|
| 25 |
|
---|
[308] | 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);
|
---|
[307] | 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();
|
---|
[308] | 52 |
|
---|
[307] | 53 | if (o != null)
|
---|
| 54 | {
|
---|
[308] | 55 | OfferBy ob = new OfferBy(sender, o);
|
---|
| 56 | if (validate(ob))
|
---|
[307] | 57 | {
|
---|
[308] | 58 | log(ob);
|
---|
[307] | 59 | to.put(o);
|
---|
| 60 | }
|
---|
[308] | 61 | else // drop!
|
---|
| 62 | System.err.println("Warning: " + ob + " was not a valid offer. Log: " + log.toString());
|
---|
[307] | 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 |
|
---|
[308] | 89 | private void log(OfferBy ob)
|
---|
[307] | 90 | {
|
---|
[308] | 91 | log.add(ob);
|
---|
| 92 | System.out.println("Logged: " + ob);
|
---|
[307] | 93 | }
|
---|
| 94 |
|
---|
[308] | 95 | protected OfferBy getLastOffer()
|
---|
[307] | 96 | {
|
---|
| 97 | return log.get(log.size() - 1);
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | }
|
---|