diff --git a/src/Makefile.am b/src/Makefile.am index b43e511..c4e4b41 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,25 +1,25 @@ MAINTAINERCLEANFILES = Makefile.in AM_CFLAGS = -fPIC -Werror -funsigned-char -Wno-pointer-sign AM_CPPFLAGS = -I$(top_builddir)/include sbin_PROGRAMS = boothd boothd_SOURCES = config.c main.c raft.c ticket.c transport.c \ - pacemaker.c handler.c + pacemaker.c handler.c request.c if BUILD_TIMER_C boothd_SOURCES += timer.c endif boothd_LDFLAGS = $(OS_DYFLAGS) -L./ boothd_LDADD = -lplumb -lplumbgpl -lz -lm boothd_CPPFLAGS = $(GLIB_CFLAGS) noinst_HEADERS = booth.h pacemaker.h \ - config.h log.h raft.h ticket.h transport.h handler.h + config.h log.h raft.h ticket.h transport.h handler.h request.h lint: -splint $(INCLUDES) $(LINT_FLAGS) $(CFLAGS) *.c diff --git a/src/request.c b/src/request.c new file mode 100644 index 0000000..3c7d136 --- /dev/null +++ b/src/request.c @@ -0,0 +1,81 @@ +/* + * Copyright (C) 2015 Dejan Muhamedagic + * + * This program 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.1 of the License, or (at your option) any later version. + * + * This software 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 this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include +#include +#include +#include +#include "booth.h" +#include "ticket.h" +#include "request.h" + +static GList *req_l = NULL; +static int req_id_cnt; + +/* add request to the queue; it is up to the caller to manage + * memory for the three parameters + */ + +void *add_req( + struct ticket_config *tk, + struct client *req_client, + struct boothc_ticket_msg *msg) +{ + struct request *rp; + + rp = g_new(struct request, 1); + if (!rp) + return NULL; + rp->id = req_id_cnt++; + rp->tk = tk; + rp->cl = req_client; + rp->msg = msg; + req_l = g_list_append(req_l, rp); + return rp; +} + +int get_req_id(const void *rp) +{ + if (!rp) + return -1; + return ((struct request *)rp)->id; +} + +static void del_req(GList *lp) +{ + if (!lp) + return; + req_l = g_list_delete_link(req_l, lp); +} + +void foreach_tkt_req(struct ticket_config *tk, req_fp f) +{ + GList *lp, *next; + struct request *rp; + + lp = g_list_first(req_l); + while (lp) { + next = g_list_next(lp); + rp = (struct request *)lp->data; + if (rp->tk == tk && + (f)(rp->tk, rp->cl, rp->msg) == 0) { + del_req(lp); /* don't need this request anymore */ + } + lp = next; + } +} diff --git a/src/request.h b/src/request.h new file mode 100644 index 0000000..90e335d --- /dev/null +++ b/src/request.h @@ -0,0 +1,56 @@ +/* + * Copyright (C) 2015 Dejan Muhamedagic + * + * This program 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.1 of the License, or (at your option) any later version. + * + * This software 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 this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#ifndef _REQUEST_H +#define _REQUEST_H + +#include "booth.h" +#include "config.h" + +/* Requests are coming from clients and get queued in a + * round-robin queue (fixed size) + * + * This is one way to make the server more responsive and less + * dependent on misbehaving clients. The requests are queued and + * later served from the server loop. + */ + +struct request { + /** Request ID */ + int id; + + /** The ticket. */ + struct ticket_config *tk; + + /** The client which sent the request */ + struct client *cl; + + /** The message containing the request */ + struct boothc_ticket_msg *msg; +}; + +typedef int (*req_fp)( + struct ticket_config *, struct client *, + struct boothc_ticket_msg *); + +void *add_req(struct ticket_config *tk, struct client *req_client, + struct boothc_ticket_msg *msg); +void foreach_tkt_req(struct ticket_config *tk, req_fp f); +int get_req_id(const void *rp); + +#endif /* _REQUEST_H */