Exemple #1
0
/**
 * Removes a connection from the bandwidth allocator
 * Requests all other connections update their speed
 */
int BandwidthAllocator::removeBandwidth(TCPConnection* connection) {
	boost::unique_lock<boost::mutex> lock(queueMutex);

	// If we are not in a consistant state - wait
	while(unsettled.size() > 0 || currentlyWorking) queueWaiting.wait(lock);

	printf("1\n");
	currentlyWorking = true;

	int removeConnectionFd = connection->getSockFd();
	int numConnections = settled.size();
	int nodeSpeed = UPLINK_SPEED;
	if (numConnections != 1) nodeSpeed = UPLINK_SPEED / (numConnections - 1);
	printf("2\n");
	TCPConnection* currentConnection;
	for (int i = 0; i < numConnections; i++) {
		currentConnection = settled.front();
		settled.pop();
		if (currentConnection->getSockFd() != removeConnectionFd) {
		currentConnection->updateRemoteSpeed(nodeSpeed);
		unsettled[currentConnection->getSockFd()] = currentConnection;
		}
	}
	printf("3\n");
	while(unsettled.size() > 0) queueWaiting.wait(lock);
	printf("4\n");
	currentlyWorking = false;
	queueWaiting.notify_all();
	printf("5\n");
	return 0;

}
Exemple #2
0
/**
 * Allocates some bandwidth for the new TCP connection
 * Notifies all the other connections to change their speed
 */
int BandwidthAllocator::getBandwidth(TCPConnection* connection) {
	boost::unique_lock<boost::mutex> lock(queueMutex);

	while(unsettled.size() > 0 || currentlyWorking) queueWaiting.wait(lock);

	int numConnections = settled.size();
	int nodeSpeed = UPLINK_SPEED / (numConnections + 1);

	TCPConnection* currentConnection;
	for (int i = 0; i < numConnections; i++) {
		currentConnection = settled.front();
		settled.pop();
		unsettled[currentConnection->getSockFd()] = currentConnection;
		currentConnection->updateRemoteSpeed(nodeSpeed);
	}

	puts("Waiting for unsettled to sort itself out...");
	while(unsettled.size() > 0) queueWaiting.wait(lock);

	settled.push(connection);

	currentlyWorking = false;
	queueWaiting.notify_all();

	// Return the speed we need to run at
	// When we update this to handle flows at any time reverse so
	// we send a bandwidth alloc to the receipent
	return nodeSpeed;

}