mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-05-31 12:10:53 +00:00
docs: explanation for reactor (#2960)
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user