Esempio n. 1
0
def YCallBack(value): # Forward
    if trainerIP in conns.keys():
        msg = server_pb2.server()
        msg.sequence_number = 999
        msg.uid = "server"	
        msg.mCmd.ctl_msg.direction = 9
        message_queues[conns[trainerIP]].put(msg)
Esempio n. 2
0
 def readMessage(self,sock):
     data, size, position = self.read_socket(sock)
     try:
         msg = server_pb2.server()
         msg.ParseFromString(data[position:position + size])
         return msg
     except:
         self.msgs_dropped += 1
         print 'Message Dropped! Last sequence number = ', self.sn
         return None
Esempio n. 3
0
 def test_extra_bytes(self):
     msg = server_pb2.server()
     msg.sequence_number = 101
     msg.uid = "server"
     msg.mCmd.ctl_msg.direction = 1
     size = msg.ByteSize()
     z = "~~~" + _VarintBytes(size) + "FAILURE" + msg.SerializeToString() + "}}}"
     self.conn.sendall(z)
     data = self.readMessage(self.conn)
     if(data.HasField('again')):
         return True
     return False
Esempio n. 4
0
def sendButton(xboxControlId, value):
    if(value != 0):
        if(mode > 0):
            print 'Controller ID: ', xboxControlId
            print 'Value:         ', value
            print ''
        # send to GUI only
        if guiIP in conns.keys():
            msg = server_pb2.server()
            msg.sequence_number = 999
            msg.uid = "server"
            msg.ctl.btn.button_id = xboxControlId
            msg.ctl.btn.value = int(value)
            message_queues[conns[trainerIP]].put(msg)
Esempio n. 5
0
def movementCallBack(value):
    # Compute direction, giving preference to vertical
    global prevDirection
    global prevMovementVal
    val = 0
    toMove = False
    if(value[1] == 1 and prevDirection != 2): # North        
        val = 1
    elif(value[1] == -1 and prevDirection != 1): # South
        val = 2
    elif(value[0] == 1 and prevDirection != 4) : # East
        val = 3
    elif(value[0] == -1 and prevDirection != 3): # West
        val = 4
    
    
    if(val != 0 and not freeze):
        if(mode > 0):
            print 'Previous: ', prevDirection
            print 'Value:    ', val
            print ''
        # Create message
        msg = server_pb2.server()
        msg.sequence_number = 999
        msg.uid = "server"	
        msg.ctl.go.direction = val
        if trainerIP in conns.keys():
            message_queues[conns[trainerIP]].put(msg)
        elif guiIP in conns.keys():
            message_queues[conns[guiIP]].put(msg)
        
        prevDirection = val
    if (val == 0):
        prevMovementVal = 0
    else:
        prevMovementVal = 1
Esempio n. 6
0
    def test_sendall_messages(self):
        sn = 1
        dict = {}
        try:
            msg = server_pb2.server()
            msg.sequence_number = sn
            msg.uid = "server"
            msg.mCmd.ctl_msg.direction = 1
            msg.mCmd.ctl_msg.adj = 0
            size = msg.ByteSize()
            z = "~~~" + _VarintBytes(size) + msg.SerializeToString()+ "}}}"
            dict.update({msg.sequence_number: z})
            sn += 1
            
##            msg = server_pb2.server()
##            msg.sequence_number = sn
##            msg.uid = "server"
##            msg.mCmd.data.right_ticks = 10
##            msg.mCmd.data.right_dir = 5
##            msg.mCmd.data.left_ticks = 5
##            msg.mCmd.data.left_dir = 5
##            
##            size = msg.ByteSize()
##            z = "~~~" + _VarintBytes(size) + msg.SerializeToString()+ "}}}"
##            dict.update({msg.sequence_number: z})
##            sn += 1
            
            """ Motor messages complete, begin IR Sensor"""
            msg = server_pb2.server()
            msg.sequence_number = sn
            msg.uid = "server"
            msg.sCmd.dist_data.distance = 5
            size = msg.ByteSize()
            z = "~~~" + _VarintBytes(size) + msg.SerializeToString()+ "}}}"
            dict.update({msg.sequence_number: z})
            sn += 1
            
            msg = server_pb2.server()
            msg.sequence_number = sn
            msg.uid = "server"
            msg.sCmd.color_data.color_code = 255
            msg.sCmd.color_data.x = 255
            msg.sCmd.color_data.y = 255
            size = msg.ByteSize()
            z = "~~~" + _VarintBytes(size) + msg.SerializeToString()+ "}}}"
            dict.update({msg.sequence_number: z})
            sn += 1
            
            """ Sensor messages complete, begin Emulator"""
            msg = server_pb2.server()
            msg.sequence_number = sn
            msg.uid = "server"
            msg.bCmd.battle_ctl.player_status = True
            msg.bCmd.battle_ctl.match_inProgress = True
            size = msg.ByteSize()
            z = "~~~" + _VarintBytes(size) + msg.SerializeToString()+ "}}}"
            dict.update({msg.sequence_number: z})
            sn += 1
            
##            msg = server_pb2.server()
##            msg.sequence_number = sn
##            msg.uid = "server"
##            msg.bCmd.battle_move.trainer = True
##            msg.bCmd.battle_move.hit_miss = True
##            size = msg.ByteSize()
##            z = "~~~" + _VarintBytes(size) + msg.SerializeToString()+ "}}}"
##            dict.update({msg.sequence_number: z})
##            sn += 1
            
            """ Final status messages"""
            msg = server_pb2.server()
            msg.sequence_number = sn
            msg.uid = "server"
            msg.again.sequence_number = sn
            size = msg.ByteSize()
            z = "~~~" + _VarintBytes(size) + msg.SerializeToString()+ "}}}"
            dict.update({msg.sequence_number: z})
            sn += 1
            
            msg = server_pb2.server()
            msg.sequence_number = sn
            msg.uid = "server"
            msg.done.sequence_number = sn
            size = msg.ByteSize()
            z = "~~~" + _VarintBytes(size) + msg.SerializeToString()+ "}}}"
            dict.update({msg.sequence_number: z})
            sn += 1
            
            for k,v in dict.items():
                self.conn.sendall(v)
                data = self.readMessage(self.conn)
                if(data.HasField('done')):
                    if(dict.has_key(data.sequence_number)):
                        del dict[data.sequence_number]
##                        print 'removed', data.sequence_number, 'from dict'
     
            if len(dict) == 0:                
                return True
            else:
                return False
        
        except:
            return False
Esempio n. 7
0
    def test_loopback(self):
        print 'Entering loopback...'
        cond = False
        while 1:
            try:
                # TODO Update to have GUI to Rover comms
                try:
                    next_msg = self.msgQ.get_nowait()
                except Queue.Empty:
                    # No messages waiting so stop checking for writability
                    print >>sys.stderr, 'output queue for', self.conn.getpeername(), 'is empty'
                else:
                    print >>sys.stderr, 'sending "%s" to %s' % (next_msg, self.conn.getpeername())
                    size = next_msg.ByteSize()
                    next_msg.sequence_number = self.sn
                    self.sn += 1
                    z = "~~~" + _VarintBytes(size) + next_msg.SerializeToString() + "}}}"
                    self.conn.sendall(z)
                    if self.sn > 127:
                        self.sn = 1
                    self.dict.update({next_msg.sequence_number: z})
                if(cond == False):
                    data = self.readMessage(self.conn)
                    #  recv returns 0 if peer closes connection
                    if not data:
                        print ("connection closed from " + self.ip + str(self.port))
                        conns.remove(self.conn)
                        self.conn.close()
                        self.status= 'crashed'
            ##            break
                    test = server_pb2.server()
                    test.sequence_number = self.sn
                    test.uid = "server"
                    test.done.sequence_number = test.sequence_number
                    
                    # turn data into binary
                    #print ':'.join(x.encode('hex') for x in test.SerializeToString())
                    self.msgQ.put(test)
                    cond = True
                else:
                    data = self.readMessage(self.conn)
                    
                    #Check for empty message
                    if not data:
                        print ("connection closed from " + self.ip + str(self.port))
                        del conns[self.conn]
                        self.conn.close()
                        self.status = 'crashed'
                        break
                    if(data.HasField('done')):
                        if(self.dict.has_key(data.done.sequence_number)):
                            del self.dict[data.done.sequence_number]
                            print 'removed', data.done.sequence_number, 'from dict'
                    # TODO Parse out all message types
                    if(self.mode == 2):
                        print("data: " + str(data))

                    ## Construct response for loopback test
                    if(self.mode > 0):
                        test = server_pb2.server()
                        test.sequence_number = self.sn
                        test.uid = "server"
                        test.done.sequence_number = test.sequence_number
                        # print ':'.join(x.encode('hex') for x in test.SerializeToString())
                        self.msgQ.put(test)
            except socket.timeout:
                self.status = 'crashed'
                print 'socket_timeout'
                break
        self.status = 'crashed'
        return True