All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.jsync.http.impl.WebSocketImplBase Maven / Gradle / Ivy

There is a newer version: 1.10.13
Show newest version
/*
 * 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.buffer.Buffer;
import io.jsync.eventbus.Message;
import io.jsync.http.WebSocketBase;
import io.jsync.http.WebSocketFrame;
import io.jsync.http.impl.ws.DefaultWebSocketFrame;
import io.jsync.http.impl.ws.WebSocketFrameInternal;
import io.jsync.impl.AsyncInternal;
import io.jsync.net.impl.ConnectionBase;
import io.netty.buffer.ByteBuf;

import java.net.InetSocketAddress;
import java.util.UUID;

/**
 * @author Tim Fox
 */
public abstract class WebSocketImplBase implements WebSocketBase {

    protected final ConnectionBase conn;
    private final String textHandlerID;
    private final String binaryHandlerID;
    private final AsyncInternal async;
    protected Handler frameHandler;
    protected Handler dataHandler;
    protected Handler drainHandler;
    protected Handler exceptionHandler;
    protected Handler closeHandler;
    protected Handler endHandler;
    protected Handler> binaryHandler;
    protected Handler> textHandler;
    protected boolean closed;

    protected WebSocketImplBase(AsyncInternal async, ConnectionBase conn) {
        this.async = async;
        this.textHandlerID = UUID.randomUUID().toString();
        this.binaryHandlerID = UUID.randomUUID().toString();
        this.conn = conn;
        binaryHandler = new Handler>() {
            public void handle(Message msg) {
                writeBinaryFrameInternal(msg.body());
            }
        };
        async.eventBus().registerLocalHandler(binaryHandlerID, binaryHandler);
        textHandler = new Handler>() {
            public void handle(Message msg) {
                writeTextFrameInternal(msg.body());
            }
        };
        async.eventBus().registerLocalHandler(textHandlerID, textHandler);
    }

    public String binaryHandlerID() {
        return binaryHandlerID;
    }

    public String textHandlerID() {
        return textHandlerID;
    }

    public boolean writeQueueFull() {
        checkClosed();
        return conn.doWriteQueueFull();
    }

    public void close() {
        checkClosed();
        conn.close();
        cleanupHandlers();
    }

    @Override
    public InetSocketAddress localAddress() {
        return conn.localAddress();
    }

    @Override
    public InetSocketAddress remoteAddress() {
        return conn.remoteAddress();
    }

    protected void writeBinaryFrameInternal(Buffer data) {
        ByteBuf buf = data.getByteBuf();
        WebSocketFrame frame = new DefaultWebSocketFrame(WebSocketFrame.FrameType.BINARY, buf);
        writeFrame(frame);
    }

    protected void writeTextFrameInternal(String str) {
        WebSocketFrame frame = new DefaultWebSocketFrame(str);
        writeFrame(frame);
    }


    private void cleanupHandlers() {
        if (!closed) {
            async.eventBus().unregisterHandler(binaryHandlerID, binaryHandler);
            async.eventBus().unregisterHandler(textHandlerID, textHandler);
            closed = true;
        }
    }

    protected void writeFrame(WebSocketFrame frame) {
        checkClosed();
        conn.write(frame);
    }

    protected void checkClosed() {
        if (closed) {
            throw new IllegalStateException("WebSocket is closed");
        }
    }

    void handleFrame(WebSocketFrameInternal frame) {
        if (dataHandler != null) {
            Buffer buff = new Buffer(frame.getBinaryData());
            dataHandler.handle(buff);
        }

        if (frameHandler != null) {
            frameHandler.handle(frame);
        }
    }

    void writable() {
        if (drainHandler != null) {
            Handler dh = drainHandler;
            drainHandler = null;
            dh.handle(null);
        }
    }

    void handleException(Throwable t) {
        if (exceptionHandler != null) {
            exceptionHandler.handle(t);
        }
    }

    void handleClosed() {
        cleanupHandlers();
        if (endHandler != null) {
            endHandler.handle(null);
        }
        if (closeHandler != null) {
            closeHandler.handle(null);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy