Name:

SE550 midterm exam: Winter 2002-2003

Answer all questions.

Time allowed: 2 hours

Total number of points: 100

Counter server

Some questions will refer to a Counter server. It is defined here.

public class CounterServer implements Runnable {

    public static void main (final String[] args) throws IOException {        
        final ServerSocket sSocket = new ServerSocket (2000);
        while (true) {
            final Socket socket = sSocket.accept ();
            final CounterServer server = new CounterServer (socket);
            server.start ();
        }
    }

    static int count;

    final Socket socket;
    final PrintWriter out;
    final BufferedReader in;
    final Thread thread;

    CounterServer (final Socket socket) throws IOException {
        this.socket = socket;
        this.out = new PrintWriter (socket.getOutputStream (), true);
        this.in = new BufferedReader (new InputStreamReader (socket.getInputStream ()));
        this.thread = new Thread (this);
    }

    public void start () { thread.start (); }

    public void run () {
        try {
            String line = in.readLine (); 
            while (line != null && !line.equals ("QUIT")) {
                if (line.equals ("GET")) { get (); } 
                else if (line.equals ("INC")) { inc (); } 
                else if (line.startsWith ("SET ")) { set (Integer.parseInt (line.substring (4))); } 
                else { error (); }
                line = in.readLine ();
            }
        } catch (final IOException ex) {
        } finally {
            try { out.close (); in.close (); socket.close (); } 
            catch (final IOException ex) { }
        }
    }

    void get () { out.println ("COUNTER=" + count); }

    void inc () { count++; out.println ("OK"); }

    void set (int value) { count=value; out.println ("OK"); }

    void error () { out.println ("ERROR"); }

}

Counter client

Some questions will refer to a Counter client. It is defined here.

public class CounterClient {

    public static void main (final String[] args) {
        try {
            final Socket socket = new Socket (args[0], 2000);
            final PrintWriter out = new PrintWriter (socket.getOutputStream (), true);
            final BufferedReader in = new BufferedReader (new InputStreamReader (socket.getInputStream ()));
            out.println ("INC");
            final String response1 = in.readLine ();
            if (!response1.equals ("OK")) {
                System.err.println ("Oh no!");
                System.exit (1);
            }
            out.println ("GET");
            final String response2 = in.readLine ();
            if (response2.startsWith ("COUNTER=")) {
                final int count = Integer.parseInt (response2.substring (8));
                System.out.println ("Current value = " + count);
            } else {
                System.err.println ("Oh no!");
                System.exit (1);
            } 
            out.println ("QUIT");
            out.close ();
            in.close ();
            socket.close ();
        } catch (final IOException ex) {
            System.err.println ("Network error: " + ex);
            System.exit (1);
        }
    }

}

Counter request parser

options { static=false; }
PARSER_BEGIN (CounterRequestParser)
public class CounterRequestParser {
    public void handleIncRequest () { code for an INC request }
    public void handleGetRequest () { code for a GET request }
    public void handleSetRequest (int value) { code for a SET request }
    public void handleQuitRequest () { code for a QUIT request }
}
PARSER_END (CounterRequestParser)

TOKEN : {
  <INC: "INC">
| <GET: "GET">
| <SET: "SET">
| <QUIT: "QUIT">
| <CRLF: "\n" | "\r" | "\n\r">
| <SPACE: " ">
| <NUMBER: ( "+" | "-" )? ( ["0"-"9"] )+ >
}

void request () : {} {
    incRequest () | getRequest () | setRequest () | quitRequest ()
}

void incRequest () : {} {
    <INC> <CRLF>
    { handleIncRequest (); }
}

void getRequest () : {} {
    <GET> <CRLF>
    { handleGetRequest (); }
}

void setRequest () : {
    int value;
}{
    <SET> <SPACE> value=number () <CRLF>
    { handleSetRequest (value); }
}

void quitRequest () : {} {
    ( <QUIT> <CRLF> ) | <EOF>
    { handleQuitRequest (); }
}

int number () : {
    Token t;
}{
    t = <NUMBER>
    { return Integer.parseInt (t.image); }
}

Question 1 (10 points)

Give a one- or two-sentence answer to each of the following:

a) What is a firewall?

b) Why does a programmer designing a distributed application need to be aware of firewalls?

c) What is network address translation?

d) Why does a programmer designing a distributed application need to be aware of firewalls?

Question 2 (10 points)

Assume that on machine server the following command is run:

  java CounterServer

and that on machine client the following command is run:

    java CounterClient server

a) What messages are sent between the client and the server (assuming no network problems)?

b) What is the value of count after the messages are sent?

Question 2 continued

Question 3 (10 points)

If a client sends the following requests in order to the CounterServer, what responses will be returned?

  SET 37






  GET






  INC





  GET





  GET 37





  QUIT

Question 4 (20 points)

The client code only allows users to increment the counter, and doesn't allow the counter to be set.

Write a client which allows users to set the value of the counter. This should send a GET message, a SET message, a GET message (to make sure the value of the counter is correct) then a QUIT message. It should print the messages "Old=old value." and "New=new value." with the old and new values of the counter. It should print "Oh dear..." if an error occurred.

public class CounterSetClient {

    public static void main (final String[] args) {

        try {
            final Socket socket = new Socket (args[0], 2000);
            final int value = Integer.parseInt (args[1];
            final PrintWriter out = new PrintWriter (socket.getOutputStream (), true);
            final BufferedReader in = new BufferedReader (new InputStreamReader (socket.getInputStream ()));
            do (value, in, out);
            out.close ();
            in.close ();
            socket.close ();
        } catch (final IOException ex) {
            System.err.println ("Network error: " + ex);
            System.exit (1);
        }
    }

    static void do (int value, BufferedReader in, PrintWriter out) throws IOException {

        Your code starts here

Question 4 continued

Question 5 (20 points)

Give two grammars for the protocol which is used by the Counter clients and server.

a) Your first grammar should define a nonterminal <request>, which defines the language of requests sent by the clients.

Question 5 continued

b) Your second grammar should define a nonterminal <response>, which defines the language of responses sent by the server.

Question 6 (20 points)

The JavaCC parser CounterRequestParser gives a parser for requests. Write a parser for responses.

options { static=false; }
PARSER_BEGIN (CounterResponseParser)
public class CounterResponseParser {
    public void handleCounterResponse () { code for an COUNTER response }
    public void handleOKResponse () { code for a OK response }
    public void handleErrorResponse (int value) { code for a ERROR response }
}
PARSER_END (CounterResponseParser)

TOKEN : {
  some definitions go here
















| <CRLF: "\n" | "\r" | "\n\r">
| <SPACE: " ">
| <NUMBER: ( "+" | "-" )? ( ["0"-"9"] )+ >
}

int number () : {
    Token t;
}{
    t = <NUMBER>
    { return Integer.parseInt (t.image); }
}

// Some productions go here

Question 6 continued

Question 7 (10 points)

Consider the following class definition:

class Point {
  int x;
  int y;
  Point (int x, int y) {
    this.x = x; this.y = y;
  }
}

a) Write the Java code which could generate the following XML serialization:

  <Object class="Point" id="handle1">
    <int value="8">
    <int value="12">
  </Object>












b) Write the XML which is sent when the following code is executed:

  Point foo = new Point (5, 7);
  Point bar = new Point (5, 7);
  xmlSerializer.writeObject (foo);
  xmlSerializer.writeObject (bar);
  foo = new Point (5, 12);
  bar.y = 12;
  xmlSerializer.writeObject (foo);
  xmlSerializer.writeObject (bar);

Question 7 continued

c) Give a one- or two-sentence explaination for why it is good practice to only serialize immutable objects.

Worksheet

You can use this sheet as scrap paper.

Worksheet

You can use this sheet as scrap paper.