docs: explanation for reactor (#2960)

This commit is contained in:
Ilkka Seppälä
2024-05-18 12:05:33 +03:00
committed by GitHub
parent 1da0f41381
commit a766f188a8
5 changed files with 107 additions and 71 deletions
@@ -52,8 +52,7 @@ public class LoggingHandler implements ChannelHandler {
if (readObject instanceof ByteBuffer) {
doLogging((ByteBuffer) readObject);
sendReply(channel, key);
} else if (readObject instanceof DatagramPacket) {
var datagram = (DatagramPacket) readObject;
} else if (readObject instanceof DatagramPacket datagram) {
doLogging(datagram.getData());
sendReply(channel, datagram, key);
} else {
@@ -32,6 +32,7 @@ import java.util.Map;
import java.util.Queue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import lombok.Getter;
/**
* This represents the <i>Handle</i> of Reactor pattern. These are resources managed by OS which can
@@ -46,6 +47,7 @@ import java.util.concurrent.ConcurrentLinkedQueue;
public abstract class AbstractNioChannel {
private final SelectableChannel channel;
@Getter
private final ChannelHandler handler;
private final Map<SelectableChannel, Queue<Object>> channelToPendingWrites;
private NioReactor reactor;
@@ -104,15 +106,6 @@ public abstract class AbstractNioChannel {
*/
public abstract Object read(SelectionKey key) throws IOException;
/**
* Get handler.
*
* @return the handler associated with this channel.
*/
public ChannelHandler getHandler() {
return handler;
}
/*
* Called from the context of reactor thread when the key becomes writable. The channel writes the
* whole pending block of data at once.
@@ -31,6 +31,8 @@ import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.DatagramChannel;
import java.nio.channels.SelectionKey;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
/**
@@ -131,9 +133,12 @@ public class NioDatagramChannel extends AbstractNioChannel {
/**
* Container of data used for {@link NioDatagramChannel} to communicate with remote peer.
*/
@Getter
public static class DatagramPacket {
private SocketAddress sender;
private final ByteBuffer data;
@Setter
private SocketAddress sender;
@Setter
private SocketAddress receiver;
/**
@@ -144,50 +149,5 @@ public class NioDatagramChannel extends AbstractNioChannel {
public DatagramPacket(ByteBuffer data) {
this.data = data;
}
/**
* Get sender address.
*
* @return the sender address.
*/
public SocketAddress getSender() {
return sender;
}
/**
* Sets the sender address of this packet.
*
* @param sender the sender address.
*/
public void setSender(SocketAddress sender) {
this.sender = sender;
}
/**
* Get receiver address.
*
* @return the receiver address.
*/
public SocketAddress getReceiver() {
return receiver;
}
/**
* Sets the intended receiver address. This must be set when writing to the channel.
*
* @param receiver the receiver address.
*/
public void setReceiver(SocketAddress receiver) {
this.receiver = receiver;
}
/**
* Get data.
*
* @return the underlying message that will be written on channel.
*/
public ByteBuffer getData() {
return data;
}
}
}