/* Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "mpm.h"
#include "ap_mpm.h"
#include "httpd.h"
#include "http_config.h"
#include "mpm_common.h"

#include "scoreboard.h"

#include "simple_types.h"

/* Thie file contains the absolute minimal MPM API, to interface with httpd. */

ap_generation_t volatile ap_my_generation = 0;
server_rec *ap_server_conf = NULL;

/* pqXXXXXX: removed from trunk. */
int
ap_graceful_stop_signalled()
{
  return 0;
}

apr_status_t
ap_mpm_query(int query_code, int *result)
{
  simple_core_t* sc = simple_core_get();

  switch (query_code) {
    case AP_MPMQ_IS_THREADED:
      *result = AP_MPMQ_STATIC;
      return APR_SUCCESS;
      break;
    case AP_MPMQ_IS_FORKED:
      *result = AP_MPMQ_DYNAMIC;
      return APR_SUCCESS;
      break;
    case AP_MPMQ_IS_ASYNC:
      *result = 1;
      return APR_SUCCESS;
      break;
    /* pqXXXXXXX: support configuration queries */
    case AP_MPMQ_MAX_DAEMON_USED:
      *result = sc->procmgr.daemons_to_start;
      return APR_SUCCESS;
      break;
    case AP_MPMQ_HARD_LIMIT_DAEMONS:
      *result = sc->procmgr.daemons_to_start;
      return APR_SUCCESS;
      break;
    case AP_MPMQ_HARD_LIMIT_THREADS:
      *result = sc->procmgr.thread_count;
      return APR_SUCCESS;
      break;
    case AP_MPMQ_MAX_THREADS:
      *result = sc->procmgr.thread_count;
      return APR_SUCCESS;
      break;
    case AP_MPMQ_MAX_SPARE_DAEMONS:
      *result = sc->procmgr.daemons_max_free;
      return APR_SUCCESS;
      break;
    case AP_MPMQ_MIN_SPARE_DAEMONS:
      *result = sc->procmgr.daemons_min_free;
      return APR_SUCCESS;
      break;
    case AP_MPMQ_MIN_SPARE_THREADS:
    case AP_MPMQ_MAX_SPARE_THREADS:
      *result = 0;
      return APR_SUCCESS;
      break;
    case AP_MPMQ_MAX_REQUESTS_DAEMON:
      *result = sc->procmgr.max_requests_per_child;
      return APR_SUCCESS;
      break;
    case AP_MPMQ_MAX_DAEMONS:
      *result = sc->procmgr.daemons_to_start;
      return APR_SUCCESS;
      break;
    case AP_MPMQ_MPM_STATE:
      *result = sc->mpm_state;
      return APR_SUCCESS;
    default:
      break;
  }

  return APR_ENOTIMPL;
}

static int simple_open_logs(apr_pool_t *p,
                            apr_pool_t *plog,
                            apr_pool_t *ptemp,
                            server_rec *s)
{
  ap_server_conf = s;
  /**/
  
}

static void
simple_process_start(process_rec *process)
{
  apr_status_t rv;
  /* this is our first 'real' entry point, so reset everything here. */
  rv = simple_core_init(simple_core_get(), process->pool);
  
  if (rv) {
    /* pqXXXXX: make smarter */
    fprintf(stderr, "Unable to init Simple MPM Core. (rv=%d)", rv);
    exit(EXIT_FAILURE);
  }

  ap_mpm_rewrite_args(process);
}

static int
simple_check_config(apr_pool_t *p, apr_pool_t *plog,
                    apr_pool_t *ptemp, server_rec *s)
{
  /* pqXXXX: check configuration */
  return OK;
}
  
static void
simple_hooks(apr_pool_t *p)
{
  static const char *const aszSucc[] = {"core.c", NULL};

  ap_hook_open_logs(simple_open_logs, NULL,
                    aszSucc, APR_HOOK_REALLY_FIRST);

  ap_hook_check_config(simple_check_config,
                       NULL, NULL, APR_HOOK_MIDDLE);

}

static const char*
set_start(cmd_parms *cmd, void *baton, const char *arg)
{
  const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
  if (err != NULL) {
    return err;
  }
  
  simple_core_get()->procmgr.daemons_to_start = atoi(arg);
  return NULL;
}


static const char*
set_poolsize(cmd_parms *cmd, void *baton, const char *arg1, const char *arg2)
{
  const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
  if (err != NULL) {
    return err;
  }
  
  simple_core_get()->procmgr.daemons_min_free = atoi(arg1);
  simple_core_get()->procmgr.daemons_max_free = atoi(arg2);
  return NULL;
}


static const char*
set_threadcount(cmd_parms *cmd, void *baton, const char *arg)
{
  const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
  if (err != NULL) {
    return err;
  }

  simple_core_get()->procmgr.thread_count = atoi(arg);
  return NULL;
}

static const command_rec simple_cmds[] = {
  AP_INIT_TAKE1("SimpleProcStart", set_start, NULL, RSRC_CONF,
                "Number of child processes launched at server startup"),
  AP_INIT_TAKE2("SimpleProcPool", set_poolsize, NULL, RSRC_CONF,
                "Set minimum and maximum number of spare processes"),
  AP_INIT_TAKE1("SimpleThreadCount", set_threadcount, NULL, RSRC_CONF,
                "Set the number of Worker Threads Per-Process"),
  { NULL }
};



module AP_MODULE_DECLARE_DATA mpm_simple_module = {
  MPM20_MODULE_STUFF,
  simple_process_start,       /* hook to run before apache parses args */
  NULL,                       /* create per-directory config structure */
  NULL,                       /* merge per-directory config structures */
  NULL,                       /* create per-server config structure */
  NULL,                       /* merge per-server config structures */
  simple_cmds,                /* command apr_table_t */
  simple_hooks                /* register_hooks */
};

  

