Esempio n. 1
0
def send_init():
    # Send initialisation information from mesh
    print "Connecting to mesh network..."
    global mesh_listening_socket, mesh_sending_socket, n_players, base_location
    
    mesh_listening_socket = SocketConnection('localhost')
    
    # Perform initialisation actions with mesh: get location and assign addresses.
    while True:
        if mesh_listening_socket.connectAsReceiver():
            mesh_sending_socket = SocketConnection('', 29877)
            if mesh_sending_socket.connectAsSender():
                
                # Start the connection to the mesh
                print "Sending init packet to mesh"
                mesh_sending_socket.sendData('*')
                
                # Get the base station location packet
                firstData = mesh_listening_socket.receiveData()
                print "First Data: " + firstData.encode('hex_codec')
                first_packet = Packet(firstData)
                first_payload = first_packet.getPayload()
                print first_payload
                
                mesh_listening_socket.setTimeout(1.0)
                n_players = 3
                # Assign addresses to the expected number of nodes
                s_time = time.time()
                while (len(id_dict) <= 1):
                    if (time.time() > s_time + 10):
                        break;
                    print "Time remaining: " + str(int(s_time + 10 - time.time() + 0.5))
                    try:
                        data = mesh_listening_socket.receiveData()
                        
                        packet = Packet(data)
                        if packet.isIdentification():
                            speck_id = packet.getPayload().getId()
                            
                            print "Id request from %s" % speck_id
                            id_dict.append(speck_id) 
                            # Respond to all requests: packet may have dropped
                            assign_address(speck_id)
                            print "Address assigned"
                            # Reset the start time so we wait from last receive
                            s_time = time.time()
                            print "Time Reset"
                    except:
                        print "No Data received. Retrying"
                
                break
            # If creating sockets doesn't work, wait and try again
            else: 
                time.sleep(1)
        else:
            time.sleep(1)
    
    assign_names()
Esempio n. 2
0
try:
	listeningSocket = SocketConnection('localhost')
		
	if(listeningSocket.connectAsReceiver()):
		sendingSocket = SocketConnection('', 29877)
		if(not sendingSocket.connectAsSender()):
			print "Could not connect as sender"
		else :
			threading.Thread(target = getKeyInput).start()
			while(True):
				
				data = listeningSocket.receiveData()
				packet = Packet(data)
				print "Current Packet: "
				print packet
				if(packet.isIdentification()):
					print "Identification packet!"
					assignId(packet.getPayload().getId())
				else:
					print "Not Identification"
	else:
		print "Could not connect as receiver"
		
except Exception, e:
	print "Exception: %s" % e
finally:
	print "Finally: "
	quit()

Esempio n. 3
0
def send_init():
    # Send initialisation information from mesh
    print "Connecting to mesh network..."
    global mesh_listening_socket, mesh_sending_socket, n_players, base_gps
    
    mesh_listening_socket = SocketConnection('localhost')
    
    # Perform initialisation actions with mesh: get location and assign addresses.
    while True:
        if mesh_listening_socket.connectAsReceiver():
            mesh_sending_socket = SocketConnection('', 29877)
            if mesh_sending_socket.connectAsSender():
                
                # Start the connection to the mesh
                print "Sending init packet to mesh"
                mesh_sending_socket.sendData('*')
                
                # Get the base station location packet
                firstData = mesh_listening_socket.receiveData()
                print "First Data: " + firstData.encode('hex_codec')
                first_packet = Packet(firstData)
                first_payload = first_packet.getPayload()
                print first_payload
                base_gps = Point(first_payload.getDecimalLatitude(), first_payload.getDecimalLongitude(), first_payload.getElevation())
                print "Base GPS: " + str(base_gps)
                
                mesh_listening_socket.setTimeout(1.0)
                                
                # Assign addresses to the expected number of nodes
                s_time = time.time()
                while (len(id_dict) <= n_players):
                    if (time.time() > s_time + 20):
                        break;
                    print "Time remaining: " + str(int(s_time + 20 - time.time() + 0.5))
                    try:
                        data = mesh_listening_socket.receiveData()
                        
                        packet = Packet(data)
                        if packet.isIdentification():
                            speck_id = packet.getPayload().getId()
                            
                            print "Id request from %s" % speck_id
                            
                            # Respond to all requests: packet may have dropped
                            assign_address(speck_id)
                            print "Address assigned"
                            # Reset the start time so we wait from last receive
                            s_time = time.time()
                            print "Time Reset"
                    except:
                        print "No Data received. Retrying"
                
                # Tell the nodes to start using TDMA
                assign_basestation_tdma_info()  
                time.sleep(1)
                print "SENDING PINGS"
                messages = ['Hello','from','slip','group','d']
                counter = -1
                while True:
                        counter += 1
                        counter = counter % len(messages)
                        print "SENDING PING"
                        payload = PayloadMessage()
                        payload.initialise(messages[counter] + "$PING")
                        create_and_send_packet(0xFF,0x01,0x03,0x00,payload)
                        time.sleep(1)
                break
            # If creating sockets doesn't work, wait and try again
            else: 
                time.sleep(1)
        else:
            time.sleep(1)
    
    # Package the assigned addresses for server
    ids_to_send = [str(item) for item in id_dict.values()]
    
    base_location = loc_translate(base_gps)
    
    # Send the itialisation message to the server
    initMessage = {"state": "init", "base_location": base_location,"device_ids": ids_to_send}
    print "INIT MESSAGE: " + str(initMessage)
    pair_stream.send_json(initMessage)