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

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

There is a newer version: 0.4.0
Show newest version
package com.github.thorbenkuck.network.connection;

import com.github.thorbenkuck.network.pipeline.Branch;
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 Branch mainBranch = new Branch<>();

	NativeConnectionContext(Connection connection, Supplier idSupplier, Function convert) {
		this.connection = connection;
		connection.setOnDisconnect(c -> mainBranch.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) {
		mainBranch.add(consumer);
	}

	@Override
	public void onDisconnect(Runnable runnable) {
		mainBranch.add(runnable);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy