def main(): # Make pygame window screen = pygame.display.set_mode((640,640)) timer = 0 numClients = 50 # Create network of clients clients = [] for i in xrange(0, numClients): clients.append(client.Client(i)) # Set up random connections amongst the clients for i in xrange(0, numClients): for j in xrange(0, 5): clients[i].addConnection(clients[random.randrange(0, numClients)]) # Instantiate network layer networkLayer = Network(clients) for c in clients: c.networkLayer = networkLayer; # Get user input keyInput = None while keyInput != 'q': message = None sourceID = -1 destinationID = -1 keyInput = raw_input("Select Option:\n's' - start\n'q' - end the program\n") # Start forming a message if keyInput == 's': # Choose a valid client index while sourceID < 0 or sourceID >= numClients: sourceID = int(raw_input("Enter the Client ID: ")) source = clients[sourceID] # Choose an option keyInput = raw_input("\nSelect Option:\n'm' - submit message\n'd' - display client\n") # Send a direct message if keyInput == 'm': while destinationID < 0 or destinationID >= numClients: destinationID = int(raw_input("Enter the Reciever ID: ")) destination = clients[destinationID] message = raw_input("Enter the message to send:\n") clients[sourceID].sendMessage(clients[sourceID].user, clients[destinationID].user, message, timer) if (len(source.networkLayer.BFS(source.ip, destination.ip)) == 0): print "No path between clients" continue; # Process TCP packets on sender and receiver while networkLayer.routing(timer) or len(source.transportLayer.tcpPackets) > 0 or len(destination.transportLayer.tcpPackets) > 0: drawClient(screen, source, 20, 20, destination) drawClient(screen, destination, 300, 20) # Checks if the source has packet to send if source.transportLayer.sendPacket(): srcPacket = source.transportLayer.tcpPackets.pop(0) srcPacket.addHeader("send_time", timer) networkLayer.process(srcPacket) # Send the packet to a buffer for resubmission if not srcPacket.getHeader("syn") and not srcPacket.getHeader("fin"): newPacket = Packet(srcPacket) message = newPacket.getBody("message") newPacket.addBody("message", message+"(r)") newPacket.addHeader("fail", False) source.transportLayer.sentPackets.append(newPacket) # Checks for a retransmission packet retansmitPacket = source.transportLayer.checkRetransmission(timer) if retansmitPacket != None: nextPacket = Packet(retansmitPacket) message = nextPacket.getBody("message") networkLayer.process(retansmitPacket) nextPacket.addBody("message", message+"(r)") nextPacket.addHeader("fail", False) source.transportLayer.sentPackets.append(nextPacket) if destination.transportLayer.sendPacket(): networkLayer.process(destination.transportLayer.tcpPackets.pop(0)) timer += 1 time.sleep(5) # Reset the transport layers source.transportLayer.reset() destination.transportLayer.reset() # Clients update their inbox drawClient(screen, source, 20, 20, destination) drawClient(screen, destination, 300, 20) # Display all IP addresses a client can talk to if keyInput == 'd': print clients[sourceID].connections drawClient(screen, source, 20, 20)