Skip to content

Commit b54a3c8

Browse files
committed
Re-Implemented the routing function to match exact specifications of search algorithm. Along with it also implemented intelligent routing for cases not mentioned in the algorithm.
1 parent 2076a6e commit b54a3c8

2 files changed

Lines changed: 76 additions & 48 deletions

File tree

DS_Project/Machines.h

Lines changed: 62 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -408,81 +408,95 @@ class Machines {
408408
This function takes hashed key of data and machine from the Ring_DHT class and then
409409
performs the search according to the given keys
410410
*/
411-
Machine_Node<D,T>* searchResponsibleMachine(T dataKey, T machineKey)
411+
Machine_Node<D, T>* searchResponsibleMachine(T dataKey, T machineKey)
412412
{
413413
cout << "\n\n------ Routing Starting From Machine " << setfill('0') << setw(3) << machineKey << " ------" << endl;
414+
cout << "| Routing Started" << endl;
414415
cout << "|" << endl;
415-
Machine_Node<D,T>* startingMachine = getMachine(machineKey);
416+
Machine_Node<D, T>* currentMachine = getMachine(machineKey);
417+
int totalRoutingLookups = 0;
416418
for (int i = 0; i < routingTableSize; i++)
417419
{
418-
Machine_Node<D,T>* temp = static_cast<Machine_Node<D,T>*>(startingMachine->routingTable->getElement(i));
419-
Machine_Node<D,T>* temp2 = static_cast<Machine_Node<D,T>*>(startingMachine->routingTable->getElement(i + 1));
420-
if (dataKey > getLastMachine()->data)
420+
if (totalRoutingLookups > pow(2, routingTableSize)) // More than N lookups have been done but no responsible machine was found
421421
{
422-
cout << "| Routing From Machine : " << setfill('0') << setw(3) << startingMachine->data << " -> " << setfill('0') << setw(3) << getFirstMachine()->data << endl;
423-
startingMachine = getFirstMachine();
424-
cout << "|" << endl;
425-
cout << "-----------------------------------------------" << endl << endl;
426-
return startingMachine;
427-
}
428-
else if (dataKey < getFirstMachine()->data)
429-
{
430-
cout << "| Routing From Machine : " << setfill('0') << setw(3) << startingMachine->data << " -> " << setfill('0') << setw(3) << getFirstMachine()->data << endl;
431-
startingMachine = getFirstMachine();
432-
cout << "|" << endl;
433-
cout << "-----------------------------------------------" << endl << endl;
434-
return startingMachine;
422+
break;
435423
}
436-
else if (isLastMachine(startingMachine->data) == true && dataKey >= startingMachine->data)
424+
Machine_Node<D, T>* routingTableMachine = static_cast<Machine_Node<D, T>*>(currentMachine->routingTable->getElement(i));
425+
Machine_Node<D, T>* preMachine = getPredecessorMachine(currentMachine->data);
426+
if (isFirstMachine(currentMachine->data) == true)
437427
{
438-
cout << "| Routing From Machine: " << setfill('0') << setw(3) << startingMachine->data << " -> " << setfill('0') << setw(3) << getFirstMachine()->data << endl;
439-
startingMachine = getFirstMachine();
440-
cout << "|" << endl;
441-
cout << "-----------------------------------------------" << endl << endl;
442-
return startingMachine;
428+
if (preMachine->data != NULL && dataKey > preMachine->data || dataKey <= currentMachine->data)
429+
{
430+
cout << "| Routing From Machine: " << setfill('0') << setw(3) << currentMachine->data << " -> " << setfill('0') << setw(3) << currentMachine->data << endl;
431+
cout << "|" << endl;
432+
cout << "| Routing Ended" << endl;
433+
cout << "-----------------------------------------------" << endl << endl;
434+
return currentMachine;
435+
}
443436
}
444-
else if (dataKey == startingMachine->data)
437+
else if (isLastMachine(currentMachine->data) == true)
445438
{
446-
cout << "| Routing From Machine: " << setfill('0') << setw(3) << startingMachine->data << endl;
447-
cout << "|" << endl;
448-
cout << "-----------------------------------------------" << endl << endl;
449-
return startingMachine;
439+
if (dataKey > currentMachine->data)
440+
{
441+
cout << "| Routing From Machine: " << setfill('0') << setw(3) << currentMachine->data << " -> " << setfill('0') << setw(3) << getFirstMachine()->data << endl;
442+
cout << "|" << endl;
443+
cout << "| Routing Ended" << endl;
444+
cout << "-----------------------------------------------" << endl << endl;
445+
currentMachine = getFirstMachine();
446+
return currentMachine;
447+
}
450448
}
451-
else if (temp->data == dataKey)
449+
if (preMachine->data != NULL && dataKey > preMachine->data && dataKey <= currentMachine->data)
452450
{
453-
cout << "| Routing From Machine: " << setfill('0') << setw(3) << startingMachine->data << " -> " << setfill('0') << setw(3) << temp->data << endl;
454-
startingMachine = temp;
451+
cout << "| Routing From Machine: " << setfill('0') << setw(3) << currentMachine->data << " -> " << setfill('0') << setw(3) << currentMachine->data << endl;
455452
cout << "|" << endl;
453+
cout << "| Routing Ended" << endl;
456454
cout << "-----------------------------------------------" << endl << endl;
457-
return startingMachine;
455+
return currentMachine;
458456
}
459-
else if (dataKey > startingMachine->data && temp->data >= dataKey)
457+
else if (i == 0 && dataKey > currentMachine->data && dataKey <= routingTableMachine->data)
460458
{
461-
cout << "| Routing From Machine: " << setfill('0') << setw(3) << startingMachine->data << " -> " << setfill('0') << setw(3) << temp->data << endl;
462-
startingMachine = temp;
463-
cout << "|" << endl;
464-
cout << "-----------------------------------------------" << endl << endl;
465-
return startingMachine;
459+
cout << "| Routing From Machine: " << setfill('0') << setw(3) << currentMachine->data << " -> " << setfill('0') << setw(3) << routingTableMachine->data << endl;
460+
currentMachine = routingTableMachine;
461+
i = -1;
462+
totalRoutingLookups++;
466463
}
467-
else if (dataKey > temp->data && temp2 != NULL && dataKey < temp2->data)
464+
else if (i < routingTableSize - 1)
468465
{
469-
cout << "| Routing From Machine: " << setfill('0') << setw(3) << startingMachine->data << " -> " << setfill('0') << setw(3) << temp->data << endl;
470-
startingMachine = temp;
471-
i = -1;
466+
Machine_Node<D, T>* routingTableNextMachine = static_cast<Machine_Node<D, T>*>(currentMachine->routingTable->getElement(i + 1));
467+
if (dataKey > routingTableMachine->data && dataKey <= routingTableNextMachine->data)
468+
{
469+
cout << "| Routing From Machine: " << setfill('0') << setw(3) << currentMachine->data << " -> " << setfill('0') << setw(3) << routingTableMachine->data << endl;
470+
currentMachine = routingTableMachine;
471+
i = -1;
472+
totalRoutingLookups++;
473+
}
472474
}
473-
else if (dataKey > temp->data && temp2 == NULL)
475+
else if (i == routingTableSize-1) // No suitable routing machine found, So we'll treat this special case differently
474476
{
475-
cout << "| Routing From Machine: " << setfill('0') << setw(3) << startingMachine->data << " -> " << setfill('0') << setw(3) << temp->data << endl;
476-
startingMachine = temp;
477+
Machine_Node<D, T>* routingTableBiggestMachine = static_cast<Machine_Node<D, T>*>(currentMachine->routingTable->getElement(0));
478+
for (int j = 0; j < routingTableSize; j++)
479+
{
480+
Machine_Node<D, T>* routingTableIteratedMachine = static_cast<Machine_Node<D, T>*>(currentMachine->routingTable->getElement(j));
481+
if (routingTableIteratedMachine->data > routingTableBiggestMachine->data)
482+
{
483+
routingTableBiggestMachine = static_cast<Machine_Node<D, T>*>(currentMachine->routingTable->getElement(j));
484+
}
485+
}
486+
// initially lets rout to the biggest availble machine of the current routing table
487+
cout << "| Routing From Machine: " << setfill('0') << setw(3) << currentMachine->data << " -> " << setfill('0') << setw(3) << routingTableBiggestMachine->data << " [Intelligent]" <<endl;
488+
currentMachine = routingTableBiggestMachine;
477489
i = -1;
490+
totalRoutingLookups++;
478491
}
479492
}
480-
cout << "| -NULL-" << endl;
493+
cout << "| No Routing Machine Found" << endl;
494+
cout << "| " << endl;
495+
cout << "| Routing Ended" << endl;
481496
cout << "-----------------------------------------------" << endl << endl;
482497
return NULL;
483498
}
484499

485-
486500
/*
487501
This function adjusts routing tables for every machine
488502
*/

DS_Project/main.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,23 @@ int main()
3535

3636
cout << "| >. Specify the number of machines: ";
3737
cin >> numberOfMachines;
38+
while (cin.fail())
39+
{
40+
cout << "| >. Specify the number of machines: ";
41+
cin.clear();
42+
std::cin.ignore(256, '\n');
43+
cin >> numberOfMachines;
44+
}
3845

3946
cout << "| >. Specify the size of identifier space in bits: ";
4047
cin >> identifierSpace;
48+
while (cin.fail())
49+
{
50+
cout << "| >. Specify the size of identifier space in bits: ";
51+
cin.clear();
52+
std::cin.ignore(256, '\n');
53+
cin >> identifierSpace;
54+
}
4155

4256
RingDHT <string, int> dht(identifierSpace, numberOfMachines);
4357

0 commit comments

Comments
 (0)