
#include <unistd.h>

#include "client.h"

#include "command_handlers.h"
#include "http_request_handler.h"

#include "http_request_list.h"
#include "http_response.h"

Processor *g_processor;

void
client_init (const key_t ipcKey)
{
    logFinit();

    logFmsg (CONT_LOG_DEBUG, "causality-client/%d: initting client.", getpid());

    thrFmod_init();

    g_processor = processor_new (ipcKey, true);

    processor_addRequestHandler (g_processor, &command_HttpRequestStart, g_processor);
    processor_addRequestHandler (g_processor, &command_HttpRequestPing, NULL);

    processor_finishedInit (g_processor);

    logFmsg (CONT_LOG_DEBUG, "causality-client/%d: finished initting client.", getpid());
}

void
client_destroy()
{
    HttpRequestList *requestList = http_request_list_getRequestList();

    http_request_list_destroy (requestList);

    processor_destroy (g_processor);
}

void *
client_get_request()
{
    HttpRequestList *requestList = http_request_list_getRequestList();
    HttpRequest *request = http_request_list_getNextRequest (requestList);

    return (request);
}

bool
client_write_data (HttpRequest *httpRequest, const char *data, int dataLen)
{
    return (http_request_writeData (httpRequest, data, dataLen));
}

bool
client_write_string (HttpRequest *httpRequest, const char *data)
{
    return (http_request_writeString (httpRequest, data));
}

const char *
client_get_remote_ip (HttpRequest *instance)
{
    return (http_request_getRemoteIP (instance));
}

const char *
client_get_uri (const HttpRequest *httpRequest)
{
    return (http_request_getUri (httpRequest));
}

const char *
client_get_query (const HttpRequest *httpRequest)
{
    return (http_request_getQuery (httpRequest));
}

const char *
client_get_variable (const HttpRequest *httpRequest, char *key)
{
    return (http_request_getVariable (httpRequest, key));
}

const char *
client_get_request_header (const HttpRequest *httpRequest, const char *key)
{
    return (http_request_getRequestHeader (httpRequest, key));
}

const char *
client_get_method (const HttpRequest *httpRequest)
{
    return (http_request_getMethod (httpRequest));
}

int
client_get_entity (HttpRequest *httpRequest, char *buf, int bufLen)
{
    return (http_request_getEntity (httpRequest, buf, bufLen));
}

bool
client_is_entire_entity_loaded (const HttpRequest *httpRequest)
{
    return (http_request_isEntireEntityLoaded (httpRequest));
}

void
client_set_response_header (const HttpRequest *httpRequest, const char *key, const char *value)
{
    http_request_setResponseHeader (httpRequest, key, value);
}

void
client_set_status (HttpRequest *httpRequest, const int status, const char *description)
{
    http_request_setHttpStatus (httpRequest, status, description);
}

bool
client_headers_sent (const HttpRequest *request)
{
    return (http_request_headersSent (request));
}

bool
client_finish (const HttpRequest *httpRequest)
{
    bool ret;

    ret = http_request_finish (httpRequest);

    http_request_list_removeRequest (http_request_list_getRequestList(), 
            http_request_getToken (httpRequest));

    return (ret);
}
