source: src/main/java/geniusweb/partiesserver/websocket/PartiesListSocket.java@ 47

Last change on this file since 47 was 46, checked in by ruud, 22 months ago

Fixed small issues in domaineditor.

File size: 3.8 KB
Line 
1package geniusweb.partiesserver.websocket;
2
3import java.io.IOException;
4import java.net.URI;
5import java.net.URISyntaxException;
6import java.net.UnknownHostException;
7import java.util.LinkedList;
8import java.util.List;
9import java.util.logging.Level;
10
11import javax.management.MalformedObjectNameException;
12import javax.websocket.OnClose;
13import javax.websocket.OnError;
14import javax.websocket.OnOpen;
15import javax.websocket.Session;
16import javax.websocket.server.ServerEndpoint;
17
18import geniusweb.partiesserver.Constants;
19import geniusweb.partiesserver.repository.AvailablePartiesRepo;
20import geniusweb.partiesserver.repository.AvailableParty;
21import geniusweb.partiesserver.repository.GeneralPartyInfo;
22import tudelft.utilities.listener.Listener;
23import tudelft.utilities.logging.ReportToLogger;
24import tudelft.utilities.logging.Reporter;
25
26/**
27 * Returns a websocket that communicates the list of currently available
28 * (runnable) parties. Every time something changes, a new list of parties is
29 * sent.
30 */
31@ServerEndpoint("/available")
32public class PartiesListSocket {
33
34 private static final String DEFAULT_ADDRESS = "localhost:8080";
35 private final Reporter log;
36 private Listener<String> changeListener; // final, but only initialized in
37 // startSession.
38 private Session session;
39 private AvailablePartiesRepo repo;
40 private SendBuffer outstream;
41
42 public PartiesListSocket() {
43 this(new ReportToLogger("partiesserver"));
44 }
45
46 public PartiesListSocket(Reporter reporter) {
47 if (reporter == null)
48 throw new IllegalArgumentException("reporter must be not null");
49 this.log = reporter;
50 }
51
52 @OnOpen
53 public void start(Session session) throws IOException {
54 this.session = session;
55 this.outstream = new SendBuffer(session.getBasicRemote(), log);
56
57 repo = getRepo();
58 changeListener = new Listener<String>() {
59 @Override
60 public void notifyChange(String data) {
61 sendUpdatedParties();
62 }
63 };
64 repo.addListener(changeListener);
65
66 sendUpdatedParties();
67 }
68
69 /**
70 * Send the currently available parties to the client.
71 *
72 * @throws IOException
73 */
74 private void sendUpdatedParties() {
75 log.log(Level.INFO, "sending updated parties list1");
76 String ip = DEFAULT_ADDRESS; // default if something fails
77 try {
78 ip = Constants.getIpAddressAndPort() + "/" + getServerName();
79 } catch (MalformedObjectNameException | UnknownHostException e1) {
80 log.log(Level.SEVERE,
81 "Can't get proper server name and port, reverting to "
82 + DEFAULT_ADDRESS,
83 e1);
84 }
85 try {
86 List<GeneralPartyInfo> list = new LinkedList<>();
87 for (AvailableParty party : repo.list()) {
88 URI uri = new URI("http://" + ip + "/run/" + party.getName());
89 list.add(new GeneralPartyInfo(uri, party.getCapabilities(),
90 party.getDescription()));
91 }
92 String text = Constants.getJackson().writeValueAsString(list);
93 outstream.send(text);
94 } catch (IOException | URISyntaxException e) {
95 log.log(Level.SEVERE, "Bug sending updated parties list", e);
96 }
97 }
98
99 @OnClose
100 public void onClose() throws IOException {
101 repo.removeListener(changeListener);
102 outstream.stop();
103 }
104
105 @OnError
106 public void onError(Throwable t) throws Throwable {
107 if (t instanceof IOException && "Broken pipe".equals(t.getMessage())) {
108 log.log(Level.INFO, "Socket pipe broke:" + session);
109 } else {
110 log.log(Level.SEVERE,
111 "Tomcat reported an error on the connection:" + session, t);
112 }
113 }
114
115 /**
116 * FactoryMethod for testing
117 *
118 * @return RunningPartiesRepo
119 */
120 protected AvailablePartiesRepo getRepo() {
121 return AvailablePartiesRepo.instance();
122 }
123
124 /**
125 *
126 * @return name of our service, eg "profilesserver"
127 */
128 private String getServerName() {
129 // typically something like /profilesserver/websocket/liststream
130 // profilesserver is the name we're looking for
131 return session.getRequestURI().getPath().split("/")[1]; // drop leading
132 // '/'
133 }
134
135
136
137}
Note: See TracBrowser for help on using the repository browser.