
reactor.net.spec.NetServerSpec Maven / Gradle / Ivy
package reactor.net.spec;
import reactor.core.spec.support.EventRoutingComponentSpec;
import reactor.function.Consumer;
import reactor.io.Buffer;
import reactor.io.encoding.Codec;
import reactor.net.NetChannel;
import reactor.net.NetServer;
import reactor.net.config.ServerSocketOptions;
import reactor.util.Assert;
import javax.annotation.Nonnull;
import java.net.InetSocketAddress;
import java.util.Collection;
import java.util.Collections;
/**
* @author Jon Brisbin
*/
public abstract class NetServerSpec, N extends NetServer>
extends EventRoutingComponentSpec {
protected ServerSocketOptions options = new ServerSocketOptions();
protected Collection>> channelConsumers = Collections.emptyList();
protected InetSocketAddress listenAddress;
protected Codec codec;
/**
* Set the common {@link ServerSocketOptions} for channels made in this server.
*
* @param options
* The options to set when new channels are made.
*
* @return {@literal this}
*/
@SuppressWarnings("unchecked")
public S options(@Nonnull ServerSocketOptions options) {
Assert.notNull(options, "ServerSocketOptions cannot be null.");
this.options = options;
return (S)this;
}
/**
* The port on which this server should listen, assuming it should bind to all available addresses.
*
* @param port
* The port to listen on.
*
* @return {@literal this}
*/
@SuppressWarnings("unchecked")
public S listen(int port) {
return listen(new InetSocketAddress(port));
}
/**
* The host and port on which this server should listen.
*
* @param host
* The host to bind to.
* @param port
* The port to listen on.
*
* @return {@literal this}
*/
@SuppressWarnings("unchecked")
public S listen(String host, int port) {
if(null == host) {
host = "localhost";
}
return listen(new InetSocketAddress(host, port));
}
/**
* The {@link java.net.InetSocketAddress} on which this server should listen.
*
* @param listenAddress
* the listen address
*
* @return {@literal this}
*/
@SuppressWarnings("unchecked")
public S listen(InetSocketAddress listenAddress) {
this.listenAddress = listenAddress;
return (S)this;
}
/**
* The {@link Codec} to use to encode and decode data.
*
* @param codec
* The codec to use.
*
* @return {@literal this}
*/
@SuppressWarnings("unchecked")
public S codec(@Nonnull Codec codec) {
Assert.notNull(codec, "Codec cannot be null.");
this.codec = codec;
return (S)this;
}
/**
* Callback to invoke when a new input message is created.
*
* @param inputConsumer
* The callback to invoke for new messages.
*
* @return {@literal this}
*/
public S consumeInput(@Nonnull final Consumer inputConsumer) {
Collection>> channelConsumers = Collections.>>singletonList(
new Consumer>() {
@Override
public void accept(NetChannel channel) {
channel.consume(inputConsumer);
}
});
return consume(channelConsumers);
}
/**
* Callback to invoke when a new channel is created.
*
* @param channelConsumer
* The callback to invoke for new channels.
*
* @return {@literal this}
*/
public S consume(@Nonnull Consumer> channelConsumer) {
return consume(Collections.singletonList(channelConsumer));
}
/**
* Callbacks to invoke when a new channel is created.
*
* @param channelConsumers
* The callbacks to invoke for new channels.
*
* @return {@literal this}
*/
@SuppressWarnings("unchecked")
public S consume(@Nonnull Collection>> channelConsumers) {
Assert.notNull(channelConsumers, "Connection consumers cannot be null.");
this.channelConsumers = channelConsumers;
return (S)this;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy