started some code for hcl client

This commit is contained in:
hyung-hwan 2018-03-18 15:29:16 +00:00
parent 5aa3a72eea
commit 89f3fc2d08
5 changed files with 426 additions and 51 deletions

View File

@ -27,6 +27,25 @@
#include "hcl-c.h"
#include "hcl-prv.h"
enum hcl_reply_type_t
{
HCL_REPLY_TYPE_OK = 0,
HCL_REPLY_TYPE_ERROR = 1
};
typedef enum hcl_reply_type_t hcl_reply_type_t;
enum hcl_client_proto_state_t
{
HCL_CLIENT_PROTO_STATE_START,
HCL_CLIENT_PROTO_STATE_IN_REPLY_NAME,
HCL_CLIENT_PROTO_STATE_IN_REPLY_VALUE,
HCL_CLIENT_PROTO_STATE_IN_HEADER_NAME,
HCL_CLIENT_PROTO_STATE_IN_HEADER_VALUE,
HCL_CLIENT_PROTO_STATE_IN_DATA,
HCL_CLIENT_PROTO_STATE_IN_BINARY_DATA,
HCL_CLIENT_PROTO_STATE_IN_CHUNK,
};
typedef enum hcl_client_proto_state_t hcl_client_proto_state_t;
struct hcl_client_proto_t
{
@ -38,25 +57,45 @@ struct hcl_client_proto_t
hcl_oow_t len;
hcl_oow_t capa;
} req;
hcl_client_proto_state_t state;
struct
{
/*
hcl_ooch_t* ptr;
hcl_oow_t len;
hcl_oow_t capa;
*/
struct
{
hcl_ooch_t* ptr;
hcl_oow_t len;
hcl_oow_t capa;
} tok;
hcl_reply_type_t type;
} rep;
};
typedef struct hcl_client_proto_t hcl_client_proto_t;
struct hcl_client_t
{
hcl_mmgr_t* mmgr;
hcl_cmgr_t* cmgr;
/*hcl_t* dummy_hcl;*/
hcl_errnum_t errnum;
struct
{
hcl_ooch_t buf[HCL_ERRMSG_CAPA];
hcl_oow_t len;
} errmsg;
};
/* ========================================================================= */
#if 0
static void proto_start_request (hcl_client_proto_t* proto)
{
proto->req.len = 0;
@ -74,24 +113,162 @@ static int proto_end_request (hcl_client_proto_t* proto)
return 0;
}
#endif
static void proto_start_response (hcl_client_proto_t* proto)
{
proto->state = HCL_CLIENT_PROTO_STATE_START;
//proto->rep.len = 0;
}
static void proto_feed_reply_data (hcl_client_proto_t* proto, const hcl_bch_t* ptr, hcl_oow_t len)
{
const hcl_bch_t* end = ptr + len;
while (ptr < end)
{
hcl_bch_t b = *ptr++;
switch (b)
static HCL_INLINE int is_spacechar (hcl_bch_t c)
{
/* TODO: handle other space unicode characters */
switch (c)
{
case ' ':
case '\f': /* formfeed */
case '\r': /* carriage return */
case '\t': /* horizon tab */
case '\v': /* vertical tab */
return 1;
default:
return 0;
}
}
static HCL_INLINE int is_alphachar (hcl_ooci_t c)
{
/* TODO: support full unicode */
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
}
static HCL_INLINE int is_digitchar (hcl_ooci_t c)
{
/* TODO: support full unicode */
return (c >= '0' && c <= '9');
}
static void clear_reply_token (hcl_client_proto_t* proto)
{
proto->rep.tok.len = 0;
}
static int add_to_reply_token (hcl_client_proto_t* proto, hcl_ooch_t ch)
{
if (proto->rep.tok.len >= proto->rep.tok.capa)
{
hcl_ooch_t* tmp;
hcl_oow_t newcapa;
newcapa = HCL_ALIGN_POW2(proto->rep.tok.len + 1, 128);
tmp = hcl_client_reallocmem(proto->client, proto->rep.tok.ptr, newcapa * HCL_SIZEOF(*tmp));
if (!tmp) return -1;
proto->rep.tok.capa = newcapa;
proto->rep.tok.ptr = tmp;
}
proto->rep.tok.ptr[proto->rep.tok.len++] = ch;
return -1;
}
static int proto_feed_reply_data (hcl_client_proto_t* proto, const hcl_bch_t* data, hcl_oow_t len)
{
const hcl_bch_t* ptr;
const hcl_bch_t* end;
ptr = data;
end = ptr + len;
if (proto->state == HCL_CLIENT_PROTO_STATE_IN_BINARY_DATA)
{
}
else
{
while (ptr < end)
{
case '\0':
hcl_ooch_t c;
#if defined(HCL_OOCH_IS_UCH)
hcl_oow_t bcslen, ucslen;
int n;
bcslen = end - ptr;
ucslen = 1;
n = hcl_conv_bcsn_to_ucsn_with_cmgr(ptr, &bcslen, &c, &ucslen, proto->client->cmgr, 0);
if (n <= -1)
{
if (n == -3)
{
/* incomplete sequence */
}
}
#else
c = *ptr++;
#endif
switch (proto->state)
{
case HCL_CLIENT_PROTO_STATE_START:
if (c == '.')
{
proto->state = HCL_CLIENT_PROTO_STATE_IN_REPLY_NAME;
clear_reply_token (proto);
if (add_to_reply_token(proto, c) <= -1) goto oops;
break;
}
else if (!is_spacechar(c))
{
/* TODO: set error code? or log error messages? */
goto oops;
}
break;
case HCL_CLIENT_PROTO_STATE_IN_REPLY_NAME:
if (is_alphachar(c) || (proto->rep.tok.len > 2 && c == '-'))
{
if (add_to_reply_token(proto, c) <= -1) goto oops;
}
else
{
if (hcl_compoocharsbcstr(proto->rep.tok.ptr, proto->rep.tok.len, ".OK") == 0)
{
proto->rep.type = HCL_REPLY_TYPE_OK;
}
else if (hcl_compoocharsbcstr(proto->rep.tok.ptr, proto->rep.tok.len, ".ERROR") == 0)
{
proto->rep.type = HCL_REPLY_TYPE_ERROR;
}
clear_reply_token (proto);
if (add_to_reply_token(proto, c) <= -1) goto oops;
}
break;
/*case HCL_CLIENT_PROTO_STATE_IN_START_LINE:*/
case HCL_CLIENT_PROTO_STATE_IN_HEADER_NAME:
break;
case HCL_CLIENT_PROTO_STATE_IN_HEADER_VALUE:
break;
}
}
}
return 0;
oops:
/* TODO: compute the number of processes bytes so far and return it via a parameter??? */
return -1;
}
/* ========================================================================= */
@ -217,6 +394,64 @@ void hcl_client_close (hcl_client_t* client)
HCL_MMGR_FREE (client->mmgr, client);
}
hcl_errnum_t hcl_client_geterrnum (hcl_client_t* client)
{
return client->errnum;
}
const hcl_ooch_t* hcl_client_geterrstr (hcl_client_t* client)
{
return hcl_errnum_to_errstr(client->errnum);
}
const hcl_ooch_t* hcl_client_geterrmsg (hcl_client_t* client)
{
if (client->errmsg.len <= 0) return hcl_errnum_to_errstr(client->errnum);
return client->errmsg.buf;
}
void hcl_client_seterrnum (hcl_client_t* client, hcl_errnum_t errnum)
{
/*if (client->shuterr) return; */
client->errnum = errnum;
client->errmsg.len = 0;
}
void* hcl_client_allocmem (hcl_client_t* client, hcl_oow_t size)
{
void* ptr;
ptr = HCL_MMGR_ALLOC(client->mmgr, size);
if (!ptr) hcl_client_seterrnum (client, HCL_ESYSMEM);
return ptr;
}
void* hcl_client_callocmem (hcl_client_t* client, hcl_oow_t size)
{
void* ptr;
ptr = HCL_MMGR_ALLOC(client->mmgr, size);
if (!ptr) hcl_client_seterrnum (client, HCL_ESYSMEM);
else HCL_MEMSET (ptr, 0, size);
return ptr;
}
void* hcl_client_reallocmem (hcl_client_t* client, void* ptr, hcl_oow_t size)
{
ptr = HCL_MMGR_REALLOC(client->mmgr, ptr, size);
if (!ptr) hcl_client_seterrnum (client, HCL_ESYSMEM);
return ptr;
}
void hcl_client_freemem (hcl_client_t* client, void* ptr)
{
HCL_MMGR_FREE (client->mmgr, ptr);
}
/* ========================================================================= */
int hcl_client_start (hcl_client_t* client, const hcl_bch_t* addrs)
{
@ -228,3 +463,6 @@ int hcl_client_start (hcl_client_t* client, const hcl_bch_t* addrs)
int hcl_client_sendreq (hcl_client_t* client, const hcl_bch_t* addrs)
{
}

View File

@ -49,6 +49,92 @@ HCL_EXPORT void hcl_client_close (
);
HCL_EXPORT hcl_errnum_t hcl_client_geterrnum (
hcl_client_t* client
);
HCL_EXPORT const hcl_ooch_t* hcl_client_geterrstr (
hcl_client_t* client
);
HCL_EXPORT const hcl_ooch_t* hcl_client_geterrmsg (
hcl_client_t* client
);
HCL_EXPORT void hcl_client_seterrnum (
hcl_client_t* client,
hcl_errnum_t errnum
);
HCL_EXPORT void* hcl_client_allocmem (
hcl_client_t* client,
hcl_oow_t size
);
HCL_EXPORT void* hcl_client_callocmem (
hcl_client_t* client,
hcl_oow_t size
);
HCL_EXPORT void* hcl_client_reallocmem (
hcl_client_t* client,
void* ptr,
hcl_oow_t size
);
HCL_EXPORT void hcl_client_freemem (
hcl_client_t* client,
void* ptr
);
HCL_EXPORT hcl_errnum_t hcl_client_geterrnum (
hcl_client_t* client
);
HCL_EXPORT const hcl_ooch_t* hcl_client_geterrstr (
hcl_client_t* client
);
HCL_EXPORT const hcl_ooch_t* hcl_client_geterrmsg (
hcl_client_t* client
);
HCL_EXPORT void hcl_client_seterrnum (
hcl_client_t* client,
hcl_errnum_t errnum
);
HCL_EXPORT void* hcl_client_allocmem (
hcl_client_t* client,
hcl_oow_t size
);
HCL_EXPORT void* hcl_client_callocmem (
hcl_client_t* client,
hcl_oow_t size
);
HCL_EXPORT void* hcl_client_reallocmem (
hcl_client_t* client,
void* ptr,
hcl_oow_t size
);
HCL_EXPORT void hcl_client_freemem (
hcl_client_t* client,
void* ptr
);
#if defined(__cplusplus)
}
#endif

View File

@ -1046,7 +1046,7 @@ hcl_server_proto_t* hcl_server_proto_open (hcl_oow_t xtnsize, hcl_server_worker_
vmprim.vm_gettime = vm_gettime;
vmprim.vm_sleep = vm_sleep;
proto = (hcl_server_proto_t*)HCL_MMGR_ALLOC(worker->server->mmgr, HCL_SIZEOF(*proto));
proto = (hcl_server_proto_t*)hcl_server_allocmem(worker->server, HCL_SIZEOF(*proto));
if (!proto) return HCL_NULL;
HCL_MEMSET (proto, 0, HCL_SIZEOF(*proto));
@ -1087,16 +1087,16 @@ oops:
if (proto)
{
if (proto->hcl) hcl_close (proto->hcl);
HCL_MMGR_FREE (proto->worker->server->mmgr, proto);
hcl_server_freemem (proto->worker->server, proto);
}
return HCL_NULL;
}
void hcl_server_proto_close (hcl_server_proto_t* proto)
{
if (proto->tok.ptr) HCL_MMGR_FREE (proto->worker->server->mmgr, proto->tok.ptr);
if (proto->tok.ptr) hcl_server_freemem (proto->worker->server, proto->tok.ptr);
hcl_close (proto->hcl);
HCL_MMGR_FREE (proto->worker->server->mmgr, proto);
hcl_server_freemem (proto->worker->server, proto);
}
static int write_reply_chunk (hcl_server_proto_t* proto)
@ -1293,6 +1293,18 @@ static HCL_INLINE int is_spacechar (hcl_ooci_t c)
}
}
static HCL_INLINE int is_alphachar (hcl_ooci_t c)
{
/* TODO: support full unicode */
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
}
static HCL_INLINE int is_digitchar (hcl_ooci_t c)
{
/* TODO: support full unicode */
return (c >= '0' && c <= '9');
}
static HCL_INLINE int read_char (hcl_server_proto_t* proto)
{
proto->lxc = hcl_readchar(proto->hcl);
@ -1329,7 +1341,7 @@ static HCL_INLINE int add_token_char (hcl_server_proto_t* proto, hcl_ooch_t c)
hcl_oow_t capa;
capa = HCL_ALIGN_POW2(proto->tok.len + 1, HCL_SERVER_TOKEN_NAME_ALIGN);
tmp = (hcl_ooch_t*)HCL_MMGR_REALLOC(proto->worker->server->mmgr, proto->tok.ptr, capa * HCL_SIZEOF(*tmp));
tmp = (hcl_ooch_t*)hcl_server_reallocmem(proto->worker->server, proto->tok.ptr, capa * HCL_SIZEOF(*tmp));
if (!tmp)
{
HCL_LOG0 (proto->hcl, SERVER_LOGMASK_ERROR, "Out of memory in allocating a token buffer\n");
@ -1344,18 +1356,6 @@ static HCL_INLINE int add_token_char (hcl_server_proto_t* proto, hcl_ooch_t c)
return 0;
}
static HCL_INLINE int is_alphachar (hcl_ooci_t c)
{
/* TODO: support full unicode */
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
}
static HCL_INLINE int is_digitchar (hcl_ooci_t c)
{
/* TODO: support full unicode */
return (c >= '0' && c <= '9');
}
static void classify_current_ident_token (hcl_server_proto_t* proto)
{
static struct cmd_t
@ -1832,10 +1832,10 @@ int hcl_server_proto_handle_request (hcl_server_proto_t* proto)
hcl_server_t* hcl_server_open (hcl_mmgr_t* mmgr, hcl_oow_t xtnsize, hcl_server_prim_t* prim, hcl_errnum_t* errnum)
{
hcl_server_t* server;
hcl_t* hcl;
hcl_server_t* server = HCL_NULL;
hcl_t* hcl = HCL_NULL;
hcl_vmprim_t vmprim;
hcl_tmr_t* tmr;
hcl_tmr_t* tmr = HCL_NULL;
dummy_hcl_xtn_t* xtn;
int pfd[2], fcv;
unsigned int trait;
@ -1857,26 +1857,19 @@ hcl_server_t* hcl_server_open (hcl_mmgr_t* mmgr, hcl_oow_t xtnsize, hcl_server_p
vmprim.vm_sleep = vm_sleep;
hcl = hcl_open(mmgr, HCL_SIZEOF(*xtn), 2048, &vmprim, errnum);
if (!hcl)
{
HCL_MMGR_FREE (mmgr, server);
return HCL_NULL;
}
if (!hcl) goto oops;
tmr = hcl_tmr_open(hcl, 0, 1024); /* TOOD: make the timer's default size configurable */
if (!tmr)
{
hcl_close (hcl);
HCL_MMGR_FREE (mmgr, server);
return HCL_NULL;
if (errnum) *errnum = HCL_ESYSMEM;
goto oops;
}
if (pipe(pfd) <= -1)
{
hcl_tmr_close (tmr);
hcl_close (hcl);
HCL_MMGR_FREE (mmgr, server);
return HCL_NULL;
if (errnum) *errnum = hcl_syserr_to_errnum(errno);
goto oops;
}
#if defined(O_CLOEXEC)
@ -1932,13 +1925,20 @@ hcl_server_t* hcl_server_open (hcl_mmgr_t* mmgr, hcl_oow_t xtnsize, hcl_server_p
hcl_setoption (server->dummy_hcl, HCL_TRAIT, &trait);
return server;
oops:
/* NOTE: pipe should be closed if jump to here is made after pipe() above */
if (tmr) hcl_tmr_close (tmr);
if (hcl) hcl_close (hcl);
if (server) HCL_MMGR_FREE (mmgr, server);
return HCL_NULL;
}
void hcl_server_close (hcl_server_t* server)
{
if (server->wid_map.ptr)
{
HCL_MMGR_FREE (server->mmgr, server->wid_map.ptr);
hcl_server_freemem(server, server->wid_map.ptr);
server->wid_map.capa = 0;
server->wid_map.free_first = HCL_SERVER_WID_INVALID;
server->wid_map.free_last = HCL_SERVER_WID_INVALID;
@ -1976,12 +1976,8 @@ static HCL_INLINE int prepare_to_acquire_wid (hcl_server_t* server)
new_capa = HCL_SERVER_WID_MAX;
}
tmp = (hcl_server_wid_map_data_t*)HCL_MMGR_REALLOC(server->mmgr, server->wid_map.ptr, HCL_SIZEOF(*tmp) * new_capa);
if (!tmp)
{
hcl_server_seterrnum (server, HCL_ESYSERR);
return -1;
}
tmp = (hcl_server_wid_map_data_t*)hcl_server_reallocmem(server, server->wid_map.ptr, HCL_SIZEOF(*tmp) * new_capa);
if (!tmp) return -1;
server->wid_map.free_first = server->wid_map.capa;
for (i = server->wid_map.capa, j = server->wid_map.capa + 1; j < new_capa; i++, j++)
@ -2039,7 +2035,7 @@ static hcl_server_worker_t* alloc_worker (hcl_server_t* server, int cli_sck, con
{
hcl_server_worker_t* worker;
worker = (hcl_server_worker_t*)HCL_MMGR_ALLOC(server->mmgr, HCL_SIZEOF(*worker));
worker = (hcl_server_worker_t*)hcl_server_allocmem(server, HCL_SIZEOF(*worker));
if (!worker) return HCL_NULL;
HCL_MEMSET (worker, 0, HCL_SIZEOF(*worker));
@ -2053,7 +2049,7 @@ static hcl_server_worker_t* alloc_worker (hcl_server_t* server, int cli_sck, con
if (server->wid_map.free_first == HCL_SERVER_WID_INVALID && prepare_to_acquire_wid(server) <= -1)
{
HCL_MMGR_FREE (server->mmgr, worker);
hcl_server_freemem (server, worker);
return HCL_NULL;
}
@ -2092,7 +2088,7 @@ static void free_worker (hcl_server_worker_t* worker)
}
release_wid (worker->server, worker);
HCL_MMGR_FREE (worker->server->mmgr, worker);
hcl_server_freemem (worker->server, worker);
}
static void add_worker_to_server (hcl_server_t* server, hcl_server_worker_state_t wstate, hcl_server_worker_t* worker)
@ -2604,3 +2600,34 @@ void hcl_server_seterrufmt (hcl_server_t* server, hcl_errnum_t errnum, const hcl
hcl_copyoochars (server->errmsg.buf, server->dummy_hcl->errmsg.buf, HCL_COUNTOF(server->errmsg.buf));
server->errmsg.len = server->dummy_hcl->errmsg.len;
}
void* hcl_server_allocmem (hcl_server_t* server, hcl_oow_t size)
{
void* ptr;
ptr = HCL_MMGR_ALLOC(server->mmgr, size);
if (!ptr) hcl_server_seterrnum (server, HCL_ESYSMEM);
return ptr;
}
void* hcl_server_callocmem (hcl_server_t* server, hcl_oow_t size)
{
void* ptr;
ptr = HCL_MMGR_ALLOC(server->mmgr, size);
if (!ptr) hcl_server_seterrnum (server, HCL_ESYSMEM);
else HCL_MEMSET (ptr, 0, size);
return ptr;
}
void* hcl_server_reallocmem (hcl_server_t* server, void* ptr, hcl_oow_t size)
{
ptr = HCL_MMGR_REALLOC(server->mmgr, ptr, size);
if (!ptr) hcl_server_seterrnum (server, HCL_ESYSMEM);
return ptr;
}
void hcl_server_freemem (hcl_server_t* server, void* ptr)
{
HCL_MMGR_FREE (server->mmgr, ptr);
}

View File

@ -176,6 +176,29 @@ HCL_EXPORT void hcl_server_logufmt (
...
);
HCL_EXPORT void* hcl_server_allocmem (
hcl_server_t* server,
hcl_oow_t size
);
HCL_EXPORT void* hcl_server_callocmem (
hcl_server_t* server,
hcl_oow_t size
);
HCL_EXPORT void* hcl_server_reallocmem (
hcl_server_t* server,
void* ptr,
hcl_oow_t size
);
HCL_EXPORT void hcl_server_freemem (
hcl_server_t* server,
void* ptr
);
#if defined(__cplusplus)
}
#endif

View File

@ -240,6 +240,7 @@ static hcl_oop_t string_to_num (hcl_t* hcl, hcl_oocs_t* str, int radixed)
if (negsign) base = -base;
return hcl_strtoint(hcl, ptr, end - ptr, base);
}
static HCL_INLINE int is_spacechar (hcl_ooci_t c)
{
/* TODO: handle other space unicode characters */