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

reactor.net.NetChannel Maven / Gradle / Ivy

package reactor.net;

import reactor.core.composable.Promise;
import reactor.core.composable.Stream;
import reactor.function.Consumer;
import reactor.function.Function;
import reactor.function.batch.BatchConsumer;

import java.net.InetSocketAddress;

/**
 * {@code NetChannel} implementations handle interacting with the client.
 *
 * @author Jon Brisbin
 */
public interface NetChannel {

	/**
	 * Get the address of the remote peer.
	 *
	 * @return the peer's address
	 */
	InetSocketAddress remoteAddress();

	/**
	 * {@link reactor.core.composable.Stream} of incoming decoded data.
	 *
	 * @return input {@link reactor.core.composable.Stream}
	 */
	Stream in();

	/**
	 * {@link reactor.function.batch.BatchConsumer} for efficiently data to the peer.
	 *
	 * @return output {@link reactor.function.batch.BatchConsumer}
	 */
	BatchConsumer out();

	/**
	 * When an error of the given type occurs, handle it with the given {@link reactor.function.Consumer}.
	 *
	 * @param type
	 * 		type of error
	 * @param onError
	 * 		error handler
	 * @param 
	 * 		type of the exception
	 *
	 * @return {@literal this}
	 */
	 NetChannel when(Class type, Consumer onError);

	/**
	 * Efficiently consume incoming decoded data.
	 *
	 * @param consumer
	 * 		the incoming data {@link reactor.function.Consumer}
	 *
	 * @return {@literal this}
	 */
	NetChannel consume(Consumer consumer);

	/**
	 * Handle incoming data and return the response.
	 *
	 * @param fn
	 * 		request handler
	 *
	 * @return {@literal this}
	 */
	NetChannel receive(Function fn);

	/**
	 * Send data to the peer that passes through the given {@link reactor.core.composable.Stream}.
	 *
	 * @param data
	 * 		the {@link reactor.core.composable.Stream} of data to monitor
	 *
	 * @return {@literal this}
	 */
	NetChannel send(Stream data);

	/**
	 * Send data to the peer.
	 *
	 * @param data
	 * 		the data to send
	 *
	 * @return a {@link reactor.core.composable.Promise} indicating when the send operation has completed
	 */
	Promise send(OUT data);

	/**
	 * Send data to the peer.
	 *
	 * @param data
	 * 		the data to send
	 *
	 * @return {@literal this}
	 */
	NetChannel sendAndForget(OUT data);

	/**
	 * Send data to the peer and expect a response.
	 *
	 * @param data
	 * 		the data to send
	 *
	 * @return a {@link reactor.core.composable.Promise} representing the response from the peer
	 */
	Promise sendAndReceive(OUT data);

	/**
	 * Close this {@literal NetChannel}.
	 */
	Promise close();

	/**
	 * Close this {@link reactor.net.NetChannel} and invoke the given {@link reactor.function.Consumer} when closed.
	 *
	 * @param onClose
	 */
	void close(Consumer onClose);

	/**
	 * Assign event handlers to certain channel lifecycle events.
	 *
	 * @return
	 */
	ConsumerSpec on();

	/**
	 * Spec class for assigning multiple event handlers on a channel.
	 */
	public static interface ConsumerSpec {
		/**
		 * Assign a {@link Runnable} to be invoked when the channel is closed.
		 *
		 * @param onClose
		 * 		the close event handler
		 *
		 * @return {@literal this}
		 */
		ConsumerSpec close(Runnable onClose);

		/**
		 * Assign a {@link Runnable} to be invoked when reads have become idle for the given timeout.
		 *
		 * @param idleTimeout
		 * 		the idle timeout
		 * @param onReadIdle
		 * 		the idle timeout handler
		 *
		 * @return {@literal this}
		 */
		ConsumerSpec readIdle(long idleTimeout, Runnable onReadIdle);

		/**
		 * Assign a {@link Runnable} to be invoked when writes have become idle for the given timeout.
		 *
		 * @param idleTimeout
		 * 		the idle timeout
		 * @param onWriteIdle
		 * 		the idle timeout handler
		 *
		 * @return {@literal this}
		 */
		ConsumerSpec writeIdle(long idleTimeout, Runnable onWriteIdle);
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy