Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright (C) 2000-2024 Vaadin Ltd
*
* This program is available under Vaadin Commercial License and Service Terms.
*
* See for the full
* license.
*/
package com.vaadin.server.communication;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.Reader;
import java.io.Serializable;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.logging.ConsoleHandler;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import org.atmosphere.cpr.AtmosphereResource;
import org.atmosphere.cpr.AtmosphereResource.TRANSPORT;
import org.atmosphere.cpr.BroadcastFilterAdapter;
import org.atmosphere.util.Version;
import com.vaadin.shared.communication.PushConstants;
import com.vaadin.ui.UI;
/**
* A {@link PushConnection} implementation using the Atmosphere push support
* that is by default included in Vaadin.
*
* @author Vaadin Ltd
* @since 7.1
*/
public class AtmospherePushConnection implements PushConnection {
public static String getAtmosphereVersion() {
try {
String v = Version.getRawVersion();
assert v != null;
return v;
} catch (NoClassDefFoundError e) {
return null;
}
}
/**
* 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;
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
*/
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;
}
private final UI ui;
private transient State state = State.DISCONNECTED;
private transient AtmosphereResource resource;
private transient FragmentedMessage incomingMessage;
private transient Future