Name:

SE550 midterm exam: Autumn 2000-2001

Answer all questions.

Time allowed: 2 hours

Total number of points: 100

Lookup

Some questions will refer to LookupServer, LookupClient and LookupRequestParser classes. They are defined here.

public class LookupServer 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 LookupServer server = new LookupServer (socket);
            server.start ();
        }
    }

    protected final Hashtable table = new Hashtable ();
    protected final Socket socket;
    protected final PrintWriter out;
    protected final BufferedReader in;
    protected final Thread thread = new Thread (this);

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

    public void start () { 
        table.put ("fred", "flintstone");
        thread.start (); 
    }

    public void run () {
        try {
            String line = in.readLine (); 
            while (line != null && !line.equals ("QUIT")) {
                if (line.equals ("PUT")) {
                    put ();
                } else if (line.equals ("GET")) {
                    get ();
                } else {
                    error ();
                }
                line = in.readLine ();
            }
        } catch (final IOException ex) {
        } finally {
            try {
                out.close ();
                in.close ();
                socket.close ();
            } catch (final IOException ex) {
            }
        }
    }

Lookup cont.

    protected void put () throws IOException {
        table.put (in.readLine (), in.readLine ());
        out.println ("OK");
    }

    protected void get () throws IOException {
        String result = (String)(table.get (in.readLine ()));
        if (result == null) {
            out.println ("NONE");
        } else {
            out.println ("GOT " + result);
        }
    }

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

}

Lookup cont.

public class LookupGetClient {

    public static void main (final String[] args) throws IOException {
        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 ()));
        for (int i=1; i < args.length; i++) {
            out.println ("GET");            
            out.println (args[i]);            
            final String reply = in.readLine ();
            if (reply.startsWith ("GOT ")) {
                System.out.println ("Looked up " + args[i] + " got " + reply.substring (4));
            } else if (reply.equals ("NONE")) {
                System.out.println ("No entry for " + args[i]);
            } else {
                System.out.println ("This shouldn't happen...");
            }
        }
        out.println ("QUIT");
        out.close ();
        in.close ();
        socket.close ();
    }

}

Lookup cont.

public grammar class LookupRequestParser {

  public void handleQuitRequest () {
    // code for handling a quit request
  }

  public void handleGetRequest (String keyword) {
    // code for handling a get request
  }

  public void handlePutRequest (String keyword, String value) {
    // code for handling a put request
  }

  token {
    <QUIT: "QUIT">
  | <GET: "GET">
  | <PUT: "PUT">
  | <CRLF: "\n" | "\r" | "\r\n">
  | <SPACE: " ">
  | <NAME: (<CHAR>)+>
  | <CHAR: ['a'-'z'] | ['A'-'Z'] | ['0'-'9']>
  }

  public production void request () {
    quitRequest () | getRequest () | putRequest ()
  }

  production void quitRequest () {
    ( <QUIT> <CRLF> ) | <eof>
    { handleQuitRequest (); }
  }

  production void getRequest () {
    { String keyword; }
    <GET> <CRLF> keyword=name () <CRLF>
    { handleGetRequest (keyword); }
  }

  production void putRequest () {
    { String keyword; String value; }
    <PUT> <CRLF> keyword=name () <CRLF> value = name () <CRLF>
    { handlePutRequest (keyword, value); }
  }

  production String name () {
    { Token t; }
    t = <NAME>
    { return t.image; }
  }

}

Question 1 (10 points)

a) What is TCP?

b) What is IP?

c) What is the difference between TCP and IP?

Question 2 (15 points)

Assume that on machine server the following command is run:

  java LookupServer

and that on machine client the following command is run:

  java LookupGetClient server fred barney

a) What messages are sent between the client and the server?

b) What messages are printed out by the client?

Question 3 (20 points)

The client code only allows reads to the lookup database, not writes. Write a client which adds entries to the lookup database. For example, running:

  java LookupPutClient server foo bar
  java LookupGetClient server foo

should produce the result:

  Looked up foo got bar
public class LookupPutClient {

    public static void main (final String[] args) throws IOException {
        final Socket socket = new Socket (args[0], 2000);
        final String keyword = args[1];
        final String value = args[2];
        final PrintWriter out = new PrintWriter (socket.getOutputStream (), true);
        final BufferedReader in = new BufferedReader (new InputStreamReader (socket.getInputStream ()));
        // your code goes here

Question 3 cont.

Question 4 (20 points)

Give a grammar for the protocol which is used by the LookupGetClient, LookupPutClient and LookupServer programs.

(For purposes of this answer, you can assume that keywords and values can only contain alphanumeric characters.)

Question 4 cont.

Question 5 (20 points)

The mparse parser LookupRequestParser gives a parser for name requests. Write a parser for lookup responses.

public grammar class LookupResponseParser {

  public void handleOKResponse () {
    // code for handling an OK response
  }

  public void handleGotResponse (String keyword) {
    // code for handling a got response
  }

  public void handleNoneResponse () {
    // code for handling a none response
  }

  public void handleErrorResponse () {
    // code for handling an error response
  }

  // your code goes here

Question 5 cont.

Question 6 (10 points)

In a bank account application, a Account class might be defined:

  class Account extends Serializable {
    final int accountNumber;
    final int pin;
    Account (final int accountNumber, final int pin) {
      this.accountNumber = accountNumber; this.pin = pin;
    }
  }

The accountNumber can be sent in plaintext, but the pin should not be sent in plaintext.

a) Show how the transient feature of Java can be used to make sure that pin is not sent across the network in plaintext.

b) Show how the Externalizable feature of Java can be used to make sure that pin is not sent across the network in plaintext.

(For this question, you can assume functions encrypt and decrypt which encrypt and decrypt data.)

Question 6 cont.

Question 7 (5 points)

a) What is your favorite programming language apart from Java?

b) Name one feature of Java which is better than your favorite language, in your opinion.

c) Name one feature of Java which is worse than your favorite language, in your opinion.

Worksheet

You can use this sheet as scrap paper.

Worksheet

You can use this sheet as scrap paper.

Worksheet

You can use this sheet as scrap paper.

Worksheet

You can use this sheet as scrap paper.