gpt4 book ai didi

java - Netty 客户端的 MVC 前端

转载 作者:太空宇宙 更新时间:2023-11-04 06:38:32 25 4
gpt4 key购买 nike

我对当前的报价 ( QOTM ) 进行了一些修改,并希望构建一个 GUI 前端。将对象从 DatagramClientHandler 传递到 GUI 非常简单。然而,GUI 引用处理程序似乎很棘手。

QuotesGUI 类扩展了 JFrame,以利用 Netbeans 拖放选项板轻松添加 Swing 组件。实在是太啰嗦了。

显然,解决方案是:

Well It depends as there are more then one solution. One could be to inject a listener to the ChannelHandler which then will get notified once the message was received. An other solution could be to send events to a topic once a message was received and register the interested swing parts on the topic, so they get notified.

https://stackoverflow.com/a/8780410/262852

DatagramClientHandler:

package net.bounceme.dur.netty;

import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.socket.DatagramPacket;
import io.netty.util.CharsetUtil;
import java.net.InetSocketAddress;
import java.util.logging.Logger;
import net.bounceme.dur.client.gui.QuotesGUI;

public class DatagramClientHandler extends SimpleChannelInboundHandler<DatagramPacket> {

private static final Logger log = Logger.getLogger(DatagramClientHandler.class.getName());
private final QuotesGUI gui = new QuotesGUI();
private volatile Channel channel = null;

DatagramClientHandler() {
log.info("starting..");
gui.setVisible(true);
}

private DatagramPacket getNext() {
DatagramPacket packet = new DatagramPacket(
Unpooled.copiedBuffer("QOTM?", CharsetUtil.UTF_8),
new InetSocketAddress("localhost", 4454));
return packet;
}

@Override
public void channelRead0(ChannelHandlerContext ctx, DatagramPacket msg) throws Exception {
String response = msg.content().toString(CharsetUtil.UTF_8);
log.info(response);
gui.setQuote(response);
}

@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
log.severe(cause.toString());
ctx.close();
}
}

GUI 中的示例方法。

public void setQuote(String packet) {
text.setText(packet);
}

最佳答案

首先划分您的职责层次......

我可能会首先定义某种可以使用 DatagramClientHandler 实例注册的监听器接口(interface)。此接口(interface)将允许感兴趣的各方收到DatagramClientHandler内的更改或事件通知,并按照他们认为合适的方式处理这些事件...

public interface MessageListener {
public void quoteRecieved(SimpleChannelInboundHandler source, String quote);
public void errorOccured(SimpleChannelInboundHandler source, Throwable cause);
}

那么您需要为监听器提供支持...

public class DatagramClientHandler extends SimpleChannelInboundHandler<DatagramPacket> {

private static final Logger log = Logger.getLogger(DatagramClientHandler.class.getName());
//private final QuotesGUI gui = new QuotesGUI();
private volatile Channel channel = null;
private List<MessageListener> listeners;

DatagramClientHandler() {
listeners = new ArrayList<MessageListener>(25);
//...
}

public synchronized void addMessageListener(MessageListener listener) {
listeners.add(listener);
}

public synchronized void removeMessageListener(MessageListener listener) {
listeners.remove(listener);
}

protected synchronized void fireQuoteRecieved(String quote) {
for (MessageListener listener : listeners) {
listener.quoteRecieved(this, quote);
}
}

@Override
public void channelRead0(ChannelHandlerContext ctx, DatagramPacket msg) throws Exception {
String response = msg.content().toString(CharsetUtil.UTF_8);
log.info(response);
fireQuoteRecieved(response);
}

//...etc...

现在,当您想要接收通知时,您可以使用 DatagramClientHandler 实例注册 MessageListener 实例...

您将遇到的问题是确保您对 UI 所做的任何更新都在 EDT 中正确执行...

//...
public void quoteRecieved(SimpleChannelInboundHandler source, final String quote) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
text.setText(packet);
}
});
}

现在,如果您确实愿意,您可以进一步将代码与另一个接口(interface)解耦...

public interface QuoteFactory {
public synchronized void addMessageListener(MessageListener listener);
public synchronized void removeMessageListener(MessageListener listener);
}

然后,这将由 DatagramClientHandler 实现,并且您的 UI 需要将 QuoteFactory 的实例传递给它,以便它可以注册兴趣,以便在发生某些情况时收到通知...

关于java - Netty 客户端的 MVC 前端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24986736/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com