-
Notifications
You must be signed in to change notification settings - Fork 0
/
FingerTableGen.java
165 lines (129 loc) · 3.98 KB
/
FingerTableGen.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is furnished to do
* so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
/**
* Class to generate Finger Tables , Predecessor and Successor pointers automatically
* @author Hoang-Duong Nguyen
*
*/
public class FingerTableGen {
private static final int START_REMOTE_PORT = 10003;
private static int num;
private static int p;
private static boolean isSingleServer = false;
//private static int[] ids = {2,7,8,29,33,37,48,51,60,63};
private static int[] ids;
//private static int[] pre = {63,2,7,8,29,33,37,48,51,60};
private static int[] pre;
//private static int[] succ = {7,8,29,33,37,48,51,60,63,2};
private static int[] succ;
private static int[] ports;
public static void main(String[] args){
if(args == null || args.length == 0)
num = args.length;
else
num = args.length;
if(num == 0){
num = 10;
// No arguments => 10 default servers
ids = new int[]{2,7,8,29,33,37,48,51,60,63};
pre = new int[]{63,2,7,8,29,33,37,48,51,60};
succ = new int[]{7,8,29,33,37,48,51,60,63,2};
ports = new int[10];
p = START_REMOTE_PORT;
for(int i = 0; i<10; i++){
ports[i] = p;
p = p + 3;
}
p = START_REMOTE_PORT - 2;
}
else{
// In case there is only 1 server
if(num == 1){
isSingleServer = true;
num = 2;
ids = new int []{Integer.parseInt(args[0]),Integer.parseInt(args[0])};
}
else{
ids = new int [num];
for (int i=0; i < num; i++){
ids[i] = Integer.parseInt(args[i]);
}
}
// else create requested servers
pre = new int [num];
succ = new int [num];
ports = new int[num];
// Initialized server IDs
pre[0] = ids[num-1];
succ[0] = ids[1];
for (int j = 1; j < num-1; j++){
pre[j] = ids[j-1];
succ[j]= ids[j+1];
}
pre[num-1] = ids[num-2];
succ[num-1] = ids[0];
// Initialize port list
p = START_REMOTE_PORT;
for(int i = 0; i<num; i++){
ports[i] = p;
p = p + 3;
}
p = START_REMOTE_PORT - 2;
if(isSingleServer)
num = 1;
}
printStart();
}
/**
* Compute the Finger Tables and Print out the content of method start() to run 10 servers
**/
private static void printStart(){
for(int i = 0; i < num; i++){
System.out.println("» Server " + i + "\n");
for (int j=0; j < 6; j++){
System.out.println(j + " " + suc(ids[i],j));
}
System.out.println("Successor: " + succ[i]);
System.out.println("Predecessor: " + pre[i]);
System.out.println("\n__________________________\n");
}
}
/**
* Return the successor according to the given id and offset
**/
private static int suc(int id, int offset){
int targ = (id + (int)Math.pow(2, (double) offset)) % ((int)Math.pow(2, (double) 6));
for(int i = 0; i<num; i++){
if(ids[i] >= targ)
return ids[i];
}
return ids[0];
}
/**
* Return the port of the given server ID
**/
private static int getPort(int id){
for(int i=0; i<num; i++){
if(ids[i]==id)
return ports[i];
}
return 0;
}
}