8000 Multi java virtual machine splitting · umeer/DistributedAlgorithms@d3f64b8 · GitHub
[go: up one dir, main page]

Skip to content

Commit d3f64b8

Browse files
committed
Multi java virtual machine splitting
1 parent e5cd331 commit d3f64b8

File tree

7 files changed

+308
-70
lines changed
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ javac.source=1.8
4040
javac.target=1.8
4141
javac.test.classpath=\
4242
${javac.classpath}:\
43-
${build.classes.dir}
43+
${build.classes.dir}:\
44+
${libs.junit_4.classpath}:\
45+
${libs.hamcrest.classpath}
4446
javac.test.processorpath=\
4547
${javac.test.classpath}
4648
javadoc.additionalparam=
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
package distributedalgorithms1;
7+
8+
import java.net.MalformedURLException;
9+
import java.rmi.Naming;
10+
import java.rmi.RemoteException;
11+
12+
/**
13+
*
14+
* @author Thinkpad
15+
*/
16+
public class DistributedAlgorithm2 {
17+
18+
/**
19+
* @param args the command line arguments
20+
*/
21+
public static void main(String[] args) throws RemoteException, MalformedURLException {
22+
//Setting up RMI
23+
try {
24+
java.rmi.registry.LocateRegistry.createRegistry(1102);
25+
} catch (RemoteException e) {
26+
e.printStackTrace();
27+
}
28+
29+
System.setProperty(("java.rmi.server.hostname"), "127.0.0.1");
30+
31+
Naming.rebind("rmi://localhost:1102/p2", new Process(2));
32+
33+
System.out.printf("Process p2 ready ");
34+
}
35+
36+
}
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
1-
/*
2-
* To change this license header, choose License Headers in Project Properties.
3-
* To change this template file, choose Tools | Templates
4-
* and open the template in the editor.
5-
*/
61
package distributedalgorithms1;
72

83
import java.net.MalformedURLException;
94
import java.rmi.Naming;
105
import java.rmi.NotBoundException;
6+
import java.rmi.RMISecurityManager;
117
import java.rmi.Remote;
128
import java.rmi.RemoteException;
139
import java.util.ArrayList;
@@ -18,39 +14,39 @@
1814
* @author Thinkpad
1915
*/
2016
public class DistributedAlgorithms1 {
21-
//Ciao
22-
17+
2318
public static void main(String[] args) throws RemoteException, MalformedURLException, NotBoundException, InterruptedException {
24-
19+
2520
//Setting up RMI
2621
try {
27-
java.rmi.registry.LocateRegistry.createRegistry(1100);
22+
java.rmi.registry.LocateRegistry.createRegistry(1101);
2823
} catch (RemoteException e) {
2924
e.printStackTrace();
3025
}
3126
System.setProperty(("java.rmi.server.hostname"), "127.0.0.1");
32-
33-
27+
//
28+
// if (System.getSecurityManager() == null) {
29+
// System.setSecurityManager(new RMISecurityManager());
30+
// }
31+
3432
//Setting up network of process
35-
Naming.rebind("rmi://localhost:1100/p1", new Process("p1"));
36-
Naming.rebind("rmi://localhost:1100/p2", new Process("p2"));
37-
Naming.rebind("rmi://localhost:1100/p3", new Process("p3"));
38-
39-
List<ProcessInterface> list = new ArrayList<ProcessInterface>();
40-
list.add((ProcessInterface) Naming.lookup("rmi://localhost:1100/p1"));
41-
list.add((ProcessInterface) Naming.lookup("rmi://localhost:1100/p2"));
42-
list.add((ProcessInterface) Naming.lookup("rmi://localhost:1100/p3"));
43-
44-
for (ProcessInterface p : list) {
45-
p.setNeighbor(list);
33+
Naming.rebind("rmi://localhost:1101/p1", new Process(1));
34+
//Naming.rebind("rmi://localhost:1100/p2", new Process("p2"));
35+
//Naming.rebind("rmi://localhost:1100/p3", new Process("p3"));
36+
37+
List<ProcessInterface> processList = new ArrayList<ProcessInterface>();
38+
processList.add((ProcessInterface) Naming.lookup("rmi://localhost:1101/p1"));
39+
processList.add((ProcessInterface) Naming.lookup("rmi://localhost:1102/p2"));
40+
processList.add((ProcessInterface) Naming.lookup("rmi://localhost:1103/p3"));
41+
42+
for (ProcessInterface p : processList) {
43+
p.setNeighbor(processList);
4644
}
47-
48-
49-
45+
5046
//Start comunication
51-
list.get(1).broadcast("Ciao");
52-
list.get(2).broadcast("ciao2");
53-
System.out.println(" main end");
54-
55-
}
47+
processList.get(1).broadcast("Ciao");
48+
//processList.get(2).broadcast("Ciao2");
49+
System.out.println(" main end ");
50+
51+
}
5652
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package distributedalgorithms1;
2+
3+
import java.net.MalformedURLException;
4+
import java.rmi.Naming;
5+
import java.rmi.NotBoundException;
6+
import java.rmi.RMISecurityManager;
7+
import java.rmi.Remote;
8+
import java.rmi.RemoteException;
9+
import java.util.ArrayList;
10+
import java.util.List;
11+
12+
/**
13+
*
14+
* @author Thinkpad
15+
*/
16+
public class DistributedAlgorithms3 {
17+
18+
public static void main(String[] args) throws RemoteException, MalformedURLException, NotBoundException, InterruptedException {
19+
//Setting up RMI
20+
try {
21+
java.rmi.registry.LocateRegistry.createRegistry(1103);
22+
} catch (RemoteException e) {
23+
e.printStackTrace();
24+
}
25+
26+
System.setProperty(("java.rmi.server.hostname"), "127.0.0.1");
27+
28+
Naming.rebind("rmi://localhost:1103/p3", new Process(3));
29+
30+
System.out.printf("Process p3 ready ");
31+
}
32+
33+
}
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,132 @@
1-
/*
2-
* To change this license header, choose License Headers in Project Properties.
3-
* To change this template file, choose Tools | Templates
4-
* and open the template in the editor.
5-
*/
61
package distributedalgorithms1;
72

83
import java.rmi.RemoteException;
94
import java.rmi.server.UnicastRemoteObject;
105
import java.util.ArrayList;
116
import java.util.List;
7+
import java.util.Random;
8+
import java.util.logging.Level;
9+
import java.util.logging.Logger;
1210

1311
public class Process extends UnicastRemoteObject implements ProcessInterface {
1412

15-
private List<ProcessInterface> list = new ArrayList<ProcessInterface>();
13+
private final int ID;
14+
private List<ProcessInterface> neighborList = new ArrayList<ProcessInterface>();
15+
private List<MessageBuffer> bufferMessage = new ArrayList<MessageBuffer>();
16+
private List<MessageBuffer> bufferAck = new ArrayList<MessageBuffer>();
17+
private String deliveredMessage = "";
1618

17-
public Process() throws RemoteException {
19+
public Process(int id) throws RemoteException {
20+
this.ID = id;
21+
}
22+
23+
@Override
24+
public void receviveMessage(String message, long timestamp) throws RemoteException {
25+
System.out.println("I'm: " + this.toString() + " and i just recevived: " + message + " with timestamp: " + timestamp);
26+
27+
synchronized (this) {
28+
//Put the message in a waiting list
29+
bufferMessage.add(new MessageBuffer(message, timestamp));
30+
}
1831

32+
//Send ack to all process
33+
long timestampAck = System.currentTimeMillis() / 1000l;
34+
for (ProcessInterface p : neighborList) {
35+
p.receviveAck(message, timestampAck);
36+
}
1937
}
2038

2139
@Override
22-
public void receviveMessage(String message, long timestamp) {
23-
System.out.print("Sono: " + this.toString() + " e ho ricevuto: " + message + " at: " + timestamp);
40+
public void receviveAck(String message, long timestamp) throws RemoteException {
41+
System.out.println("Ack receviced by: " + this.toString());
2442

43+
synchronized (this) {
44+
bufferAck.add(new MessageBuffer(message, timestamp));
45+
}
46+
checkMessageDelivery();
47+
}
48+
49+
private void checkMessageDelivery() throws RemoteException {
50+
long maxTimestamp = 0;
51+
52+
for (MessageBuffer message : bufferMessage) {
53+
if (message.timestamp > maxTimestamp) {
54+
maxTimestamp = message.timestamp;
55+
}
56+
}
57+
58+
for (int i = 0; i < bufferMessage.size(); i++) {
59+
//All the ack has been recived and is the oldest message, if yes deliver the message
60+
if (bufferMessage.get(i).timestamp == maxTimestamp) {
61+
if (getAckNumber(bufferMessage.get(i).message) == neighborList.size()) {
62+
System.out.println("Message delivered by: " + this.toString());
63+
deliveredMessage = bufferMessage.get(i).message;
64+
bufferMessage.remove(i);
65+
//Extra TODO remove the ack
66+
}
67+
}
68+
}
69+
}
70+
71+
@Override
72+
public int getAckNumber(String message) throws RemoteException {
73+
int counter = 0;
74+
for (MessageBuffer ack : bufferAck) {
75+
if (ack.message.compareTo(message) == 0) {
76+
counter++;
77+
}
78+
}
79+
return counter;
2580
}
2681

2782
@Override
2883
public void setNeighbor(List<ProcessInterface> list) throws RemoteException {
29-
this.list = list;
84+
this.neighborList = list;
3085
}
31-
86+
3287
@Override
33-
public void broadcast() throws RemoteException {
88+
public void broadcast(String message) throws RemoteException {
89+
System.out.println("Message broadcasted by: " + this.toString());
90+
Random rand = new Random();
91+
92+
long timestamp = System.currentTimeMillis() / 1000l;
93+
for (ProcessInterface p : neighborList) {
3494

35-
95+
new Thread(new Runnable() {
96+
public void run() {
97+
try {
98+
Thread.sleep(rand.nextInt(200) + 10);
99+
p.receviveMessage(message, timestamp);
100+
} catch (RemoteException ex) {
101+
Logger.getLogger(Process.class.getName()).log(Level.SEVERE, null, ex);
102+
} catch (InterruptedException ex) {
103+
Logger.getLogger(Process.class.getName()).log(Level.SEVERE, null, ex);
104+
}
105+
}
106+
}).start();
107+
108+
}
109+
}
110+
111+
@Override
112+
public String toString() {
113+
return ID + "";
36114
}
37-
38115

116+
//TESTING METHODS
39117
@Override
40118
public void test() {
41-
System.out.print("Ciao come ti chiami io sono: " + this.toString());
42-
}
43-
44-
// private static Process instance = null;
45-
//
46-
// protected Process() {
47-
// // Exists only to defeat instantiation.
48-
// }
49-
//
50-
// public static Process getInstance() {
51-
// if (instance == null) {
52-
// instance = new Process();
53-
// }
54-
// return instance;
55-
// }
56-
57-
119+
System.out.println("Hi i'm: " + this.toString());
120+
}
121+
122+
@Override
123+
public int getID() throws RemoteException {
124+
return ID;
125+
}
126+
127+
@Override
128+
public String getDeliveredMessage() throws RemoteException {
129+
return deliveredMessage;
130+
}
131+
58132
}
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
/*
2-
* To change this license header, choose License Headers in Project Properties.
3-
* To change this template file, choose Tools | Templates
4-
* and open the template in the editor.
5-
*/
61
package distributedalgorithms1;
72

83
import java.rmi.Remote;
@@ -17,9 +12,18 @@ public interface ProcessInterface extends Remote {
1712

1813
public void receviveMessage(String message, long timestamp) throws RemoteException;
1914

20-
public void setNeighbor( List<ProcessInterface> list) throws RemoteException;
21-
22-
public void broadcast() throws RemoteException;
15+
public void receviveAck(String message, long timestamp) throws RemoteException;
16+
17+
public void setNeighbor(List<ProcessInterface> list) throws RemoteException;
18+
19+
public void broadcast(String message) throws RemoteException;
20+
21+
//Testing ones
22+
public int getID() throws RemoteException;
23+
24+
public String getDeliveredMessage() throws RemoteException;
25+
26+
public int getAckNumber(String message) throws RemoteException;
2327

2428
public void test() throws RemoteException;
2529