8000 -Implemented dynamic process creation NUMBER_OF_PROCESS · umeer/DistributedAlgorithms@501dccc · GitHub
[go: up one dir, main page]

Skip to content

Commit 501dccc

Browse files
committed
-Implemented dynamic process creation NUMBER_OF_PROCESS
1 parent 122f5bc commit 501dccc

File tree

4 files changed

+100
-70
lines changed

4 files changed

+100
-70
lines changed

DistributedAlgorithms3/src/distributedalgorithms3/DistributedAlgorithms1.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@
88
* In this program there is implemented the Afek and Gafni async election
99
* system, you have to run "DistributedAlghorithm1" and "DistributedAlgorithm2"
1010
* first and than "DistributedAlghorithm3". You can select how many process
11-
* there are in the network form the "DistributedAlghorithm3" file. The process
12-
* can be candidate or ordinary, the ordinary process wont compete in the
13-
* election race. You can set this value during the process creation.
11+
* there are in the network form the "DistributedAlghorithm3" file by changing
12+
* NUMBER_OF_PROCESS. The process can be candidate or ordinary, the ordinary
13+
* process wont compete in the election race. You can set this value during the
14+
* process creation.
1415
*
1516
*/
1617
public class DistributedAlgorithms1 {

DistributedAlgorithms3/src/distributedalgorithms3/DistributedAlgorithms2.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@
88
* In this program there is implemented the Afek and Gafni async election
99
* system, you have to run "DistributedAlghorithm1" and "DistributedAlgorithm2"
1010
* first and than "DistributedAlghorithm3". You can select how many process
11-
* there are in the network form the "DistributedAlghorithm3" file. The process
12-
* can be candidate or ordinary, the ordinary process wont compete in the
13-
* election race. You can set this value during the process creation.
11+
* there are in the network form the "DistributedAlghorithm3" file by changing
12+
* NUMBER_OF_PROCESS. The process can be candidate or ordinary, the ordinary
13+
* process wont compete in the election race. You can set this value during the
14+
* process creation.
1415
*
1516
*/
1617
public class DistributedAlgorithms2 {

DistributedAlgorithms3/src/distributedalgorithms3/DistributedAlgorithms3.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,18 @@
1111
* In this program there is implemented the Afek and Gafni async election
1212
* system, you have to run "DistributedAlghorithm1" and "DistributedAlgorithm2"
1313
* first and than "DistributedAlghorithm3". You can select how many process
14-
* there are in the network form the "DistributedAlghorithm3" file. The process
15-
* can be candidate or ordinary, the ordinary process wont compete in the
16-
* election race. You can set this value during the process creation.
14+
* there are in the network form the "DistributedAlghorithm3" file by changing
15+
* NUMBER_OF_PROCESS. The process can be candidate or ordinary, the ordinary
16+
* process wont compete in the election race. You can set this value during the
17+
* process creation.
1718
*
1819
*/
1920
public class DistributedAlgorithms3 {
2021

2122
public static void main(String[] args) throws RemoteException, MalformedURLException, NotBoundException, InterruptedException {
23+
24+
final int NUMBER_OF_PROCESS = 10;
25+
2226
//Setting up RMI
2327
try {
2428
java.rmi.registry.LocateRegistry.createRegistry(1103);
@@ -36,6 +40,13 @@ public static void main(String[] args) throws RemoteException, MalformedURLExcep
3640
processList.add((ProcessInterface) Naming.lookup("rmi://localhost:1102/p2"));
3741
processList.add((ProcessInterface) Naming.lookup("rmi://localhost:1103/p3"));
3842

43+
for (int i = 4; i < NUMBER_OF_PROCESS + 4; i++) {
44+
//Creation of process
45+
Naming.rebind(&qu 6D40 ot;rmi://localhost:1103/" + (i), new Process(i, TypeOfProcess.ORDINARY));
46+
47+
//Linking of process
48+
processList.add((ProcessInterface) Naming.lookup("rmi://localhost:1103/" + (i)));
49+
}
3950
} catch (Exception e) {
4051
System.out.println(" Error: Some process are offline, make sure to run ahead the programs DisrbutedAlgorithms1 and DisrbutedAlgorithms2");
4152
System.exit(0);

DistributedAlgorithms3/src/distributedalgorithms3/Process.java

Lines changed: 78 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
import java.rmi.server.UnicastRemoteObject;
55
import java.util.ArrayList;
66
import java.util.List;
7+
import java.util.Random;
8+
import java.util.logging.Level;
9+
import java.util.logging.Logger;
710

811
public class Process extends UnicastRemoteObject implements ProcessInterface {
912

@@ -20,6 +23,8 @@ public class Process extends UnicastRemoteObject implements ProcessInterface {
2023
private boolean killed = false;
2124
private List<ProcessInterface> rivals = new ArrayList<>();
2225

26+
Random rand = new Random();
27+
2328
public Process(int id, TypeOfProcess type) throws RemoteException {
2429
this.realID = id;
2530
this.id = id;
@@ -28,68 +33,81 @@ public Process(int id, TypeOfProcess type) throws RemoteException {
2833

2934
@Override
3035
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) {
3436

35-
if (compareLevelAndID(linkID, linkLevel) == 0 && killed == false) {
37+
new Thread(new Runnable() {
38+
public void run() {
3639
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);
62108
}
63109
}
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();
93111
}
94112

95113
private int compareLevelAndID(int linkID, int linkLevel) {
@@ -134,7 +152,7 @@ public void startElection() throws RemoteException {
134152
id = realID;
135153
level = 0;
136154
// 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);
138156
rivals.get(0).receviveMessage(realID, id, level);
139157
} else {
140158
System.out.println("I win even before starting");
@@ -158,8 +176,7 @@ public void setNeighbor(List<ProcessInterface> list) throws RemoteException {
158176
}
159177
}
160178

161-
@Override
162-
public String toString() {
179+
public String myInfo() {
163180
return "Hi i'm: " + realID + " with id: " + id + " and level: " + level;
164181
}
165182

0 commit comments

Comments
 (0)
0