/*  -
   Copyright (C) 2006 Weongyo Jeong (weongyo@gmail.com)

This file is part of ROVM.

ROVM is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 2, or (at your option) any later
version.

ROVM is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING.  If not, write to the Free
Software Foundation, 59 Temple Place - Suite 330, Boston, MA
02111-1307, USA.  */

#ifndef _ROVM_REQUEST_H
#define _ROVM_REQUEST_H

#define	ERRLOG_EMERG		0	/* system is unusable */
#define	ERRLOG_ALERT		1	/* action must be taken immediately */
#define	ERRLOG_CRIT		2	/* critical conditions */
#define	ERRLOG_ERR		3	/* error conditions */
#define	ERRLOG_WARNING		4	/* warning conditions */
#define	ERRLOG_NOTICE		5	/* normal but significant condition */
#define	ERRLOG_INFO		6	/* informational */
#define	ERRLOG_DEBUG		7	/* debug-level messages */

#define	ERRLOG_LEVELMASK	7	/* mask off the level value */

#define ERRLOG_WITHERRNO	(ERRLOG_LEVELMASK + 1)
#define ERRLOG_MARK		__FILE__,__LINE__

#define ERRORS_LEVEL(NODE)		((NODE)->level)
#define ERRORS_STRING(NODE)		((NODE)->str)
#define ERRORS_NEXT(NODE)		((NODE)->next)

/**
   Request 를 처리하면서 발생한 오류가 존재할 경우, 해당 메세지들을 linked-list
   로 구성되어 존재하게 되며, 결과값으로 사용자에게 전달되게 된다.
 */
struct request_errors
{
  /** 오류 혹은 exception 의 error level.  위에 정의된 ERRLOG_* 매크로를
      참조하라.  */
  char level;
  /** 오류 문자열.  */
  char *str;

  struct request_errors *next;
};

#define REQUEST_ROVM(NODE)              (CONN_TS (REQUEST_CONN (NODE))->arg)
#define REQUEST_CONN(NODE)              ((NODE)->conn)
#define REQUEST_POOL(NODE)              ((NODE)->pool)
#define REQUEST_ERRORS(NODE)            ((NODE)->errors)

/**
   Request 에 대한 정보를 가지고 있는 구조체입니다.
 */
struct request_rec
{
  /** 현재 연결된 Connection 정보를 가지고 있는 정보.  */
  conn_rec *conn;

  /** Reqeuest 가 사용할 메모리 풀.  Connection 구조체에서 물려 받는다.  */
  rc_pool_t *pool;

  /** 현재 request 를 처리하면서 만난 오류들에 대한 linked-list.  이것은 connection 이
      중간에 갑자기 끊어지지 않는 한, 연결 마지막 부분에서 client 에게 보내게 된다.  */
  struct request_errors *errors;
};

typedef struct request_rec request_rec;

extern int rc_process_request (conn_rec *);
extern int request_add_error (request_rec *r, int level, const char *file, int line, const char *fmt, ...);
extern int request_add_error_core (request_rec *, int, const char *, int, const char *, va_list);

#endif
