Recall the pattern for wait and notify where some predicate p must be true before a thread can proceed:
- The thread that needs p to be true executes while (!p) o.wait(); and it must have a lock on o.
- Any thread that can cause p to become true executes o.notifyAll(); and it must have a lock on o.
- It is handy to have o be the object that must be manipulated in a thread-safe manner, so that only one thread at a time modifies o.
Have the professor or TA check your work here.
In a real game, the ConsoleHandler could be used to see how the game is going, change parameters about the game's execution, shutdown the game, etc. Because the ConsoleHandler is under control of the game operator, it can usually do more things with less checking than a PlayerHandler can.
Add code to ConsoleHndler so that if the game operator enters die die die, the shutDown method of the
game object is called. You can't test this yet, but try to modify ConsoleHandler anyway to incorporate this feature.
If this kind of message were accepted by a PlayerHandler, then any player could shut down the game.
In the Server's run() method, write code that:
Why is it important to .start() the new PlayerHandler instead of .run()ning it?
Complete the code as directed by the comments in the code.
- If you have a DataInputStream object, you can call readUTF() on that object to read a Java String.
- Look at how the code handles any exceptions. You can always throw an Error if you want the code to stop.
- Be sure you understand just how the try...catch...finally code works, especially if you had trouble on the midterm.
Do not do the work directly! Instead, use addRunnable to queue up the work for the server to perform.
The GameController has a method tellClient that is incomplete. Finish this so that it calls tellClient in PlayerHandler. The code currently in PlayerHandler currently just prints out a message on the game console.
If you have time,
You must agree on the protocol for sending and receiving such messages. Question: Do you need to worry about race conditions as the server sends messages back to the client? Why or why not?The client now should get messages everytime a player joins. Have it display them client-side on the console. You can force a new player to join server-side by typing spawn into the ConsoleHandler on the server side.
If you have time, you should next try running this studio in a distributed fashion using your studio partner or another studio team:
Demo your work (run GameController as a Java application) to the TA or professor, and fill out the form as usual.
When you done with this studio, you must be cleared by the TA to receive credit.
- Commit all your work to your repository
- Fill in the form below with the relevant information
- Have a TA check your work
- The TA should check your work and then fill in his or her name
- Click OK while the TA watches