def thread_TokenBucket(): #Funcao que quando chega pacote e nao tem pacotes na fila entao envia ou adiciona na fila global clientSocket, serverSocket, bucket_size, semaphore, bucket_max_size, queue, queue_max_size, debug, n_transmitted, n_dropped, n_received while 1: if debug: if n_received == 500: exit() contentReceived = clientSocket.recv(65535) if (pp.packetAnalysis(contentReceived, serverSocket) == 1): n_received += 1 packet_size = pp.ipPacketSize(contentReceived) semaphore.acquire() if bucket_size < packet_size: consumeQueue() if len(queue) < queue_max_size: queue.append(contentReceived) if debug: time_in = time.time() print("Mensagem adicionada na fila") times_queue.append(time_in) else: if debug: print("Mensagem dropada") n_dropped += 1 if pp.numberPacketsProcessed(n_transmitted, n_dropped, 500): saveInfos() else: if debug: print("Transmitindo pacote") n_transmitted += 1 if pp.numberPacketsProcessed(n_transmitted, n_dropped, 500): saveInfos() serverSocket.send(contentReceived) bucket_size -= packet_size consumeQueue() semaphore.release()
def thread_TokenBucket(): #Funcao que quando chega pacote e nao tem pacotes na fila entao envia ou adiciona na fila global clientSocket, serverSocket, bucket_size, semaphore, bucket_max_size, dropped, debug, n_transmitted, n_dropped while 1: contentReceived = clientSocket.recv(65535) if (pp.packetAnalysis(contentReceived, serverSocket) == 1): packet_size = pp.ipPacketSize(contentReceived) semaphore.acquire() if bucket_size < packet_size: dropped.append(contentReceived) if debug: print("Mensagem dropada") n_dropped += 1 if pp.numberPacketsProcessed(n_transmitted, n_dropped, 500): saveInfos() else: if debug: print("Transmitindo pacote") n_transmitted += 1 if pp.numberPacketsProcessed(n_transmitted, n_dropped, 500): saveInfos() serverSocket.send(contentReceived) bucket_size -= packet_size semaphore.release()
def thread_TwoRateThreeColor(): #Funcao que quando chega pacote e nao tem pacotes na fila entao envia ou adiciona na fila global clientSocket, serverSocket, bucketF_size, semaphore_trTCM, bucketF_max_size, bucketS_size, bucketS_max_size, dropped, n_transmitted, n_dropped, n_Reds, n_Yellows, n_Greens, color_awares while 1: if debug: if color_aware: if n_transmitted == 500: exit() contentReceived = clientSocket.recv(65535) if (pp.packetAnalysis(contentReceived, serverSocket) == 1): if debug: if color_aware: n_transmitted += 1 packet_size = pp.ipPacketSize(contentReceived) semaphore_trTCM.acquire() if bucketS_size < packet_size: if color_aware: if debug: print("Mensagem marcada: Red") color_awares.append(threading.Thread(target=colorAware, args=(contentReceived, "Red"))) color_awares[-1].start() else: if debug: print("Red Action") n_dropped += 1 n_Reds += 1 if pp.numberPacketsProcessed(n_transmitted, n_dropped, 500): saveInfos() dropped.append(contentReceived) else: if bucketF_size < packet_size: if color_aware: if debug: print("Mensagem maracada: Yellow") color_awares.append(threading.Thread(target=colorAware, args=(contentReceived, "Yellow"))) color_awares[-1].start() else: if debug: print("Yellow Action") n_transmitted += 1 n_Yellows += 1 if pp.numberPacketsProcessed(n_transmitted, n_dropped, 500): saveInfos() serverSocket.send(contentReceived) bucketS_size -= packet_size else: if color_aware: if debug: print("Mensagem maracada: Green") color_awares.append(threading.Thread(target=colorAware, args=(contentReceived, "Green"))) color_awares[-1].start() else: if debug: print("Green Action") n_transmitted += 1 n_Greens += 1 if pp.numberPacketsProcessed(n_transmitted, n_dropped, 500): saveInfos() serverSocket.send(contentReceived) bucketF_size -= packet_size bucketS_size -= packet_size semaphore_trTCM.release()
def thread_LeakyBucket(): #Funcao que quando chega pacote e nao tem pacotes na fila entao envia ou adiciona na fila global clientSocket, serverSocket, packets_to_release, bucket, semaphore, bucket_max_size, debug, n_delay, n_dropped, n_transmitted, last_number_message_transmitted while 1: contentReceived = clientSocket.recv(65535) if (pp.packetAnalysis(contentReceived, serverSocket) == 1): if debug: if pp.numberPacketsProcessed(n_transmitted, n_dropped, 500): exit() if len(bucket): if len(bucket) < bucket_max_size: if debug: print("Adicionou na fila e bucket nao vazio") bucket.append(contentReceived) else: if debug: print("Mensagem dropada") n_dropped += 1 if pp.numberPacketsProcessed(n_transmitted, n_dropped, 500): saveInfos() else: semaphore.acquire() if packets_to_release > 0: if debug: print("Transmitindo pacote") n_transmitted += 1 if pp.numberPacketsProcessed(n_transmitted, n_dropped, 500): saveInfos() serverSocket.send(contentReceived) packets_to_release -= 1 else: if debug: print("Adicionou na fila e bucket vazio") bucket.append(contentReceived) semaphore.release()