-
Notifications
You must be signed in to change notification settings - Fork 1
/
DistributedRemote.java
executable file
·96 lines (90 loc) · 2.49 KB
/
DistributedRemote.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import java.rmi.*;
import java.rmi.server.*;
import java.io.*;
import java.util.*;
import java.util.concurrent.TimeUnit;
public class DistributedRemote extends UnicastRemoteObject implements Distributed{
int lockOn=1,lockOff=0;
int _lock = lockOff;
int reset=0,stop=-1,reading=1,pause=2;
int read[] = {0, 0, 0};
DistributedRemote()throws RemoteException{
super();
}
public void readingControl(int i, String val){
switch(val){
case "start":
reading(i);
break;
case "pause":
System.out.println("*****************Reader "+i+" paused reading*****************");
read[i] = pause;
break;
case "stop":
stopReading(i);
break;
case "resume":
System.out.println("*****************Reader "+i+" resumed reading*****************");
read[i]=reading;
break;
}
}
public void stopReading(int i){
read[i]=stop;
}
public void lock(){
System.out.print("*****************Writer started writing, Reader ");
for(int i = 1 ; i <= 2 ; i++)
{
if(read[i]==reading || read[i]==pause)
System.out.print(i+" ");
}
System.out.print("on hold*****************\n");
_lock = lockOn;
}
public void release(){
if(read[1]==reading)
read[1]=reset;
if(read[2]==reading)
read[2]=reset;
_lock = lockOff;
System.out.println("*****************Writer finished writing*****************");
}
public void writing(String input){
try{
FileWriter fw=new FileWriter("content",true);
fw.write(input);
fw.close();
}catch(Exception e){System.out.println(e);}
System.out.println(input+" Written successfully...");
}
public void reading(int i){
try {
File file = new File("content");
Scanner sc = new Scanner(file);
while (sc.hasNext())
{
if(_lock==lockOff)
{
int status=read[i];
// int reset=0, stop=-1,reading=1;
switch(status){
case -1:
read[i]=reset;
System.out.println("*****************Reader "+i+" reading incomplete*****************");
break;
case 0:
System.out.println("*****************Reader "+i+" started reading*****************");
read[i]=reading;
break;
case 1:
System.out.println("Reader "+i+" reads "+sc.next());
TimeUnit.SECONDS.sleep(2);
break;
}
}
}
}catch(Exception e){System.out.println(e);}
System.out.println("*****************Reader "+i+" exits*****************");
}
}