def get_configs_handler(self, message, command_id, user_data,
                            command_connection, closed=False):
        req_id = user_data
        try:
            data_connection = self.get_configs_map[command_id]
        except:
            err_msg = 'AgentListener: Failed sending agent GET_CONFIGS response.'
            err_msg += '\ndata_connection object not found for command_id: %s' %\
                        command_id
            err_msg += '\nDumping get_configs_map dict:\n'
            err_msg += '%s' % str(self.get_configs_map)
            logging.error(err_msg, exc_info=True)
            return

        if closed:
            err_msg = 'AgentListener: Failed GET_CONFIGS from agent.\n'
            err_msg += 'Connection closed by agent.'
            logging.warning(err_msg)
            response = ServerInterface.build_internal_error_response(req_id)
            data_connection.send_message(json.dumps(response), True)
            return

        if command_id not in self.get_configs_map.keys():
            err_msg = 'AgentListener: GET_CONFIGS agent request with invalid id'
            logging.warning(err_msg)
            response = ServerInterface.build_internal_error_response(req_id)
            data_connection.send_message(json.dumps(response), True)
            return

        configs = message
        response = ServerInterface.build_accepted_response(req_id)
        body = dict()
        body[AgentGetConfigsResponseBody.configs] = configs
        response[ResponseFields.body] = body
        data_connection.send_message(json.dumps(response), True)
    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 handle_real_time_request(self, device_sensor_request, req_id,
                                 data_connection):
        agent_hostname = device_sensor_request.get_agent_hostname()

        command_id = self.command_tracker.send_command(
            agent_hostname, 'DeviceSensor', 'REAL_TIME_REQUEST',
            handler_function=self.real_time_command_callback,
            handler_user_data=data_connection)

        if command_id is None:
            logging.warning('DeviceSensor: Error sending command')
            response = ServerInterface.build_internal_error_response(req_id)
            data_connection.send_message(json.dumps(response), True)
            return

        gui_hostname = data_connection.peer_host
        self.active_command_connections[(gui_hostname, req_id)] =\
            command_id
    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)