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

com.github.ocraft.s2client.api.vertx.VertxChannel Maven / Gradle / Ivy

package com.github.ocraft.s2client.api.vertx;

/*-
 * #%L
 * ocraft-s2client-api
 * %%
 * Copyright (C) 2017 - 2018 Ocraft Project
 * %%
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 * #L%
 */

import com.github.ocraft.s2client.api.Channel;
import io.reactivex.Observable;
import io.reactivex.subjects.PublishSubject;
import io.reactivex.subjects.Subject;
import io.vertx.reactivex.core.eventbus.EventBus;
import io.vertx.reactivex.core.eventbus.Message;
import io.vertx.reactivex.core.eventbus.MessageProducer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.nio.BufferOverflowException;
import java.util.UUID;

import static com.github.ocraft.s2client.api.OcraftApiConfig.*;
import static com.github.ocraft.s2client.protocol.Preconditions.isSet;

public class VertxChannel implements Channel {

    private final Logger log = LoggerFactory.getLogger(VertxChannel.class);

    private static final String OUTPUT_STREAM = "OUTPUT_" + UUID.randomUUID().toString();
    private static final String INPUT_STREAM = "INPUT_" + UUID.randomUUID().toString();

    private final Observable outputStream;
    private final Observable inputStream;
    private final Subject errorStream = PublishSubject.create().toSerialized();
    private final MessageProducer inputMessageProducer;
    private final MessageProducer outputMessageProducer;
    private volatile boolean connected;
    private Runnable onConnectionLost;

    static VertxChannel from(EventBus eventBus) {
        return new VertxChannel(eventBus);
    }

    private VertxChannel(EventBus eventBus) {
        outputStream = initOutputStream(eventBus);
        inputStream = initInputStream(eventBus);

        outputMessageProducer = eventBus
                .publisher(OUTPUT_STREAM)
                .setWriteQueueMaxSize(cfg().getInt(CLIENT_BUFFER_SIZE_RESPONSE_EVENT_BUS));

        inputMessageProducer = eventBus
                .sender(INPUT_STREAM)
                .setWriteQueueMaxSize(cfg().getInt(CLIENT_BUFFER_SIZE_REQUEST_EVENT_BUS));
    }

    private Observable initOutputStream(EventBus eventBus) {
        log.debug("registering event bus consumer [{}] for output stream", OUTPUT_STREAM);
        return eventBus
                .consumer(OUTPUT_STREAM)
                .toObservable()
                .map(Message::body)
                .doOnComplete(errorStream::onComplete);
    }

    private Observable initInputStream(EventBus eventBus) {
        log.debug("registering event bus consumer [{}] for input stream", INPUT_STREAM);
        return eventBus.consumer(INPUT_STREAM).toObservable().map(Message::body);
    }

    @Override
    public void input(byte[] inputBytes) {
        if (!inputMessageProducer.writeQueueFull()) {
            inputMessageProducer.write(inputBytes);
        } else {
            throw new BufferOverflowException();
        }
    }

    @Override
    public void output(byte[] outputBytes) {
        if (!outputMessageProducer.writeQueueFull()) {
            outputMessageProducer.write(outputBytes);
        } else {
            throw new BufferOverflowException();
        }
    }

    @Override
    public void error(Throwable error) {
        errorStream.onError(error);
    }

    @Override
    public Observable outputStream() {
        return outputStream;
    }

    @Override
    public Observable inputStream() {
        return inputStream;
    }

    @Override
    public Observable errorStream() {
        return errorStream;
    }

    @Override
    public boolean ready() {
        return connected;
    }

    @Override
    public void onConnectionLost(Runnable onConnectionLost) {
        this.onConnectionLost = onConnectionLost;
    }

    void connected() {
        if (!connected) {
            connected = true;
        }
    }

    void disconnected() {
        if (connected) {
            connected = false;
            if (isSet(onConnectionLost)) {
                onConnectionLost.run();
            }
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy