-
Notifications
You must be signed in to change notification settings - Fork 164
/
AtmospherePushConnection.java
415 lines (373 loc) · 13.4 KB
/
AtmospherePushConnection.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
/*
* Copyright 2000-2022 Vaadin Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.vaadin.flow.server.communication;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.Reader;
import java.io.Serializable;
import java.io.StringReader;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import org.atmosphere.cpr.AtmosphereResource;
import org.atmosphere.cpr.AtmosphereResource.TRANSPORT;
import org.atmosphere.util.Version;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.vaadin.flow.component.UI;
import com.vaadin.flow.internal.UsageStatistics;
import com.vaadin.flow.shared.communication.PushConstants;
import elemental.json.JsonObject;
/**
* A {@link PushConnection} implementation using the Atmosphere push support
* that is by default included in Vaadin.
* <p>
* For internal use only. May be renamed or removed in a future release.
*
* @author Vaadin Ltd
* @since 1.0
*/
public class AtmospherePushConnection implements PushConnection {
private UI ui;
private transient State state = State.DISCONNECTED;
private transient AtmosphereResource resource;
private transient FragmentedMessage incomingMessage;
private transient Future<Object> outgoingMessage;
private transient Object lock = new Object();
/**
* Represents a message that can arrive as multiple fragments.
*/
protected static class FragmentedMessage implements Serializable {
private final StringBuilder message = new StringBuilder();
private final int messageLength;
/**
* Creates a message by reading from the given reader.
* <p>
* Immediately reads the length of the message (up until
* {@value PushConstants#MESSAGE_DELIMITER}) from the reader.
*
* @param reader
* the reader to read the message from
* @throws IOException
* if an exception occurred while reading from the reader or
* if unexpected data was read
*/
public FragmentedMessage(Reader reader) throws IOException {
// Messages are prefixed by the total message length plus a
// delimiter
String length = "";
int c;
while ((c = reader.read()) != -1
&& c != PushConstants.MESSAGE_DELIMITER) {
length += (char) c;
}
try {
messageLength = Integer.parseInt(length);
} catch (NumberFormatException e) {
throw new IOException("Invalid message length " + length, e);
}
}
/**
* Appends all the data from the given Reader to this message and
* returns whether the message was completed.
*
* @param reader
* The Reader from which to read.
* @return true if this message is complete, false otherwise.
* @throws IOException
* if an IO error occurred
*/
public boolean append(Reader reader) throws IOException {
char[] buffer = new char[PushConstants.WEBSOCKET_BUFFER_SIZE];
int read;
while ((read = reader.read(buffer)) != -1) {
message.append(buffer, 0, read);
assert message.length() <= messageLength : "Received message "
+ message.length() + "chars, expected " + messageLength;
}
return message.length() == messageLength;
}
public Reader getReader() {
return new StringReader(message.toString());
}
}
protected enum State {
/**
* Not connected. Trying to push will set the connection state to
* PUSH_PENDING or RESPONSE_PENDING and defer sending the message until
* a connection is established.
*/
DISCONNECTED,
/**
* Not connected. An asynchronous push is pending the opening of the
* connection.
*/
PUSH_PENDING,
/**
* Not connected. A response to a client request is pending the opening
* of the connection.
*/
RESPONSE_PENDING,
/**
* Connected. Messages can be sent through the connection.
*/
CONNECTED;
}
/**
* Creates an instance connected to the given UI.
*
* @param ui
* the UI to which this connection belongs
*/
public AtmospherePushConnection(UI ui) {
this.ui = ui;
UsageStatistics.markAsUsed("flow/AtmospherePushConnection",
getAtmosphereVersion());
}
/**
* Gets the Atmosphere version in use, as reported by
* {@link Version#getRawVersion()}.
*
* @return the Atmosphere version in use or null if Atmosphere was not found
*/
public static String getAtmosphereVersion() {
try {
String v = Version.getRawVersion();
assert v != null;
return v;
} catch (NoClassDefFoundError e) {
return null;
}
}
@Override
public void push() {
push(true);
}
/**
* Pushes pending state changes and client RPC calls to the client. If
* {@code isConnected()} is false, defers the push until a connection is
* established.
*
* @param async
* True if this push asynchronously originates from the server,
* false if it is a response to a client request.
*/
public void push(boolean async) {
synchronized (lock) {
if (!isConnected()) {
if (async && state != State.RESPONSE_PENDING) {
state = State.PUSH_PENDING;
} else {
state = State.RESPONSE_PENDING;
}
} else {
try {
JsonObject response = new UidlWriter().createUidl(getUI(),
async);
sendMessage("for(;;);[" + response.toJson() + "]");
} catch (Exception e) {
throw new RuntimeException("Push failed", e);
}
}
}
}
/**
* Sends the given message to the current client. Cannot be called if
* {@link #isConnected()} returns false.
*
* @param message
* The message to send
*/
protected void sendMessage(String message) {
assert (isConnected());
// "Broadcast" the changes to the single client only
outgoingMessage = getResource().getBroadcaster().broadcast(message,
getResource());
}
/**
* Reads and buffers a (possibly partial) message. If a complete message was
* received, or if the call resulted in the completion of a partially
* received message, returns a {@link Reader} yielding the complete message.
* Otherwise, returns null.
*
* @param reader
* A Reader from which to read the (partial) message
* @return A Reader yielding a complete message or null if the message is
* not yet complete.
* @throws IOException
* if an IO error occurred
* @return a Reader yielding the complete message, or {@code null} if the
* received message was a partial message
*/
protected Reader receiveMessage(Reader reader) throws IOException {
if (resource == null || resource.transport() != TRANSPORT.WEBSOCKET) {
return reader;
}
if (incomingMessage == null) {
// No existing partially received message
incomingMessage = new FragmentedMessage(reader);
}
if (incomingMessage.append(reader)) {
// Message is complete
Reader completeReader = incomingMessage.getReader();
incomingMessage = null;
return completeReader;
} else {
// Only received a partial message
return null;
}
}
@Override
public boolean isConnected() {
assert state != null;
assert (state == State.CONNECTED) ^ (resource == null);
return state == State.CONNECTED;
}
/**
* Associates this {@code AtmospherePushConnection} with the given
* {@link AtmosphereResource} representing an established push connection.
* If already connected, calls {@link #disconnect()} first. If there is a
* deferred push, carries it out via the new connection.
*
* @param resource
* the resource to associate this connection with
*/
public void connect(AtmosphereResource resource) {
assert resource != null;
assert resource != this.resource;
if (isConnected()) {
disconnect();
}
this.resource = resource;
State oldState = state;
state = State.CONNECTED;
if (oldState == State.PUSH_PENDING
|| oldState == State.RESPONSE_PENDING) {
// Sending a "response" message (async=false) also takes care of a
// pending push, but not vice versa
push(oldState == State.PUSH_PENDING);
}
}
/**
* @return the UI associated with this connection.
*/
protected UI getUI() {
return ui;
}
/**
* @return The AtmosphereResource associated with this connection or null if
* connection not open.
*/
protected AtmosphereResource getResource() {
return resource;
}
@Override
public void disconnect() {
synchronized (lock) {
assert isConnected();
if (resource == null) {
// Already disconnected. Should not happen but if it does, we
// don't
// want to cause NPEs
getLogger().debug(
"AtmospherePushConnection.disconnect() called twice, this should not happen");
return;
}
if (resource.isResumed()) {
// This can happen for long polling because of
// http://dev.vaadin.com/ticket/16919
// Once that is fixed, this should never happen
connectionLost();
return;
}
if (outgoingMessage != null) {
// Wait for the last message to be sent before closing the
// connection (assumes that futures are completed in order)
try {
outgoingMessage.get(1000, TimeUnit.MILLISECONDS);
} catch (TimeoutException e) {
getLogger().info(
"Timeout waiting for messages to be sent to client before disconnect",
e);
} catch (Exception e) {
getLogger().info(
"Error waiting for messages to be sent to client before disconnect",
e);
}
outgoingMessage = null;
}
try {
resource.close();
} catch (IOException e) {
getLogger().info("Error when closing push connection", e);
}
connectionLost();
}
}
/**
* Called when the connection to the client has been lost.
*
*/
public void connectionLost() {
resource = null;
if (state == State.CONNECTED) {
// Guard against connectionLost being (incorrectly) called when
// state is PUSH_PENDING or RESPONSE_PENDING
// (http://dev.vaadin.com/ticket/16919)
state = State.DISCONNECTED;
}
}
/**
* Returns the state of this connection.
*
* @return the state of this connection
*/
protected State getState() {
return state;
}
/**
* Reinitializes this PushConnection after deserialization. The connection
* is initially in disconnected state; the client will handle the
* reconnecting.
*
* @param stream
* the object to read
* @throws IOException
* if an IO error occurred
* @throws ClassNotFoundException
* if the class of the stream object could not be found
*/
private void readObject(ObjectInputStream stream)
throws IOException, ClassNotFoundException {
stream.defaultReadObject();
state = State.DISCONNECTED;
lock = new Object();
}
private static Logger getLogger() {
return LoggerFactory
.getLogger(AtmospherePushConnection.class.getName());
}
/**
* Internal method used for reconfiguring loggers to show all Atmosphere log
* messages in the console.
*
*/
public static void enableAtmosphereDebugLogging() {
getLogger().warn(
"Enable logging of 'org.atmosphere' through your slf4j implementation"
+ " instead (i.e.: logback, log4j, etc)");
}
}