Skip to content

Commit

Permalink
RMI
Browse files Browse the repository at this point in the history
  • Loading branch information
kumar-abhishek committed Mar 31, 2012
0 parents commit da3bec6
Show file tree
Hide file tree
Showing 12 changed files with 477 additions and 0 deletions.
18 changes: 18 additions & 0 deletions Client
@@ -0,0 +1,18 @@
in Client: id : 3| type: 0| time: 1333077450548| startTime: 1333077445931
in Client: id : 6| type: 1| time: 1333077450552| startTime: 1333077445931
in Client: id : 2| type: 0| time: 1333077450555| startTime: 1333077445931
in Client: id : 5| type: 1| time: 1333077450558| startTime: 1333077445931
in Client: id : 4| type: 0| time: 1333077450576| startTime: 1333077445931
in Client: id : 1| type: 0| time: 1333077450605| startTime: 1333077445931
in Client: id : 8| type: 1| time: 1333077451778| startTime: 1333077445931
in Clientjava.rmi.UnmarshalException: Error unmarshaling return header; nested exception is:
java.io.EOFException
in Client: id : 6| type: 1| time: 1333077948735| startTime: 1333077944072
in Client: id : 3| type: 0| time: 1333077948747| startTime: 1333077944072
in Client: id : 5| type: 1| time: 1333077948755| startTime: 1333077944072
in Client: id : 7| type: 1| time: 1333077948768| startTime: 1333077944072
in Client: id : 4| type: 0| time: 1333077948769| startTime: 1333077944072
in Client: id : 1| type: 0| time: 1333077948787| startTime: 1333077944072
in Client: id : 8| type: 1| time: 1333077950078| startTime: 1333077944072
in Clientjava.rmi.UnmarshalException: Error unmarshaling return header; nested exception is:
java.io.EOFException
231 changes: 231 additions & 0 deletions Hello.java
@@ -0,0 +1,231 @@
import java.rmi.*;
import java.util.*;
import java.rmi.server.*;
import java.rmi.registry.*;
import java.rmi.registry.LocateRegistry;
import java.io.*;
class Pair{
public int id;
public int priority;
Pair(int id, int pr){
this.id = id;
this.priority = pr;
}
}
public class Hello extends UnicastRemoteObject implements HelloInterface {
public synchronized static void writeToFile(String s){
// Stream to write file
FileOutputStream fout;
try{
String fileName = "crew.log";
fout = new FileOutputStream (fileName,true);
new PrintStream(fout).println(s);
fout.flush();
fout.close();
}
catch (IOException e){
System.err.println ("Unable to write to file");
System.exit(-1);
}
}


public void shutDown() throws RemoteException {
try{
//Registry registry = LocateRegistry.getRegistry(Registry.REGISTRY_PORT);
//Registry registry = LocateRegistry.getRegistry(2222);
//registry.unbind("//thunder.cise.ufl.edu/kabhishe");
//registry.unbind(this.serviceName);
//UnicastRemoteObject.unexportObject(this, true);
System.exit(0);
}catch(Exception e){
//writeToFile("cant unbind" + e.getMessage());
}
}

private static int ServiceSequence, ObjectValue ;
private int numberOfAccesses, numberOfReaders, numberOfWriters;
private String message;
private long startTime;
public static Boolean isCriticalSectionEmpty = true;
public static ArrayList<Pair> priorityArrReaders = new ArrayList<Pair>();
public static ArrayList<Integer> priorityArrWriters = new ArrayList<Integer>();
public Hello (long sTime, int numAccesses, int port, int numReaders, int numWriters) throws RemoteException {
// writeToFile("" + sTime);
startTime = sTime;
numberOfAccesses = numAccesses;
ServiceSequence = 1;
ObjectValue = -1;
numberOfReaders = numReaders;
numberOfWriters = numWriters;
try{
// Registry registry = LocateRegistry.createRegistry(port);
//registry.rebind("//thunder.cise.ufl.edu/kabhishe", this);
}catch(Exception e){
writeToFile("exception while binding " + e);
}

}
public String say(int id, int type, int sleepTime, int opTime) throws RemoteException {
for(int num = 1; num <= numberOfAccesses ; num++)
{
synchronized(this){
// writeToFile("My id : " + id + "type: " + type + " " + System.currentTimeMillis() + "starttime: " + startTime);
}

try{
Thread.sleep(sleepTime);
}
catch(Exception e){
//writeToFile("in Hello.java: expcetion 1" );
System.out.println(e);
}

//writeToFile("checking for cs My id : " + id + "type: " + type);

Boolean canGetIntoCS = false;
//check whether its a reader's request or writer's request: GET its ID in parameter as well
//type : 0:reader, 1:writer
//if a reader' s request: append it in to priority array
synchronized(this){
if(type == 0){//reader
priorityArrReaders.add(new Pair(id,1));
}
else //writer
priorityArrWriters.add(id);
}
//writeToFile("checking for cs 2222 My id : " + id + "type: " + type);

synchronized(this) {
while(canGetIntoCS == false){
//writeToFile("checking for cs 3333 My id : " + id + "type: " + type);
if(type == 0){ //reader
//if in 1st 3 in array or no writer in array
if(priorityArrWriters.isEmpty()){
canGetIntoCS = true;
break;
}
int pos = -1;
for(int i =0;i<priorityArrReaders.size();i++){
if(priorityArrReaders.get(i).id == id){
pos = i;
break;
}
}
if(pos > 2 || pos == -1){
try{
wait();
}
catch(Exception e){ }
continue;
}

if(priorityArrReaders.get(0).priority > 3){
canGetIntoCS = true;
break;
}
try {
wait();
}
catch ( Exception e){ }
continue;
}
else{ //writer
if (isCriticalSectionEmpty == false)
try{
wait();
continue;
}
catch(Exception e){ }


int pos = priorityArrWriters.indexOf(new Integer(id));
if(pos != 0){
try{
wait();
continue;
}
catch(Exception e){ }
}

if(!priorityArrReaders.isEmpty() && priorityArrReaders.get(0).priority > 3){
try{
wait();
continue;
}
catch(Exception e) { }
}
else {
canGetIntoCS = true;
break;
}
}
}
}
//enter into CS and at the end of it notifyAll
//if(canGetIntoCS == true)
{
//writeToFile("entered cs My id : " + id + "type: " + type);

//whoever goes next into CS:
synchronized(this){
isCriticalSectionEmpty = false;
}
String AccessedBy = "invalidValue" ;
if(type == 0)
AccessedBy = "R" + id;
else if(type == 1) {
AccessedBy = "W" + id;
ObjectValue = id;
}
else {
//writeToFile("in valid vale of nextClientType");
}
try{
long start = System.currentTimeMillis() - startTime;
Thread.sleep(opTime);
long finish = System.currentTimeMillis() - startTime;
synchronized(this){
writeToFile(ServiceSequence + "\t" + ObjectValue + "\t" +AccessedBy+ "\t" + start + "-" + finish);
++ServiceSequence;
}
}
catch(Exception e){
//writeToFile("in Hello.java: expcetion 1: " + e );
System.out.println(e);
}

//done executing in CS:remove from array
if(type == 0 && !priorityArrReaders.isEmpty()){
int removeId = -1;
for(int i =0;i<priorityArrReaders.size();i++)
if(priorityArrReaders.get(i).id == id){
removeId = i;
break;
}
if(removeId != -1){
synchronized(this){
priorityArrReaders.remove(removeId);
}
}
else
System.out.println("ERROR: REMOVE ID NOT FOUND");
}
else if(type == 1 && !priorityArrWriters.isEmpty())
synchronized(this){
priorityArrWriters.remove(0);
}
synchronized(this){
isCriticalSectionEmpty = true;
notifyAll();
//writeToFile("notified by : " + id + "type: " + type );
}
}
}

if(numberOfAccesses * (numberOfReaders + numberOfWriters) <= ServiceSequence ){
shutDown();
}
return message;
}
}
5 changes: 5 additions & 0 deletions HelloInterface.java
@@ -0,0 +1,5 @@
import java.rmi.*;

public interface HelloInterface extends Remote {
public String say(int id,int type, int sleepTime ,int opTime) throws RemoteException;
}
2 changes: 2 additions & 0 deletions IamServer
@@ -0,0 +1,2 @@
in server
Server is connected and ready for operation.1333077954169
9 changes: 9 additions & 0 deletions README
@@ -0,0 +1,9 @@
Java Remote Method Invocation project:(read project 3.pdf)

to Compile and Run:
==================
javac -souce 1.6 -target 1.6 *.java
java Start



52 changes: 52 additions & 0 deletions Server.java
@@ -0,0 +1,52 @@
import java.rmi.Naming;
import java.rmi.registry.*;
import java.rmi.registry.LocateRegistry;
import java.rmi.server.*;
import java.io.*;

public class Server
{
public static void writeToFile(String s){
// Stream to write file
FileOutputStream fout;
try{
String fileName = "IamServer";
fout = new FileOutputStream (fileName,true);
new PrintStream(fout).println(s);
fout.close();
}
catch (IOException e){
System.err.println ("Unable to write to file");
System.exit(-1);
}
}

public static void main (String[] argv)
{
try {
long startTime = Long.parseLong(argv[0]);
int numberOfAccesses = Integer.parseInt(argv[1]);
int port = Integer.parseInt(argv[2]);
int numReaders = Integer.parseInt(argv[3]);
int numWriters = Integer.parseInt(argv[4]);
String server = argv[5];

/*
Registry registry = LocateRegistry.createRegistry(port);
Runtime.getRuntime().exec("rmiregistry 2222");
registry.rebind("//thunder.cise.ufl.edu/kabhishe", new Hello (startTime, numberOfAccesses));
*/
writeToFile("in server");
Runtime.getRuntime().exec("rmiregistry " + port);
//Naming.rebind ("//thunder.cise.ufl.edu:2222/kabhishe", new Hello (startTime, numberOfAccesses, port, numReaders, numWriters));
Naming.rebind ("//" + server+ ":" + port + "/kabhishe", new Hello (startTime, numberOfAccesses, port, numReaders, numWriters));
//Hello h = new Hello(startTime, numberOfAccesses, port, numReaders, numWriters);
writeToFile ("Server is connected and ready for operation." + System.currentTimeMillis());


}
catch (Exception e) {
writeToFile("in server: not connnected"+e);
}
}
}

0 comments on commit da3bec6

Please sign in to comment.