1 | package geniusweb.simplerunner;
|
---|
2 |
|
---|
3 | import java.io.IOException;
|
---|
4 | import java.nio.charset.StandardCharsets;
|
---|
5 | import java.nio.file.Files;
|
---|
6 | import java.nio.file.Paths;
|
---|
7 | import java.util.logging.Level;
|
---|
8 |
|
---|
9 | import com.fasterxml.jackson.databind.ObjectMapper;
|
---|
10 |
|
---|
11 | import geniusweb.events.ProtocolEvent;
|
---|
12 | import geniusweb.protocol.CurrentNegoState;
|
---|
13 | import geniusweb.protocol.NegoProtocol;
|
---|
14 | import geniusweb.protocol.NegoSettings;
|
---|
15 | import geniusweb.protocol.partyconnection.ProtocolToPartyConnFactory;
|
---|
16 | import tudelft.utilities.logging.ReportToLogger;
|
---|
17 | import tudelft.utilities.logging.Reporter;
|
---|
18 |
|
---|
19 | /**
|
---|
20 | * A simple tool to run a negotiation stand-alone, without starting the servers.
|
---|
21 | * All referred files and classes need to be stored locally (or be in the
|
---|
22 | * dependency list if you use maven).
|
---|
23 | *
|
---|
24 | */
|
---|
25 | public class NegoRunner implements Runnable {
|
---|
26 | private final NegoSettings settings;
|
---|
27 | private final NegoProtocol protocol;
|
---|
28 | private final ProtocolToPartyConnFactory connectionfactory;
|
---|
29 | private final Reporter log;
|
---|
30 | private final static ObjectMapper jackson = new ObjectMapper();
|
---|
31 |
|
---|
32 | public NegoRunner(NegoSettings settings,
|
---|
33 | ProtocolToPartyConnFactory connectionfactory, Reporter logger) {
|
---|
34 | if (settings == null || connectionfactory == null) {
|
---|
35 | throw new NullPointerException("Arguments must be not null");
|
---|
36 | }
|
---|
37 | this.settings = settings;
|
---|
38 | this.log = logger;
|
---|
39 | this.protocol = settings.getProtocol(log);
|
---|
40 | this.connectionfactory = connectionfactory;
|
---|
41 | }
|
---|
42 |
|
---|
43 | @Override
|
---|
44 | public void run() {
|
---|
45 | protocol.addListener(evt -> handle(evt));
|
---|
46 | protocol.start(connectionfactory);
|
---|
47 | }
|
---|
48 |
|
---|
49 | private void handle(ProtocolEvent evt) {
|
---|
50 | if (evt instanceof CurrentNegoState && ((CurrentNegoState) evt)
|
---|
51 | .getState().isFinal(System.currentTimeMillis())) {
|
---|
52 | stop();
|
---|
53 | }
|
---|
54 | }
|
---|
55 |
|
---|
56 | protected void stop() {
|
---|
57 | if (protocol.getState().getError() != null) {
|
---|
58 | log.log(Level.WARNING, "protocol terminated abnormally",
|
---|
59 | protocol.getState().getError());
|
---|
60 | } else {
|
---|
61 | log.log(Level.INFO,
|
---|
62 | "protocol ended normally: " + protocol.getState());
|
---|
63 | }
|
---|
64 | }
|
---|
65 |
|
---|
66 | public static void main(String[] args) throws IOException {
|
---|
67 | if (args.length != 1) {
|
---|
68 | showusage();
|
---|
69 | return;
|
---|
70 | }
|
---|
71 | String serialized = new String(Files.readAllBytes(Paths.get(args[0])),
|
---|
72 | StandardCharsets.UTF_8);
|
---|
73 | NegoSettings settings = jackson.readValue(serialized,
|
---|
74 | NegoSettings.class);
|
---|
75 |
|
---|
76 | NegoRunner runner = new NegoRunner(settings,
|
---|
77 | new ClassPathConnectionFactory(),
|
---|
78 | new ReportToLogger("sessionrunner"));
|
---|
79 | runner.run();
|
---|
80 | }
|
---|
81 |
|
---|
82 | private static void showusage() {
|
---|
83 | System.err.println("GeniusWeb stand-alone runner. ");
|
---|
84 | System.err.println("first argument should be <settings.json>.");
|
---|
85 | System.err.println(
|
---|
86 | "The settings.json file should contain the NegoSettings.");
|
---|
87 | System.err.println(
|
---|
88 | "See the settings.json example file and the GeniusWeb wiki pages. ");
|
---|
89 |
|
---|
90 | }
|
---|
91 | }
|
---|