-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChord.cpp
302 lines (281 loc) · 8.59 KB
/
Chord.cpp
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*------------------------------------------------------
*
* Chord.cpp
*
* Project : Chord_DHT
* Name : Chong Guo
* Student ID : 301295753
* Language : C++
* SFU username : armourg
*
* Created by Armour on 25/10/2015
* Copyright (c) 2015 Armour. All rights reserved.
*
*------------------------------------------------------
*/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iostream>
#include <fstream>
#define HASH_SPACE (s + 1)
#define MAX_FINGER_NUM floor(log2(HASH_SPACE))
using namespace std;
typedef struct Finger {
int number;
int expect_succ; /* Expected successor number */
int real_succ; /* Real successor number */
} Finger;
typedef struct Key {
int value; /* Key value */
struct Key *next; /* Stored as lists */
} Key;
typedef struct SuccTable {
int count;
Finger **f; /* This is the fingers of this node */
Key *k; /* This is the keys that stored in this node */
} SuccTable;
int s, n, m;
int *exist_key; /* Used to store existed key */
SuccTable **st; /* An list of SuccTable */
FILE *fin, *fout;
char *file_in, *file_out; /* The file name of input and output file */
/*
* Function: Create_succ_table
* -------------------
* This function is used to create a successor table of one ndoe
*
* Parameters:
* id: the index of one node
*
* Returns:
* void
*/
void create_succ_table(int id) {
st[id] = (SuccTable *)malloc(sizeof(SuccTable));
st[id]->f = (Finger **)malloc(sizeof(Finger *) * MAX_FINGER_NUM);
for (int i = 0; i < MAX_FINGER_NUM; i++) {
st[id]->f[i] = (Finger *)malloc(sizeof(Finger));
st[id]->f[i]->number = i;
st[id]->f[i]->expect_succ = (id + (int)pow(2, (double)i)) % HASH_SPACE; /* Get expect successor number */
for (int k = st[id]->f[i]->expect_succ; ; k++) {
if (k == HASH_SPACE) k = 0;
if (st[k] != NULL) {
st[id]->f[i]->real_succ = k; /* Get real successor number */
break;
}
}
}
st[id]->k = (Key *)malloc(sizeof(Key));
st[id]->k->value = -1;
st[id]->k->next = NULL;
}
/*
* Function: Update_succ_table
* -------------------
* This function is used to update successor table
*
* Parameters:
* id: the index of the node that need to update
*
* Returns:
* void
*/
void update_succ_table(int id) {
for (int i = 0; i < MAX_FINGER_NUM; i++) {
for (int k = st[id]->f[i]->expect_succ; ; k++) {
if (k == HASH_SPACE) k = 0;
if (st[k] != NULL) {
st[id]->f[i]->real_succ = k; /* Update real successor number */
break;
}
}
}
}
/*
* Function: Print_succ_table
* -------------------
* This function is used to print the state of one successor table,
* mainly used for debugging
*
* Parameters:
* no parameters
*
* Returns:
* void
*/
void print_succ_table(void) {
for (int id = 0; id < HASH_SPACE; id++) {
if (st[id]) {
cout << "Finger: " << endl;
for (int i = 0; i < MAX_FINGER_NUM; i++) { /* Print all the fingers */
cout << id << " " << i << " " << st[id]->f[i]->real_succ << endl;
}
cout << "Key: " << endl;
Key *p = st[id]->k;
cout << p->value << " ";
while (p->next != NULL) {
p = p->next;
cout << p->value << " "; /* Print all the keys */
}
cout << endl;
}
}
}
/*
* Function: Store_key
* -------------------
* This function is used to store one key to one node,
* it will automatically choose where to store this key
*
* Parameters:
* key: the key that we want to store
*
* Returns:
* void
*/
void store_key(int key) {
int i = key;
while (st[i] == NULL) { /* Find a exist node */
i++;
if (i == HASH_SPACE)
i = 0;
}
Key *p = st[i]->k;
while (p->next != NULL) /* Until find the end */
p = p->next;
p->next = (Key *)malloc(sizeof(Key));
p->next->value = key; /* Add key */
p->next->next = NULL;
}
/*
* Function: Chord_query
* -------------------
* This function is used to query one key's position
*
* Parameters:
* id: the node who give a query
* key: the asked key
* path: counter for how many hops has already been made
*
* Returns:
* void
*/
void chord_query(int id, int key, int path) {
int i;
Key *p = st[id]->k;
while (p->next != NULL) {
p = p->next;
if (p->value == key) { /* If found that key in this node */
fprintf(fout, "%d\n", path);
return;
}
}
for (i = MAX_FINGER_NUM - 1; i > 0; i--) {
int succ = st[id]->f[i]->real_succ; /* Get the real successor node */
if (succ < id) succ += HASH_SPACE;
if (succ <= (id < key? key: key + HASH_SPACE))
break;
}
chord_query(st[id]->f[i]->real_succ, key, path + 1); /* If not found, go next ndoe */
}
/*
* Function: Get_output_file
* -------------------
* This function is used to get the name of the output file
*
* Parameters:
* input: the name of the input file
*
* Returns:
* the name of the output file
*/
char *get_output_file(char *input) {
char *output;
output = (char *)malloc(sizeof(char) * 1024);
strcpy(output, input);
char *end = output + strlen(output) - 1;
while (end > output) {
if (*end == '.') { /* If found extension name */
*(end + 1) = 'o';
*(end + 2) = 'u';
*(end + 3) = 't';
*(end + 4) = '\0';
return output; /* Change extension name to .out and return it */
}
end--;
}
end = output + strlen(output) - 1; /* If not found extension name */
*(end + 1) = '.';
*(end + 2) = 'o';
*(end + 3) = 'u';
*(end + 4) = 't';
*(end + 5) = '\0'; /* Just add .out extension at the end of file name */
return output;
}
/*
* Function: Main
* -------------------
* The main function
*
* Parameters:
* argc: parameter count
* argv[]: parameter array
*
* Returns:
* void
*/
int main() {
int id, key;
file_in = (char *)malloc(sizeof(char) * 1024);
cout << "Please input a file name:" << endl;
scanf("%s", file_in); /* Get input file name */
fin = fopen(file_in, "r+");
if (!fin) { /* If input file is not exist or can't open */
cout << "Can't open input file " << file_in << "! " << endl;
return 1;
}
file_out = get_output_file(file_in);
fout = fopen(file_out, "w");
fscanf(fin, "%d", &s);
fscanf(fin, "%d %d", &n, &m);
if (n == 0) { /* There is no joined node */
cout << "No joined node!" << endl;
fprintf(fout, "No joined node!");
return 0;
}
exist_key = (int *)malloc(sizeof(int) * HASH_SPACE);
for (int i = 0; i < HASH_SPACE; i++) {
exist_key[i] = 0; /* Initial exist key array */
}
st = (SuccTable **)malloc(sizeof(SuccTable *) * (s + 1));
for (int i = 0; i < HASH_SPACE; i++)
st[i] = NULL; /* Initial all node to not-joined state */
for (int i = 0; i < n; i++) {
fscanf(fin, "%d", &id);
create_succ_table(id); /* Join one node and create successor table for that node */
for (int j = 0; j < HASH_SPACE; j++)
if (st[j] && j != id)
update_succ_table(j); /* Update other nodes' successor table */
}
for (int i = 0; i < m; i++) {
fscanf(fin, "%d", &key);
exist_key[key] = 1;
store_key(key); /* Store key */
}
//print_succ_table();
while (1) {
fscanf(fin, "%d %d", &key, &id);
if (key == -1 && id == -1) /* Until end */
break;
if (st[id] == NULL) /* If that query node is not exist */
fprintf(fout, "Node %d is not exist!\n", id);
else if (!exist_key[key]) { /* If this key is not exist */
fprintf(fout, "Can't find this key!\n");
} else {
chord_query(id, key, 0); /* Answer query */
}
}
cout << "Done!" << endl;
}