For your fourth programming assignment, you will design and
implement
an application level protocol. Here are two suggestions for
problems to work on:
1. Implement a multiplayer networked game. You may choose any game that you want, but it should have these characteristics:
I will provide a simple game to illustrate some of the issues involved.
The program is of necessity event-driven. At any given moment, the program may need to respond to GUI events (mouse clicks, etc.) or network events (receipt of a message). This lends itself to a thread-based model of computation. Java already uses a thread to respond to GUI events; it is called the event thread. It is essential that the event thread not be blocked. You need to write one or more threads that will respond to network events. In my example program, I used three threads, one for each blocking network call made by the program: a thread to listen for connections, a thread to make a remote connection, and a thread to read data from a connected socket.
My program is a networked implementation of the familiar "Rock Paper Scissors" game. I've placed the code in a jar file RPS.jar that you may download. It consists of the following Java files:
Contains the main program, which instantiates an RPS object, which in turn instantiates the RPS frame and the RPS server.
This the GUI frame for the application. It contains a menu bar used for setting up a connection to a remote player, an output area to show the progress of the game, and a row of buttons for the actual play of the game. The connection menu gives the player the option of listening for a remote connection or attempting to make a connection to a player on a remote machine.
This file contains a thread which listens for a connection on a server socket.
This is the dialog for setting up a connection. The user enters the name of the remote machine and the port number, and the program attempts to make a connection to that machine. If the connection is accepted, a game protocol object is created at both ends of the connection, and the game starts.
Most of the code here is to set up the GUI components contained in the dialog box. When RPS makes the name dialog visible, the user is supposed to enter his or her name, which is then stored in the name field of the dialog. RPS can then obtain the name using the getName method of the dialog.
This file is the application-level protocol module for the game. It contains the rules for what the program does in response to mouse events and message events.
The message events are handled by a thread called "socketReader". It reads a message from the socket and calls "respondTo" to make the appropriate response. All messages are lines of ASCII text. The first word of a message indicates the message type. There are three message types:
Mouse events are handled by actionListeners defined for the Rock, Paper, Scissors, and Quit buttons.
The protocol is symmetric. That is, the same protocol is used by both players, irrespective of which one initiated the connection.