-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.java
More file actions
executable file
·139 lines (135 loc) · 4.25 KB
/
Server.java
File metadata and controls
executable file
·139 lines (135 loc) · 4.25 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
public class Server extends Thread{
private EntryVector ev = new EntryVector(4); /* entry vector for request/release entries */
private EntryVector nested_ev = new EntryVector(3); /* nested entry vector for use entries */
private boolean RAfree; /* state of the resource RA */
private boolean RBfree; /* state of the resource RB */
private int RAwaiting; /* number of clients waiting for the resource RA */
private int RBwaiting; /* number of clients waiting for the resource RB */
private int waitingAny; /* number of clients waiting for any resource */
private int allocated; /* holds the last allocated resource through a request (RA == 0 ; RB == 1) */
private int released; /* holds the last released resource through a release (RA == 0 ; RB == 1) */
/* Constants definition for each entry */
private final int RAreq = 0;
private final int RBreq = 1;
private final int reqAny = 2;
private final int release = 3;
private final int RAuse = 0;
private final int RBuse = 1;
private final int useAny = 2;
/*--------------------------------------*/
public Server(String name){
super(name);
RAfree = true;
RBfree = true;
RAwaiting = 0;
RBwaiting = 0;
waitingAny = 0;
allocated = -1;
this.start();
}
public void RArequest(){
try {
ev.entryCall(RAreq);
ev.entryRet(RAreq);
} catch (InterruptedException ie){};
}
public int RAuse(){
try {
nested_ev.entryCall(RAuse);
nested_ev.entryRet(RAuse);
} catch (InterruptedException ie){};
return allocated;
}
public void RBrequest(){
try {
ev.entryCall(RBreq);
ev.entryRet(RBreq);
} catch (InterruptedException ie){};
}
public int RBuse(){
try {
nested_ev.entryCall(RBuse);
nested_ev.entryRet(RBuse);
} catch (InterruptedException ie){};
return allocated;
}
public void requestAny(){
try {
ev.entryCall(reqAny);
ev.entryRet(reqAny);
} catch (InterruptedException ie){};
}
public int useAny(){
try {
nested_ev.entryCall(useAny);
nested_ev.entryRet(useAny);
} catch (InterruptedException ie){};
return allocated;
}
public void release(int r){
try {
ev.entryCall(release);
released = r;
ev.entryRet(release);
} catch (InterruptedException ie){};
}
public void run(){
while (true){
try{
int i = ev.acceptAny(); /* accept any request/release */
switch (i){
case RAreq: /* received a request for RA */
if (RAfree){
RAfree = false;
allocated = 0; /* allocation of RA */
nested_ev.accept(RAuse);
}else
RAwaiting++;
break;
case RBreq: /* received a request for RB */
if (RBfree){
RBfree = false;
allocated = 1; /* allocation of RB */
nested_ev.accept(RBuse);
}else
RBwaiting++;
break;
case reqAny: /* received a request for RA or RB */
if (RAfree || RBfree){
if (RAfree){ /* if both are free the chosen one it's RA */
RAfree = false;
allocated = 0; /* allocation of RA */
} else {
RBfree = false;
allocated = 1; /* allocation of RB */
}
nested_ev.accept(useAny);
}else
waitingAny++;
break;
case release: /* release of the resource previously allocated */
if (waitingAny > 0){ /* priority to clients waiting for any resource */
waitingAny--;
allocated = released; /* allocation of the resource just released */
nested_ev.accept(useAny);
} else{
if (released == 0 && RAwaiting > 0){ /* if released == RA and some client it's waiting */
RAwaiting--;
allocated = 0;
nested_ev.accept(RAuse);
}else if (released == 1 && RBwaiting > 0){ /* if released == RB and some client it's waiting */
RBwaiting--;
allocated = 1;
nested_ev.accept(RBuse);
}else { /* if none it's waiting for a resource */
RAfree = (released == 0)?true:RAfree;
RBfree = (released == 1)?true:RBfree;
allocated = -1;
}
}
break;
}
}catch(Exception e){};
}
}
}