source: src/main/java/bargainingchips/protocol/BilateralProtocol.java@ 337

Last change on this file since 337 was 337, checked in by Tim Baarslag, 5 years ago

Added support for PreAccept and FinalAccept

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