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 colorAware(contentReceived, color): global ca_bucketF_size, ca_bucketS_size, ca_dropped, serverSocket, ca_n_transmitted, ca_n_dropped, n_Reds, n_Yellows, n_Greens, greenToRed, yellowToRed, color_awares, semaphore_ca packet_size = pp.ipPacketSize(contentReceived) semaphore_ca.acquire() if color == "Red": n_Reds += 1 ca_dropped.append(contentReceived) if debug: print("ColorAware: Red") ca_n_dropped += 1 if pp.numberPacketsProcessed(ca_n_transmitted, ca_n_dropped, 500): saveInfosCA() else: if ca_bucketS_size < packet_size: ca_dropped.append(contentReceived) if debug: print("ColorAware: Red") if color == "Green": n_Greens += 1 greenToRed += 1 else: n_Yellows += 1 yellowToRed += 1 ca_n_dropped += 1 if pp.numberPacketsProcessed(ca_n_transmitted, ca_n_dropped, 500): saveInfosCA() else: if color == "Yellow": n_Yellows += 1 ca_bucketS_size -= packet_size serverSocket.send(contentReceived) if debug: print("ColorAware: Yellow") ca_n_transmitted +=1 if pp.numberPacketsProcessed(ca_n_transmitted, ca_n_dropped, 500): saveInfosCA() else: n_Greens += 1 if ca_bucketF_size < packet_size: serverSocket.send(contentReceived) ca_bucketS_size -= packet_size if debug: print("ColorAware: Yellow") ca_n_transmitted +=1 greenToYellow += 1 if pp.numberPacketsProcessed(ca_n_transmitted, ca_n_dropped, 500): saveInfosCA() else: if debug: print("ColorAware: Green") ca_n_transmitted +=1 if pp.numberPacketsProcessed(ca_n_transmitted, ca_n_dropped, 500): saveInfosCA() serverSocket.send(contentReceived) ca_bucketF_size -= packet_size ca_bucketS_size -= packet_size color_awares.pop(0) semaphore_ca.release() exit()
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_Time(thread_name, interval): global semaphore_srTCM, rate, bucketF_size, bucketF_max_size, bucketS_size, bucketS_max_size while 1: #Ver condicao do while semaphore_srTCM.acquire() semaphore_ca.acquire() bucketF_size = bucketF_size + rate if bucketF_size + rate <= bucketF_max_size else bucketF_max_size bucketS_size = bucketS_size + rate if bucketS_size + rate <= bucketS_max_size else bucketS_max_size if color_aware: colorAwareBucketRate() if debug: if color_aware: if pp.numberPacketsProcessed(ca_n_transmitted, ca_n_dropped, 500): exit() else: if pp.numberPacketsProcessed(n_transmitted, n_dropped, 500): exit() semaphore_ca.release() semaphore_srTCM.release() time.sleep(interval)
def thread_Time(thread_name, interval): global semaphore, rate, bucket_size, bucket_max_size while 1: semaphore.acquire() bucket_size = bucket_size + rate if bucket_size + rate <= bucket_max_size else bucket_max_size if debug: if pp.numberPacketsProcessed(n_transmitted, n_dropped, 500): exit() semaphore.release() time.sleep(interval)
def thread_Time(thread_name, interval): global semaphore, packets_to_release, packets_to_release_value while 1: #Ver condicao do while semaphore.acquire() packets_to_release = packets_to_release_value consumeBucket() if debug: if pp.numberPacketsProcessed(n_transmitted, n_dropped, 500): exit() semaphore.release() time.sleep(interval)
def consumeBucket(): global bucket, packets_to_release, serverSocket, debug, n_transmitted #, last_number_message_transmitted, n_delay, sum_delay if (len(bucket) == 0): return while (len(bucket)) and (packets_to_release > 0): if debug: print("Transmitindo pacote fila") n_transmitted += 1 if pp.numberPacketsProcessed(n_transmitted, n_dropped, 500): saveInfos() serverSocket.send(bucket.pop(0)) packets_to_release -= 1
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()
def consumeQueue(): global queue, serverSocket, bucket_size, debug, n_transmitted, n_delay, sum_delay, queue_transmissions sentQueue = 0 if (len(queue) == 0): return while sentQueue == 0: contentReceived = queue[0] packet_size = pp.ipPacketSize(contentReceived) if packet_size <= bucket_size: serverSocket.send(queue.pop(0)) bucket_size -= packet_size if debug: queue_time_out = time.time() print("Transmitindo pacote da fila") n_transmitted += 1 queue_transmissions += 1 queue_time_in = times_queue.pop(0) sum_delay += queue_time_out - queue_time_in n_delay = sum_delay/queue_transmissions if pp.numberPacketsProcessed(n_transmitted, n_dropped, 500): saveInfos() if (len(queue) == 0): sentQueue = 1 else: sentQueue = 1