Further Java Practical Class

Further Java Ticklet 5*

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 5 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 5 as well as your implementation of the Swing GUI client into uk.ac.cam.crsid.fjava.tick5star. 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 called run which creates 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 a 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, we have been unable to send any which contain anonymous inner classes. As the work-around is simple (avoid using anonymous inner classes) we haven't investigated any further. Therefore extra credit (a double star!) will be awarded to the first student to produce a correct implementation of the server which is able to send anonymous inner classes to a client. If you succeed in doing this, please email ticks1b-admin@cl.cam.ac.uk once you have passed the unit test for this ticklet to explain what you did.

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.crsid.tick5star and uk.ac.cam.cl.fjava.messages into a jar file called crsid-tick5star.jar. Please email the jar file to ticks1b-java@cl.cam.ac.uk. You should receive a response via email within an hour. If you do not, please send an email to ticks1b-admin@cl.cam.ac.uk.

Copyright Alastair R. Beresford and Andrew C. Rice 2008,2009