1

我正在 Netty 之上开发一个拍卖系统。我使用 Netty 是因为谷歌搜索告诉我 NIO 可以处理比普通套接字编程更大的客户端。

基本上我是 Netty 的初学者。我已经介绍了教程和用户指南。就是这样。所以请理解我的问题是否不符合“问题”的条件。以下是我的客户端的伪代码。

public class AuctionClient
{
    private boolean loggedIn;

    public AuctionClient() //constructor

    ///
    .... // various functions
    ///

    public void run()
    {
        while (true)
        {
            int option = getUserOption(); //get user menu selection

            switch(option)
            {
                case 1:
                    login(); //user enters username and password. The info is converted into JSON string and sent to AuctionServer. 
                             //AuctionServer returns true if the info is correct, false otherwise
                    break;

                case 2:
                    startAuction(); //start a new auction
                    break;

                case 3:
                    makeBid(); //make a bid to an existing auction
                    break;

                case 4:
                    logout(); //log out from the AuctionServer
                    break;

                default:
                    break;
            }
        }
    }

    public static void main() // Creates AuctionClient class and runs it.
}

这是我正在尝试做的事情的要点。问题是,只有当变量 loggedIn 为真时,我才想启用 startAuction()、makeBid() 和 logout()。所以我必须知道登录是否成功才能更改loggedIn的值。

但是由于处理 login() 的结果的是 AuctionClientHandler(虽然现在在这里显示),所以 AuctionClient 无法知道登录是否成功。

有没有优雅的方法来解决这个问题。我想避免使用 BlockingQueue 在 AuctionClient 和 AuctionClientHandler 之间传递信息。或者拍卖系统有更好的设计吗?

任何帮助,将不胜感激。

爱德华

4

2 回答 2

1

我认为您的问题可以归结为一个简单的事实。您需要保留先前操作的“状态”。为此,每次在您的PipelineFactory实现中都需要创建一个新的处理程序。例如pipeline.addLast('MyAuctionHandler',new AuctionHandlerClass());
,第一次登录成功时,您LoginHandler应该pipeline发送一条消息给AuctionClientHandler一个特殊的对象,然后您可以使用该对象将login标志设置为true
例如代码,您可能想看看我发布的Java Game Server 。它还处理类似的登录、会话管理等。这是处理登录的处理程序,唯一的区别是这个处理程序不是有状态的,因为我将状态移动到会话LookupService类。

于 2012-09-16T20:17:58.083 回答
0

另一种方法是将状态作为附件存储在 Channel 上。

Channel.setAttachment(..);
于 2012-09-17T04:59:48.907 回答