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