1 | package geniusweb.partiesserver.websocket;
|
---|
2 |
|
---|
3 | import java.io.IOException;
|
---|
4 | import java.util.List;
|
---|
5 | import java.util.logging.Level;
|
---|
6 | import java.util.stream.Collectors;
|
---|
7 |
|
---|
8 | import javax.websocket.OnClose;
|
---|
9 | import javax.websocket.OnError;
|
---|
10 | import javax.websocket.OnMessage;
|
---|
11 | import javax.websocket.OnOpen;
|
---|
12 | import javax.websocket.Session;
|
---|
13 | import javax.websocket.server.ServerEndpoint;
|
---|
14 |
|
---|
15 | import geniusweb.actions.PartyId;
|
---|
16 | import geniusweb.partiesserver.Constants;
|
---|
17 | import geniusweb.partiesserver.repository.RunningPartiesRepo;
|
---|
18 | import geniusweb.partiesserver.repository.RunningPartyInfo;
|
---|
19 | import tudelft.utilities.listener.Listener;
|
---|
20 | import tudelft.utilities.logging.ReportToLogger;
|
---|
21 | import tudelft.utilities.logging.Reporter;
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * Returns a websocket that communicates the list of currently available
|
---|
25 | * (runnable) parties. Every time something changes, a new list of parties is
|
---|
26 | * sent.
|
---|
27 | */
|
---|
28 | @ServerEndpoint("/running")
|
---|
29 | public class RunningListSocket {
|
---|
30 |
|
---|
31 | private Listener<PartyId> changeListener; // final, but only initialized in
|
---|
32 | // startSession.
|
---|
33 | private Session session;
|
---|
34 | private RunningPartiesRepo repo;
|
---|
35 | private SendBuffer outstream;
|
---|
36 | private final Reporter log;
|
---|
37 |
|
---|
38 | public RunningListSocket() {
|
---|
39 | this(new ReportToLogger("partiesserver"));
|
---|
40 | }
|
---|
41 |
|
---|
42 | public RunningListSocket(Reporter reporter) {
|
---|
43 | this.log = reporter;
|
---|
44 | }
|
---|
45 |
|
---|
46 | @OnOpen
|
---|
47 | public void start(Session session) throws IOException {
|
---|
48 | this.session = session;
|
---|
49 | this.outstream = new SendBuffer(session.getBasicRemote(), log);
|
---|
50 |
|
---|
51 | repo = getRepo();
|
---|
52 | changeListener = new Listener<PartyId>() {
|
---|
53 | @Override
|
---|
54 | public void notifyChange(PartyId data) {
|
---|
55 | sendUpdatedParties();
|
---|
56 | }
|
---|
57 | };
|
---|
58 | repo.addListener(changeListener);
|
---|
59 |
|
---|
60 | sendUpdatedParties();
|
---|
61 | }
|
---|
62 |
|
---|
63 | /**
|
---|
64 | * Send the currently running parties to the client.
|
---|
65 | */
|
---|
66 | private void sendUpdatedParties() {
|
---|
67 | List<RunningPartyInfo> list = repo.list().stream()
|
---|
68 | .map(party -> new RunningPartyInfo(party))
|
---|
69 | .collect(Collectors.toList());
|
---|
70 | log.log(Level.FINE, "sending updated running parties list " + list);
|
---|
71 | try {
|
---|
72 | outstream.send(Constants.getJackson().writeValueAsString(repo.list()));
|
---|
73 | } catch (IOException e) {
|
---|
74 | log.log(Level.SEVERE, "Internal problem converting json", e);
|
---|
75 | }
|
---|
76 | }
|
---|
77 |
|
---|
78 | @OnMessage
|
---|
79 | public void onMessage(String message, Session session) {
|
---|
80 | log.log(Level.WARNING,
|
---|
81 | "runninglistsocket does not handle incoming messages ("
|
---|
82 | + message + ")");
|
---|
83 | }
|
---|
84 |
|
---|
85 | @OnClose
|
---|
86 | public void onClose() {
|
---|
87 | try {
|
---|
88 | outstream.stop();
|
---|
89 | session.close();
|
---|
90 | } catch (IOException e) {
|
---|
91 | log.log(Level.SEVERE, "Failed to close websocket", e);
|
---|
92 | }
|
---|
93 | repo.removeListener(changeListener);
|
---|
94 | }
|
---|
95 |
|
---|
96 | @OnError
|
---|
97 | public void onError(Throwable t) throws Throwable {
|
---|
98 | log.log(Level.SEVERE, "Internal problem converting json", t);
|
---|
99 | }
|
---|
100 |
|
---|
101 | /**
|
---|
102 | * FactoryMethod for testing
|
---|
103 | *
|
---|
104 | * @return RunningPartiesRepo
|
---|
105 | */
|
---|
106 | protected RunningPartiesRepo getRepo() {
|
---|
107 | return RunningPartiesRepo.instance();
|
---|
108 | }
|
---|
109 |
|
---|
110 | }
|
---|