8000 Distributed versions of client 1 and 2 · rpytel1/distributed-algorithms@231bc3a · GitHub
[go: up one dir, main page]

Skip to content
8000
< 10000 div style="--spacing:var(--spacing-none)" class="prc-PageLayout-PageLayoutRoot-1zlEO">

Commit 231bc3a

Browse files
author
LENOVO-PC\Vasilis
committed
Distributed versions of client 1 and 2
1 parent e217d19 commit 231bc3a

File tree

2 files changed

+58
-9
lines changed

2 files changed

+58
-9
lines changed

Assignment1/src/server/Client1.java

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.io.IOException;
66
import java.rmi.AlreadyBoundException;
77
import java.rmi.NotBoundException;
8+
import java.rmi.RMISecurityManager;
89
import java.rmi.registry.LocateRegistry;
910
import java.rmi.registry.Registry;
1011
import java.util.ArrayList;
@@ -36,12 +37,30 @@ public class Client1 {
3637
private static ArrayList<Integer> localIDS; // ids of local processes
3738

3839
public static void main(String[] args) throws AlreadyBoundException, NotBoundException, IOException, InterruptedException {
39-
Registry registry = LocateRegistry.getRegistry("localhost", Constant.RMI_PORT);
40+
/*if (System.getSecurityManager() == null) {
41+
System.setSecurityManager(new RMISecurityManager());
42+
}*/
43+
//Registry registry = LocateRegistry.getRegistry("localhost", Constant.RMI_PORT);
44+
Registry registry = LocateRegistry.createRegistry(Constant.RMI_PORT);
4045

4146
// "clients" files contain the name of the remote processes used
4247
BufferedReader br = new BufferedReader(new FileReader("tests/clients3.txt"));
4348
String line = "";
44-
numProc = registry.list().length;
49+
numProc = 0; // added later
50+
while ((line = br.readLine()) != null) {
51+
String[] split_line = line.split(" ");
52+
if(Integer.parseInt(split_line[1]) == 1){
53+
registry.bind("//localhost:"+Constant.RMI_PORT+"/"+split_line[0], new RemoteEntityImpl());
54+
}
55+
/*else{
56+
registry.bind("//145.94.185.243:"+Constant.RMI_PORT+"/"+split_line[0], new RemoteEntityImpl());
57+
}*/
58+
numProc++;
59+
}
60+
br.close();
61+
br = new BufferedReader(new FileReader("tests/clients3.txt"));
62+
line = "";
63+
//numProc = registry.list().length;
4564
localProc = 0;
4665
int i = 0;
4766
local = new int[numProc];
@@ -58,6 +77,9 @@ public static void main(String[] args) throws AlreadyBoundException, NotBoundExc
5877
}
5978
br.close();
6079
setRegistry();
80+
System.out.println("Press enter to continue");
81+
Scanner scan = new Scanner(System.in);
82+
scan.nextLine();
6183
System.out.println("Client1 started");
6284
}
6385

@@ -66,7 +88,10 @@ public static void setRegistry() throws NotBoundException, NumberFormatException
6688
RMI_IDS = new IRemoteEntity[numProc]; // the remote process array is instantiated
6789
Thread[] myThreads = new Thread[localProc]; // and localProc number of threads are created
6890
for(int i=0; i<numProc; i++){
69-
RMI_IDS[i] = (IRemoteEntity) registry.lookup(registry.list()[i]);
91+
if (local[i]==1)
92+
RMI_IDS[i] = (IRemoteEntity) registry.lookup(registry.list()[i]);
93+
else
94+
RMI_IDS[i] = (IRemoteEntity) java.rmi.Naming.lookup("//145.94.185.243:"+Constant.RMI_PORT+"/Client"+(i+1));
7095
}
7196

7297
// "messages" files contain the messages to be sent and are constructed in the following way

Assignment1/src/server/Client2.java

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,29 @@ public class Client2 {
3636
private static ArrayList<Integer> localIDS; // ids of local processes
3737

3838
public static void main(String[] args) throws AlreadyBoundException, NotBoundException, IOException, InterruptedException {
39-
Registry registry = LocateRegistry.getRegistry("localhost", Constant.RMI_PORT);
40-
41-
// "clients" files contain the name of the remote processes used
42-
BufferedReader br = new BufferedReader(new FileReader("tests/clients3.txt"));
43-
String line = "";
39+
/*if (System.getSecurityManager() == null) {
40+
System.setSecurityManager(new RMISecurityManager());
41+
}*/
42+
//Registry registry = LocateRegistry.getRegistry("localhost", Constant.RMI_PORT);
43+
Registry registry = LocateRegistry.createRegistry(Constant.RMI_PORT);
44+
45+
// "clients" files contain the name of the remote processes used
46+
BufferedReader br = new BufferedReader(new FileReader("tests/clients3.txt"));
47+
String line = "";
48+
numProc = 0; // added later
49+
while ((line = br.readLine()) != null) {
50+
String[] split_line = line.split(" ");
51+
if(Integer.parseInt(split_line[1]) == 2){
52+
registry.bind("//localhost:"+Constant.RMI_PORT+"/"+split_line[0], new RemoteEntityImpl());
53+
}
54+
/*else{
55+
registry.bind("//145.94.185.243:"+Constant.RMI_PORT+"/"+split_line[0], new RemoteEntityImpl());
56+
}*/
57+
numProc++;
58+
}
59+
br.close();
60+
br = new BufferedReader(new FileReader("tests/clients3.txt"));
61+
line = "";
4462
numProc = registry.list().length;
4563
localProc = 0;
4664
int i = 0;
@@ -58,6 +76,9 @@ public static void main(String[] args) throws AlreadyBoundException, NotBoundExc
5876
}
5977
br.close();
6078
setRegistry();
79+
System.out.println("Press enter to continue");
80+
Scanner scan = new Scanner(System.in);
81+
scan.nextLine();
6182
System.out.println("Client2 started");
6283
}
6384

@@ -66,7 +87,10 @@ public static void setRegistry() throws NotBoundException, NumberFormatException
6687
RMI_IDS = new IRemoteEntity[numProc]; // the remote process array is instantiated
6788
Thread[] myThreads = new Thread[localProc]; // and localProc number of threads are created
6889
for(int i=0; i<numProc; i++){
69-
RMI_IDS[i] = (IRemoteEntity) registry.lookup(registry.list()[i]);
90+
if (local[i]==1)
91+
RMI_IDS[i] = (IRemoteEntity) registry.lookup(registry.list()[i]);
92+
else
93+
RMI_IDS[i] = (IRemoteEntity) java.rmi.Naming.lookup("//145.94.185.243:"+Constant.RMI_PORT+"/Client"+(i+1));
7094
}
7195

7296
// "messages" files contain the messages to be sent and are constructed in the following way

0 commit comments

Comments
 (0)
0