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

Last change on this file since 16 was 1, checked in by bart, 5 years ago

Initial Release

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 com.fasterxml.jackson.databind.ObjectMapper;
16
17import geniusweb.actions.PartyId;
18import geniusweb.partiesserver.repository.RunningPartiesRepo;
19import geniusweb.partiesserver.repository.RunningPartyInfo;
20import tudelft.utilities.listener.Listener;
21import tudelft.utilities.logging.ReportToLogger;
22import 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")
30public 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}
Note: See TracBrowser for help on using the repository browser.