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

Last change on this file since 33 was 33, checked in by bart, 3 years ago

Major update. Changes in json serialization, maven dependencies and BOA. Fix in stand-alone runner.

File size: 3.6 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 /**
36 *
37 * @param uri the URI that will provide the {@link Profile}
38 * @param reporter the {@link Reporter} for logging issues
39 * @param wscontainer the {@link WebSocketContainer} that can provide new
40 * websockets. Typically ContainerProvider
41 * .getWebSocketContainer()
42 * @throws DeploymentException if the annotated endpoint instance is not
43 * valid
44 * @throws IOException if there was a network or protocol problem
45 * that prevented the client endpoint being
46 * connected to its server.
47 *
48 */
49 public WebsocketProfileConnector(URI uri, Reporter reporter,
50 WebSocketContainer wscontainer)
51 throws DeploymentException, IOException {
52 if (uri == null || reporter == null || wscontainer == null) {
53 throw new IllegalArgumentException(
54 "uri, reporter and wsconnector must be not null");
55 }
56 this.uri = uri;
57 this.logger = reporter;
58 // see #1763 expected max size of profiles.
59 wscontainer.setDefaultMaxTextMessageBufferSize(200000);
60 wscontainer.connectToServer(this, uri);
61 }
62
63 @OnOpen
64 public void onOpen(Session session) {
65 this.session = session;
66 logger.log(Level.FINE, "Connected: " + session.getBasicRemote());
67 }
68
69 @OnClose
70 public void onClose() {
71 if (session != null) {
72 logger.log(Level.FINE,
73 "Closed websocket: " + session.getBasicRemote());
74 this.session = null;
75 // remove listeners seems not needed
76 }
77 }
78
79 @OnMessage
80 public void processMessage(String message) throws IOException {
81 // this will be called every time the profile changes.
82 logger.log(Level.FINE, "Received profile: " + message);
83 profile = jackson.readValue(message, Profile.class);
84 notifyListeners(profile);
85 }
86
87 @OnError
88 public void processError(Throwable t) {
89 logger.log(Level.SEVERE,
90 "Something went wrong while processing downloaded profile", t);
91 }
92
93 @Override
94 public Profile getProfile() throws IOException {
95 int remaining_wait = TIMEOUT_MS;
96 try {
97 while (profile == null && remaining_wait > 0) {
98 remaining_wait -= 100;
99 Thread.sleep(100);
100 }
101 } catch (InterruptedException e) {
102 logger.log(Level.SEVERE, "interrupted while waiting for profile",
103 e);
104 }
105 if (profile == null) {
106 throw new IOException("Failed to fetch profile from " + uri);
107 }
108 return profile;
109 }
110
111 @Override
112 public void close() {
113 if (session != null) {
114 try {
115 session.close();
116 } catch (IOException e) {
117 logger.log(Level.WARNING,
118 "Falled to close websocket connection: "
119 + session.getBasicRemote());
120 }
121 }
122 }
123
124}
Note: See TracBrowser for help on using the repository browser.