-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRingDHT.h
547 lines (528 loc) · 17.4 KB
/
RingDHT.h
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
#pragma once
#include<cstdlib>
#include<ctime>
#include<string>
#include<cmath>
#include"FingerTable.h"
#include"AVLtree.h"
#include"FunctionUtil.h"
using namespace std;
// Dht Node where you have to change the data members
template <typename T>
struct dhtNode
{
T id;
RoutingTable<T>FingerTable;
AVL<T> avlTree;
dhtNode* next;
dhtNode()
{
next = NULL;
}
dhtNode(T i)
{
id = i;
next = NULL;
}
};
// DHT Class Implemenation
// A circular Linklist holding Each of the Machine.
template <typename T>
class DHT {
public:
dhtNode<T>* head, * tail;
int machines;
int BitSpace;
DHT()
{
head = NULL;
}
DHT(int Machine, int Space, bool IDAssignment)
{
machines = Machine;
BitSpace = Space;
head = tail = NULL;
assign_ids(IDAssignment);
buildRoutingdtable();
}
// This function make a machine in the Ring
void Insert(T val)
{
dhtNode<T>* temp = new dhtNode<T>(val);
if (head == NULL)
{
head = tail = temp;
tail->next = head;
}
//Insertion at the Head Position
else if (head->id > temp->id)
{
tail->next = temp;
temp->next = head;
head = temp;
}
// Insertion At the End
else if (tail->id < temp->id)
{
temp->next = head;
tail->next = temp;
tail = temp;
}
// Insertion in the Middle
else
{
dhtNode<T>* ptr = head;
while (ptr != tail)
{
if (ptr->next->id > temp->id)
break;
ptr = ptr->next;
}
temp->next = ptr->next;
ptr->next = temp;
}
}
// This function Build a Routing table for each Node
void buildRoutingdtable()
{
dhtNode<T>* ptr = head, * temp, * flag;
do
{
for (int i = 1; i <= BitSpace; i++)
{
temp = ptr;
T str = ResolveID(ptr->id, i, BitSpace);
bool con = false;
dhtNode<T>* temporary = head;
do {
if (str <= temporary->id) {
temp->FingerTable.insert(temporary);
con = true;
break;
}
temporary = temporary->next;
} while (temporary != head);
if (con == false)
temp->FingerTable.insert(head);
}
ptr = ptr->next;
} while (ptr != head);
}
T ResolveID(T a, int Number, int Bits)
{
string res = "";
int s = 0;
string b = To_Binary_String(pow(2, Number - 1), Bits);
int i = a.size() - 1, j = b.size() - 1;
while (i >= 0 || j >= 0 || s == 1)
{
s += ((i >= 0) ? a[i] - '0' : 0);
s += ((j >= 0) ? b[j] - '0' : 0);
res = char(s % 2 + '0') + res;
s /= 2;
i--; j--;
}
if (a.size() != res.size())
{
string final = "";
int res_len = res.size();
for (int i = res.size() - a.size(); i < res.size(); i++)
{
final = final + res[i];
}
res = final;
}
return res;
}
dhtNode<T>* findThroughRountingTable(int MachineNo, string key)
{
string binary = To_Binary_String(MachineNo, BitSpace);
SinglyListNode<T>* ptr = head->FingerTable.head;
cout << "Machine ID form where Insertion Takes:: " << MachineNo << endl;
dhtNode<T>* tail, * temp = head;
do {
tail = temp;
temp = temp->next;
} while (temp != head);
// Implementation of Arriving on that machine Using FingerTable
if (head->id != binary)
{
cout << "Path to Reach on the Entered Machine :: ";
do {
cout << To_Integer(temp->id) << "->";
if (ptr->Pointer->id == binary)
break;
while (ptr != NULL)
{
if (ptr->Pointer == head || binary <= ptr->Pointer->id)
break;
temp = ptr->Pointer;
ptr = ptr->next;
}
ptr = temp->FingerTable.head;
} while (true);
cout << To_Integer(ptr->Pointer->id) << endl;
temp = ptr->Pointer;
}
// Finding the Machine number on Which Data is to be Inserted
binary = Gethashedvalue<T>(key, BitSpace);
cout << "Hashed Value of the key to Reach :: " << To_Integer(binary) << endl;
if (temp == head && binary < temp->id)
{
cout << "Path to Reach on machine Responsible for Hashed Key" << endl;
cout << To_Integer(temp->id) << endl;
}
else
{
if (temp->id != binary)
{
ptr = temp->FingerTable.head;
bool con, con2, con3;
binary == tail->id ? con2 = true : con2 = false;
binary > tail->id ? con3 = true : con3 = false;
temp->id > binary ? con = false : con = true;
cout << "Path to Reach on machine Responsible for Hashed Key" << endl;
do {
cout << To_Integer(temp->id) << "->";
if (binary <= ptr->Pointer->id && con)
break;
if (con3 && ptr->Pointer == head)
{
break;
}
while (ptr != NULL)
{
if (ptr->Pointer->id <= head->next->id)
con = true;
if ((binary <= ptr->Pointer->id && con))
break;
if (con2 && ptr->Pointer->id < temp->id)
break;
if (ptr->Pointer == head && con3)
break;
temp = ptr->Pointer;
ptr = ptr->next;
}
ptr = temp->FingerTable.head;
} while (true);
cout << To_Integer(ptr->Pointer->id) << endl;
temp = ptr->Pointer;
}
}
return temp;
}
void InsertKeyValuePair(int MachineNo, string key, string Value)
{
string binary = Gethashedvalue<T>(key, BitSpace);
dhtNode<T>* temp = findThroughRountingTable(MachineNo, key);
// Inserting the Data on the Required
string c = "./textFiles/" + temp->id + ".txt";
temp->avlTree.root = temp->avlTree.insert(binary, c, Value, temp->avlTree.root);
}
void deleteValue(int MachineID, string key)
{
T binary = Gethashedvalue<T>(key, BitSpace);
dhtNode<T>* temp = findThroughRountingTable(MachineID, key);
temp->avlTree.removeAVLNodeAndBalance(binary, temp->avlTree.root);
}
// Assigning ids and it takes a boolean as a parameter
// as we want to automatically assign ids or enter from user
void assign_ids(bool takeInput)
{
if (takeInput == false)
{
string s = "a";
T ID;
for (int i = 1; i <= machines; i++)
{
bool Con = false;
ID = Gethashedvalue<T>(s, BitSpace);
// Check for a Duplicate Value While Assigning Number ID
// Regenerating the Automatic Hash in Case of Duplicate
dhtNode<T>* flag = head;
if (flag != NULL)
{
do {
if (flag->id == ID)
Con = true;
flag = flag->next;
} while (flag != head);
}
if (Con)
{
i--;
}
else {
Insert(ID);
}
string fileID = "./textFiles/" + ID + "_0.txt";
ofstream openFile;
openFile.open(fileID);
openFile.close();
s += "f";
}
}
else {
int choice;
for (int i = 1; i <= machines; i++)
{
cout << "Assign ID to Machine " << i << " :: ";
cin >> choice;
T ID = To_Binary_String(choice, BitSpace);
Insert(ID);
string fileID = "./textFiles/" + ID + "_0.txt";
ofstream openFile(fileID);
openFile.close();
}
}
}
// Inserting A new machine to the System
void InsertMachine(int ID)
{
T MachineID = To_Binary_String(ID, BitSpace);
dhtNode<T>* temp, * ptr = head;
bool check = true;
//Checking if such Machine Already Exit
do {
if (ptr->id == MachineID)
check = false;
ptr = ptr->next;
} while (ptr != head);
// Insertion Take Place if machine doesn't already Exit
if (check)
{
Insert(MachineID);
ptr = head;
do
{
if (ptr->id == MachineID)
break;
ptr = ptr->next;
} while (ptr != head);
ofstream openFile("./textFiles/" + MachineID + "_0.txt");
bool con = true;
while (con)
con = InsertAndUpdateAVL(ptr->id, ptr->next->id, (ptr->avlTree), (ptr->next->avlTree), ptr->next->avlTree.root);
cout << "The Updated AVl of the inserted Machine ::" << To_Integer(ptr->id) << endl;
ptr->avlTree.Display(ptr->avlTree.root);
temp = head;
do
{
temp->FingerTable.clear();
temp = temp->next;
} while (temp != head);
buildRoutingdtable();
}
else
cout << "Duplicate Machine are not allowed in the Ring DHT" << endl;
}
bool InsertAndUpdateAVL(T id1, T id2, AVL<T>& AVLtree1, AVL<T>& AVLtree2, AVLNode<T>* RootNode)
{
if (RootNode != NULL)
{
if (RootNode->key > id2 || RootNode->key <= id1)
{
string c = "./textFiles/" + id1 + ".txt";
string fileName = RootNode->fileName;
int fileNo = RootNode->lineNumber;
string data;
ifstream p(fileName);
getline(p, data);
for (int i = 1; i <= fileNo; i++)
getline(p, data);
AVLtree1.root = AVLtree1.insert(RootNode->key, c, data, AVLtree1.root);
AVLtree2.removeAVLNodeAndBalance(RootNode->key, AVLtree2.root);
return true;
}
if (RootNode->right)
{
if (RootNode->right->key <= id1 || RootNode->right->key > id2)
{
string c = "./textFiles/" + id1 + ".txt";
string fileName = RootNode->right->fileName;
int fileNo = RootNode->right->lineNumber;
string data;
ifstream p(fileName);
getline(p, data);
for (int i = 1; i <= fileNo; i++)
getline(p, data);
AVLtree1.root = AVLtree1.insert(RootNode->right->key, c, data, AVLtree1.root);
AVLtree2.removeAVLNodeAndBalance(RootNode->right->key, AVLtree2.root);
return true;
}
}
if (RootNode->left)
{
if (RootNode->left->key > id2 || RootNode->left->key <= id1)
{
string c = "./textFiles/" + id1 + ".txt";
string fileName = RootNode->left->fileName;
int fileNo = RootNode->left->lineNumber;
string data;
ifstream p(fileName);
getline(p, data);
for (int i = 1; i <= fileNo; i++)
getline(p, data);
AVLtree1.root = AVLtree1.insert(RootNode->left->key, c, data, AVLtree1.root);
AVLtree2.removeAVLNodeAndBalance(RootNode->left->key, AVLtree2.root);
return true;
}
}
return false;
}
return false;
}
// Deletion of a node from the Circular list
void deleteCircularNode(T ID)
{
if (head == NULL)
return;
if (head->id == ID && head->next == head)
{
delete head;
head = NULL;
return;
}
dhtNode<T>* flag, * temp = head;
if (head->id == ID)
{
while (temp->next != head)
temp = temp->next;
temp->next = head->next;
delete head;
head = temp->next;
return;
}
while (temp->next != head && temp->next->id != ID)
{
temp = temp->next;
}
if (temp->next->id == ID)
{
flag = temp->next;
temp->next = flag->next;
delete flag;
}
else
cout << "no such Machine found in the DHT";
}
// Insert a Machine to the System and Updating files Accordingly
bool DeleteMachine(int ID)
{
string MachineID = To_Binary_String(ID, BitSpace);
bool check;
dhtNode<T>* ptr = head;
do
{
if (ptr->id == MachineID)
{
check = true;
break;
}
ptr = ptr->next;
} while (ptr != head);
if (check)
{
bool con = true;
//for Removal of files of that machines
string c = "./textFiles/" + ptr->id + "_0.txt";
char* file = new char[c.size()];
for (int i = 0; i < c.size(); i++)
file[i] = c[i];
// AVL Removal and insertin in the next c
if (ptr->avlTree.root)
{
while (con)
con = DeleteMAchineAndUpdateAvl(ptr->id, ptr->next->id, (ptr->avlTree), (ptr->next->avlTree), ptr->avlTree.root);
}
// Removal of the file
remove(file);
delete[]file;
T flag1 = ptr->id;
// Clearance of the finger table.
ptr = head;
do {
ptr->FingerTable.clear();
ptr = ptr->next;
} while (ptr != head);
// Deletion of the Node from DHT circular link.
deleteCircularNode(flag1);
//Rebuilding of the Routing tables
buildRoutingdtable();
return true;
}
return false;
}
// Deleting a machine from the System and Updating files accordingly
bool DeleteMAchineAndUpdateAvl(T id1, T id2, AVL<T>& AVLtree1, AVL<T>& AVLtree2, AVLNode<T>* RootNode)
{
if (RootNode)
{
string c = "./textFiles/" + id2 + ".txt";
string fileName = RootNode->fileName;
int fileNo = RootNode->lineNumber;
string data;
ifstream p(fileName);
getline(p, data);
for (int i = 1; i <= fileNo; i++)
getline(p, data);
AVLtree2.root = AVLtree2.insert(RootNode->key, c, data, AVLtree2.root);
AVLtree1.removeAVLNodeAndBalance(RootNode->key, AVLtree1.root);
return true;
}
return false;
}
// Searching the value Using it Key and Machine Number
void searchNode(string key, int MachineID)
{
dhtNode<T>* temp = findThroughRountingTable(MachineID, key);
T binary = Gethashedvalue<T>(key, BitSpace);
// Searching the Value in the Reached Machine
cout << "Value: ";
temp->avlTree.Search(temp->avlTree.root, binary);
cout << endl;
}
// Displays the Each Machine in the Ring DHT with their Machine ID.
void Display()
{
if (head == NULL)
return;
dhtNode<T>* temp = head;
do
{
cout << "Machine ID ::" << To_Integer(temp->id) << endl;
temp = temp->next;
} while (temp != head);
}
void displayRoutingTable(int machineNumber)
{
string machineBinary = To_Binary_String(machineNumber, BitSpace);
dhtNode<T>* temp = head;
do {
if (machineBinary == temp->id)
{
cout << "Routing table of entered Machine ID :: ";
temp->FingerTable.display();
break;
}
temp = temp->next;
} while (temp != head);
}
// This function Printing the Avl Matained on any Key
void displayAVLTree(int machineNumber)
{
string machineBinary = To_Binary_String(machineNumber, BitSpace);
dhtNode<T>* temp = head;
do {
if (temp->id == machineBinary)
{
cout << "The Machine data stored on Tree is: \n";
temp->avlTree.Display(temp->avlTree.getRoot());
cout << endl;
return;
}
temp = temp->next;
} while (temp != head);
cout << "Machine Not Found!\n";
}
};