Java Chat Client/Server Architecture

I have built a Java chat client and server using Xsocket (see: http://xsocket.sourceforge.net/ ) which is going to act as a game lobby for a virtual card-game.

The clients are single threaded applications that use my own ad-hoc custom-event driven GUI framework.
(see the framework basics: http://forums.dzone.com/java/372-java-gui-framework-cranegorm.html )

The server uses NIO channels. New connections spawn threads which called “connection handlers” which interpret/validate messages and fire off valid network messages as events (I send serialized objects between client/server). The server uses the same GUI framework for a console.

First, I’m interested in if people use this type of network flow or why they do not (other than speed of updates which is of large concern to other types of frame dependent games)?

How is the communication implemented? Are you send data via HTTP, i.e. HTTP tunneling?

I am opening a Nonblocking socket to the server on a particular port. There is no specified protocol, just serialized objects as streams.

Spawning a new thread per connection isn’t a scalable approach. One of the reasons NIO provided accessors to fd_sets and selectors was to allow scalability. Thread creation is first time consuming and second often eats system resources very quickly. This provides some good information though isn’t totally apt: http://www.ibm.com/developerworks/library/j-jtp0730.html

Using default serialization is a bit of an network overhead but for the purposes of a non-realtime game probably doesn’t matter too much.

Kev

Ok. So I’ve removed thread spawning and use the selector approach.

What about the event-driven model for network messages? As I understand it, the event dispatching is a separate thread. So I think it’s fine for me.

When I attempt to send multiple messages from the client (which go out to the server before coming back to the client as all messages should), I get bad behavior from the DefaultListModel or JList or JScrollPane. I get inconsistent scrolling using MyJList.ensureIndexIsVisible(lastIndex); sometimes the last message is visible sometimes it’s not and sometimes the messages are rearranged when displayed (I started tagging them all with an integer to observe the behavior). Is there a better Model to use or a reason this might be happening?

Are you updating the ListModel/JList/JScrollPane from the AWT event thread or from another thread? Swing is not thread safe, so if you modify Swing components from a different thread than the AWT event thread, you can get all kinds of strange behaviours…

[quote]Are you updating the ListModel/JList/JScrollPane from the AWT event thread or from another thread?
[/quote]
I’m not sure. It’s funny that I can grasp NIO but not this stupid GUI problem. I have a JFrame with a Text Input that has a keylistener added to it

inputTextField.addKeyListener(
                new KeyListener() 
                {
                   public void keyPressed( KeyEvent e )
                   {
                	   if (e.getKeyCode() == KeyEvent.VK_ENTER)
                	   {
                		   sendMessage(inputTextField.getText().trim());
                		   inputTextField.setText("");
                	   }                	   
                   }
                   public void keyTyped( KeyEvent e ){}
                   public void keyReleased( KeyEvent e ){}
                }
             );

Send message is defined:

	public void sendMessage(String message)
	{
		fireCranegormEvent(new ClientChatEvent(Constants.CLIENT_DISPLAY_MATCHMAKER, MVC_Provider.getUserModel().getId(), message));
		inputTextField.setText("");
	}

The constructor of the JFrame has

		addCranegormEventListener(MVC_Provider.getGC_EventController());		

The event controller is what hears and handles ALL events. When a network message is received from the server, the event controller modifies the DefaultListModel, which is assigned to the JList, which is contained in a JScrollPane. Is this somehow broken? It works really well for everything so far, excepting this JScrollPane problem.