[46] | 1 | package geniusweb.partiesserver.websocket;
|
---|
| 2 |
|
---|
| 3 | import java.io.IOException;
|
---|
| 4 | import java.net.URI;
|
---|
| 5 | import java.net.URISyntaxException;
|
---|
| 6 | import java.net.UnknownHostException;
|
---|
| 7 | import java.util.LinkedList;
|
---|
| 8 | import java.util.List;
|
---|
| 9 | import java.util.logging.Level;
|
---|
| 10 |
|
---|
| 11 | import javax.management.MalformedObjectNameException;
|
---|
| 12 | import javax.websocket.OnClose;
|
---|
| 13 | import javax.websocket.OnError;
|
---|
| 14 | import javax.websocket.OnOpen;
|
---|
| 15 | import javax.websocket.Session;
|
---|
| 16 | import javax.websocket.server.ServerEndpoint;
|
---|
| 17 |
|
---|
| 18 | import geniusweb.partiesserver.Constants;
|
---|
| 19 | import geniusweb.partiesserver.repository.AvailablePartiesRepo;
|
---|
| 20 | import geniusweb.partiesserver.repository.AvailableParty;
|
---|
| 21 | import geniusweb.partiesserver.repository.GeneralPartyInfo;
|
---|
| 22 | import tudelft.utilities.listener.Listener;
|
---|
| 23 | import tudelft.utilities.logging.ReportToLogger;
|
---|
| 24 | import 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")
|
---|
| 32 | public 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 | }
|
---|