source: src/main/java/geniusweb/partiesserver/websocket/RunningListSocket.java@ 27

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

some minor fixes

File size: 2.8 KB
Line 
1package geniusweb.partiesserver.websocket;
2
3import java.io.IOException;
4import java.util.List;
5import java.util.logging.Level;
6import java.util.stream.Collectors;
7
8import javax.websocket.OnClose;
9import javax.websocket.OnError;
10import javax.websocket.OnMessage;
11import javax.websocket.OnOpen;
12import javax.websocket.Session;
13import javax.websocket.server.ServerEndpoint;
14
15import geniusweb.actions.PartyId;
16import geniusweb.partiesserver.Jackson;
17import geniusweb.partiesserver.repository.RunningPartiesRepo;
18import geniusweb.partiesserver.repository.RunningPartyInfo;
19import tudelft.utilities.listener.Listener;
20import tudelft.utilities.logging.ReportToLogger;
21import 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")
29public 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(Jackson.instance().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}
Note: See TracBrowser for help on using the repository browser.