def evaluate_restart_request(self, agent_request, req_id, data_connection):
        # Check the format is correct
        restart_request = AgentRestartRequest(agent_request)
        if not restart_request.sanity_check():
            response = ServerInterface.build_invalid_response(req_id)
            data_connection.send_message(json.dumps(response), True)
            return

        # Make sure the agent tracker is up
        hostname = restart_request.get_hostname()
        if self.agent_tracker is None or self.command_tracker is None:
            response = ServerInterface.build_internal_error_response(req_id)
            data_connection.send_message(json.dumps(response), True)
            return

        # Check the host is tracked
        if not self.agent_tracker.have_host(hostname):
            response = ServerInterface.build_invalid_response(req_id)
            data_connection.send_message(json.dumps(response), True)
            return

        # Try sending the command
        command_id = self.command_tracker.send_command(hostname,\
                'GENERAL', 'RESTART')
        if command_id is -1:
            response = ServerInterface.build_internal_error_response(req_id)
            data_connection.send_message(json.dumps(response), True)
        else:
            response = ServerInterface.build_accepted_response(req_id)
            data_connection.send_message(json.dumps(response), True)
    def evaluate_get_configs_request(self, agent_request, req_id,\
                                     data_connection):
        get_configs_request = AgentGetConfigsRequest(agent_request)
        # Check the format is correct
        if not get_configs_request.sanity_check():
            response = ServerInterface.build_invalid_response(req_id)
            data_connection.send_message(json.dumps(response), True)
            return

        # Make sure the agent tracker is up
        hostname = get_configs_request.get_hostname()
        if self.agent_tracker is None or self.command_tracker is None:
            response = ServerInterface.build_internal_error_response(req_id)
            data_connection.send_message(json.dumps(response), True)
            return

        # Check the host is tracked
        if not self.agent_tracker.have_host(hostname):
            response = ServerInterface.build_invalid_response(req_id)
            data_connection.send_message(json.dumps(response), True)
            return

        command_id = self.command_tracker.send_command(hostname,\
                'GENERAL', 'GET_CONFIGS',\
                handler_function=self.get_configs_handler,\
                handler_user_data=req_id)

        # If we failed sending the command
        if command_id is -1:
            response = ServerInterface.build_internal_error_response(req_id)
            data_connection.send_message(json.dumps(response), True)
        else:
            self.get_configs_map[command_id] = data_connection
    def evaluate_set_configs_request(self, agent_request, req_id,\
                                     data_connection):
        # Check the format is correct
        set_configs_request = AgentSetConfigsRequest(agent_request)
        if not set_configs_request.sanity_check():
            response = ServerInterface.build_invalid_response(req_id)
            data_connection.send_message(json.dumps(response), True)
            return

        # Make sure the agent tracker is up
        hostname = set_configs_request.get_hostname()
        if self.agent_tracker is None or self.command_tracker is None:
            response = ServerInterface.build_internal_error_response(req_id)
            data_connection.send_message(json.dumps(response), True)
            return

        # Check the host is tracked
        if not self.agent_tracker.have_host(hostname):
            response = ServerInterface.build_invalid_response(req_id)
            data_connection.send_message(json.dumps(response), True)
            return

        # Try sending the command
        configs = set_configs_request.get_configs()
        
        command_sent = self.command_tracker.send_command(hostname,\
                'GENERAL', 'SET_CONFIGS', command_body=configs)
        if not command_sent:
            response = ServerInterface.build_internal_error_response(req_id)
            data_connection.send_message(json.dumps(response), True)
        else:
            response = ServerInterface.build_accepted_response(req_id)
            data_connection.send_message(json.dumps(response), True)
    def evaluate_del_user_request(self, agent_request, req_id, data_connection):
        del_user_request = AgentDelUserRequest(agent_request)
        if not del_user_request.sanity_check():
            response = ServerInterface.build_invalid_response(req_id)
            data_connection.send_message(json.dumps(response), True)
            return

        username = del_user_request.get_username()
        self.del_user(username)
        response = ServerInterface.build_accepted_response(req_id)
        data_connection.send_message(json.dumps(response), True)
    def evaluate_add_user_request(self, agent_request, req_id, data_connection):
        add_user_request = AgentAddUserRequest(agent_request)
        if not add_user_request.sanity_check():
            response = ServerInterface.build_invalid_response(req_id)
            data_connection.send_message(json.dumps(response), True)
            return

        username = add_user_request.get_username()
        password = add_user_request.get_password()
        logging.info('########## %s %s', username, password)
        self.add_user(username, password)
        response = ServerInterface.build_accepted_response(req_id)
        data_connection.send_message(json.dumps(response), True)
    def evaluate_request(self, request, data_connection):
        req_id = request.get_request_id()
    
        agent_request = AgentRequest(request)

        if not agent_request.sanity_check():
            response = ServerInterface.build_invalid_response(req_id)
            data_connection.send_message(json.dumps(response), True)
            return

        request_type = agent_request.get_type()
        try:
            self.request_handlers[request_type](agent_request, req_id,\
                                                data_connection)
        except:
            logging.warning('AgentListener: Invalid request type', exc_info=True)
    def handle_real_time_close(self, device_sensor_request, req_id,
                               data_connection):
        close_request = DeviceSensorRealTimeClose(device_sensor_request)
        if not close_request.sanity_check():
            response = ServerInterface.build_invalid_response(req_id)
            data_connection.send_message(json.dumps(response), True)
            return

        original_req_id = close_request.get_request_id()
        gui_hostname = data_connection.peer_host
        try:
            command_id = self.active_command_connections[(gui_hostname,
                                                          original_req_id)]
            agent_hostname = device_sensor_request.get_agent_hostname()
            self.command_tracker.close_command_connection(agent_hostname,
                                                          command_id)
        except:
            pass

        response = ServerInterface.build_accepted_response(req_id)
        data_connection.send_message(json.dumps(response), True)
    def evaluate_request(self, request, data_connection):
        logging.debug('DeviceSensor: Evaluating request ...')
        
        req_id = request.get_request_id()
        device_sensor_request = DeviceSensorRequest(request)
        if not device_sensor_request.sanity_check():
            logging.warning('DeviceSensor: Invalid request')
            response = ServerInterface.build_invalid_response(req_id)
            data_connection.send_message(json.dumps(response), True)
            return

        if self.command_tracker is None:
            logging.warning('DeviceSensor: CommandTracker not found')
            response = ServerInterface.build_internal_error_response(req_id)
            data_connection.send_message(json.dumps(response), True)
            return

        request_type = device_sensor_request.get_type()
        try:
            self.request_handlers[request_type](device_sensor_request, req_id,
                                                data_connection)
        except:
            logging.warning('DeviceSensor: Invalid request type', exc_info=True)