
io.jsync.http.impl.DefaultServerWebSocket Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jsync.io Show documentation
Show all versions of jsync.io Show documentation
jsync.io is a non-blocking, event-driven networking framework for Java
/*
* Copyright (c) 2011-2013 The original author or authors
* ------------------------------------------------------
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Apache License v2.0 which accompanies this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* The Apache License v2.0 is available at
* http://www.opensource.org/licenses/apache2.0.php
*
* You may elect to redistribute this code under either of these licenses.
*/
package io.jsync.http.impl;
import io.jsync.Handler;
import io.jsync.MultiMap;
import io.jsync.buffer.Buffer;
import io.jsync.http.ServerWebSocket;
import io.jsync.http.WebSocketFrame;
import io.jsync.impl.AsyncInternal;
import io.jsync.net.impl.ConnectionBase;
public class DefaultServerWebSocket extends WebSocketImplBase implements ServerWebSocket {
private final String uri;
private final String path;
private final String query;
private final Runnable connectRunnable;
private final MultiMap headers;
private boolean connected;
private boolean rejected;
public DefaultServerWebSocket(AsyncInternal async, String uri, String path, String query, MultiMap headers,
ConnectionBase conn, Runnable connectRunnable) {
super(async, conn);
this.uri = uri;
this.path = path;
this.query = query;
this.headers = headers;
this.connectRunnable = connectRunnable;
}
@Override
public String uri() {
return uri;
}
@Override
public String path() {
return path;
}
@Override
public String query() {
return query;
}
@Override
public MultiMap headers() {
return headers;
}
@Override
public ServerWebSocket reject() {
checkClosed();
if (connectRunnable == null) {
throw new IllegalStateException("Cannot reject websocket on the client side");
}
if (connected) {
throw new IllegalStateException("Cannot reject websocket, it has already been written to");
}
rejected = true;
return this;
}
@Override
public void close() {
checkClosed();
if (this.connectRunnable != null) {
// Server side
if (rejected) {
throw new IllegalStateException("Cannot close websocket, it has been rejected");
}
if (!connected && !closed) {
connect();
}
}
super.close();
}
@Override
public ServerWebSocket dataHandler(Handler handler) {
checkClosed();
this.dataHandler = handler;
return this;
}
@Override
public ServerWebSocket endHandler(Handler handler) {
checkClosed();
this.endHandler = handler;
return this;
}
@Override
public ServerWebSocket exceptionHandler(Handler handler) {
checkClosed();
this.exceptionHandler = handler;
return this;
}
@Override
public ServerWebSocket closeHandler(Handler handler) {
checkClosed();
this.closeHandler = handler;
return this;
}
@Override
public ServerWebSocket frameHandler(Handler handler) {
checkClosed();
this.frameHandler = handler;
return this;
}
@Override
public ServerWebSocket pause() {
checkClosed();
conn.doPause();
return this;
}
@Override
public ServerWebSocket resume() {
checkClosed();
conn.doResume();
return this;
}
@Override
public ServerWebSocket setWriteQueueMaxSize(int maxSize) {
checkClosed();
conn.doSetWriteQueueMaxSize(maxSize);
return this;
}
@Override
public boolean writeQueueFull() {
checkClosed();
return conn.doWriteQueueFull();
}
@Override
public ServerWebSocket write(Buffer data) {
writeBinaryFrame(data);
return this;
}
@Override
public ServerWebSocket drainHandler(Handler handler) {
checkClosed();
this.drainHandler = handler;
return this;
}
@Override
public ServerWebSocket writeBinaryFrame(Buffer data) {
super.writeBinaryFrameInternal(data);
return this;
}
@Override
public ServerWebSocket writeTextFrame(String str) {
super.writeTextFrameInternal(str);
return this;
}
@Override
protected void writeFrame(WebSocketFrame frame) {
if (connectRunnable != null) {
if (rejected) {
throw new IllegalStateException("Cannot write to websocket, it has been rejected");
}
if (!connected && !closed) {
connect();
}
}
super.writeFrame(frame);
}
private void connect() {
connectRunnable.run();
connected = true;
}
// Connect if not already connected
void connectNow() {
if (!connected && !rejected) {
connect();
}
}
boolean isRejected() {
return rejected;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy