source: profileconnection/src/main/java/geniusweb/profileconnection/WebsocketProfileConnector.java@ 52

Last change on this file since 52 was 52, checked in by ruud, 14 months ago

Fixed small issues in domaineditor.

File size: 3.8 KB
Line 
1package geniusweb.profileconnection;
2
3import java.io.IOException;
4import java.net.URI;
5import java.util.logging.Level;
6
7import javax.websocket.ClientEndpoint;
8import javax.websocket.DeploymentException;
9import javax.websocket.OnClose;
10import javax.websocket.OnError;
11import javax.websocket.OnMessage;
12import javax.websocket.OnOpen;
13import javax.websocket.Session;
14import javax.websocket.WebSocketContainer;
15
16import com.fasterxml.jackson.databind.ObjectMapper;
17
18import geniusweb.profile.Profile;
19import tudelft.utilities.listener.DefaultListenable;
20import tudelft.utilities.logging.Reporter;
21
22/**
23 * Container for the latest available profile on the profiles server
24 */
25@ClientEndpoint
26public class WebsocketProfileConnector extends DefaultListenable<Profile>
27 implements ProfileInterface {
28 private final static ObjectMapper jackson = new ObjectMapper();
29 private static final int TIMEOUT_MS = 2000;
30 private final Reporter logger;
31 private final URI uri; // to log possible errors
32 private Profile profile = null;
33 private Session session;
34
35 private StringBuffer buffer = new StringBuffer();
36
37 /**
38 *
39 * @param uri the URI that will provide the {@link Profile}
40 * @param reporter the {@link Reporter} for logging issues
41 * @param wscontainer the {@link WebSocketContainer} that can provide new
42 * websockets. Typically ContainerProvider
43 * .getWebSocketContainer()
44 * @throws DeploymentException if the annotated endpoint instance is not
45 * valid
46 * @throws IOException if there was a network or protocol problem
47 * that prevented the client endpoint being
48 * connected to its server.
49 *
50 */
51 public WebsocketProfileConnector(URI uri, Reporter reporter,
52 WebSocketContainer wscontainer)
53 throws DeploymentException, IOException {
54 if (uri == null || reporter == null || wscontainer == null) {
55 throw new IllegalArgumentException(
56 "uri, reporter and wsconnector must be not null");
57 }
58 this.uri = uri;
59 this.logger = reporter;
60 // see #1763 expected max size of profiles. #50 why we don't want that.
61 // wscontainer.setDefaultMaxTextMessageBufferSize(200000);
62 wscontainer.connectToServer(this, uri);
63 }
64
65 @OnOpen
66 public void onOpen(Session session) {
67 this.session = session;
68 logger.log(Level.FINE, "Connected: " + session.getBasicRemote());
69 }
70
71 @OnClose
72 public void onClose() {
73 if (session != null) {
74 logger.log(Level.FINE,
75 "Closed websocket: " + session.getBasicRemote());
76 this.session = null;
77 // remove listeners seems not needed
78 }
79 }
80
81 @OnMessage
82 public void processMessage(String message, boolean islast)
83 throws IOException {
84 buffer.append(message);
85 if (islast) {
86 String msg = buffer.toString();
87 buffer = new StringBuffer();
88 // this will be called every time the profile changes.
89 logger.log(Level.FINE, "Received profile: " + msg);
90 profile = jackson.readValue(msg, Profile.class);
91 notifyListeners(profile);
92 }
93 }
94
95 @OnError
96 public void processError(Throwable t) {
97 logger.log(Level.SEVERE,
98 "Something went wrong while processing downloaded profile", t);
99 }
100
101 @Override
102 public Profile getProfile() throws IOException {
103 int remaining_wait = TIMEOUT_MS;
104 try {
105 while (profile == null && remaining_wait > 0) {
106 remaining_wait -= 100;
107 Thread.sleep(100);
108 }
109 } catch (InterruptedException e) {
110 logger.log(Level.SEVERE, "interrupted while waiting for profile",
111 e);
112 }
113 if (profile == null) {
114 throw new IOException("Failed to fetch profile from " + uri);
115 }
116 return profile;
117 }
118
119 @Override
120 public void close() {
121 if (session != null) {
122 try {
123 session.close();
124 } catch (IOException e) {
125 logger.log(Level.WARNING,
126 "Falled to close websocket connection: "
127 + session.getBasicRemote());
128 }
129 }
130 }
131
132}
Note: See TracBrowser for help on using the repository browser.