MAIN MENU BIODATA ASSIGNMENTS TUTORIALS EMAILS
DISTRIBUTED SYSTEMS (TCT 2034)
ASSIGNMENT 3
( RMI )
QUESTION:
Do RMI programming which include 3 programs that are server, client and interface. In the interface include the data there-no need to do database, and put it in one file-no need to do RMI.
Answers:
class Result implements Serializable
{
private String surname;
private int mark;
public Result(String name, int score)
{
surname = name;
mark = score;
}
public String getName()
{
return surname;
}
public void setName(String name)
{
surname = name;
}
public int getMark()
{
return mark;
}
public void setMark(int score)
{
if((score>=0) && (score<=100))
mark = score;
}
}
----------------------------------------------------------------------------------------------------------------------------------------------------
import java.io.*;
public class FileResult
{
private static final long REC_SIZE = 34;
private static final int SURNAME_SIZE = 15;
private ststic String surname, textMark;
private static int mark;
public static void main(String[] args){
throws IOException
{
/*******************************************
***SUPPLY CODE FOR main!***
********************************************/
}
public static void writeString(RandomAccessFile f,
String s, int fixedSize) throws IOException
{
int size = s.length();
if(size<=fixedSize)
{
f.writeChars(s);
for(int i=size; i<fixedSize; i++)
f.writeChar(' ');
}
else
f.writeChars(s.substring(0,fixedSize);
}
public static String readString(RandomAccessFile f,
int fixedSize) throws IOException
{
String value = "";
for(int i=0; i<fixedSize; i++)
value+=f.readChar();
return value;
}
}
----------------------------------------------------------------------------------------------------------------------------------------------------
import java.rmi.*;
import java.util.Vector;
public class ResultServer
{
private static final String HOST = "localhost";
public static void main(String[] args throws Exception
{
//Create an initialized array of three Result objects...
Result[] result=
{new Result(0001,"Ahmad","Hakimi", 65),
new Result(0012,"Farah","Rashid", 50),
new Result(0023,"Muhammad Nabil","Helmi", 85),
Vector rsltDetails = new Vector();
//Insert the Result objects into the Vector...
for(int i=0; i<result.length; i++)
rsltDetails.addElement(result[i]);
//Create an implementation object, passing the above
//Vector to the constructor...
ResultImpl temp = new ResultImpl(rsltFDetails);
//Save the object's name in a String...
String rmiObjectName = "rmi://" + HOST + "/Results";
//Bind the object's name to its reference
Naming.rebind(rmiObjectName, temp);
System.out.println("Binding complete...\n");
}
}
-----------------------------------------------------------------------------------------------------------------------------------------------------import java.rmi.*;
import java.util.Vector;
public class ResultClient
{
private static final String HOST = "localhost";
public static void main(String[] args)
{
try
{
//Obtain a reference to the object from the
//registry and typecast it into the appropriate type...
Vector rsltDetails=temp.getFileResult();
//Simply display all the result details...
for(int i=0; i<rsltDetails.size(); i++)
{
//Retrieve an Result object from the Vector
//(typecasting it from class Object)...
Result rslt = (Result) rsltDetails.elementAt(i);
//Now invoke the methods of the Result object
//to display its details
System.out.println("\nStudent number: "
+ rslt.getRsltNum());
System.out.println("Name: " + rslt.getName());
System.out.println("Mark: "
+ rslt.getMark());
}
}
catch(ConnectException conEx)
{
System.out.println("Unable to connect to server!");
System.exit(1);
}
catch(Exception e)
{
e.printStackTrace();
System.exit(1);
}
}
}
-----------------------------------------------------------------------------------------------------------------------------------------------------
OUTPUT