8000 Not yet working, trying to solve serialization problems · rpytel1/distributed-algorithms@da1dbf6 · GitHub
[go: up one dir, main page]

Skip to content

Commit da1dbf6

Browse files
author
rpytel1
committed
Not yet working, trying to solve serialization problems
1 parent 4f3d18e commit da1dbf6

File tree

2 files changed

+89
-61
lines changed

2 files changed

+89
-61
lines changed

Assignment2/src/assignment2/server/RemoteComponentImpl.java

Lines changed: 68 additions & 59 deletions
< 9E88 tr class="diff-line-row">
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,17 @@ protected RemoteComponentImpl() throws RemoteException {
3131

3232
@Override
3333
public void init(int n) {
34-
this.N = new int[n];
35-
this.States = new String[n];
36-
this.tk = new Token(n);
34+
N = new int[n];
35+
States = new String[n];
36+
tk = new Token(n);
3737
for (int i = 0; i < n; i++) N[i] = 0;
3838
// different type of initialization for process 0 and the others
39-
if (this.id == 0) {
40-
this.States[0] = "H";
41-
for (int i = 1; i < n; i++) this.States[i] = "O";
39+
if (id == 0) {
40+
States[0] = "H";
41+
for (int i = 1; i < n; i++) States[i] = "O";
4242
} else {
43-
for (int i = 0; i < this.id; i++) this.States[i] = "R";
44-
for (int i = this.id; i < n; i++) this.States[i] = "O";
43+
for (int i = 0; i < id; i++) States[i] = "R";
44+
for (int i = id; i < n; i++) States[i] = "O";
4545
}
4646
index = 100 * id;
4747
}
@@ -53,8 +53,8 @@ public void init(int n) {
5353
public void sendRequest() throws RemoteException {
5454
System.out.println(id + " " + "Started sending request");
5555

56-
if (this.States[this.id].equals("H")) {
57-
System.out.println(getStateStamp() + this.id + ":Sending request to itself");
56+
if (States[id].equals("H")) {
57+
System.out.println(getStateStamp() + id + ":Sending request to itself");
5858

5959
new java.util.Timer().schedule(
6060
new java.util.TimerTask() {
@@ -70,13 +70,13 @@ public void run() {
7070
200 // and the delivery delay is set
7171
);
7272
}
73-
this.States[this.id] = "R";
74-
this.N[this.id] += 1;
73+
States[id] = "R";
74+
N[id] += 1;
7575
int i = 0;
76-
while (i < this.numProc) {
77-
if (i != this.id) {
78-
if (this.States[i].equals("R")) {
79-
System.out.println(getStateStamp() + this.id + ": Sending request to process " + i);
76+
while (i < numProc) {
77+
if (i != id) {
78+
if (States[i].equals("R")) {
79+
System.out.println(getStateStamp() + id + ": Sending request to process " + i);
8080
int finalI = i;
8181
new java.util.Timer().schedule(
8282
new java.util.TimerTask() {
@@ -108,17 +108,17 @@ public void receiveRequest(int reqId, int numReq) throws RemoteException {
108108
System.out.println(id + " " + index + "Started receiving request" + reqId);
109109
// Update requests
110110
System.out.println(getStateStamp() + id + ":Receiving request from process " + reqId);
111-
this.N[reqId] = numReq;
111+
N[reqId] = numReq;
112112

113113
if (id == reqId) {
114114
States[id] = "H";
115115
}
116116

117-
if (this.States[this.id].equals("O") || this.States[this.id].equals("E"))
118-
this.States[reqId] = "R";
119-
else if (this.States[this.id].equals("R")) {
120-
if (!this.States[reqId].equals("R")) {
121-
this.States[reqId] = "R";
117+
if (States[id].equals("O") || States[id].equals("E"))
118+
States[reqId] = "R";
119+
else if (States[id].equals("R")) {
120+
if (!States[reqId].equals("R")) {
121+
States[reqId] = "R";
122122
new java.util.Timer().schedule(
123123
new java.util.TimerTask() {
124124
@Override
@@ -134,14 +134,24 @@ public void run() {
134134

135135
}
136136
} else {
137-
this.States[reqId] = "R";
138-
this.States[this.id] = "O";
139-
this.tk.setTS(reqId, "R");
140-
this.tk.setTN(reqId, numReq);
141-
System.out.println(getStateStamp() + this.id + ": Sending token to process " + reqId);
137+
States[reqId] = "R";
138+
States[id] = "O";
139+
tk.setTS(reqId, "R");
140+
tk.setTN(reqId, numReq);
141+
System.out.println(getStateStamp() + id + ": Sending token to process " + reqId);
142142

143-
144-
RD[reqId].receiveToken();
143+
new java.util.Timer().schedule(
144+
new java.util.TimerTask() {
145+
@Override
146+
public void run() {
147+
try {
148+
RD[reqId].receiveToken();
149+
} catch (RemoteException e) {
150+
e.printStackTrace();
151+
}
152+
}
153+
},
154+
200); // and the delivery delay is set
145155

146156

147157
}
@@ -156,37 +166,36 @@ public void run() {
156166
public void receiveToken() throws RemoteException {
157167
System.out.println(id + "Started receiving tokken");
158168
System.out.println(getStateStamp() + id + ":Token received ");
159-
this.States[this.id] = "E";
169+
States[id] = "E";
160170
criticalSection();
161-
this.States[this.id] = "O";
162-
this.tk.setTS(this.id, "O");
163-
for (int i = 0; i < this.numProc; i++) {
171+
States[id] = "O";
172+
tk.setTS(id, "O");
173+
for (int i = 0; i < numProc; i++) {
164174
System.out.println("Executions" + N[i] + " " + tk.getTN(i));
165175
System.out.println("States" + States[i] + " " + tk.getTS(i));
166-
if (this.N[i] > this.tk.getTN(i)) {
167-
this.tk.setTN(i, this.N[i]);
168-
this.tk.setTS(i, this.States[i]);
176+
if (N[i] > tk.getTN(i)) {
177+
tk.setTN(i, N[i]);
178+
tk.setTS(i, States[i]);
169179
} else {
170-
this.N[i] = this.tk.getTN(i);
171-
this.States[i] = this.tk.getTS(i);
180+
N[i] = tk.getTN(i);
181+
States[i] = tk.getTS(i);
172182
}
173183
}
174-
System.out.println(getStateStamp());
175184
boolean flag = true;
176-
for (int i = 0; i < this.numProc; i++) {
177-
if (!this.States[i].equals("O")) {
185+
for (int i = 0; i < numProc; i++) {
186+
if (!States[i].equals("O")) {
178187
flag = false;
179188
}
180189
}
181190
if (flag) {
182-
this.States[this.id] = "H";
191+
States[id] = "H";
183192
} else {
184-
for (int i = 1; i < this.numProc + 1; i++) {
193+
for (int i = 1; i < numProc + 1; i++) {
185194
// Cyclic check
186-
int reqID = (this.id + i) % this.numProc;
195+
int reqID = (id + i) % numProc;
187196
// Send to first process with outstanding request
188-
if (this.States[reqID].equals("R")) {
189-
System.out.println(getStateStamp() + this.id + " sending token to process " + reqID);
197+
if (States[reqID].equals("R")) {
198+
System.out.println(getStateStamp() + id + " sending token to process " + reqID);
190199
new java.util.Timer().schedule(
191200
new java.util.TimerTask() {
192201
@Override
@@ -212,46 +221,46 @@ public void run() {
212221
/* Method implementing the critical section of each process
213222
*/
214223
private void criticalSection() {
215-
//this.States[this.id] = "E";
216-
System.out.println(getStateStamp() + this.id + " Entering critical section");
224+
//States[id] = "E";
225+
System.out.println(getStateStamp() + id + " Entering critical section");
217226
try {
218227
Thread.sleep(2000);
219228
} catch (InterruptedException e) {
220229
System.out.println("Critical section interrupted");
221230
}
222-
System.out.println(getStateStamp() + this.id + " leaving critical section");
223-
//this.States[this.id] = "O";
224-
//this.tk.setTS(this.id, "O");
231+
System.out.println(getStateStamp() + id + " leaving critical section");
232+
//States[id] = "O";
233+
//tk.setTS(id, "O");
225234
}
226235

227236
@Override
228237
public int[] getListOfRequests() throws RemoteException {
229-
return this.N;
238+
return N;
230239
}
231240

232241
@Override
233242
public String[] getListOfStates() throws RemoteException {
234-
return this.States;
243+
return States;
235244
}
236245

237246
@Override
238247
public void setListOfRequests(int[] arr) throws RemoteException {
239-
this.N = Arrays.copyOf(arr, this.numProc);
248+
N = Arrays.copyOf(arr, numProc);
240249
}
241250

242251
@Override
243252
public void setListOfStates(String[] arr) throws RemoteException {
244-
this.States = Arrays.copyOf(arr, this.numProc);
253+
States = Arrays.copyOf(arr, numProc);
245254
}
246255

247256
@Override
248257
public void setEntities(IComponent[] entities) throws RemoteException {
249-
this.RD = entities;
258+
RD = entities;
250259
}
251260

252261
@Override
253262
public void setName(String name) throws RemoteException {
254-
this.name = name;
263+
name = name;
255264
}
256265

257266
@Override
@@ -272,13 +281,13 @@ public int getId() throws RemoteException {
272281

273282
@Override
274283
public void setNumProc(int n) throws RemoteException {
275-
this.numProc = n;
284+
numProc = n;
276285
}
277286

278287
private String getStateStamp() {
279288
String stateStamp = "[";
280289
for (int i = 0; i < States.length; i++) {
281-
stateStamp += this.States[i];
290+
stateStamp += States[i];
282291
}
283292
stateStamp += "]";
284293
return stateStamp;

Assignment2/src/assignment2/util/Token.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ public class Token implements Serializable{
99
private static final long serialVersionUID = 1L;
1010
private int[] TN; // the token array with the number of requests of each process
1111
private String[] TS; // the token array with the state of each process
12-
12+
13+
public Token(){
14+
15+
}
16+
1317
public Token(int n){
1418
this.TN = new int[n];
1519
this.TS = new String[n];
@@ -34,5 +38,20 @@ public int getTN(int i){
3438
public String getTS(int i){
3539
return this.TS[i];
3640
}
37-
41+
42+
public int[] getTN() {
43+
return TN;
44+
}
45+
46+
public void setTN(int[] TN) {
47+
this.TN = TN;
48+
}
49+
50+
public String[] getTS() {
51+
return TS;
52+
}
53+
54+
public void setTS(String[] TS) {
55+
this.TS = TS;
56+
}
3857
}

0 commit comments

Comments
 (0)
0