Exemple #1
0
def getInput():
    global printNulls
    try:
        print "Waiting for input socket connection"
        listeningSocket = SocketConnection('',29877)
        print "Input connection created"
        if listeningSocket.connectAsReceiver():
            print "Connected as receiver"
            global isReady
            if not isReady:
                print "Waiting for ready message...."
                while True:
                    data = listeningSocket.receiveData(1)
                    if data == '*':
                        print "Received ready message"
                        isReady = True
                        #threading.Thread(target = starTimeouter).start()
                        break

            while True:
                data = listeningSocket.receiveData(32)
                if(not isNulls(data)):
                    print "Write: " + data.encode('hex_codec')
                    serial.write(data)
        else:
            print "NO LISTENING CONNECTION"
    except:
        pass
    finally:
        listeningSocket.close()
Exemple #2
0
def send_init_no_mesh():
    global mesh_listening_socket,mesh_sending_socket, current_free_address, n_players, pair_stream
    
    mesh_listening_socket = SocketConnection('localhost')
    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('*')
    
                   # Tell the nodes to start using TDMA
                assign_basestation_tdma_info()  
                break
            # If creating sockets doesn't work, wait and try again
            else: 
                time.sleep(1)
        else:
            time.sleep(1)
            
    # Add dummy adresses for expected number of players
    for i in range(0, n_players):
        id_dict[i] = current_free_address
        current_free_address += 1
    ids_to_send = [str(item) for item in id_dict.values()]
    
    base_location = loc_translate(left_corner_of_area)
    
    # Send the itialisation message to the server
    initMessage = {"state": "init", "base_location": base_location,"device_ids": ids_to_send}
    pair_stream.send_json(initMessage)
Exemple #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_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()
Exemple #4
0
		print "\n\nPress enter to start... ",
		data = raw_input()
		sendStartInstruction()
		print "Send initialising data"


ids = {}
currentFreeAddress = 1

listeningSocket = None
sendingSocket = None

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:
Exemple #5
0
from SocketConnection import SocketConnection
  

socket = SocketConnection('localhost')

if(socket.connectAsReceiver()):
	while(True):
		data = socket.receiveData()
		if(data == 'quit'):
			break
		else:
			if(data != ''):
				print data,
	
	socket.close()
Exemple #6
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)