Name:

SE550 midterm exam: Spring 2000-2001

Answer all questions.

Time allowed: 2 hours

Total number of points: 100

Telephone directory service

Some questions will refer to a Telephone Directory service. It is defined here.

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

    protected final static Hashtable names = new Hashtable ();
    protected final static Hashtable telephones = new Hashtable ();
    protected final Socket socket;
    protected final PrintWriter out;
    protected final BufferedReader in;
    protected final Thread thread;

    protected TelephoneDirectoryServer (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.startsWith ("NEW ")) {
                    put (line.substring (4));
                } else if (line.startsWith ("GET NAME ")) {
                    getName (line.substring (9));
                } else if (line.startsWith ("GET TELEPHONE ")) {
                    getTelephone (line.substring (14));
                } else {
                    error ();
                }
                line = in.readLine ();
            }
        } catch (final IOException ex) {
        } finally {
            try {
                out.close ();
                in.close ();
                socket.close ();
            } catch (final IOException ex) {
            }
        }
    }

Telephone directory service continued

    protected void put (final String userid) throws IOException {
        names.put (userid, in.readLine ());
        telephones.put (userid, in.readLine ());
        out.println ("OK");
    }

    protected void getName (final String userid) throws IOException {
        String result = (String)(names.get (userid));
        if (result == null) {
            out.println ("UNKNOWN USER " + userid);
        } else {
            out.println ("NAME OF " + userid);
            out.println ("IS " + result);
        }
    }

    protected void getTelephone (final String userid) throws IOException {
        String result = (String)(telephones.get (userid));
        if (result == null) {
            out.println ("UNKNOWN USER " + userid);
        } else {
            out.println ("TELEPHONE OF " + userid);
            out.println ("IS " + result);
        }
    }

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

}

Telephone directory service continued

public class TelephoneDirectoryAddUserClient {

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

        try {
            final Socket socket = new Socket (args[0], 2000);
            final String userid = args[1];
            final String name = args[2];
            final String telephone = args[3];
            final PrintWriter out = new PrintWriter (socket.getOutputStream (), true);
            final BufferedReader in = new BufferedReader (new InputStreamReader (socket.getInputStream ()));
            out.println ("NEW " + userid);
            out.println (name);
            out.println (telephone);
            if (!(in.readLine ().equals ("OK"))) {
                System.err.println ("Adding user " + userid + " failed");
                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);
        }
    }

}

Telephone directory service continued

PARSER_BEGIN (TelephoneDirectoryRequestParser)
public class TelephoneDirectoryRequestParser {
    public void handleQuitRequest () { code for a QUIT request }
    public void handleNewRequest (String userid, String name, String telephone) { code for a NEW request }
    public void handleGetNameRequest (String userid) { code for a GET NAME request }
    public void handleGetTelephoneRequest (String userid) { code for a GET TELEPHONE request }
}
PARSER_END (TelephoneDirectoryRequestParser)

TOKEN : {
  <QUIT: "QUIT">
| <NEW: "NEW">
| <GET: "GET">
| <TELEPHONE: "TELEPHONE">
| <NAME: "NAME">
| <CRLF: "\n" | "\r" | "\n\r">
| <SPACE: " ">
| <STRING: ( ~["\n","\r"] )+>
}

void request () : {} {
    newRequest () | getRequest () | quitRequest ()
}

void newRequest () : {
    String userid; String name; String telephone;
}{
    <NEW> <SPACE> userid = string () <CRLF>
    name = string () <CRLF>
    telephone = string () <CRLF>
    { handleNewRequest (userid, name, telephone); }
}

void getRequest () : {
    String userid;
}{
    <GET> (
      <NAME> userid = string () <CRLF> 
      { handleGetNameRequest (userid); }
    | <TELEPHONE> userid = string () <CRLF> 
      { handleGetTelephoneRequest (userid); }
    )
}

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

String string () : {
    Token t;
}{
    t = <STRING>
    { return t.image; }
}

Question 1 (10 points)

a) What is the Transmission Control Protocol (TCP)?

b) What is the Internet Protocol (IP?

c) Name three differences between TCP and IP?

Question 2 (10 points)

Assume that on machine server the following command is run:

  java TelephoneDirectoryServer

and that on machine client the following command is run:

  java TelephoneDirectoryAddUserClient server fred "Fred Flintstone" "(555) 123 4567"
  java TelephoneDirectoryAddUserClient server wilma "Wilma Flintstone" "(555) 123 7654"

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

b) What are the contents of TelephoneDirectoryServer.names and TelephoneDirectoryServer.telephones after the messages are sent.

Question 3 (10 points)

If a client sends the following requests to the TelephoneDirectoryServer, what responses will be returned?

  NEW barney
  Barney Rubble
  (555) 111 2222






  NEW betty
  Betty Rubble
  (555) 333 4444






  GET NAME barney






  GET TELEPHONE betty





  GET ADDRESS betty





  SET NAME betty
  Betty Rubble





  GET TELEPHONE pebbles




  QUIT

Question 4 (20 points)

The client code only allows new users to be created, and doesn't allow information to be read from the server. Write a client which reads information from the server. For example, running:

  java TelephoneDirectoryGetClient server fred wilma foobar

could produce the result:

  fred:
    name = Fred Flintstone
    telephone = (555) 123 4567
  wilma:
    name = Wilma Flintstone
    telephone = (555) 123 7654
  foobar:
    No such user!

The command line arguments to the program are the name of the server, and the userids to fetch information about.

public class TelephoneDirectoryGetClient {

    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 ()));
            // your code goes here

Question 4 continued

Question 5 (20 points)

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

(For purposes of this question, you can assume that userids, telephone numbers and names can contain any characters other than new line characters.)

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 TelephoneDirectoryRequestParser gives a parser for requests. Write a parser for responses.

PARSER_BEGIN (TelephoneDirectoryResponseParser)
public class TelephoneDirectoryResponseParser {
    public void handleOKResponse () { code for an OK response }
    public void handleNameResponse (String userid, String name) { code for a NAME OF response }
    public void handleTelephoneResponse (String userid, String telephone) { code for a TELEPHONE OF response }
    public void handleUnknownUserResponse (String userid) { code for an UNKNOWN USER response }
    public void handleErrorResponse () { code for an ERROR response }
}

PARSER_END (TelephoneDirectoryResponseParser)

TOKEN : {
  <ERROR: "ERROR">
| <IS: "IS">
| <NAME: "NAME">
| <OF: "OF">
| <OK: "OK">
| <TELEPHONE: "TELEPHONE">
| <UNKNOWN: "UNKNOWN">
| <USER: "USER">
| <CRLF: "\n" | "\r" | "\n\r">
| <SPACE: " ">
| <STRING: ( ~["\n","\r"] )+>
}

// your code goes here

Question 6 continued

Question 7 (10 points)

Consider the following class definitions:

class Pair {
  Object first;
  Object second;
  Pair (Object first, Object second) {
    this.first = first; this.second = second;
  }
}

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

  Pair foo = new Pair ("fred", "wilma");
  Pair baz = new Pair (foo, "barney");
  Pair bar = new Pair ("betty", baz);
  foo.first = baz;
  xmlSerializer.writeObject (bar);

Question 7 continued

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

  <Pair id="1">
    <Pair id="2">
      <String>hello</String>
      <Pair href="#2"/>
    </Pair>
    <Pair href="#1"/>
  </Pair>

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.