CNode* CMigrationInfo::FindNodeWithFreeCpu(CNode* i_sourceNode, CProcess* i_process)
{
	i_sourceNode->WriteLog("MIGRATION - CPU ISSUES - Searching for destination node to establish migration");
	CNode* returnValue = NULL;
	bool found = false;
	uint32_t minIndex = 0;
	double minCpu = 1;
	if (i_sourceNode->GetAvailableBandwidth() > 0)
	{
		for (uint32_t i = 0; i < CMasterSingleton::GetInstance()->GetNodeCount(); ++i)
		{
			CNode* currentNode = CMasterSingleton::GetInstance()->GetNode(i);
			double cpuUsage = currentNode->GetCpuUsage();
			if (minCpu > cpuUsage && i_sourceNode->GetId() != currentNode->GetId() && CanBeDestination(currentNode, i_process))
			{
				minCpu = cpuUsage;
				minIndex = i;
				found = true;
			}
		}

		// if found is false, we will return a null pointer meaning that the migration should not happen
		if (found)
			returnValue = CMasterSingleton::GetInstance()->GetNode(minIndex);
	}

	return returnValue;
}