4
4
import java .rmi .server .UnicastRemoteObject ;
5
5
import java .util .ArrayList ;
6
6
import java .util .List ;
7
+ import java .util .Random ;
8
+ import java .util .logging .Level ;
9
+ import java .util .logging .Logger ;
7
10
8
11
public class Process extends UnicastRemoteObject implements ProcessInterface {
9
12
@@ -20,6 +23,8 @@ public class Process extends UnicastRemoteObject implements ProcessInterface {
20
23
private boolean killed = false ;
21
24
private List <ProcessInterface > rivals = new ArrayList <>();
22
25
26
+ Random rand = new Random ();
27
+
23
28
public Process (int id , TypeOfProcess type ) throws RemoteException {
24
29
this .realID = id ;
25
30
this .id = id ;
@@ -28,68 +33,81 @@ public Process(int id, TypeOfProcess type) throws RemoteException {
28
33
29
34
@ Override
30
35
public void receviveMessage (int linkRealID , int linkID , int linkLevel ) throws RemoteException {
31
- System .out .println (toString () + " and i just recevived a message from: " + linkRealID + " with id: " + linkID + " with level: " + linkLevel );
32
-
33
- if (type == TypeOfProcess .CANDIDATE ) {
34
36
35
- if (compareLevelAndID (linkID , linkLevel ) == 0 && killed == false ) {
37
+ new Thread (new Runnable () {
38
+ public void run () {
36
39
try {
37
- rivals .remove (findRivalsIndex (linkRealID ));
38
- level ++;
39
- } catch (Exception e ) {
40
- }
41
- if (rivals .size () > 0 && !killed ) {
42
- System .out .println (toString () + " and i just sent a message to: " + rivals .get (0 ).getID () + " with id: " + id + " with level: " + level );
43
- rivals .get (0 ).receviveMessage (realID , id , level );
44
- }
45
- } else if (compareLevelAndID (linkID , linkLevel ) < 0 ) {
46
- System .out .println (toString () + " and i just sent a message to: " + linkRealID + " with id: " + linkID + " with level: " + linkLevel );
47
- neighborList .get (findNeighborIndex (linkRealID )).receviveMessage (realID , linkID , linkLevel );
48
-
49
- killed = true ;
50
- System .out .println (" I just got killed: " + realID + " Level: " + level );
51
-
52
- } else if (!killed ) { //In case the other is smaller, this can happen in case other is a candidate testing me and is small
53
- System .out .println (toString () + " and i just sent a message to: " + linkRealID + " with id: " + id + " with level: " + level );
54
- neighborList .get (findNeighborIndex (linkRealID )).receviveMessage (realID , id , level );
55
- }
56
-
57
- if (rivals .isEmpty ()) {
58
- if (!killed ) {
59
- System .out .println ("I'm the president. ID: " + realID + " Level: " + level );
60
- } else {
61
- System .out .println ("I'm not the president. ID: " + realID + " Level: " + level );
40
+ Thread .sleep (rand .nextInt (200 ) + 10 );
41
+
42
+ System .out .println (myInfo () + " and i just recevived a message from: " + linkRealID + " with id: " + linkID + " with level: " + linkLevel );
43
+
44
+ if (type == TypeOfProcess .CANDIDATE ) {
45
+
46
+ if (compareLevelAndID (linkID , linkLevel ) == 0 && killed == false ) {
47
+ try {
48
+ rivals .remove (findRivalsIndex (linkRealID ));
49
+ level ++;
50
+ } catch (Exception e ) {
51
+ }
52
+ if (rivals .size () > 0 && !killed ) {
53
+ System .out .println (myInfo () + " and i just sent a message to: " + rivals .get (0 ).getID () + " with id: " + id + " with level: " + level );
54
+ rivals .get (0 ).receviveMessage (realID , id , level );
55
+ }
56
+ } else if (compareLevelAndID (linkID , linkLevel ) < 0 ) {
57
+ System .out .println (myInfo () + " and i just sent a message to: " + linkRealID + " with id: " + linkID + " with level: " + linkLevel );
58
+ neighborList .get (findNeighborIndex (linkRealID )).receviveMessage (realID , linkID , linkLevel );
59
+
60
+ killed = true ;
61
+ System .out .println (" I just got killed: " + realID + " Level: " + level );
62
+
63
+ } else if (!killed ) { //In case the other is smaller, this can happen in case other is a candidate testing me and is small
64
+ System .out .println (myInfo () + " and i just sent a message to: " + linkRealID + " with id: " + id + " with level: " + level );
65
+ neighborList .get (findNeighborIndex (linkRealID )).receviveMessage (realID , id , level );
66
+ }
67
+
68
+ if (rivals .isEmpty ()) {
69
+ if (!killed ) {
70
+ System .out .println ("I'm the president. ID: " + realID + " Level: " + level );
71
+ } else {
72
+ System .out .println ("I'm not the president. ID: " + realID + " Level: " + level );
73
+ }
74
+ }
75
+
76
+ } else if (type == TypeOfProcess .ORDINARY ) { //This is the ordinary process
77
+
78
+ if (compareLevelAndID (linkID , linkLevel ) > 0 ) { //In case a candidate ask me but i'm big, but i don't care to be elected so i let him thinking that he own me
79
+ if (father == -1 ) {
80
+ System .out .println (myInfo () + " and i just sent a message to: " + linkRealID + " with id: " + linkID + " with level: " + linkLevel );
81
+ neighborList .get (findNeighborIndex (linkRealID )).receviveMessage (realID , linkID , linkLevel );
82
+
83
+ } else {
84
+ System .out .println (myInfo () + " and i just sent a message to: " + linkRealID + " with id: " + id + " with level: " + level );
85
+ neighborList .get (findNeighborIndex (linkRealID )).receviveMessage (realID , id , level );
86
+
87
+ }
88
+ } else if (compareLevelAndID (linkID , linkLevel ) < 0 ) {
89
+ potentialFather = linkID ;
90
+ id = linkID ;
91
+ level = linkLevel ;
92
+ if (father == -1 ) {
93
+ father = potentialFather ;
94
+ }
95
+ System .out .println (myInfo () + " and i just sent a message to: " + father + " with id: " + id + " with level: " + level );
96
+ neighborList .get (findNeighborIndex (father )).receviveMessage (realID , id , level );
97
+ } else { //In case they are equal
98
+ father = potentialFather ;
99
+ System .out .println (myInfo () + " and i just sent a message to: " + father + " with id: " + id + " with level: " + level );
100
+ neighborList .get (findNeighborIndex (father )).receviveMessage (realID , id , level );
101
+ }
102
+
103
+ }
104
+ } catch (RemoteException ex ) {
105
+ Logger .getLogger (Process .class .getName ()).log (Level .SEVERE , null , ex );
106
+ } catch (InterruptedException ex ) {
107
+ Logger .getLogger (Process .class .getName ()).log (Level .SEVERE , null , ex );
62
108
}
63
109
}
64
-
65
- } else if (type == TypeOfProcess .ORDINARY ) { //This is the ordinary process
66
-
67
- if (compareLevelAndID (linkID , linkLevel ) > 0 ) { //In case a candidate ask me but i'm big, but i don't care to be elected so i let him thinking that he own me
68
- if (father == -1 ) {
69
- System .out .println (toString () + " and i just sent a message to: " + linkRealID + " with id: " + linkID + " with level: " + linkLevel );
70
- neighborList .get (findNeighborIndex (linkRealID )).receviveMessage (realID , linkID , linkLevel );
71
-
72
- } else {
73
- System .out .println (toString () + " and i just sent a message to: " + linkRealID + " with id: " + id + " with level: " + level );
74
- neighborList .get (findNeighborIndex (linkRealID )).receviveMessage (realID , id , level );
75
-
76
- }
77
- } else if (compareLevelAndID (linkID , linkLevel ) < 0 ) {
78
- potentialFather = linkID ;
79
- id = linkID ;
80
- level = linkLevel ;
81
- if (father == -1 ) {
82
- father = potentialFather ;
83
- }
84
- System .out .println (toString () + " and i just sent a message to: " + father + " with id: " + id + " with level: " + level );
85
- neighborList .get (findNeighborIndex (father )).receviveMessage (realID , id , level );
86
- } else { //In case they are equal
87
- father = potentialFather ;
88
- System .out .println (toString () + " and i just sent a message to: " + father + " with id: " + id + " with level: " + level );
89
- neighborList .get (findNeighborIndex (father )).receviveMessage (realID , id , level );
90
- }
91
-
92
- }
110
+ }).start ();
93
111
}
94
112
95
113
private int compareLevelAndID (int linkID , int linkLevel ) {
@@ -134,7 +152,7 @@ public void startElection() throws RemoteException {
134
152
id = realID ;
135
153
level = 0 ;
136
154
// rivals = neighborList;
137
- System .out .println (toString () + " and i just sent a message to: " + rivals .get (0 ).getID () + " with id: " + id + " with level: " + level );
155
+ System .out .println (myInfo () + " and i just sent a message to: " + rivals .get (0 ).getID () + " with id: " + id + " with level: " + level );
138
156
rivals .get (0 ).receviveMessage (realID , id , level );
139
157
} else {
140
158
System .out .println ("I win even before starting" );
@@ -158,8 +176,7 @@ public void setNeighbor(List<ProcessInterface> list) throws RemoteException {
158
176
}
159
177
}
160
178
161
- @ Override
162
- public String toString () {
179
+ public String myInfo () {
163
180
return "Hi i'm: " + realID + " with id: " + id + " and level: " + level ;
164
181
}
165
182
0 commit comments