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