Further Java Practical Class
Table of Contents
In order to gain a star in the mark sheet you must complete this exercise. Completing the exercise does not gain you any credit in the examination. In this exercise you will extend your implementation of the Java chat server from Workbook 4 to send mobile code to the client. The code that you send to the client should display a Swing chat client interface on the client machine, thereby upgrading the client from a text-based application to a graphical one.
The first thing to write is a simple Swing GUI which is able to interact with your Java chat server. The precise layout of the GUI is up to you, but at the very least it should contain a box in which the user can type in new messages, an area of the screen devoted to displaying messages from other users, and a means of changing the nickname of the user. Test your Swing GUI with your existing server and make sure it works reliably.
Copy your implementation of the Java chat server for Ticklet 4 as well as your implementation of the Swing GUI client into
uk.ac.cam.crsid.fjava.tick4star. As you may remember from Workbook 2, in order to upgrade the client in-place, you will first need to send the definitions of all the classes inside
NewMessageType messages. Finally, you should send the definition of another new class which extends
Message and contains a method with the appropriate run-time annotation to create an instance of your Swing GUI; since you've already sent the definitions of all the required classes, the Java chat client should then display the upgraded interface correctly!
In order to send the definitions of the classes you need to be able to write the definitions into an array of bytes. You might find the following snippet of code helpful:
Class clazz = ... //get a reference to the class you want to serialise String name = clazz.getName(); InputStream is = clazz.getClassLoader().getResourceAsStream( name.replaceAll("\\.", "/") + ".class"); ... //Use "is" to copy the definition of "clazz" into an array of bytes
When testing your code, please make sure you run the client outside the Eclipse environment, since otherwise your client is likely to find the correct classes, not because they have been sent correctly over the socket, but because Eclipse will find them in the filesystem for you!
When sending new class definitions to the client, it turns out to be non-trivial to send anonymous inner classes; the work-around is simple (avoid using anonymous inner classes). Extra credit (a double star!) will be awarded to those students who produce a correct implementation of the server which is able to send anonymous inner classes to a client without modifying the source of
DynamicObjectInputStream. If you succeed in doing this, please email
firstname.lastname@example.org once you have passed the unit test for this ticklet to explain what you did. (No email, no double star.)
You may change the implementation of your server as you see fit to support this new functionality, but you must not change your implementation of the Java chat client. In other words, your implementation should continue to work with the code you wrote for Ticklet 2 and 2*.
Please put the source code and byte code of the package
uk.ac.cam.cl.fjava.messages into a jar file called
crsid-tick4star.jar. Please email the jar file to
email@example.com. You should receive a response via email within an hour. If you do not, please send an email to