def testRecvJob(): print '=' * 60 print '-' * 20, 'testRecvJob', '-'* 19 manager = ConnectionManager() processor = Processor(4) protocol = Protocol() newSock = AsynClientSocket() connection = SocketConnection(newSock, protocol, processor) manager.addConnection(newSock.getFileNo(), connection) assert len(manager) == 1 newSock.status.addStatus(CONST.STATUS_WF) connection.genJobs() print processor.list[-1].__doc__ assert processor.list[-1].__doc__ == "Protocol.handleConnected" newSock.status.addStatus(CONST.STATUS_RF) connection.genJobs() print processor.list[-1].__doc__ assert processor.list[-1].__doc__ == "SocketConnection.recvImpl" assert connection.status.has(CONST.STATUS_C) print newSock.dump() print processor.dump() print '-' * 20, 'test done', '-' * 20 print '=' * 60
def testConnectFailed(): print '=' * 60 print '-' * 20, 'testConnectFailed', '-'* 20 protocol = Protocol() newSock = AsynClientSocket() connection = SocketConnection(newSock, protocol, processor) manager.addConnection(newSock.getFileNo(), connection) assert len(manager) == 1 print "connect failed immediately." connection.connect("localhost", 12345) sleepTime = 5 while not connection.status.has(CONST.STATUS_E) and sleepTime > 0: time.sleep(1) sleepTime = sleepTime - 1 assert sleepTime > 0 connection.status.dump() print "\n\nconnect failed later." connection.connect("202.38.193.244", 12345) sleepTime = 5 while not connection.status.has(CONST.STATUS_E) and sleepTime > 0: time.sleep(1) sleepTime = sleepTime - 1 assert sleepTime > 0 connection.status.dump() newSock.status.addStatus(CONST.STATUS_UD) manager.clean() assert len(manager) == 0 print '-' * 20, 'test done', '-' * 20 print '=' * 60
def testSend(testId): print '=' * 60 print '-' * 20, 'testSend(', testId, ')', '-'* 20 protocol = Protocol() newSock = AsynClientSocket() connection = SocketConnection(newSock, protocol, processor) connection.connect(host, port) manager.addConnection(newSock.getFileNo(), connection) assert len(manager) == 1 timeout = 0 while not connection.status.has(CONST.STATUS_UD) and timeout < testTimeout: time.sleep(1) timeout += 1 if timeout >= testTimeout: pdb.set_trace() manager.clean() assert len(manager) == 0 print '-' * 20, 'test done', '-' * 20 print '=' * 60
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()
def testSend(): print '=' * 60 print '-' * 20, 'testSend', '-'* 20 protocol = Protocol() newSock = AsynClientSocket() connection = SocketConnection(newSock, protocol, processor) manager.addConnection(newSock.getFileNo(), connection) assert len(manager) == 1 connection.connect(host, port) while not connection.status.has(CONST.STATUS_C): time.sleep(1) while not connection.status.has(CONST.STATUS_UD): time.sleep(1) manager.clean() assert len(manager) == 0 print '-' * 20, 'test done', '-' * 20 print '=' * 60
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()
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)
def testConnectSuccess(): print '=' * 60 print '-' * 20, 'testConnectSuccess', '-'* 20 protocol = Protocol() newSock = AsynClientSocket() connection = SocketConnection(newSock, protocol, processor) manager.addConnection(newSock.getFileNo(), connection) assert len(manager) == 1 connection.connect(host, port) sleepTime = 5 while not connection.status.has(CONST.STATUS_C) and sleepTime > 0: time.sleep(1) sleepTime = sleepTime - 1 assert sleepTime > 0 newSock.status.addStatus(CONST.STATUS_UD) manager.clean() assert len(manager) == 0 print '-' * 20, 'test done', '-' * 20 print '=' * 60
def getKeyInput(): 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!"
while True: print "Enter old ID: " oldId = sys.stdin.readline() print "Enter new ID: " newId = sys.stdin.readline() '''if(data == 'quit'): break if(data == ''): continue ''' payload = PayloadIdentification() payload.initialise(int(oldId),int(newId)) p = Packet() p.initialise(0,0xFF,1,0,0,payload) print p global socketConnection socketConnection.sendData(p.getBytes()) socketConnection = SocketConnection('', 29877) if(not socketConnection.connectAsSender()): sys.exit(0) try: getInput() except: pass finally: quit()
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()
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)