Roger L. Kraft

CS 40400 / CS 59000-01 - Programming Assignment 2

This assignment is due Tuesday, October 22.

This homework assignment modifies Assignment 1 so that the solution runs over a network.

Download this zip file.

For this assignment you will write three Java programs, Query.java, which will be a client program, Middle.java, which will be both a server and a client program, and BackEnd.java, which will be a server program. Overall, this assignment has a "three tier" organization. A client program, Query.java, communicates with a middle tier program, Middle.java, which acts as a server as far as Query.java is concerned. The program Middle.java communicates with a third tier, BackEnd.java. In this communication, Middle.java acts as a client program to the server BackEnd.java.

The program Query does not do too much in this design. It simply takes four command line arguments and passes them (over the network) to the server Middle for processing and then Query waits for the final result back from Middle. The syntax for running Query is the same as in Assignment 1.

  Query [-p <pattern>] [-f {income | population | lifeExpectancy}] [-yr <year>] [-op {total | avg | max | min | count}]

We need to describe the (application level) protocol that Query uses to communicate with its server Middle. After establishing its connection to the server, Query writes four lines to the server, one line for each of the pattern, field, year, and operation parameters (in that specific order). After sending these four lines, Query should read all the lines returned to it by Middle (and print them to standard output).

Now let us describe what the middle tier, Middle.java, is responsible for. The main job of Middle is to have grep.exe search for lines from the nations.json file that satisfy pattern and send them (along with the other three parameters field, year, and operation) to the BackEnd.java server for processing.

The Middle program should run as a server that, as soon as it has established a connection with a client, should read the four lines sent to it by the client. The four lines are the parameters pattern, field, year, and operation. After Middle gets these four parameters, it should establish a connection to the server BackEnd and write three lines to the server, one line for each of the field, year, and operation parameters (in that specific order). After sending these three lines, Middle should run grep.exe (as a child process) with the command line

      grep\grep.exe pattern nations.json

This command line tells grep.exe to read the file nations.json (so you do not need to worry about redirecting any input into the grep.exe process). The Middle process should send each line that it reads from the standard output of grep.exe to the server BackEnd. When Middle detects that there are no more lines of output from grep.exe, Middle should send one blank line to BackEnd (this tells BackEnd that there are no more lines of input from its client Middle). After Middle has sent the blank line to BackEnd, Middle should read all the lines returned to it by BackEnd and echo those lines to Middle's client, Query.

Now let us describe what the third tier, BackEnd.java, is responsible for. The BackEnd.java program in this assignment does pretty much what BackEnd.java did in the first assignment, it should do the appropriate operation on the value it finds for the year in the appropriate field of each line of its input.

The BackEnd program should run as a server that, as soon as it has established a connection with a client, should read the three lines sent to it by the client. The three lines are the parameters field, year, and operation. After BackEnd gets these three parameters, it should enter a loop that reads lines of nation data from its socket and processes those line in exactly the same way that BackEnd in Assignment 1 processed its lines read from standard input. But in this assignment, BackEnd should stop reading lines when it gets a blank line (not when it detects EOF, as in Assignment 1). When BackEnd detects its last input line, it should calculate its final result and write that back to its client Middle.

The last thing to describe for this assignment is how the clients and servers get the appropriate IP addresses and port numbers. The port number for BackEnd's server socket should be given as a command line parameter to BackEnd. The port number for Middle to use with its server socket should be Middle's first command line parameter, and the IP address and port number for BackEnd should be the second and third command line parameters for Middle. The Query program already has a lot of command line parameters, so it will get the IP address and port number for Middle from the first two lines of the query.cfg configuration file.

In the zip file there are two files Hw1_Query.java and Hw1_BackEnd.java which are a working solution to Assignment 1. You can do this assignment by either starting with your own solution to Assignment 1, or you can start with the code provided in the zip file.

Also in the zip file there are three files, Hw2_Demo_Query.class, Hw2_Demo_Middle.class and Hw2_Demo_BackEnd.class, that are a working version of this assignment. You can experiment with these programs to see how your final version should work.

Debugging network code is not easy. You have to come up with strategies for writing and debugging small chunks of the assignment at a time and not try to write and debug the whole assignment as one large program. When you run this program, you will need to use one console window for each tier, Query, Middle and BackEnd. Log lots of information to these console windows. Most of the actual code that you need can be found in, and mostly cut and pasted from, the example programs in Inter-Process Communication.zip and Network Communication.zip. If you have questions about how to think about and organize the writing of these program, please come and see me.

Turn in a zip file called CS404Hw2Surname.zip containing your versions of Query.java, Middle.java and BackEnd.java. Include in the zip file everything needed to compile and run your programs. Please remember to put your name inside of each of your source files.

This assignment is due Tuesday, October 22.