Recent Changes - Search:

Distributed Computing

This website demonstrates using wikis as teaching and learning tool.

The course instructor is also happy to share the teaching materials here with those who find it readable.

Distributed Computing with Java Sockets

Introduction

This coursework is about implementing distributed computing using only Java sockets and streams. You will apply the knowledge you got from the course to make it possible to transmit runtime object over an IP network. The application we consider is to transmit an object that performs a computation-extensive job. Since the state of the object is dynamic, it is difficult, if not impossible, to tell the server what to do simply by message-based communication. Instead, we can exploits Java's object streams and object's serializability to send the whole object to the server. The object then lives in the server space and uses the server resource to perform its task. The object will come back to its original client side with the updated state.

You will use streams and sockets to transmit a runtime Java object over the network. You need to implement a client that creates an object, serializes it, sends it to a server, and waits for the object to come back. You also need to implement a server that is able to accept a runtime Java object via TCP port, and then let the object to run within its space.

Specification and protocol of the computation server

A computation server is a multithreaded server that is able to serve multiple clients at a time. It would operate at port 8020 (both TCP and UDP) to listen for client connections. For each client connection, it will create a handler thread to talk with the client, accept a runtime object, allow the object to execute within it, and send the object back to the client. The following describes the protocol:

1. It first reads a message from the client.
2. If the client says: 'Dear greatest computer have you any time to spare', it would response 'Yes you are welcome' when the number of handler threads in execution is less than 100. Otherwise, it would response 'No I am busy'. The connection with that client is closed if the message it sends is the latter.
3. If the client does not speak the right thing as specified above, the server would send a textual message to the client and close that connection at once. The message is: 'I do not understand you'.
4. After sending the message 'Yes you are welcome', it reads an object that implements the Softbot interface. This interface specifies only one method: void execute().
import java.io.Serializable;

public interface Softbot extends Serializable {
    public void execute();
}
5. It invokes the execute() method of the object.
6. After the completion of the execute() method, it sends the message 'Your task is done hope to serve you soon'. It then sends the object to the client. After that, the connection is closed.
7. If any exception occurs during the execution of the execute() method, it sends the message 'You task cannot be completed sorry'. The connection is closed then.

Here shows you a sequence of the operations from the server side:

   objOut = new ObjectOutputStream(socket.getOutputStream());
   objIn = new ObjectInputStream(socket.getInputStream());
   objIn.readUTF();
   objOut.writeUTF(message);
   Object obj = objIn.readObject();
   Softbot softbot = (Softbot)obj;
   softbot.execute();
   objOut.writeUTF(message);
   objOut.writeObject(obj);

An application of computation servers

Here we consider an application that can make use of computation servers to perform some computation-intensive tasks.

Computation of pi:

The application is to compute the value of pi (π) to the specified number of digits after the decimal point. For example, the following shows you the value of pi to 1000 decimal points:

  3.141592653589793238462643383279502884197169399375105820974944592307816406286208
  99862803482534211706798214808651328230664709384460955058223172535940812848111745
  02841027019385211055596446229489549303819644288109756659334461284756482337867831
  65271201909145648566923460348610454326648213393607260249141273724587006606315588
  17488152092096282925409171536436789259036001133053054882046652138414695194151160
  94330572703657595919530921861173819326117931051185480744623799627495673518857527
  24891227938183011949129833673362440656643086021394946395224737190702179860943702
  77053921717629317675238467481846766940513200056812714526356082778577134275778960
  91736371787214684409012249534301465495853710507922796892589235420199561121290219
  60864034418159813629774771309960518707211349999998372978049951059731732816096318
  59502445945534690830264252230825334468503526193118817101000313783875288658753320
  83814206171776691473035982534904287554687311595628638823537875937519577818577805
  321712268066130019278766111959092164201989

One method to compute pi is to use Machin's formula:

  pi = 4 [4 arctan(1/5) - arctan(1/239)]

This method requires computation of arc tangent to sufficient precision. The following power series can be applied to compute the value of an arc tangent:

  arctan(x) = x - (x^3)/3 + (x^5)/5 - (x^7)/7 + (x^9)/9 - (x^11)/11 + . . .

A class for computing arc tangent:

To make use of the computation server, we create the following class for computing arc tangent to the specified number of digits after the decimal point.

import java.math.*;
public class InvArctan {
private int inverseX; // the value for calculating inverse arctan
private int digits; // number of digits required
private BigDecimal result; // the result of the calculation

public InvArctan(int inverseX, int digits) {
                this.inverseX = inverseX;
                this.digits = digits;
}

public BigDecimal getResult() {
                return result;
}

public void execute() {
                BigDecimal ONE = BigDecimal.valueOf(1);
                BigDecimal ZERO = BigDecimal.valueOf(0);
                int roundingMode = BigDecimal.ROUND_HALF_EVEN;
                BigDecimal invX, invX2, numer, term;
                invX = BigDecimal.valueOf(inverseX);
                invX2 = BigDecimal.valueOf(inverseX * inverseX);
                numer = ONE.divide(invX, digits, roundingMode);
                result = numer;
                int i = 1;
                do {
                        numer = numer.divide(invX2, digits, roundingMode);
                        int denom = 2 * i + 1;
                        term = numer.divide(BigDecimal.valueOf(denom),
                                       digits, roundingMode);
                        if ((i % 2) != 0) {
                        result = result.subtract(term);
                        } else {
                                result = result.add(term);
                        }
                        i++;
                        } while (term.compareTo(ZERO) != 0);
} // End of execute method
} // End of class

The following show you how to use this class to calculate the value of arctan(1/5) to 100 digits after the decimal point:

  InvArctan arctan1_5_object = new InvArctan(5,100);

  arctan1_5_object.execute();

  BigDecimal arctan1_5 = arctan1_5_object.getResult();

Computation of pi with the InvArctan class:

The following code shows you how to use InvArctan class to compute pi to 1000 (just an example) digits after the decimal point:

  int digits = 1000; // number of digits for pi
  int scale = digits + 5; // number of digits for arctan
  InvArctan a1_5 = new InvArctan(5,scale);
  InvArctan a1_239 = new InvArctan(239,scale);
  a1_5.execute(); 
  a1_239.execute();
  // Calculation of pi using Machin's formula
  BigDecimal arctan1_5 = a1_5.getResult();
  BigDecimal arctan1_239 = a1_239.getResult();
  BigDecimal ONE = BigDecimal.valueOf(1);
  BigDecimal ZERO = BigDecimal.valueOf(0);
  BigDecimal FOUR = BigDecimal.valueOf(4);
  BigDecimal pi = arctan1_5.multiply(FOUR).subtract(
				arctan1_239).multiply(FOUR);
  pi = pi.setScale(digits,BigDecimal.ROUND_HALF_UP);
  System.out.println(pi);

Computation of pi with computation servers:

In the computation of pi using Machin's formula, the most computation extensive task is the computation of arc tangent. We can make use of one or two computation servers specified above to perform the task. Specifically, we can first create two InvArctan objects, and then send them to computation servers. After getting back these two InvArctan objects from the servers, the arc tangent results are already stored within the objects. We then use them to calculate the value of pi.


Your Tasks

  • Implement a computation server class (let it call TCPComputeServer) that operates only on TCP. That is, it is able to receive and send objects via stream sockets. Note that you also need to write an interface called Softbot.
  • Writing a client program that computes pi with the help of TCP computation server. Note that you need to modify the header of InvArctan class so that it implements Softbot interface and it is serializable. Name this client class be TCPComputePi.
  • Implement a computation server class (let it call UDPComputeServer) that is able to operate on UDP. That is, it is able to receive and send objects via datagram packets.
  • Writing a client program that computes pi with the help of UDP computation server. Name this client class be UDPCOmputePi.

Note 1: You must do your best to complete the first two tasks. However, the last two tasks are optional. They are designed for ambitious students who aim at getting very high marks in their continuous assessment.

Note 2: I will use my own implementation of client to test your server code, and use my own server to test your client code. If you want to test your client code before submission, download the sample implementation of the server.


References


Course Requirement and Assessment

You need to demonstrate your work to your instructor in a tutorial session. Your instructor may ask you some questions to test your knowledge.

This programming task is a part of your continuous assessment. The maximum point awarded is 20. The assessment is roughly based on the following criteria:

  1. Implementation of the computation server. (10 points)
  2. Implementation of the computation client. (5 points)
  3. Implementation of the UDP version for the computation server and client. (5 points)
Edit - History - Print - Recent Changes - Search
Page last modified on November 03, 2008, at 10:10 AM