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

com.github.thorbenkuck.network.connection.NativeConnectionContext Maven / Gradle / Ivy

The newest version!
package com.github.thorbenkuck.network.connection;

import com.github.thorbenkuck.network.stream.DataStream;
import com.github.thorbenkuck.network.stream.EventStream;

import java.net.SocketAddress;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

class NativeConnectionContext implements ConnectionContext {

    private final Connection connection;
    private final Supplier idSupplier;
    private final Function convert;
    private final EventHandler disconnected = new EventHandler<>();

    NativeConnectionContext(Connection connection, Supplier idSupplier, Function convert) {
        this.connection = connection;
        connection.setOnDisconnect(c -> disconnected.propagate(this));
        this.idSupplier = idSupplier;
        this.convert = convert;
    }

    @Override
    public void write(Object o) {
        inputStream().push(convert.apply(o));
    }

    @Override
    public DataStream inputStream() {
        return connection.input();
    }

    @Override
    public EventStream outputStream() {
        return connection.output();
    }

    @Override
    public DataStream systemInput() {
        return connection.systemInput();
    }

    @Override
    public EventStream systemOutput() {
        return connection.systemOutput();
    }

    @Override
    public String getIdentifier() {
        return idSupplier.get();
    }

    @Override
    public SocketAddress remoteAddress() {
        return connection.remoteAddress();
    }

    @Override
    public Connection connection() {
        return connection;
    }

    @Override
    public void onDisconnect(Consumer consumer) {
        disconnected.addHandler(consumer);
    }

    @Override
    public void onDisconnect(Runnable runnable) {
        disconnected.addHandler(runnable);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy