[316] | 1 | package bargainingchips.protocol;
|
---|
[315] | 2 |
|
---|
| 3 | import java.util.List;
|
---|
| 4 |
|
---|
| 5 | import java.util.ArrayList;
|
---|
| 6 | import java.util.concurrent.BlockingQueue;
|
---|
| 7 |
|
---|
[316] | 8 | import bargainingchips.actions.Offer;
|
---|
| 9 | import bargainingchips.actions.OfferBy;
|
---|
[315] | 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
|
---|
[322] | 19 | protected final BlockingQueue<OfferBy> from;
|
---|
[337] | 20 |
|
---|
| 21 | // Should be considered final
|
---|
| 22 | protected String nameA;
|
---|
| 23 | protected BlockingQueue<Offer> toA;
|
---|
| 24 | protected String nameB;
|
---|
| 25 | protected BlockingQueue<Offer> toB;
|
---|
[315] | 26 |
|
---|
[322] | 27 | protected final List<OfferBy> log;
|
---|
[315] | 28 |
|
---|
[322] | 29 | public BilateralProtocol(BlockingQueue<OfferBy> from, String nameA,
|
---|
| 30 | BlockingQueue<Offer> toA, String nameB, BlockingQueue<Offer> toB) {
|
---|
[315] | 31 | super();
|
---|
[322] | 32 | this.from = from;
|
---|
| 33 | this.nameA = nameA;
|
---|
[315] | 34 | this.toA = toA;
|
---|
[322] | 35 | this.nameB = nameB;
|
---|
[315] | 36 | this.toB = toB;
|
---|
| 37 | this.log = new ArrayList<OfferBy>();
|
---|
| 38 | }
|
---|
| 39 |
|
---|
[319] | 40 | protected abstract ValidationResult validate(OfferBy o);
|
---|
[315] | 41 |
|
---|
| 42 | @Override
|
---|
| 43 | public void run()
|
---|
| 44 | {
|
---|
| 45 | // Wait for a message from A or B
|
---|
| 46 | try {
|
---|
| 47 | while (true)
|
---|
| 48 | {
|
---|
[322] | 49 | OfferBy ob = from.poll();
|
---|
[315] | 50 |
|
---|
[322] | 51 | if (ob != null)
|
---|
[315] | 52 | {
|
---|
[322] | 53 | String sender = ob.getSender();
|
---|
| 54 | Offer o = ob.getOffer();
|
---|
[319] | 55 | ValidationResult validationResult = validate(ob);
|
---|
[322] | 56 |
|
---|
| 57 | if (validationResult.isValid()) // if the offer was valid
|
---|
[315] | 58 | {
|
---|
[322] | 59 | log(ob); // make it official
|
---|
[337] | 60 | BlockingQueue<Offer> to = addressee(sender); // put it in the right outbox by looking up sender among nameA and nameB
|
---|
[315] | 61 | to.put(o);
|
---|
| 62 | }
|
---|
| 63 | else // drop!
|
---|
[317] | 64 | System.err.println("Warning: " + ob + " was not a valid offer and was dropped. Log: " + log.toString());
|
---|
[319] | 65 |
|
---|
| 66 | // The validation could result in a state where the negotiation ended.
|
---|
[322] | 67 | // We have an outcome, we let both agents know and clean up
|
---|
[319] | 68 | if (validationResult.hasEnded())
|
---|
| 69 | {
|
---|
| 70 | Offer finalOutcome = validationResult.getOutcome();
|
---|
| 71 | toA.put(finalOutcome);
|
---|
| 72 | toB.put(finalOutcome);
|
---|
| 73 | System.out.println("Protocol detected final outcome: " + finalOutcome +
|
---|
| 74 | ". Notified agents and quitting.");
|
---|
| 75 | return;
|
---|
| 76 | }
|
---|
[315] | 77 | }
|
---|
| 78 | }
|
---|
| 79 | } catch (InterruptedException e) {
|
---|
| 80 | // TODO Auto-generated catch block
|
---|
| 81 | e.printStackTrace();
|
---|
| 82 | }
|
---|
| 83 | }
|
---|
| 84 |
|
---|
[322] | 85 | /**
|
---|
| 86 | * Gives the correct outbox for the sender
|
---|
| 87 | */
|
---|
| 88 | private BlockingQueue<Offer> addressee(String sender)
|
---|
[315] | 89 | {
|
---|
[322] | 90 | if (nameA.equals(sender))
|
---|
| 91 | return toB;
|
---|
| 92 |
|
---|
| 93 | if (nameB.equals(sender))
|
---|
| 94 | return toA;
|
---|
| 95 |
|
---|
| 96 | throw new IllegalStateException("Sender " + sender + " unknown! Should be either " + nameA + " or " + nameB);
|
---|
[315] | 97 | }
|
---|
| 98 |
|
---|
| 99 | private void log(OfferBy ob)
|
---|
| 100 | {
|
---|
| 101 | log.add(ob);
|
---|
[337] | 102 | System.out.println("Logged: " + ob);
|
---|
[315] | 103 | }
|
---|
| 104 |
|
---|
| 105 | protected OfferBy getLastOffer()
|
---|
| 106 | {
|
---|
| 107 | return log.get(log.size() - 1);
|
---|
[319] | 108 | }
|
---|
[315] | 109 |
|
---|
| 110 | }
|
---|