The Client Class
The
Client object establishes communication with the server. The sample application, which has a chat and a whiteboard, creates a single
Client object to share between the chat and the whiteboard. If you had more sub-clients, then they would share the
Client object as well.
The construction for Client takes a hostname and port:
Client client = new Client( hostname, port );
Once you have an instance of Client, you can pass it to each of the sub-clients.
The Mailbox Class
The central class in this program is Mailbox, which provides a convenient way to exchange objects with the server. Each sub-client should have its own Mailbox. But Mailbox is a parameterized classyou need to specify what kind of object each Mailbox is supposed to send and receive. For example, the chat sub-client sends and receives strings, so it creates its Mailbox like this:
Mailbox<String> mailbox = new Mailbox<String>( client, "chat" );
The first parameter to the constructor for Mailbox is the Client object, which lets the Mailbox talk to the server. The second parameter defines the channel that this Mailbox will use. Each sub-client uses a different channel. That way, you make sure that chat messages go only to other chat sub-clients and not to, say, audio sub-clients.
The Chat Mailbox
Using Mailbox is quite simple. Here's how you send a text string:
mailbox.send( "hello chat server" );
Because this Mailbox is parameterized on the String type, its send() method takes a String as an argument. Likewise for the receive() method:
String message = mailbox.receive();
Take a look at the Chat.java file included in the source code download. You can see how simply the chat sub-client is constructed using the Mailbox class.
The Whiteboard Mailbox
The whiteboard is a bit more complicated. It lets you draw lines into a window and these lines show up on everyone else's screens. So you need to send lines across the network.
To handle this, you create a class called Line, which contains the (x ,y) positions of a line's endpoints. To send and receive such objects, you create an appropriate Mailbox:
Mailbox<Line> mailbox = new Mailbox<Line>( client, "whiteboard" );
Here, you've used the Line class instead of the String class. It doesn't matter that Line is a class that you created and that String is built into Javaany class will do.