Skip to content

Commit c6b7e52

Browse files
committed
Added some corner cases to the routing algorithm
1 parent b54a3c8 commit c6b7e52

1 file changed

Lines changed: 46 additions & 2 deletions

File tree

DS_Project/Machines.h

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,50 @@ class Machines {
404404
return false;
405405
}
406406

407+
/*
408+
This function returns true if the data key is greater than the current machine
409+
it was made seperate to cater the circular property of the identifier space
410+
*/
411+
bool isDataKeyGreater(T key, Machine_Node<D, T>* currentMachine)
412+
{
413+
bool result = false;
414+
if (key > currentMachine->data)
415+
{
416+
result = true;
417+
}
418+
else if (isLastMachine(currentMachine->data) == true)
419+
{
420+
Machine_Node<D, T>* nextMachine = getSuccessorMachine(currentMachine->data);
421+
if (key < nextMachine->data)
422+
{
423+
result = true;
424+
}
425+
}
426+
return result;
427+
}
428+
429+
/*
430+
This function returns true if the data key is lesser than the current machine
431+
it was made seperate to cater the circular property of the identifier space
432+
*/
433+
bool isDataKeyLesser(T key, Machine_Node<D, T>* currentMachine)
434+
{
435+
bool result = false;
436+
if (key < currentMachine->data)
437+
{
438+
result = true;
439+
}
440+
else if (isFirstMachine(currentMachine->data) == true)
441+
{
442+
Machine_Node<D, T>* prevMachine = getPredecessorMachine(currentMachine->data);
443+
if (key > prevMachine->data)
444+
{
445+
result = true;
446+
}
447+
}
448+
return result;
449+
}
450+
407451
/*
408452
This function takes hashed key of data and machine from the Ring_DHT class and then
409453
performs the search according to the given keys
@@ -454,7 +498,7 @@ class Machines {
454498
cout << "-----------------------------------------------" << endl << endl;
455499
return currentMachine;
456500
}
457-
else if (i == 0 && dataKey > currentMachine->data && dataKey <= routingTableMachine->data)
501+
else if (i == 0 && isDataKeyGreater(dataKey, currentMachine) == true && (isDataKeyLesser(dataKey, routingTableMachine) == true || dataKey == routingTableMachine->data))
458502
{
459503
cout << "| Routing From Machine: " << setfill('0') << setw(3) << currentMachine->data << " -> " << setfill('0') << setw(3) << routingTableMachine->data << endl;
460504
currentMachine = routingTableMachine;
@@ -464,7 +508,7 @@ class Machines {
464508
else if (i < routingTableSize - 1)
465509
{
466510
Machine_Node<D, T>* routingTableNextMachine = static_cast<Machine_Node<D, T>*>(currentMachine->routingTable->getElement(i + 1));
467-
if (dataKey > routingTableMachine->data && dataKey <= routingTableNextMachine->data)
511+
if (isDataKeyGreater(dataKey, routingTableMachine) == true && (isDataKeyLesser(dataKey, routingTableNextMachine) == true || dataKey == routingTableNextMachine->data))
468512
{
469513
cout << "| Routing From Machine: " << setfill('0') << setw(3) << currentMachine->data << " -> " << setfill('0') << setw(3) << routingTableMachine->data << endl;
470514
currentMachine = routingTableMachine;

0 commit comments

Comments
 (0)