Skip to content

Commit 16a4886

Browse files
committed
Add launcher scripts and config parser
0 parents  commit 16a4886

7 files changed

+173
-0
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## Leader Election
2+
3+
This repository contains the implementation of Peleg's leader election algorithm.

cleanup.sh

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/usr/bin/env bash
2+
3+
4+
# Change this to your netid
5+
netid=kxp210004
6+
7+
8+
# Root directory of your project
9+
PROJECT_DIR=$HOME/Desktop/LeaderElection
10+
11+
12+
# Directory where the config file is located on your local system
13+
CONFIG_LOCAL=$PROJECT_DIR/config.txt
14+
15+
n=0
16+
17+
cat $CONFIG_LOCAL | sed -e "s/#.*//" | sed -e "/^\s*$/d" |
18+
(
19+
read i
20+
echo $i
21+
while [[ $n -lt $i ]]
22+
do
23+
read line
24+
host=$( echo $line | awk '{ print $2 }' )
25+
26+
echo $host
27+
osascript -e '
28+
tell app "Terminal"
29+
do script "ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no '$netid@$host' killall -u '$netid'"
30+
end tell'
31+
sleep 1
32+
33+
n=$(( n + 1 ))
34+
done
35+
36+
)
37+
38+
39+
echo "Cleanup complete"

config.txt

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# number of nodes in the system
2+
5
3+
4+
# nodeUID hostName listeningPort
5+
123 dc02 2234
6+
5 dc03 3233
7+
23 dc04 5217
8+
1047 dc05 2432
9+
89 dc06 6221
10+
11+
# space delimited list of neighbors for each node
12+
5 23
13+
123 1047
14+
123 1047 89
15+
5 23 89
16+
23 1047

launcher-mac.sh

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#!/usr/bin/env bash
2+
3+
# Change this to your netid
4+
netid=kxp210004
5+
6+
# Root directory of your project
7+
PROJECT_DIR=/home/013/k/kx/kxp210004/LeaderElection
8+
9+
# Directory where the config file is located on your local system
10+
CONFIG_LOCAL=$HOME/Desktop/LeaderElection/config.txt
11+
12+
# Directory your java classes are in
13+
BINARY_DIR=$PROJECT_DIR/bin
14+
15+
# Your main project class
16+
PROGRAM=Main
17+
18+
i=0
19+
20+
declare -A uidHostMap
21+
22+
cat $CONFIG_LOCAL | sed -e "s/#.*//" | sed -e "/^\s*$/d" |
23+
(
24+
mapfile -t configFile
25+
26+
totalNodes=${configFile[0]}
27+
counter=0
28+
29+
while [[ $counter -lt $totalNodes ]]
30+
do
31+
line=(${configFile[$((counter))]})
32+
uid="${line[0]}"
33+
host="${line[1]}"
34+
port="${line[2]}"
35+
36+
uidHostMap[$uid]="$host,$port"
37+
38+
counter=$((counter + 1))
39+
done
40+
41+
# Uncomment to debug the values in the uidHostMap hashmap.
42+
# for key in ${!uidHostMap[@]}; do
43+
# echo ${key} ${uidHostMap[${key}]}
44+
# done
45+
46+
i=1
47+
while [[ $i -lt $totalNodes ]]
48+
do
49+
line=(${configFile[$((i))]})
50+
uid="${line[0]}"
51+
host="${line[1]}"
52+
port="${line[2]}"
53+
54+
echo $uid $host $port
55+
56+
neighborsArray=(${configFile[$i + totalNodes]})
57+
neighCounter=0
58+
59+
for neighborUID in ${neighborsArray[@]}; do
60+
neighborsArray[$neighCounter]=${uidHostMap[${neighborUID}]}
61+
neighCounter=$(( neighCounter+1 ))
62+
done
63+
64+
neighbors=$(printf "_%s" "${neighborsArray[@]}")
65+
66+
echo "Neighbors ${neighbors}"
67+
68+
osascript -e '
69+
tell app "Terminal"
70+
do script "ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no '$netid@$host' java -cp '$BINARY_DIR' '$PROGRAM' '$uid' '$host' '$port' '$neighbors'"
71+
end tell'
72+
73+
i=$(( i + 1 ))
74+
done
75+
)

src/ConfigParser.java

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import java.util.Arrays;
2+
import java.util.HashMap;
3+
import java.util.List;
4+
import java.util.Map;
5+
6+
public class ConfigParser{
7+
public String uid;
8+
public String host;
9+
public String port;
10+
public Map<String, String> neighbors = new HashMap<>();
11+
12+
ConfigParser(String[] args) {
13+
uid = args[0];
14+
host = args[1];
15+
port = args[2];
16+
17+
List<String> neighborsDetails = Arrays.asList(args[3].split("_"));
18+
neighborsDetails = neighborsDetails.subList(1, neighborsDetails.size());
19+
20+
for (String detail: neighborsDetails) {
21+
String[] detailArray = detail.split(",");
22+
neighbors.put(detailArray[0], detailArray[1]);
23+
}
24+
}
25+
26+
}

src/LeaderElection.java

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
public class LeaderElection {
3+
4+
}

src/Main.java

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
public class Main {
3+
public static void main(String[] args) {
4+
ConfigParser config = new ConfigParser(args);
5+
6+
System.out.println("UID " + config.uid);
7+
System.out.println("Host Name " + config.host);
8+
System.out.println("Port " + config.port);
9+
}
10+
}

0 commit comments

Comments
 (0)