source: simplerunner/src/main/java/geniusweb/simplerunner/NegoRunner.java@ 24

Last change on this file since 24 was 24, checked in by bart, 4 years ago

Fixes an issue with processing maxPower of a vote. Javadoc maven plugin now uses latest version.

File size: 4.4 KB
Line 
1package geniusweb.simplerunner;
2
3import java.io.IOException;
4import java.nio.charset.StandardCharsets;
5import java.nio.file.Files;
6import java.nio.file.Paths;
7import java.util.logging.Level;
8
9import com.fasterxml.jackson.core.JsonProcessingException;
10import com.fasterxml.jackson.databind.ObjectMapper;
11
12import geniusweb.events.ProtocolEvent;
13import geniusweb.protocol.CurrentNegoState;
14import geniusweb.protocol.NegoProtocol;
15import geniusweb.protocol.NegoSettings;
16import geniusweb.protocol.NegoState;
17import geniusweb.protocol.partyconnection.ProtocolToPartyConnFactory;
18import tudelft.utilities.logging.Reporter;
19
20/**
21 * A simple tool to run a negotiation stand-alone, without starting the servers.
22 * All referred files and classes need to be stored locally (or be in the
23 * dependency list if you use maven).
24 *
25 */
26public class NegoRunner implements Runnable {
27 private final NegoSettings settings;
28 private final NegoProtocol protocol;
29 private final ProtocolToPartyConnFactory connectionfactory;
30 protected final Reporter log;
31 private boolean properlyStopped = false;
32 private final static ObjectMapper jackson = new ObjectMapper();
33 private final int LOOPTIME = 200;// ms
34 private long maxruntime;
35
36 /**
37 *
38 * @param settings the {@link NegoSettings}
39 * @param connectionfactory the {@link ProtocolToPartyConnFactory}
40 * @param logger the {@link Reporter} to log problems
41 * @param maxruntime limit in millisecs. Ignored if 0
42 */
43 public NegoRunner(NegoSettings settings,
44 ProtocolToPartyConnFactory connectionfactory, Reporter logger,
45 long maxruntime) {
46 if (settings == null || connectionfactory == null) {
47 throw new NullPointerException("Arguments must be not null");
48 }
49 this.settings = settings;
50 this.log = logger;
51 this.protocol = settings.getProtocol(log);
52 this.connectionfactory = connectionfactory;
53 this.maxruntime = maxruntime;
54 }
55
56 /**
57 *
58 * @return true if the runner has finished
59 */
60 public boolean isProperlyStopped() {
61 return properlyStopped;
62 }
63
64 @Override
65 public void run() {
66 protocol.addListener(evt -> handle(evt));
67 protocol.start(connectionfactory);
68 long remainingtime = maxruntime;
69 while (!properlyStopped && (maxruntime == 0 || remainingtime > 0)) {
70 try {
71 Thread.sleep(LOOPTIME);
72 remainingtime -= LOOPTIME;
73 } catch (InterruptedException e) {
74 e.printStackTrace();
75 }
76 }
77 System.out.println("end run");
78 }
79
80 private void handle(ProtocolEvent evt) {
81 if (evt instanceof CurrentNegoState && ((CurrentNegoState) evt)
82 .getState().isFinal(System.currentTimeMillis())) {
83 stop();
84 }
85 }
86
87 protected void stop() {
88 logFinal(Level.INFO, protocol.getState());
89 properlyStopped = true;
90 }
91
92 /**
93 * Separate so that we can intercept this when mocking, as this will crash
94 * on mocks because {@link #jackson} can not handle mocks.
95 *
96 * @param level the log {@link Level}
97 * @param state the {@link NegoState} to log
98 */
99 protected void logFinal(Level level, NegoState state) {
100 try {
101 log.log(level, "protocol ended normally: "
102 + jackson.writeValueAsString(protocol.getState()));
103 } catch (JsonProcessingException e) {
104 e.printStackTrace();
105 }
106 }
107
108 /**
109 * The main runner
110 *
111 * @param args should have 1 argument, the settings.json file to be used.
112 * @throws IOException if problem occurs
113 */
114 public static void main(String[] args) throws IOException {
115 if (args.length != 1) {
116 showusage();
117 return;
118 }
119 String serialized = new String(Files.readAllBytes(Paths.get(args[0])),
120 StandardCharsets.UTF_8);
121 NegoSettings settings = jackson.readValue(serialized,
122 NegoSettings.class);
123
124 NegoRunner runner = new NegoRunner(settings,
125 new ClassPathConnectionFactory(), new StdOutReporter(), 0);
126 runner.run();
127 }
128
129 private static void showusage() {
130 System.err.println("GeniusWeb stand-alone runner.");
131 System.err.println("first argument should be <settings.json>.");
132 System.err.println(
133 "The settings.json file should contain the NegoSettings.");
134 System.err.println(
135 "See the settings.json example file and the GeniusWeb wiki pages. ");
136
137 }
138
139 /**
140 * @return protocol that runs/ran the session.
141 */
142 public NegoProtocol getProtocol() {
143 return protocol;
144 }
145}
146
147class StdOutReporter implements Reporter {
148
149 @Override
150 public void log(Level arg0, String arg1) {
151 System.out.println(arg0 + ":" + arg1);
152 }
153
154 @Override
155 public void log(Level arg0, String arg1, Throwable arg2) {
156 System.out.println(arg0 + ">" + arg1);
157 }
158
159}
Note: See TracBrowser for help on using the repository browser.