Mercurial > MadButterfly
changeset 1039:176cba3ad32c refine_backend_if
IO manager for njs
author | Thinker K.F. Li <thinker@codemud.net> |
---|---|
date | Tue, 23 Nov 2010 10:12:49 +0800 |
parents | 48c69a18f2e4 |
children | 22e5cf38d8e8 |
files | nodejs/X_supp_njs.c nodejs/X_supp_njs.h nodejs/mbfly_njs.cc |
diffstat | 3 files changed, 122 insertions(+), 12 deletions(-) [+] |
line wrap: on
line diff
--- a/nodejs/X_supp_njs.c Tue Nov 23 09:30:48 2010 +0800 +++ b/nodejs/X_supp_njs.c Tue Nov 23 10:12:49 2010 +0800 @@ -18,6 +18,9 @@ #define ASSERT(x) #endif +#define OK 0 +#define ERR -1 + static void timer_cb(EV_P_ ev_timer *tmwatcher, int revent); /*! \brief Register next timeout with libev. @@ -76,6 +79,112 @@ set_next_timeout(rt); } +/*! \defgroup njs_io_man IO manager for nodejs. + * @{ + */ +struct _njs_io_reg { + ev_io iowatcher; + int fd; + mb_IO_cb_t cb; + void *data; +}; + +static int njs_io_man_reg(struct _mb_IO_man *io_man, + int fd, MB_IO_TYPE type, mb_IO_cb_t cb, void *data); +static void njs_io_man_unreg(struct _mb_IO_man *io_man, int io_hdl); +static mb_IO_man_t *njs_io_man_new(void); +static void njs_io_man_free(mb_IO_man_t *io_man); + +static mb_IO_man_t njs_io_man = { + njs_io_man_reg, + njs_io_man_unreg +}; + +/*! \brief IO factory to integrate MadButterfly to event loop of nodejs. + */ +static mb_IO_factory_t njs_io_factory = { + njs_io_man_new, + njs_io_man_free +}; + +/*! \brief Bridge libev callback to IO manager callback. + */ +static void +njs_io_man_cb(EV_P_ ev_io *iowatcher, int revent) { + struct _njs_io_reg *io_reg = + MEM2OBJ(iowatcher, struct _njs_io_reg, iowatcher); + MB_IO_TYPE type; + + switch(revent & (EV_READ | EV_WRITE)) { + case EV_READ: + type = MB_IO_R; + break; + case EV_WRITE: + type = MB_IO_W; + break; + case EV_READ | EV_WRITE: + type = MB_IO_RW; + break; + } + + io_reg->cb((int)io_reg, io_reg->fd, type, io_reg->data); +} + +static int +njs_io_man_reg(struct _mb_IO_man *io_man, + int fd, MB_IO_TYPE type, mb_IO_cb_t cb, void *data) { + int _type; + struct _njs_io_reg *io_reg; + + if(type == MB_IO_R) + _type = EV_READ; + else if(type == MB_IO_W) + _type == EV_WRITE; + else if(type == MB_IO_RW) + _type == EV_READ | EV_WRITE; + else + return ERR; + + io_reg = O_ALLOC(struct _njs_io_reg); + if(io_reg == NULL) + return ERR; + + io_reg->fd = fd; + io_reg->cb = cb; + io_reg->data = data; + + ev_io_init(&io_reg->iowatcher, njs_io_man_cb, fd, _type); + ev_io_start(&io_reg->iowatcher); + + return (int)io_reg; +} + +static void +njs_io_man_unreg(struct _mb_IO_man *io_man, int io_hdl) { + struct _njs_io_reg *io_reg = (struct _njs_io_reg *)io_hdl; + + ev_io_stop(&io_reg->iowatcher); + free(io_reg); +} + +static mb_IO_man_t * +njs_io_man_new(void) { + return &njs_io_man; +} + +static void +njs_io_man_free(mb_IO_man_t *io_man) { +} + +/*! \brief Register an IO factory with MadButterfly backend. + */ +void +X_njs_MB_reg_IO_man(void) { + mb_reg_IO_facotry(&njs_io_factory); +} + +/* @} */ + /*! \brief Handle connection coming data and timeout of timers. * * \param rt is a runtime object for X. @@ -193,15 +302,15 @@ */ void X_njs_MB_no_more_event(njs_runtime_t *rt) { - void *xrt = rt->xrt; - extern void _X_MB_no_more_event(void *rt); + mb_rt_t *xrt = rt->xrt; + extern void _X_MB_no_more_event(mb_rt_t *rt); _X_MB_no_more_event(xrt); } /*! \brief Get X runtime that is backend of this njs runtime. */ -void * +mb_rt_t * _X_njs_MB_get_X_runtime(njs_runtime_t *rt) { return rt->xrt; }
--- a/nodejs/X_supp_njs.h Tue Nov 23 09:30:48 2010 +0800 +++ b/nodejs/X_supp_njs.h Tue Nov 23 10:12:49 2010 +0800 @@ -4,15 +4,16 @@ #define __X_SUPP_NJS_H_ #include <ev.h> +#include <mb_backend.h> typedef struct _njs_runtime { - ev_io iowatcher; ev_timer tmwatcher; int enable_io; int enable_timer; - void *xrt; + mb_rt_t *xrt; } njs_runtime_t; +extern void X_njs_MB_reg_IO_man(void); extern void X_njs_MB_init_handle_connection(njs_runtime_t *rt); extern void X_njs_MB_free(njs_runtime_t *rt); extern njs_runtime_t *X_njs_MB_new(char *display_name, int w, int h); @@ -21,13 +22,12 @@ extern int X_njs_MB_flush(njs_runtime_t *rt); extern void X_njs_MB_handle_single_event(njs_runtime_t *rt, void *evt); extern void X_njs_MB_no_more_event(njs_runtime_t *rt); -extern void *_X_njs_MB_get_X_runtime(njs_runtime_t *rt); +extern mb_rt_t *_X_njs_MB_get_X_runtime(njs_runtime_t *rt); -#define X_njs_MB_kbevents(rt) X_MB_kbevents((rt)->xrt) -#define X_njs_MB_rdman(rt) X_MB_rdman((rt)->xrt) -#define X_njs_MB_tman(rt) X_MB_tman((rt)->xrt) -#define X_njs_MB_ob_factory(rt) X_MB_ob_factory((rt)->xrt) -#define X_njs_MB_img_ldr(rt) X_MB_img_ldr((rt)->xrt) -#define X_njs_MB_kbevents(rt) X_MB_kbevents((rt)->xrt) +#define X_njs_MB_kbevents(rt) mb_runtime_kbevents((rt)->xrt) +#define X_njs_MB_rdman(rt) mb_runtime_rdman((rt)->xrt) +#define X_njs_MB_timer_man(rt) mb_runtime_timer_man((rt)->xrt) +#define X_njs_MB_ob_factory(rt) mb_runtime_ob_factory((rt)->xrt) +#define X_njs_MB_img_ldr(rt) mb_runtime_img_ldr((rt)->xrt) #endif /* __X_SUPP_NJS_H_ */
--- a/nodejs/mbfly_njs.cc Tue Nov 23 09:30:48 2010 +0800 +++ b/nodejs/mbfly_njs.cc Tue Nov 23 10:12:49 2010 +0800 @@ -185,6 +185,7 @@ xnjsmb_auto_mb_rt_temp->GetFunction()); target->Set(String::New("mb_rt_with_win"), xnjsmb_auto_mb_rt_with_win_temp->GetFunction()); + X_njs_MB_reg_IO_man(); } /* @} */