source: protocol/src/main/java/geniusweb/protocol/tournament/allpermutations/AllPermutationsProtocol.java@ 32

Last change on this file since 32 was 32, checked in by bart, 3 years ago

Multiple learns with repeated tournament, maven use https.

File size: 3.8 KB
Line 
1package geniusweb.protocol.tournament.allpermutations;
2
3import java.util.logging.Level;
4
5import geniusweb.events.ProtocolEvent;
6import geniusweb.events.TournamentStarted;
7import geniusweb.protocol.CurrentNegoState;
8import geniusweb.protocol.partyconnection.ProtocolToPartyConnFactory;
9import geniusweb.protocol.session.SessionProtocol;
10import geniusweb.protocol.session.SessionResult;
11import geniusweb.protocol.session.SessionSettings;
12import geniusweb.protocol.session.SessionState;
13import geniusweb.protocol.tournament.TournamentProtocol;
14import geniusweb.protocol.tournament.TournamentState;
15import geniusweb.references.PartyWithProfile;
16import geniusweb.references.ProtocolRef;
17import tudelft.utilities.listener.DefaultListenable;
18import tudelft.utilities.logging.Reporter;
19
20/**
21 * This protocol creates sequentially all permutations of available parties and
22 * profiles. Only 1 session is run at a time because managing multiple sessions
23 * is more complex to manage in the {@link TournamentState}
24 *
25 */
26public class AllPermutationsProtocol extends DefaultListenable<ProtocolEvent>
27 implements TournamentProtocol {
28 private static final ProtocolRef APP = new ProtocolRef("APP");
29 private final transient Reporter log;
30
31 private ProtocolToPartyConnFactory connectionFactory; // FINAL
32 private AllPermutationsState state; // MUTABLE
33
34 /**
35 *
36 * @param state the {@link AllPermutationsState}
37 * @param logger used for all logging, about the tournament, and the created
38 * the session and protocols.
39 */
40 public AllPermutationsProtocol(AllPermutationsState state,
41 Reporter logger) {
42 this.state = state;
43 this.log = logger;
44 }
45
46 @Override
47 public void start(ProtocolToPartyConnFactory connectionfactory) {
48 this.connectionFactory = connectionfactory;
49 notifyListeners(new TournamentStarted(state.getSize().longValue()));
50 startNextSession();
51 }
52
53 /**
54 * Start the next session in a new thread. Using a new thread ensures that
55 * all calls to this return immediately.
56 */
57 private void startNextSession() {
58 new Thread(new Runnable() {
59
60 @Override
61 public void run() {
62 log.log(Level.INFO,
63 "Tournament " + this + " starting next session ");
64 SessionSettings settings = state.getNextSettings();
65 SessionProtocol sessionprotocol = settings.getProtocol(log);
66 sessionprotocol.addListener(evt -> sessionEvent(evt));
67 sessionprotocol.start(connectionFactory);
68 }
69 }).start();
70 }
71
72 /**
73 * Called when the session completed.
74 *
75 * @param data the ProtocolEvent data,
76 */
77 private void sessionEvent(ProtocolEvent data) {
78 if (data instanceof CurrentNegoState) {
79 SessionState sessionstate = (SessionState) ((CurrentNegoState) data)
80 .getState();
81 long now = System.currentTimeMillis();
82 if (sessionstate.isFinal(now)) {
83 SessionResult result = sessionstate.getResult();
84 state = state.with(result);
85 notifyListeners(new CurrentNegoState(state));
86 if (!state.isFinal(now)) {
87 startNextSession();
88 }
89 }
90 }
91 }
92
93 @Override
94 public String getDescription() {
95 return "Runs all possible permutations of provided parties and profiles, "
96 + "with the given number of parties per session. Sessions run one at a time. "
97 + "If reuseParties is set, then a party can occur more than 1 time in each session. "
98 + "The sessionSettings determine the baseline settings for each session, eg the deadline "
99 + "and the protocol to be used. The AllPermutationsProtocol supports all basic SessionProtocols. "
100 + "parties and protocols already available in the settings will participate in every session. ";
101 }
102
103 @Override
104 public TournamentState getState() {
105 return state;
106 }
107
108 @Override
109 public ProtocolRef getRef() {
110 return APP;
111 }
112
113 @Override
114 public void addParticipant(PartyWithProfile party) {
115 log.log(Level.INFO, "tournament ignoring join attempt by " + party);
116 }
117
118}
Note: See TracBrowser for help on using the repository browser.