Name:

SE550 Example midterm exam

Answer all questions.

Time allowed: 2 hours

Total number of points: 100


Name lookup

Some questions will refer to NameServer, NameClient and NameRequestParser classes. They are defined here.

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

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

    protected NameServer (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 () { 
	thread.start (); 
    }

    public void run () {
	try {
	    String line = in.readLine ();
	    while (line != null && !line.equals ("QUIT")) {
		if (line.startsWith ("LOOKUP ")) {
		    nameLookup (line.substring (7));
		} else {
		    error ();
		}
		line = in.readLine ();
	    }
	} catch (final IOException ex) {
	} 
    }

    protected void nameLookup (final String hostName) {
	try {
	    final InetAddress addr = InetAddress.getByName (hostName);
	    out.println ("ADDRESS " + addr.getHostAddress ());
	} catch (final UnknownHostException ex) {
	    out.println ("UNKNOWN HOST");
	}
    }

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

}

Name lookup cont.

public class NameClient {

    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 ("LOOKUP " + args[i]);
	    final String reply = in.readLine ();
	    if (reply.startsWith ("ADDRESS ")) {
		System.out.println ("Address of " + args[i] + " is " + reply.substring (8));
	    } else if (reply.equals ("UNKNOWN HOST")) {
		System.out.println ("Address of " + args[i] + " is unknown");
	    } else {
		System.out.println ("This shouldn't happen...");
	    }
	}
	out.println ("QUIT");
	out.close ();
	in.close ();
	socket.close ();
    }

}

Name lookup cont.

public grammar class NameRequestParser {

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

  public void handleLookupRequest (String hostName) {
    // code for handling a lookup request
  }

  token {
    <QUIT: "QUIT">
  | <LOOKUP: "LOOKUP">
  | <CRLF: "\n" | "\r" | "\n\r">
  | <SPACE: " ">
  | <HOSTNAME: (<HOSTNAMECHAR>)+>
  | <HOSTNAMECHAR: ['a'-'z'] | ['A'-'Z'] | ['0'-'9'] | "." | "-">
  }

  public production void request () {
    quitRequest () | lookupRequest ()
  }

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

  production void lookupRequest () {
    { String name; }
    <LOOKUP> <SPACE> name=hostName () <CRLF>
    { handleLookupRequest (name); }
  }

  production String hostName () {
    { Token t; }
    t = <HOSTNAME>
    { return t.image; }
  }

}

Question 1 (5 points)

What is the client/server model? What is the difference between a client and a server?


Question 2 (5 points)

What is the difference between distributed programming, parallel programming, and concurrent programming?


Question 3 (15 points)

Assume that on machine server the following command is run:

  java NameServer

and that on machine client the following command is run:

  java NameClient server klee foobar

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

b) What messages are printed out by the client?

(You can assume that host klee has IP address 140.192.32.63, and that host foobar does not exist. You can also assume that the network functions correctly.)


Question 4 (10 points)

In the code for NameServer the server does not close the in or out streams, or the socket. Add code to do this:

    public void run () {
	try {
	    String line = in.readLine ();
	    while (line != null && !line.equals ("QUIT")) {
		if (line.startsWith ("LOOKUP ")) {
		    nameLookup (line.substring (7));
		} else {
		    error ();
		}
		line = in.readLine ();
	    }
	} catch (final IOException ex) {
	} // Your code goes here

Question 5 (15 points)

Give a grammar for the protocol which is used by the NameClient and NameServer programs.

(For purposes of this answer, you can assume that internet hostnames can only contain alphanumeric characters, together with "." and "-", and that internet addresses are four bytes separated by ".".)


Question 6 (20 points)

The mparse parser NameRequestParser gives a parser for name requests. Write a parser for name responses.

public grammar class NameResponseParser {

  public void handleAddressResponse (byte addr1, byte addr2, byte addr3, byte addr4) {
    // code for handling an address response
  }

  public void handleUnknownHostResponse () {
    // code for handling an unknown host response
  }

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

  // Your code goes here

Question 7 (15 points)

Describe the Java object serialization algorithm, including a discussion of how the algorithm deals with cyclic structures.


Question 8 (10 points)

Describe the use of stub objects in Java RMI. Your answer should include a definition of remote interfaces, marshalling, and unmarshalling.


Question 9 (5 points)

If you could change one feature of Java distributed programming, what would it be?