Reputation: 69
I am developing an auction system on top of Netty. I use Netty because googling has taught me that NIO can deal with much larger clients than ordinary socket programming can.
Basically I am a starter in Netty. I have covered the tutorials and user guide. And that is it. So plz understand if my question doesn't qualify as a "problem". The following is the pseudo-code of my client.
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.
}
This is the gist of what I am trying to do. The problem is, I want to enable startAuction(), makeBid(), and logout() only if the variable loggedIn is true. So I have to know if the login was successful in order to change the value of loggedIn.
But since it is the AuctionClientHandler(though now shown here) that deals with the result of login(), there is no way for AuctionClient to know if the login was successful.
Is there a graceful way to solve this problem. I want to avoid using BlockingQueue to pass information between AuctionClient and AuctionClientHandler. Or is there a better design for the auction system?
Any help would be appreciated.
Edward
Upvotes: 1
Views: 1329
Reputation: 23567
Another way would be to store the state as attachment on the Channel.
Channel.setAttachment(..);
Upvotes: 0
Reputation: 9041
I think your problem can be boiled down to a simple fact. You need to hold "state" from a previous operation. For this need to create a new handler each time in your PipelineFactory
implementation. For e.g. pipeline.addLast('MyAuctionHandler',new AuctionHandlerClass());
The first time login happens successfully your LoginHandler
which is in the pipeline
should send a message to the AuctionClientHandler
say a special object, which you can then use to set the login
flag to true
.
For example code, you might want to take a look at a Java Game Server I have published. It also deals with similar login, session management etc. This is the handler which deals with login, only difference is that this handler is not stateful, since I moved the state to a session LookupService
class.
Upvotes: 1