All of lore.kernel.org
 help / color / mirror / Atom feed
* [Patch 2/2] CLD: drop dependency on libevent from libcldc
@ 2009-11-28  3:52 Pete Zaitcev
  2009-11-28  9:07 ` Jeff Garzik
  0 siblings, 1 reply; 4+ messages in thread
From: Pete Zaitcev @ 2009-11-28  3:52 UTC (permalink / raw)
  To: Jeff Garzik; +Cc: Project Hail List

Let's remove the dependency on libevent completely. The core CLD daemon
did not use libeven for some time, but getting rid of it entirely required
an implementation of some kind of common main loop for tests, which is
what this patch does.

Since we were refactoring, we add a common header and library for
tests, same as Chunk has.

This patch introduces an incompatibility, so Chunk and tabled must
be rebuilt. This is because the layout of struct cldc_udp changes.

Signed-Off-By: Pete Zaitcev <zaitcev@redhat.com>

---
 configure.ac           |    4 -
 include/cldc.h         |    7 --
 lib/cldc-udp.c         |   27 --------
 pkg/cld.spec           |    2 
 test/.gitignore        |    1 
 test/Makefile.am       |    9 ++
 test/it-works.c        |   68 ++++++++++++++--------
 test/load-file-event.c |  108 +++++++++++++++--------------------
 test/lock-file-event.c |  118 ++++++++++++++++++++++-----------------
 test/save-file-event.c |  109 +++++++++++++++---------------------
 test/test.h            |   26 ++++++++
 test/timer.c           |   44 ++++++++++++++
 test/timer.h           |   18 +++++
 test/util.c            |   66 +++++++++++++++++++++
 tools/Makefile.am      |    2 
 15 files changed, 375 insertions(+), 234 deletions(-)

diff --git a/configure.ac b/configure.ac
index 26a1548..b1247b6 100644
--- a/configure.ac
+++ b/configure.ac
@@ -78,9 +78,9 @@ AC_CHECK_LIB(db-4.7, db_create, DB4_LIBS=-ldb-4.7,
 	AC_CHECK_LIB(db-4.5, db_create, DB4_LIBS=-ldb-4.5,
 	AC_CHECK_LIB(db-4.4, db_create, DB4_LIBS=-ldb-4.4,
 	AC_CHECK_LIB(db-4.3, db_create, DB4_LIBS=-ldb-4.3, exit 1)))))
-AC_CHECK_LIB(event, event_base_new, EVENT_LIBS=-levent, exit 1)
 AC_CHECK_LIB(argp, argp_parse, ARGP_LIBS=-largp)
 AC_CHECK_LIB(crypto, MD5_Init, CRYPTO_LIBS=-lcrypto)
+AC_CHECK_LIB(resolv, ns_initparse, RESOLV_LIBS=-lresolv)
 
 dnl -------------------------------------
 dnl Checks for optional library functions
@@ -108,9 +108,9 @@ PKG_PROG_PKG_CONFIG()
 AM_PATH_GLIB_2_0(2.0.0,,,gthread)
 
 AC_SUBST(DB4_LIBS)
-AC_SUBST(EVENT_LIBS)
 AC_SUBST(ARGP_LIBS)
 AC_SUBST(CRYPTO_LIBS)
+AC_SUBST(RESOLV_LIBS)
 
 AC_CONFIG_FILES([
 	Makefile
diff --git a/include/cldc.h b/include/cldc.h
index 8c2bde5..9ae6dfe 100644
--- a/include/cldc.h
+++ b/include/cldc.h
@@ -3,7 +3,6 @@
 
 #include <sys/types.h>
 #include <stdbool.h>
-#include <event.h>
 #include <glib.h>
 #include <cld_msg.h>
 
@@ -130,8 +129,6 @@ struct cldc_udp {
 
 	int		fd;
 
-	struct event	timer_ev;
-
 	struct cldc_session *sess;
 
 	int		(*cb)(struct cldc_session *, void *);
@@ -202,10 +199,6 @@ extern int cldc_udp_receive_pkt(struct cldc_udp *udp);
 extern int cldc_udp_pkt_send(void *private,
 			  const void *addr, size_t addrlen,
 			  const void *buf, size_t buflen);
-extern bool cldc_levent_timer(void *private, bool add,
-		       int (*cb)(struct cldc_session *, void *),
-		       void *cb_private,
-		       time_t secs);
 
 /* cldc-dns */
 extern int cldc_getaddr(GList **host_list, const char *thishost, bool verbose,
diff --git a/lib/cldc-udp.c b/lib/cldc-udp.c
index b212643..0a8d0ad 100644
--- a/lib/cldc-udp.c
+++ b/lib/cldc-udp.c
@@ -42,14 +42,6 @@ void cldc_udp_free(struct cldc_udp *udp)
 	free(udp);
 }
 
-static void cldc_udp_timer(int fd, short events, void *userdata)
-{
-	struct cldc_udp *udp = userdata;
-
-	if (udp->cb)
-		udp->cb(udp->sess, udp->cb_private);
-}
-
 int cldc_udp_new(const char *hostname, int port,
 		 struct cldc_udp **udp_out)
 {
@@ -100,8 +92,6 @@ int cldc_udp_new(const char *hostname, int port,
 
 	udp->fd = fd;
 
-	evtimer_set(&udp->timer_ev, cldc_udp_timer, udp);
-
 	freeaddrinfo(res);
 
 	*udp_out = udp;
@@ -149,20 +139,3 @@ int cldc_udp_pkt_send(void *private,
 	return 0;
 }
 
-bool cldc_levent_timer(void *private, bool add,
-		       int (*cb)(struct cldc_session *, void *),
-		       void *cb_private,
-		       time_t secs)
-{
-	struct cldc_udp *udp = private;
-	struct timeval tv = { secs, 0 };
-
-	if (add) {
-		udp->cb = cb;
-		udp->cb_private = cb_private;
-		return evtimer_add(&udp->timer_ev, &tv) == 0;
-	} else {
-		return evtimer_del(&udp->timer_ev) == 0;
-	}
-}
-
diff --git a/pkg/cld.spec b/pkg/cld.spec
index 3b93a0d..daeaa4a 100644
--- a/pkg/cld.spec
+++ b/pkg/cld.spec
@@ -15,7 +15,7 @@ BuildRoot:	%{_tmppath}/%{name}-%{version}-%{release}-root-%(%{__id_u} -n)
 Requires(post):		chkconfig
 Requires(preun):	chkconfig initscripts
 
-BuildRequires:	db4-devel libevent-devel glib2-devel doxygen openssl-devel
+BuildRequires:	db4-devel glib2-devel doxygen openssl-devel
 BuildRequires:	texlive-latex
 
 %description
diff --git a/test/.gitignore b/test/.gitignore
index 386e7b1..00a676c 100644
--- a/test/.gitignore
+++ b/test/.gitignore
@@ -1,5 +1,6 @@
 
 .libs
+libtest.a
 it-works
 save-file-event
 load-file-event
diff --git a/test/Makefile.am b/test/Makefile.am
index af67b90..0287127 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -3,6 +3,8 @@ INCLUDES        = -I$(top_srcdir)/include       \
 		  @GLIB_CFLAGS@
 
 EXTRA_DIST =			\
+	test.h			\
+	timer.h			\
 	prep-db			\
 	start-daemon		\
 	pid-exists		\
@@ -24,10 +26,15 @@ check_PROGRAMS		= it-works \
 			  save-file-event load-file-event lock-file-event
 
 TESTLDADD		= ../lib/libcldc.la	\
-			  @GLIB_LIBS@ @CRYPTO_LIBS@ @EVENT_LIBS@
+			  libtest.a		\
+			  @GLIB_LIBS@ @CRYPTO_LIBS@ @RESOLV_LIBS@
 it_works_LDADD		= $(TESTLDADD)
 save_file_event_LDADD	= $(TESTLDADD)
 load_file_event_LDADD	= $(TESTLDADD)
 lock_file_event_LDADD	= $(TESTLDADD)
 
+noinst_LIBRARIES	= libtest.a
+
+libtest_a_SOURCES	= timer.c util.c
+
 TESTS_ENVIRONMENT=top_srcdir=$(top_srcdir)
diff --git a/test/it-works.c b/test/it-works.c
index bd2f965..0bd7d80 100644
--- a/test/it-works.c
+++ b/test/it-works.c
@@ -6,26 +6,47 @@
 #include <string.h>
 #include <stdio.h>
 #include <cldc.h>
+#include "timer.h"
+#include "test.h"
 
 static struct cldc_udp *udp;
-static struct event udp_ev;
-static int final_rc;
+static struct timer udp_tm;
 
-static void do_event(void *private, struct cldc_session *sess,
-		     struct cldc_fh *fh, uint32_t event_mask)
+static bool do_timer_ctl(void *priv, bool add,
+			 int (*cb)(struct cldc_session *, void *),
+			 void *cb_priv, time_t secs)
 {
-	fprintf(stderr, "EVENT(%x)\n", event_mask);
+	if (priv != udp) {
+		fprintf(stderr, "IE0: misuse of timer\n");
+		exit(1);
+	}
+
+	if (add) {
+		udp->cb = cb;
+		udp->cb_private = cb_priv;
+		timer_add(&udp_tm, time(NULL) + secs);
+	} else {
+		timer_del(&udp_tm);
+	}
+
+	return true;
 }
 
-static void udp_event(int fd, short events, void *userdata)
+static void timer_udp_event(void *priv)
 {
-	int rc;
-
-	rc = cldc_udp_receive_pkt(udp);
-	if (rc) {
-		fprintf(stderr, "cldc_udp_receive_pkt failed: %d\n", rc);
+	if (priv != udp) {
+		fprintf(stderr, "IE1: misuse of timer\n");
 		exit(1);
 	}
+
+	if (udp->cb)
+		udp->cb(udp->sess, udp->cb_private);
+}
+
+static void do_event(void *private, struct cldc_session *sess,
+		     struct cldc_fh *fh, uint32_t event_mask)
+{
+	fprintf(stderr, "EVENT(%x)\n", event_mask);
 }
 
 static int end_sess_cb(struct cldc_call_opts *copts, enum cle_err_codes errc)
@@ -36,8 +57,7 @@ static int end_sess_cb(struct cldc_call_opts *copts, enum cle_err_codes errc)
 	}
 
 	/* session ended; success */
-	event_loopbreak();
-
+	exit(0);
 	return 0;
 }
 
@@ -64,7 +84,7 @@ static int new_sess_cb(struct cldc_call_opts *copts_in, enum cle_err_codes errc)
 }
 
 static struct cldc_ops ops = {
-	.timer_ctl		= cldc_levent_timer,
+	.timer_ctl		= do_timer_ctl,
 	.pkt_send		= cldc_udp_pkt_send,
 	.event			= do_event,
 };
@@ -85,6 +105,8 @@ static int init(void)
 	if (rc)
 		return rc;
 
+	timer_init(&udp_tm, timer_udp_event, udp);
+
 	memset(&copts, 0, sizeof(copts));
 	copts.cb = new_sess_cb;
 
@@ -95,23 +117,21 @@ static int init(void)
 
 	// udp->sess->verbose = true;
 
-	event_set(&udp_ev, udp->fd, EV_READ | EV_PERSIST, udp_event, udp);
-
-	if (event_add(&udp_ev, NULL) < 0) {
-		fprintf(stderr, "event_add failed\n");
-		return 1;
-	}
-
 	return 0;
 }
 
 int main (int argc, char *argv[])
 {
+	struct timer *tmvec[2];
+
 	cldc_init();
-	event_init();
 	if (init())
 		return 1;
-	event_dispatch();
-	return final_rc;
+
+	tmvec[0] = &udp_tm;
+	tmvec[1] = NULL;
+	test_loop(udp, (sizeof(tmvec)/sizeof(tmvec[0])) - 1, tmvec);
+
+	return 0;
 }
 
diff --git a/test/load-file-event.c b/test/load-file-event.c
index 1620508..904f95f 100644
--- a/test/load-file-event.c
+++ b/test/load-file-event.c
@@ -1,7 +1,6 @@
 
 /*
  * Load a file from CLD.
- * This version uses libevent.
  */
 #include <sys/types.h>
 #include <unistd.h>
@@ -9,20 +8,14 @@
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
-#include <event.h>
 #include <cldc.h>
-
-#define TESTSTR          "longertestdata\n"
-#define TESTLEN  (sizeof("longertestdata\n")-1)
-
-#define TFNAME     "/cld-test-inst"
+#include "test.h"
 
 struct run {
 	struct cldc_udp *udp;
-	struct event udp_ev;
+	struct timer tmr_udp;
 	struct cldc_fh *fh;
 	char *fname;
-	// int len;
 };
 
 static int new_sess_cb(struct cldc_call_opts *copts, enum cle_err_codes errc);
@@ -31,22 +24,43 @@ static int read_1_cb(struct cldc_call_opts *coptarg, enum cle_err_codes errc);
 static int close_1_cb(struct cldc_call_opts *coptarg, enum cle_err_codes errc);
 static int end_sess_cb(struct cldc_call_opts *copts, enum cle_err_codes errc);
 
-static void do_event(void *private, struct cldc_session *sess,
-		     struct cldc_fh *fh, uint32_t event_mask)
+static bool do_timer_ctl(void *priv, bool add,
+			 int (*cb)(struct cldc_session *, void *),
+			 void *cb_priv, time_t secs)
 {
-	fprintf(stderr, "EVENT(0x%x)\n", event_mask);
+	struct run *rp = priv;
+
+	if (add) {
+		rp->udp->cb = cb;
+		rp->udp->cb_private = cb_priv;
+		timer_add(&rp->tmr_udp, time(NULL) + secs);
+	} else {
+		timer_del(&rp->tmr_udp);
+	}
+
+	return true;
 }
 
-static void udp_event(int fd, short events, void *userdata)
+static int do_pkt_send(void *priv, const void *addr, size_t addrlen,
+		       const void *buf, size_t buflen)
 {
-	struct run *rp = userdata;
-	int rc;
+	struct run *rp = priv;
+	return cldc_udp_pkt_send(rp->udp, addr, addrlen, buf, buflen);
+}
 
-	rc = cldc_udp_receive_pkt(rp->udp);
-	if (rc) {
-		fprintf(stderr, "cldc_udp_receive_pkt failed: %d (fd %d)\n", rc, rp->udp->fd);
-		exit(1);
-	}
+static void timer_udp_event(void *priv)
+{
+	struct run *rp = priv;
+	struct cldc_udp *udp = rp->udp;
+
+	if (udp->cb)
+		udp->cb(udp->sess, udp->cb_private);
+}
+
+static void do_event(void *private, struct cldc_session *sess,
+		     struct cldc_fh *fh, uint32_t event_mask)
+{
+	fprintf(stderr, "EVENT(0x%x)\n", event_mask);
 }
 
 static int new_sess_cb(struct cldc_call_opts *coptarg, enum cle_err_codes errc)
@@ -169,19 +183,16 @@ static int end_sess_cb(struct cldc_call_opts *copts, enum cle_err_codes errc)
 	}
 
 	/* session ended; success */
-	event_loopbreak();
+	exit(0);
 
 	return 0;
 }
 
 static struct run run;
-#if 0
-static char databuf[DATAMAX];
-#endif
 
 static struct cldc_ops ops = {
-	.timer_ctl		= cldc_levent_timer,
-	.pkt_send		= cldc_udp_pkt_send,
+	.timer_ctl		= do_timer_ctl,
+	.pkt_send		= do_pkt_send,
 	.event			= do_event,
 };
 
@@ -192,24 +203,16 @@ static int init(char *name)
 	struct cldc_call_opts copts;
 
 	run.fname = name;
-#if 0
-	run.buf = databuf;
 
-	rc = read(0, databuf, DATAMAX);
-	if (rc < 0) {
-		fprintf(stderr, "read error: %s\n", strerror(errno));
-		return -1;
-	}
-	run.len = rc;
-#endif
-
-	port = cld_readport("cld.port");	/* FIXME need test.h */
+	port = cld_readport(TEST_PORTFILE_CLD);
 	if (port < 0)
 		return port;
 	if (port == 0)
 		return -1;
 
-	rc = cldc_udp_new("localhost", port, &run.udp);
+	timer_init(&run.tmr_udp, timer_udp_event, &run);
+
+	rc = cldc_udp_new(TEST_HOST, port, &run.udp);
 	if (rc)
 		return rc;
 
@@ -217,42 +220,27 @@ static int init(char *name)
 	copts.cb = new_sess_cb;
 	copts.private = &run;
 	rc = cldc_new_sess(&ops, &copts, run.udp->addr, run.udp->addr_len,
-			   "testuser", "testuser", run.udp, &run.udp->sess);
+			   TEST_USER, TEST_USER_KEY, &run, &run.udp->sess);
 	if (rc)
 		return rc;
 
 	// run.udp->sess->verbose = true;
 
-	event_set(&run.udp_ev, run.udp->fd, EV_READ | EV_PERSIST,
-		  udp_event, &run);
-
-	if (event_add(&run.udp_ev, NULL) < 0) {
-		fprintf(stderr, "event_add failed\n");
-		return 1;
-	}
-
 	return 0;
 }
 
 int main(int argc, char *argv[])
 {
-#if 0
-	if (argc != 2) {
-		fprintf(stderr, "Usage: save-file-event {filename}\n");
-		return 1;
-	}
-#endif
+	struct timer *tmvec[2];
 
 	cldc_init();
-	event_init();
-#if 0
-	if (init(argv[1]))
-		return 1;
-#else
 	if (init(TFNAME))
 		return 1;
-#endif
-	event_dispatch();
+
+	tmvec[0] = &run.tmr_udp;
+	tmvec[1] = NULL;
+	test_loop(run.udp, (sizeof(tmvec)/sizeof(tmvec[0])) - 1, tmvec);
+
 	return 0;
 }
 
diff --git a/test/lock-file-event.c b/test/lock-file-event.c
index c69da66..e3de698 100644
--- a/test/lock-file-event.c
+++ b/test/lock-file-event.c
@@ -1,7 +1,6 @@
 
 /*
  * Create a file in CLD, lock it.
- * This version uses libevent.
  */
 #include <sys/types.h>
 #include <unistd.h>
@@ -9,44 +8,63 @@
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
-#include <event.h>
 #include <cldc.h>
-
-#define TESTSTR          "testlock\n"
-#define TESTLEN  (sizeof("testlock\n")-1)
+#include "timer.h"
+#include "test.h"
 
 struct run {
 	struct cldc_udp *udp;
-	struct event udp_ev;
-	struct event tmr_ev;
+	struct timer tmr_test;
+	struct timer tmr_udp;
 	struct cldc_fh *fh;
-	char buf[TESTLEN];
+	char buf[LOCKLEN];
 };
 
 static int new_sess_cb(struct cldc_call_opts *copts, enum cle_err_codes errc);
 static int open_1_cb(struct cldc_call_opts *coptarg, enum cle_err_codes errc);
 static int write_1_cb(struct cldc_call_opts *coptarg, enum cle_err_codes errc);
 static int lock_1_cb(struct cldc_call_opts *coptarg, enum cle_err_codes errc);
-static void timer_1(int fd, short events, void *userdata);
+static void timer_1(struct run *rp);
 static int close_1_cb(struct cldc_call_opts *coptarg, enum cle_err_codes errc);
 static int end_sess_cb(struct cldc_call_opts *copts, enum cle_err_codes errc);
 
-static void do_event(void *private, struct cldc_session *sess,
-		     struct cldc_fh *fh, uint32_t event_mask)
+static bool do_timer_ctl(void *priv, bool add,
+			 int (*cb)(struct cldc_session *, void *),
+			 void *cb_priv, time_t secs)
 {
-	fprintf(stderr, "EVENT(0x%x)\n", event_mask);
+	struct run *rp = priv;
+
+	if (add) {
+		rp->udp->cb = cb;
+		rp->udp->cb_private = cb_priv;
+		timer_add(&rp->tmr_udp, time(NULL) + secs);
+	} else {
+		timer_del(&rp->tmr_udp);
+	}
+
+	return true;
 }
 
-static void udp_event(int fd, short events, void *userdata)
+static int do_pkt_send(void *priv, const void *addr, size_t addrlen,
+		       const void *buf, size_t buflen)
 {
-	struct run *rp = userdata;
-	int rc;
+	struct run *rp = priv;
+	return cldc_udp_pkt_send(rp->udp, addr, addrlen, buf, buflen);
+}
 
-	rc = cldc_udp_receive_pkt(rp->udp);
-	if (rc) {
-		fprintf(stderr, "cldc_udp_receive_pkt failed: %d\n", rc);
-		exit(1);
-	}
+static void timer_udp_event(void *priv)
+{
+	struct run *rp = priv;
+	struct cldc_udp *udp = rp->udp;
+
+	if (udp->cb)
+		udp->cb(udp->sess, udp->cb_private);
+}
+
+static void do_event(void *private, struct cldc_session *sess,
+		     struct cldc_fh *fh, uint32_t event_mask)
+{
+	fprintf(stderr, "EVENT(0x%x)\n", event_mask);
 }
 
 static int new_sess_cb(struct cldc_call_opts *coptarg, enum cle_err_codes errc)
@@ -64,7 +82,7 @@ static int new_sess_cb(struct cldc_call_opts *coptarg, enum cle_err_codes errc)
 	memset(&copts, 0, sizeof(copts));
 	copts.cb = open_1_cb;
 	copts.private = rp;
-	rc = cldc_open(rp->udp->sess, &copts, "/cld-lock-inst",
+	rc = cldc_open(rp->udp->sess, &copts, TLNAME,
 		       COM_WRITE | COM_LOCK | COM_CREATE,
 		       CE_SESS_FAILED, &rp->fh);
 	if (rc) {
@@ -96,7 +114,7 @@ static int open_1_cb(struct cldc_call_opts *coptarg, enum cle_err_codes errc)
 	memset(&copts, 0, sizeof(copts));
 	copts.cb = write_1_cb;
 	copts.private = rp;
-	rc = cldc_put(rp->fh, &copts, rp->buf, TESTLEN);
+	rc = cldc_put(rp->fh, &copts, rp->buf, LOCKLEN);
 	if (rc) {
 		fprintf(stderr, "cldc_put call error %d\n", rc);
 		exit(1);
@@ -129,25 +147,26 @@ static int write_1_cb(struct cldc_call_opts *coptarg, enum cle_err_codes errc)
 static int lock_1_cb(struct cldc_call_opts *coptarg, enum cle_err_codes errc)
 {
 	struct run *rp = coptarg->private;
-	struct timeval tv = { 40, 0 };	/* 40s to make sure session sustains */
-	int rc;
 
 	if (errc != CLE_OK) {
 		fprintf(stderr, "first-lock failed: %d\n", errc);
 		exit(1);
 	}
 
-	rc = evtimer_add(&rp->tmr_ev, &tv);
-	if (rc) {
-		fprintf(stderr, "evtimer_add call error %d\n", rc);
-		exit(1);
-	}
+	/* Idle for 40s to verify that session sustains a protocol ping. */
+	timer_add(&rp->tmr_test, time(NULL) + 40);
 	return 0;
 }
 
-static void timer_1(int fd, short events, void *userdata)
+static void timer_test_event(void *priv)
+{
+	struct run *rp = priv;
+
+	timer_1(rp);
+}
+
+static void timer_1(struct run *rp)
 {
-	struct run *rp = userdata;
 	struct cldc_call_opts copts;
 	int rc;
 
@@ -192,16 +211,15 @@ static int end_sess_cb(struct cldc_call_opts *copts, enum cle_err_codes errc)
 	}
 
 	/* session ended; success */
-	event_loopbreak();
-
+	exit(0);
 	return 0;
 }
 
 static struct run run;
 
 static struct cldc_ops ops = {
-	.timer_ctl		= cldc_levent_timer,
-	.pkt_send		= cldc_udp_pkt_send,
+	.timer_ctl		= do_timer_ctl,
+	.pkt_send		= do_pkt_send,
 	.event			= do_event,
 };
 
@@ -211,15 +229,18 @@ static int init(void)
 	int port;
 	struct cldc_call_opts copts;
 
-	memcpy(run.buf, TESTSTR, TESTLEN);
+	memcpy(run.buf, LOCKSTR, LOCKLEN);
 
-	port = cld_readport("cld.port");	/* FIXME need test.h */
+	port = cld_readport(TEST_PORTFILE_CLD);
 	if (port < 0)
 		return port;
 	if (port == 0)
 		return -1;
 
-	rc = cldc_udp_new("localhost", port, &run.udp);
+	timer_init(&run.tmr_test, timer_test_event, &run);
+	timer_init(&run.tmr_udp, timer_udp_event, &run);
+
+	rc = cldc_udp_new(TEST_HOST, port, &run.udp);
 	if (rc)
 		return rc;
 
@@ -227,31 +248,28 @@ static int init(void)
 	copts.cb = new_sess_cb;
 	copts.private = &run;
 	rc = cldc_new_sess(&ops, &copts, run.udp->addr, run.udp->addr_len,
-			   "testuser", "testuser", run.udp, &run.udp->sess);
+			   TEST_USER, TEST_USER_KEY, &run, &run.udp->sess);
 	if (rc)
 		return rc;
 
 	// run.udp->sess->verbose = true;
 
-	event_set(&run.udp_ev, run.udp->fd, EV_READ | EV_PERSIST,
-		  udp_event, &run);
-	evtimer_set(&run.tmr_ev, timer_1, &run);
-
-	if (event_add(&run.udp_ev, NULL) < 0) {
-		fprintf(stderr, "event_add failed\n");
-		return 1;
-	}
-
 	return 0;
 }
 
 int main (int argc, char *argv[])
 {
+	struct timer *tmvec[3];
+
 	cldc_init();
-	event_init();
 	if (init())
 		return 1;
-	event_dispatch();
+
+	tmvec[0] = &run.tmr_udp;
+	tmvec[1] = &run.tmr_test;
+	tmvec[2] = NULL;
+	test_loop(run.udp, (sizeof(tmvec)/sizeof(tmvec[0])) - 1, tmvec);
+
 	return 0;
 }
 
diff --git a/test/save-file-event.c b/test/save-file-event.c
index 1b3418a..7c92083 100644
--- a/test/save-file-event.c
+++ b/test/save-file-event.c
@@ -1,7 +1,6 @@
 
 /*
  * Create a file in CLD.
- * This version uses libevent.
  */
 #include <sys/types.h>
 #include <unistd.h>
@@ -9,19 +8,14 @@
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
-#include <event.h>
 #include <cldc.h>
-
-#define TESTSTR          "longertestdata\n"
-#define TESTLEN  (sizeof("longertestdata\n")-1)
-
-#define TFNAME     "/cld-test-inst"
+#include "test.h"
 
 #define DATAMAX  10000
 
 struct run {
 	struct cldc_udp *udp;
-	struct event udp_ev;
+	struct timer tmr_udp;
 	struct cldc_fh *fh;
 	char *fname;
 	char *buf;
@@ -35,22 +29,43 @@ static void call_close(struct run *rp);
 static int close_1_cb(struct cldc_call_opts *coptarg, enum cle_err_codes errc);
 static int end_sess_cb(struct cldc_call_opts *copts, enum cle_err_codes errc);
 
-static void do_event(void *private, struct cldc_session *sess,
-		     struct cldc_fh *fh, uint32_t event_mask)
+static bool do_timer_ctl(void *priv, bool add,
+			 int (*cb)(struct cldc_session *, void *),
+			 void *cb_priv, time_t secs)
 {
-	fprintf(stderr, "EVENT(0x%x)\n", event_mask);
+	struct run *rp = priv;
+
+	if (add) {
+		rp->udp->cb = cb;
+		rp->udp->cb_private = cb_priv;
+		timer_add(&rp->tmr_udp, time(NULL) + secs);
+	} else {
+		timer_del(&rp->tmr_udp);
+	}
+	return true;
 }
 
-static void udp_event(int fd, short events, void *userdata)
+static int do_pkt_send(void *priv, const void *addr, size_t addrlen,
+		       const void *buf, size_t buflen)
 {
-	struct run *rp = userdata;
-	int rc;
+	struct run *rp = priv;
+	return cldc_udp_pkt_send(rp->udp, addr, addrlen, buf, buflen);
+}
 
-	rc = cldc_udp_receive_pkt(rp->udp);
-	if (rc) {
-		fprintf(stderr, "cldc_udp_receive_pkt failed: %d\n", rc);
-		exit(1);
-	}
+static void timer_udp_event(void *priv)
+{
+	struct run *rp = priv;
+	struct cldc_udp *udp;
+
+	udp = rp->udp;
+	if (udp->cb)
+		udp->cb(udp->sess, udp->cb_private);
+}
+
+static void do_event(void *private, struct cldc_session *sess,
+		     struct cldc_fh *fh, uint32_t event_mask)
+{
+	fprintf(stderr, "EVENT(0x%x)\n", event_mask);
 }
 
 static int new_sess_cb(struct cldc_call_opts *coptarg, enum cle_err_codes errc)
@@ -172,19 +187,15 @@ static int end_sess_cb(struct cldc_call_opts *copts, enum cle_err_codes errc)
 	}
 
 	/* session ended; success */
-	event_loopbreak();
-
+	exit(0);
 	return 0;
 }
 
 static struct run run;
-#if 0
-static char databuf[DATAMAX];
-#endif
 
 static struct cldc_ops ops = {
-	.timer_ctl		= cldc_levent_timer,
-	.pkt_send		= cldc_udp_pkt_send,
+	.timer_ctl		= do_timer_ctl,
+	.pkt_send		= do_pkt_send,
 	.event			= do_event,
 };
 
@@ -195,27 +206,18 @@ static int init(char *name)
 	struct cldc_call_opts copts;
 
 	run.fname = name;
-#if 0
-	run.buf = databuf;
-
-	rc = read(0, databuf, DATAMAX);
-	if (rc < 0) {
-		fprintf(stderr, "read error: %s\n", strerror(errno));
-		return -1;
-	}
-	run.len = rc;
-#else
 	run.buf = TESTSTR;
 	run.len = TESTLEN;
-#endif
 
-	port = cld_readport("cld.port");	/* FIXME need test.h */
+	port = cld_readport(TEST_PORTFILE_CLD);
 	if (port < 0)
 		return port;
 	if (port == 0)
 		return -1;
 
-	rc = cldc_udp_new("localhost", port, &run.udp);
+	timer_init(&run.tmr_udp, timer_udp_event, &run);
+
+	rc = cldc_udp_new(TEST_HOST, port, &run.udp);
 	if (rc)
 		return rc;
 
@@ -223,42 +225,27 @@ static int init(char *name)
 	copts.cb = new_sess_cb;
 	copts.private = &run;
 	rc = cldc_new_sess(&ops, &copts, run.udp->addr, run.udp->addr_len,
-			   "testuser", "testuser", run.udp, &run.udp->sess);
+			   TEST_USER, TEST_USER_KEY, &run, &run.udp->sess);
 	if (rc)
 		return rc;
 
 	// run.udp->sess->verbose = true;
 
-	event_set(&run.udp_ev, run.udp->fd, EV_READ | EV_PERSIST,
-		  udp_event, &run);
-
-	if (event_add(&run.udp_ev, NULL) < 0) {
-		fprintf(stderr, "event_add failed\n");
-		return 1;
-	}
-
 	return 0;
 }
 
 int main(int argc, char *argv[])
 {
-#if 0
-	if (argc != 2) {
-		fprintf(stderr, "Usage: save-file-event {filename}\n");
-		return 1;
-	}
-#endif
+	struct timer *tmvec[2];
 
 	cldc_init();
-	event_init();
-#if 0
-	if (init(argv[1]))
-		return 1;
-#else
 	if (init(TFNAME))
 		return 1;
-#endif
-	event_dispatch();
+
+	tmvec[0] = &run.tmr_udp;
+	tmvec[1] = NULL;
+	test_loop(run.udp, (sizeof(tmvec)/sizeof(tmvec[0])) - 1, tmvec);
+
 	return 0;
 }
 
diff --git a/test/test.h b/test/test.h
new file mode 100644
index 0000000..ec72b2b
--- /dev/null
+++ b/test/test.h
@@ -0,0 +1,26 @@
+#ifndef _CLD_TEST_H_
+#define _CLD_TEST_H_
+
+#include <stdbool.h>
+#include <cldc.h>
+#include "timer.h"
+
+#define TESTSTR          "longertestdata\n"
+#define TESTLEN  (sizeof("longertestdata\n")-1)
+
+#define LOCKSTR          "testlock\n"
+#define LOCKLEN  (sizeof("testlock\n")-1)
+
+#define TFNAME     "/cld-test-inst"
+#define TLNAME     "/cld-lock-inst"
+
+#define TEST_HOST "localhost"
+
+#define TEST_USER "testuser"
+#define TEST_USER_KEY "testuser"
+
+#define TEST_PORTFILE_CLD	"cld.port"
+
+extern void test_loop(struct cldc_udp *udp, int timec, struct timer *timev[]);
+
+#endif
diff --git a/test/timer.c b/test/timer.c
new file mode 100644
index 0000000..ce05196
--- /dev/null
+++ b/test/timer.c
@@ -0,0 +1,44 @@
+
+/*
+ * Basic timers for CLD test suite.
+ */
+#include <string.h>
+#include "timer.h"
+
+void timer_init(struct timer *timer, void (*func)(void *), void *priv)
+{
+	memset(timer, 0, sizeof(struct timer));
+	timer->func = func;
+	timer->priv = priv;
+}
+
+void timer_add(struct timer *timer, time_t expires)
+{
+	timer->expires = expires;
+}
+
+void timer_del(struct timer *timer)
+{
+	timer->expires = 0;
+}
+
+/*
+ * Return true if timer was run.
+ * Otherwise, modify *pset if our expected expiration time is closer.
+ */
+bool timer_poke(struct timer *timer, time_t now, time_t *pset)
+{
+	if (!timer->expires)
+		return false;
+
+	if (now >= timer->expires) {
+		timer->expires = 0;
+		(*timer->func)(timer->priv);
+		return true;
+	}
+
+	if (*pset == 0 || timer->expires < *pset)
+		*pset = timer->expires;
+	return false;
+}
+
diff --git a/test/timer.h b/test/timer.h
new file mode 100644
index 0000000..e0ddcc5
--- /dev/null
+++ b/test/timer.h
@@ -0,0 +1,18 @@
+#ifndef _TEST_TIMER_H_
+#define _TEST_TIMER_H_
+
+#include <stdbool.h>
+#include <time.h>
+
+struct timer {
+	time_t expires;
+	void (*func)(void *);
+	void *priv;
+};
+
+extern void timer_init(struct timer *timer, void (*func)(void *), void *priv);
+extern void timer_add(struct timer *timer, time_t expires);
+extern void timer_del(struct timer *timer);
+extern bool timer_poke(struct timer *timer, time_t now, time_t *pset);
+
+#endif
diff --git a/test/util.c b/test/util.c
new file mode 100644
index 0000000..6e9eda6
--- /dev/null
+++ b/test/util.c
@@ -0,0 +1,66 @@
+
+/*
+ * General utilities for CLD tests.
+ */
+#include <sys/types.h>
+#include <sys/select.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include <cldc.h>
+#include "test.h"
+
+void test_loop(struct cldc_udp *udp, int timec, struct timer *timev[])
+{
+	int ufd = udp->fd;
+	fd_set rset;
+	struct timeval tm;
+	time_t now, set;
+	int i;
+	int rc;
+
+	for (;;) {
+		FD_ZERO(&rset);
+		FD_SET(ufd, &rset);
+
+		now = time(NULL);
+		set = 0;
+		for (i = 0; i < timec; i++) {
+			if (timer_poke(timev[i], now, &set))
+				break;
+		}
+		if (i < timec)
+			continue;
+		if (set) {
+			tm.tv_sec = set - now;
+			tm.tv_usec = 0;
+			rc = select(ufd + 1, &rset, NULL, NULL, &tm);
+			if (rc < 0) {
+				fprintf(stderr, "select: error\n");
+				exit(1);
+			}
+			if (rc == 0)
+				continue;
+		} else {
+			rc = select(ufd + 1, &rset, NULL, NULL, NULL);
+			if (rc <= 0) {
+				fprintf(stderr, "select: nfd %d\n", rc);
+				exit(1);
+			}
+		}
+
+		if (FD_ISSET(ufd, &rset)) {
+			rc = cldc_udp_receive_pkt(udp);
+			if (rc) {
+				fprintf(stderr,
+					"cldc_udp_receive_pkt: error %d\n", rc);
+				exit(1);
+			}
+		} else {
+			fprintf(stderr, "noevent\n");
+			exit(1);
+		}
+
+	}
+}
+
diff --git a/tools/Makefile.am b/tools/Makefile.am
index c171a78..2a6e1a5 100644
--- a/tools/Makefile.am
+++ b/tools/Makefile.am
@@ -5,5 +5,5 @@ INCLUDES	= -I$(top_srcdir)/include	\
 bin_PROGRAMS	= cldcli
 
 cldcli_LDADD	= ../lib/libcldc.la		\
-		  @GLIB_LIBS@ @CRYPTO_LIBS@ @EVENT_LIBS@ @ARGP_LIBS@
+		  @GLIB_LIBS@ @CRYPTO_LIBS@ @ARGP_LIBS@ @RESOLV_LIBS@
 

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [Patch 2/2] CLD: drop dependency on libevent from libcldc
  2009-11-28  3:52 [Patch 2/2] CLD: drop dependency on libevent from libcldc Pete Zaitcev
@ 2009-11-28  9:07 ` Jeff Garzik
  2009-11-29  6:38   ` Pete Zaitcev
  0 siblings, 1 reply; 4+ messages in thread
From: Jeff Garzik @ 2009-11-28  9:07 UTC (permalink / raw)
  To: Pete Zaitcev; +Cc: Project Hail List

On Fri, Nov 27, 2009 at 08:52:48PM -0700, Pete Zaitcev wrote:
> diff --git a/test/timer.c b/test/timer.c
> new file mode 100644
> index 0000000..ce05196
> --- /dev/null
> +++ b/test/timer.c
> @@ -0,0 +1,44 @@
> +
> +/*
> + * Basic timers for CLD test suite.
> + */
> +#include <string.h>
> +#include "timer.h"
> +
> +void timer_init(struct timer *timer, void (*func)(void *), void *priv)
> +{
> +	memset(timer, 0, sizeof(struct timer));
> +	timer->func = func;
> +	timer->priv = priv;
> +}
> +
> +void timer_add(struct timer *timer, time_t expires)
> +{
> +	timer->expires = expires;
> +}
> +
> +void timer_del(struct timer *timer)
> +{
> +	timer->expires = 0;
> +}
> +
> +/*
> + * Return true if timer was run.
> + * Otherwise, modify *pset if our expected expiration time is closer.
> + */
> +bool timer_poke(struct timer *timer, time_t now, time_t *pset)
> +{
> +	if (!timer->expires)
> +		return false;
> +
> +	if (now >= timer->expires) {
> +		timer->expires = 0;
> +		(*timer->func)(timer->priv);
> +		return true;
> +	}
> +
> +	if (*pset == 0 || timer->expires < *pset)
> +		*pset = timer->expires;
> +	return false;
> +}
> +
> diff --git a/test/timer.h b/test/timer.h
> new file mode 100644
> index 0000000..e0ddcc5
> --- /dev/null
> +++ b/test/timer.h
> @@ -0,0 +1,18 @@
> +#ifndef _TEST_TIMER_H_
> +#define _TEST_TIMER_H_
> +
> +#include <stdbool.h>
> +#include <time.h>
> +
> +struct timer {
> +	time_t expires;
> +	void (*func)(void *);
> +	void *priv;
> +};
> +
> +extern void timer_init(struct timer *timer, void (*func)(void *), void *priv);
> +extern void timer_add(struct timer *timer, time_t expires);
> +extern void timer_del(struct timer *timer);
> +extern bool timer_poke(struct timer *timer, time_t now, time_t *pset);
> +
> +#endif

hmmm, you are re-creating the self-contained timer facilities that
already exist in CLD, which are designed to be used with any of
select(2), poll(2), or epoll_wait(2).  I don't think we need two
hand-coded timer implementations.

I would suggest using some variant of the changes below as patch #1,
then add these other changes on top of that.  I have not committed
these changes, pending your comments.

If you choose to take the changes below and submit them as part
of your own patch series, make sure to add "From: Jeff Garzik
<jgarzik@redhat.com>" as the first line of the email body.
This so-called "Andrew Morton rule" triggers 'git am' and other
utilities to differentiate patch author from email author.


 include/Makefile.am |    2 -
 include/libtimer.h  |   34 ++++++++++++++++++++
 lib/.gitignore      |    2 +
 lib/Makefile.am     |    2 +
 lib/libtimer.c      |   83 +++++++++++++++++++++++++++++++++++++++++++++++++
 server/Makefile.am  |    3 +
 server/cld.h        |   25 --------------
 server/util.c       |   87 ----------------------------------------------------
 8 files changed, 125 insertions(+), 113 deletions(-)

diff --git a/include/Makefile.am b/include/Makefile.am
index 06e35a6..f7539dd 100644
--- a/include/Makefile.am
+++ b/include/Makefile.am
@@ -1,5 +1,5 @@
 
-EXTRA_DIST = cld-private.h
+EXTRA_DIST = cld-private.h libtimer.h
 
 include_HEADERS = cldc.h cld_msg.h
 
diff --git a/include/libtimer.h b/include/libtimer.h
new file mode 100644
index 0000000..aedaea0
--- /dev/null
+++ b/include/libtimer.h
@@ -0,0 +1,34 @@
+
+#ifndef __LIBTIMER_H__
+#define __LIBTIMER_H__
+
+#include <stdbool.h>
+#include <string.h>
+#include <time.h>
+
+struct timer {
+	bool			fired;
+	bool			on_list;
+	void			(*cb)(struct timer *);
+	void			*userdata;
+	time_t			expires;
+	char			name[32];
+};
+
+extern void timer_add(struct timer *timer, time_t expires);
+extern void timer_del(struct timer *timer);
+extern time_t timers_run(void);
+
+static inline void timer_init(struct timer *timer, const char *name,
+			      void (*cb)(struct timer *),
+			      void *userdata)
+{
+	memset(timer, 0, sizeof(*timer));
+	timer->cb = cb;
+	timer->userdata = userdata;
+	strncpy(timer->name, name, sizeof(timer->name));
+	timer->name[sizeof(timer->name) - 1] = 0;
+}
+
+#endif /* __LIBTIMER_H__ */
+
diff --git a/lib/.gitignore b/lib/.gitignore
index 9eaa296..dddcc4c 100644
--- a/lib/.gitignore
+++ b/lib/.gitignore
@@ -6,5 +6,7 @@ libcldc.la
 libcldc-uninstalled.pc
 libcldc.pc
 
+libtimer.a
+
 .libs
 
diff --git a/lib/Makefile.am b/lib/Makefile.am
index 5b042ef..68be429 100644
--- a/lib/Makefile.am
+++ b/lib/Makefile.am
@@ -16,6 +16,8 @@ libcldc_la_LDFLAGS = \
 	-no-undefined \
 	-export-symbols-regex "^[^_].*"
 
+noinst_LIBRARIES	= libtimer.a
+
 pkgconfigdir = $(libdir)/pkgconfig
 pkgconfig_DATA = libcldc.pc
 
diff --git a/lib/libtimer.c b/lib/libtimer.c
new file mode 100644
index 0000000..c1bcaf1
--- /dev/null
+++ b/lib/libtimer.c
@@ -0,0 +1,83 @@
+
+#include <glib.h>
+#include <libtimer.h>
+
+static GList *timer_list;
+
+static gint timer_cmp(gconstpointer a_, gconstpointer b_)
+{
+	const struct timer *a = a_;
+	const struct timer *b = b_;
+
+	if (a->expires > b->expires)
+		return 1;
+	if (a->expires == b->expires)
+		return 0;
+	return -1;
+}
+
+void timer_add(struct timer *timer, time_t expires)
+{
+	if (timer->on_list)
+		timer_list = g_list_remove(timer_list, timer);
+
+	timer->on_list = true;
+	timer->fired = false;
+	timer->expires = expires;
+
+	timer_list = g_list_insert_sorted(timer_list, timer, timer_cmp);
+}
+
+void timer_del(struct timer *timer)
+{
+	if (!timer->on_list)
+		return;
+
+	timer_list = g_list_remove(timer_list, timer);
+
+	timer->on_list = false;
+}
+
+time_t timers_run(void)
+{
+	struct timer *timer;
+	time_t now = time(NULL);
+	time_t next_timeout = 0;
+	GList *tmp, *cur;
+	GList *exec_list = NULL;
+
+	tmp = timer_list;
+	while (tmp) {
+		timer = tmp->data;
+		cur = tmp;
+		tmp = tmp->next;
+
+		if (timer->expires > now)
+			break;
+
+		timer_list = g_list_remove_link(timer_list, cur);
+		exec_list = g_list_concat(exec_list, cur);
+
+		timer->on_list = false;
+	}
+
+	tmp = exec_list;
+	while (tmp) {
+		timer = tmp->data;
+		tmp = tmp->next;
+
+		timer->fired = true;
+		timer->cb(timer);
+	}
+
+	if (timer_list) {
+		timer = timer_list->data;
+		if (timer->expires > now)
+			next_timeout = (timer->expires - now);
+		else
+			next_timeout = 1;
+	}
+
+	return next_timeout;
+}
+
diff --git a/server/Makefile.am b/server/Makefile.am
index 929a3fd..d099b4a 100644
--- a/server/Makefile.am
+++ b/server/Makefile.am
@@ -8,7 +8,8 @@ sbin_PROGRAMS	= cld cldbadm
 cld_SOURCES	= cldb.h cld.h \
 		  ../lib/common.c \
 		  cldb.c msg.c server.c session.c util.c
-cld_LDADD	= @CRYPTO_LIBS@ @GLIB_LIBS@ @DB4_LIBS@ @ARGP_LIBS@
+cld_LDADD	= ../lib/libtimer.a \
+		  @CRYPTO_LIBS@ @GLIB_LIBS@ @DB4_LIBS@ @ARGP_LIBS@
 
 cldbadm_SOURCES	= cldb.h cldbadm.c
 cldbadm_LDADD	= @CRYPTO_LIBS@ @GLIB_LIBS@ @DB4_LIBS@ @ARGP_LIBS@
diff --git a/server/cld.h b/server/cld.h
index da26862..05c93ad 100644
--- a/server/cld.h
+++ b/server/cld.h
@@ -26,8 +26,8 @@
 #include <glib.h>
 #include "cldb.h"
 #include <cld_msg.h>
+#include <libtimer.h>
 
-struct timer;
 struct client;
 struct session_outpkt;
 
@@ -40,15 +40,6 @@ enum {
 	SFL_FOREGROUND		= (1 << 0),	/* run in foreground */
 };
 
-struct timer {
-	bool			fired;
-	bool			on_list;
-	void			(*cb)(struct timer *);
-	void			*userdata;
-	time_t			expires;
-	char			name[32];
-};
-
 struct client {
 	struct sockaddr_in6	addr;		/* inet address */
 	socklen_t		addr_len;	/* inet address len */
@@ -176,20 +167,6 @@ extern void applog(int prio, const char *fmt, ...);
 extern int write_pid_file(const char *pid_fn);
 extern void syslogerr(const char *prefix);
 extern int fsetflags(const char *prefix, int fd, int or_flags);
-extern void timer_add(struct timer *timer, time_t expires);
-extern void timer_del(struct timer *timer);
-extern time_t timers_run(void);
-
-static inline void timer_init(struct timer *timer, const char *name,
-			      void (*cb)(struct timer *),
-			      void *userdata)
-{
-	memset(timer, 0, sizeof(*timer));
-	timer->cb = cb;
-	timer->userdata = userdata;
-	strncpy(timer->name, name, sizeof(timer->name));
-	timer->name[sizeof(timer->name) - 1] = 0;
-}
 
 #ifndef HAVE_STRNLEN
 extern size_t strnlen(const char *s, size_t maxlen);
diff --git a/server/util.c b/server/util.c
index 3cd072f..36fa219 100644
--- a/server/util.c
+++ b/server/util.c
@@ -32,8 +32,6 @@
 #include <syslog.h>
 #include "cld.h"
 
-static GList *timer_list;
-
 int write_pid_file(const char *pid_fn)
 {
 	char str[32], *s;
@@ -133,91 +131,6 @@ int fsetflags(const char *prefix, int fd, int or_flags)
 	return rc;
 }
 
-static gint timer_cmp(gconstpointer a_, gconstpointer b_)
-{
-	const struct timer *a = a_;
-	const struct timer *b = b_;
-
-	if (a->expires > b->expires)
-		return 1;
-	if (a->expires == b->expires)
-		return 0;
-	return -1;
-}
-
-void timer_add(struct timer *timer, time_t expires)
-{
-	if (timer->on_list) {
-		timer_list = g_list_remove(timer_list, timer);
-
-		if (debugging)
-			applog(LOG_WARNING, "BUG? timer %s added twice "
-			       "(expires: old %llu, new %llu)",
-			       timer->name,
-			       (unsigned long long) timer->expires,
-			       (unsigned long long) expires);
-	}
-
-	timer->on_list = true;
-	timer->fired = false;
-	timer->expires = expires;
-
-	timer_list = g_list_insert_sorted(timer_list, timer, timer_cmp);
-}
-
-void timer_del(struct timer *timer)
-{
-	if (!timer->on_list)
-		return;
-
-	timer_list = g_list_remove(timer_list, timer);
-
-	timer->on_list = false;
-}
-
-time_t timers_run(void)
-{
-	struct timer *timer;
-	time_t now = time(NULL);
-	time_t next_timeout = 0;
-	GList *tmp, *cur;
-	GList *exec_list = NULL;
-
-	tmp = timer_list;
-	while (tmp) {
-		timer = tmp->data;
-		cur = tmp;
-		tmp = tmp->next;
-
-		if (timer->expires > now)
-			break;
-
-		timer_list = g_list_remove_link(timer_list, cur);
-		exec_list = g_list_concat(exec_list, cur);
-
-		timer->on_list = false;
-	}
-
-	tmp = exec_list;
-	while (tmp) {
-		timer = tmp->data;
-		tmp = tmp->next;
-
-		timer->fired = true;
-		timer->cb(timer);
-	}
-
-	if (timer_list) {
-		timer = timer_list->data;
-		if (timer->expires > now)
-			next_timeout = (timer->expires - now);
-		else
-			next_timeout = 1;
-	}
-
-	return next_timeout;
-}
-
 #ifndef HAVE_STRNLEN
 size_t strnlen(const char *s, size_t maxlen)
 {

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [Patch 2/2] CLD: drop dependency on libevent from libcldc
@ 2009-11-29  6:35 Pete Zaitcev
  0 siblings, 0 replies; 4+ messages in thread
From: Pete Zaitcev @ 2009-11-29  6:35 UTC (permalink / raw)
  To: Jeff Garzik; +Cc: Project Hail List

Let's remove the dependency on libevent completely. The core CLD daemon
did not use libevent for some time, but getting rid of it entirely required
an implementation of some kind of common main loop for tests, which is
what this patch does.

Since we were refactoring, we add a common header and library for
tests, same as Chunk has.

We use the same timers that the main daemon uses (libtimer), they are
general enough for other applications such as tests.

This patch introduces an incompatibility, so Chunk and tabled must
be rebuilt. This is because the layout of struct cldc_udp changes.

Signed-Off-By: Pete Zaitcev <zaitcev@redhat.com>

---
 configure.ac           |    4 -
 include/cldc.h         |    7 --
 include/libtimer.h     |    3 -
 lib/cldc-udp.c         |   27 ---------
 pkg/cld.spec           |    2 
 test/.gitignore        |    1 
 test/Makefile.am       |    9 ++-
 test/it-works.c        |   62 +++++++++++++--------
 test/load-file-event.c |  105 +++++++++++++++---------------------
 test/lock-file-event.c |  111 +++++++++++++++++++++------------------
 test/save-file-event.c |  106 +++++++++++++++----------------------
 test/test.h            |   25 ++++++++
 test/util.c            |   58 ++++++++++++++++++++
 tools/Makefile.am      |    2 
 14 files changed, 284 insertions(+), 238 deletions(-)

commit 3930e86ee0be3cedd22f524825895dd465cddd77
Author: Master <zaitcev@lembas.zaitcev.lan>
Date:   Sat Nov 28 23:26:39 2009 -0700

    Drop libevent dependency.

diff --git a/configure.ac b/configure.ac
index 26a1548..b1247b6 100644
--- a/configure.ac
+++ b/configure.ac
@@ -78,9 +78,9 @@ AC_CHECK_LIB(db-4.7, db_create, DB4_LIBS=-ldb-4.7,
 	AC_CHECK_LIB(db-4.5, db_create, DB4_LIBS=-ldb-4.5,
 	AC_CHECK_LIB(db-4.4, db_create, DB4_LIBS=-ldb-4.4,
 	AC_CHECK_LIB(db-4.3, db_create, DB4_LIBS=-ldb-4.3, exit 1)))))
-AC_CHECK_LIB(event, event_base_new, EVENT_LIBS=-levent, exit 1)
 AC_CHECK_LIB(argp, argp_parse, ARGP_LIBS=-largp)
 AC_CHECK_LIB(crypto, MD5_Init, CRYPTO_LIBS=-lcrypto)
+AC_CHECK_LIB(resolv, ns_initparse, RESOLV_LIBS=-lresolv)
 
 dnl -------------------------------------
 dnl Checks for optional library functions
@@ -108,9 +108,9 @@ PKG_PROG_PKG_CONFIG()
 AM_PATH_GLIB_2_0(2.0.0,,,gthread)
 
 AC_SUBST(DB4_LIBS)
-AC_SUBST(EVENT_LIBS)
 AC_SUBST(ARGP_LIBS)
 AC_SUBST(CRYPTO_LIBS)
+AC_SUBST(RESOLV_LIBS)
 
 AC_CONFIG_FILES([
 	Makefile
diff --git a/include/cldc.h b/include/cldc.h
index 8c2bde5..9ae6dfe 100644
--- a/include/cldc.h
+++ b/include/cldc.h
@@ -3,7 +3,6 @@
 
 #include <sys/types.h>
 #include <stdbool.h>
-#include <event.h>
 #include <glib.h>
 #include <cld_msg.h>
 
@@ -130,8 +129,6 @@ struct cldc_udp {
 
 	int		fd;
 
-	struct event	timer_ev;
-
 	struct cldc_session *sess;
 
 	int		(*cb)(struct cldc_session *, void *);
@@ -202,10 +199,6 @@ extern int cldc_udp_receive_pkt(struct cldc_udp *udp);
 extern int cldc_udp_pkt_send(void *private,
 			  const void *addr, size_t addrlen,
 			  const void *buf, size_t buflen);
-extern bool cldc_levent_timer(void *private, bool add,
-		       int (*cb)(struct cldc_session *, void *),
-		       void *cb_private,
-		       time_t secs);
 
 /* cldc-dns */
 extern int cldc_getaddr(GList **host_list, const char *thishost, bool verbose,
diff --git a/include/libtimer.h b/include/libtimer.h
index aedaea0..c10f993 100644
--- a/include/libtimer.h
+++ b/include/libtimer.h
@@ -20,8 +20,7 @@ extern void timer_del(struct timer *timer);
 extern time_t timers_run(void);
 
 static inline void timer_init(struct timer *timer, const char *name,
-			      void (*cb)(struct timer *),
-			      void *userdata)
+			      void (*cb)(struct timer *), void *userdata)
 {
 	memset(timer, 0, sizeof(*timer));
 	timer->cb = cb;
diff --git a/lib/cldc-udp.c b/lib/cldc-udp.c
index b212643..0a8d0ad 100644
--- a/lib/cldc-udp.c
+++ b/lib/cldc-udp.c
@@ -42,14 +42,6 @@ void cldc_udp_free(struct cldc_udp *udp)
 	free(udp);
 }
 
-static void cldc_udp_timer(int fd, short events, void *userdata)
-{
-	struct cldc_udp *udp = userdata;
-
-	if (udp->cb)
-		udp->cb(udp->sess, udp->cb_private);
-}
-
 int cldc_udp_new(const char *hostname, int port,
 		 struct cldc_udp **udp_out)
 {
@@ -100,8 +92,6 @@ int cldc_udp_new(const char *hostname, int port,
 
 	udp->fd = fd;
 
-	evtimer_set(&udp->timer_ev, cldc_udp_timer, udp);
-
 	freeaddrinfo(res);
 
 	*udp_out = udp;
@@ -149,20 +139,3 @@ int cldc_udp_pkt_send(void *private,
 	return 0;
 }
 
-bool cldc_levent_timer(void *private, bool add,
-		       int (*cb)(struct cldc_session *, void *),
-		       void *cb_private,
-		       time_t secs)
-{
-	struct cldc_udp *udp = private;
-	struct timeval tv = { secs, 0 };
-
-	if (add) {
-		udp->cb = cb;
-		udp->cb_private = cb_private;
-		return evtimer_add(&udp->timer_ev, &tv) == 0;
-	} else {
-		return evtimer_del(&udp->timer_ev) == 0;
-	}
-}
-
diff --git a/pkg/cld.spec b/pkg/cld.spec
index 3b93a0d..daeaa4a 100644
--- a/pkg/cld.spec
+++ b/pkg/cld.spec
@@ -15,7 +15,7 @@ BuildRoot:	%{_tmppath}/%{name}-%{version}-%{release}-root-%(%{__id_u} -n)
 Requires(post):		chkconfig
 Requires(preun):	chkconfig initscripts
 
-BuildRequires:	db4-devel libevent-devel glib2-devel doxygen openssl-devel
+BuildRequires:	db4-devel glib2-devel doxygen openssl-devel
 BuildRequires:	texlive-latex
 
 %description
diff --git a/test/.gitignore b/test/.gitignore
index 386e7b1..00a676c 100644
--- a/test/.gitignore
+++ b/test/.gitignore
@@ -1,5 +1,6 @@
 
 .libs
+libtest.a
 it-works
 save-file-event
 load-file-event
diff --git a/test/Makefile.am b/test/Makefile.am
index af67b90..a04058c 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -3,6 +3,7 @@ INCLUDES        = -I$(top_srcdir)/include       \
 		  @GLIB_CFLAGS@
 
 EXTRA_DIST =			\
+	test.h			\
 	prep-db			\
 	start-daemon		\
 	pid-exists		\
@@ -24,10 +25,16 @@ check_PROGRAMS		= it-works \
 			  save-file-event load-file-event lock-file-event
 
 TESTLDADD		= ../lib/libcldc.la	\
-			  @GLIB_LIBS@ @CRYPTO_LIBS@ @EVENT_LIBS@
+			  ../lib/libtimer.a	\
+			  libtest.a		\
+			  @GLIB_LIBS@ @CRYPTO_LIBS@ @RESOLV_LIBS@
 it_works_LDADD		= $(TESTLDADD)
 save_file_event_LDADD	= $(TESTLDADD)
 load_file_event_LDADD	= $(TESTLDADD)
 lock_file_event_LDADD	= $(TESTLDADD)
 
+noinst_LIBRARIES	= libtest.a
+
+libtest_a_SOURCES	= util.c
+
 TESTS_ENVIRONMENT=top_srcdir=$(top_srcdir)
diff --git a/test/it-works.c b/test/it-works.c
index 0e932e7..122d3f4 100644
--- a/test/it-works.c
+++ b/test/it-works.c
@@ -5,27 +5,48 @@
 #include <stdlib.h>
 #include <string.h>
 #include <stdio.h>
+#include <libtimer.h>
 #include <cldc.h>
+#include "test.h"
 
 static struct cldc_udp *udp;
-static struct event udp_ev;
-static int final_rc;
+static struct timer udp_tm;
 
-static void do_event(void *private, struct cldc_session *sess,
-		     struct cldc_fh *fh, uint32_t event_mask)
+static bool do_timer_ctl(void *priv, bool add,
+			 int (*cb)(struct cldc_session *, void *),
+			 void *cb_priv, time_t secs)
 {
-	fprintf(stderr, "EVENT(%x)\n", event_mask);
+	if (priv != udp) {
+		fprintf(stderr, "IE0: misuse of timer\n");
+		exit(1);
+	}
+
+	if (add) {
+		udp->cb = cb;
+		udp->cb_private = cb_priv;
+		timer_add(&udp_tm, time(NULL) + secs);
+	} else {
+		timer_del(&udp_tm);
+	}
+
+	return true;
 }
 
-static void udp_event(int fd, short events, void *userdata)
+static void timer_udp_event(struct timer *timer)
 {
-	int rc;
-
-	rc = cldc_udp_receive_pkt(udp);
-	if (rc) {
-		fprintf(stderr, "cldc_udp_receive_pkt failed: %d\n", rc);
+	if (timer->userdata != udp) {
+		fprintf(stderr, "IE1: misuse of timer\n");
 		exit(1);
 	}
+
+	if (udp->cb)
+		udp->cb(udp->sess, udp->cb_private);
+}
+
+static void do_event(void *private, struct cldc_session *sess,
+		     struct cldc_fh *fh, uint32_t event_mask)
+{
+	fprintf(stderr, "EVENT(%x)\n", event_mask);
 }
 
 static int end_sess_cb(struct cldc_call_opts *copts, enum cle_err_codes errc)
@@ -36,8 +57,7 @@ static int end_sess_cb(struct cldc_call_opts *copts, enum cle_err_codes errc)
 	}
 
 	/* session ended; success */
-	event_loopbreak();
-
+	exit(0);
 	return 0;
 }
 
@@ -64,7 +84,7 @@ static int new_sess_cb(struct cldc_call_opts *copts_in, enum cle_err_codes errc)
 }
 
 static struct cldc_ops ops = {
-	.timer_ctl		= cldc_levent_timer,
+	.timer_ctl		= do_timer_ctl,
 	.pkt_send		= cldc_udp_pkt_send,
 	.event			= do_event,
 };
@@ -85,6 +105,8 @@ static int init(void)
 	if (rc)
 		return rc;
 
+	timer_init(&udp_tm, "udp-timer", timer_udp_event, udp);
+
 	memset(&copts, 0, sizeof(copts));
 	copts.cb = new_sess_cb;
 
@@ -95,13 +117,6 @@ static int init(void)
 
 	// udp->sess->verbose = true;
 
-	event_set(&udp_ev, udp->fd, EV_READ | EV_PERSIST, udp_event, udp);
-
-	if (event_add(&udp_ev, NULL) < 0) {
-		fprintf(stderr, "event_add failed\n");
-		return 1;
-	}
-
 	return 0;
 }
 
@@ -109,10 +124,9 @@ int main (int argc, char *argv[])
 {
 	g_thread_init(NULL);
 	cldc_init();
-	event_init();
 	if (init())
 		return 1;
-	event_dispatch();
-	return final_rc;
+	test_loop(udp);
+	return 0;
 }
 
diff --git a/test/load-file-event.c b/test/load-file-event.c
index 23ee4e0..fd54300 100644
--- a/test/load-file-event.c
+++ b/test/load-file-event.c
@@ -1,7 +1,6 @@
 
 /*
  * Load a file from CLD.
- * This version uses libevent.
  */
 #include <sys/types.h>
 #include <unistd.h>
@@ -9,20 +8,15 @@
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
-#include <event.h>
+#include <libtimer.h>
 #include <cldc.h>
-
-#define TESTSTR          "longertestdata\n"
-#define TESTLEN  (sizeof("longertestdata\n")-1)
-
-#define TFNAME     "/cld-test-inst"
+#include "test.h"
 
 struct run {
 	struct cldc_udp *udp;
-	struct event udp_ev;
+	struct timer tmr_udp;
 	struct cldc_fh *fh;
 	char *fname;
-	// int len;
 };
 
 static int new_sess_cb(struct cldc_call_opts *copts, enum cle_err_codes errc);
@@ -31,22 +25,43 @@ static int read_1_cb(struct cldc_call_opts *coptarg, enum cle_err_codes errc);
 static int close_1_cb(struct cldc_call_opts *coptarg, enum cle_err_codes errc);
 static int end_sess_cb(struct cldc_call_opts *copts, enum cle_err_codes errc);
 
-static void do_event(void *private, struct cldc_session *sess,
-		     struct cldc_fh *fh, uint32_t event_mask)
+static bool do_timer_ctl(void *priv, bool add,
+			 int (*cb)(struct cldc_session *, void *),
+			 void *cb_priv, time_t secs)
 {
-	fprintf(stderr, "EVENT(0x%x)\n", event_mask);
+	struct run *rp = priv;
+
+	if (add) {
+		rp->udp->cb = cb;
+		rp->udp->cb_private = cb_priv;
+		timer_add(&rp->tmr_udp, time(NULL) + secs);
+	} else {
+		timer_del(&rp->tmr_udp);
+	}
+
+	return true;
 }
 
-static void udp_event(int fd, short events, void *userdata)
+static int do_pkt_send(void *priv, const void *addr, size_t addrlen,
+		       const void *buf, size_t buflen)
 {
-	struct run *rp = userdata;
-	int rc;
+	struct run *rp = priv;
+	return cldc_udp_pkt_send(rp->udp, addr, addrlen, buf, buflen);
+}
 
-	rc = cldc_udp_receive_pkt(rp->udp);
-	if (rc) {
-		fprintf(stderr, "cldc_udp_receive_pkt failed: %d (fd %d)\n", rc, rp->udp->fd);
-		exit(1);
-	}
+static void timer_udp_event(struct timer *timer)
+{
+	struct run *rp = timer->userdata;
+	struct cldc_udp *udp = rp->udp;
+
+	if (udp->cb)
+		udp->cb(udp->sess, udp->cb_private);
+}
+
+static void do_event(void *private, struct cldc_session *sess,
+		     struct cldc_fh *fh, uint32_t event_mask)
+{
+	fprintf(stderr, "EVENT(0x%x)\n", event_mask);
 }
 
 static int new_sess_cb(struct cldc_call_opts *coptarg, enum cle_err_codes errc)
@@ -169,19 +184,16 @@ static int end_sess_cb(struct cldc_call_opts *copts, enum cle_err_codes errc)
 	}
 
 	/* session ended; success */
-	event_loopbreak();
+	exit(0);
 
 	return 0;
 }
 
 static struct run run;
-#if 0
-static char databuf[DATAMAX];
-#endif
 
 static struct cldc_ops ops = {
-	.timer_ctl		= cldc_levent_timer,
-	.pkt_send		= cldc_udp_pkt_send,
+	.timer_ctl		= do_timer_ctl,
+	.pkt_send		= do_pkt_send,
 	.event			= do_event,
 };
 
@@ -192,24 +204,16 @@ static int init(char *name)
 	struct cldc_call_opts copts;
 
 	run.fname = name;
-#if 0
-	run.buf = databuf;
 
-	rc = read(0, databuf, DATAMAX);
-	if (rc < 0) {
-		fprintf(stderr, "read error: %s\n", strerror(errno));
-		return -1;
-	}
-	run.len = rc;
-#endif
-
-	port = cld_readport("cld.port");	/* FIXME need test.h */
+	port = cld_readport(TEST_PORTFILE_CLD);
 	if (port < 0)
 		return port;
 	if (port == 0)
 		return -1;
 
-	rc = cldc_udp_new("localhost", port, &run.udp);
+	timer_init(&run.tmr_udp, "udp-timer", timer_udp_event, &run);
+
+	rc = cldc_udp_new(TEST_HOST, port, &run.udp);
 	if (rc)
 		return rc;
 
@@ -217,43 +221,22 @@ static int init(char *name)
 	copts.cb = new_sess_cb;
 	copts.private = &run;
 	rc = cldc_new_sess(&ops, &copts, run.udp->addr, run.udp->addr_len,
-			   "testuser", "testuser", run.udp, &run.udp->sess);
+			   TEST_USER, TEST_USER_KEY, &run, &run.udp->sess);
 	if (rc)
 		return rc;
 
 	// run.udp->sess->verbose = true;
 
-	event_set(&run.udp_ev, run.udp->fd, EV_READ | EV_PERSIST,
-		  udp_event, &run);
-
-	if (event_add(&run.udp_ev, NULL) < 0) {
-		fprintf(stderr, "event_add failed\n");
-		return 1;
-	}
-
 	return 0;
 }
 
 int main(int argc, char *argv[])
 {
-#if 0
-	if (argc != 2) {
-		fprintf(stderr, "Usage: save-file-event {filename}\n");
-		return 1;
-	}
-#endif
-
 	g_thread_init(NULL);
 	cldc_init();
-	event_init();
-#if 0
-	if (init(argv[1]))
-		return 1;
-#else
 	if (init(TFNAME))
 		return 1;
-#endif
-	event_dispatch();
+	test_loop(run.udp);
 	return 0;
 }
 
diff --git a/test/lock-file-event.c b/test/lock-file-event.c
index 6fc4f1a..fa7657c 100644
--- a/test/lock-file-event.c
+++ b/test/lock-file-event.c
@@ -1,7 +1,6 @@
 
 /*
  * Create a file in CLD, lock it.
- * This version uses libevent.
  */
 #include <sys/types.h>
 #include <unistd.h>
@@ -9,44 +8,63 @@
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
-#include <event.h>
+#include <libtimer.h>
 #include <cldc.h>
-
-#define TESTSTR          "testlock\n"
-#define TESTLEN  (sizeof("testlock\n")-1)
+#include "test.h"
 
 struct run {
 	struct cldc_udp *udp;
-	struct event udp_ev;
-	struct event tmr_ev;
+	struct timer tmr_test;
+	struct timer tmr_udp;
 	struct cldc_fh *fh;
-	char buf[TESTLEN];
+	char buf[LOCKLEN];
 };
 
 static int new_sess_cb(struct cldc_call_opts *copts, enum cle_err_codes errc);
 static int open_1_cb(struct cldc_call_opts *coptarg, enum cle_err_codes errc);
 static int write_1_cb(struct cldc_call_opts *coptarg, enum cle_err_codes errc);
 static int lock_1_cb(struct cldc_call_opts *coptarg, enum cle_err_codes errc);
-static void timer_1(int fd, short events, void *userdata);
+static void timer_1(struct run *rp);
 static int close_1_cb(struct cldc_call_opts *coptarg, enum cle_err_codes errc);
 static int end_sess_cb(struct cldc_call_opts *copts, enum cle_err_codes errc);
 
-static void do_event(void *private, struct cldc_session *sess,
-		     struct cldc_fh *fh, uint32_t event_mask)
+static bool do_timer_ctl(void *priv, bool add,
+			 int (*cb)(struct cldc_session *, void *),
+			 void *cb_priv, time_t secs)
 {
-	fprintf(stderr, "EVENT(0x%x)\n", event_mask);
+	struct run *rp = priv;
+
+	if (add) {
+		rp->udp->cb = cb;
+		rp->udp->cb_private = cb_priv;
+		timer_add(&rp->tmr_udp, time(NULL) + secs);
+	} else {
+		timer_del(&rp->tmr_udp);
+	}
+
+	return true;
 }
 
-static void udp_event(int fd, short events, void *userdata)
+static int do_pkt_send(void *priv, const void *addr, size_t addrlen,
+		       const void *buf, size_t buflen)
 {
-	struct run *rp = userdata;
-	int rc;
+	struct run *rp = priv;
+	return cldc_udp_pkt_send(rp->udp, addr, addrlen, buf, buflen);
+}
 
-	rc = cldc_udp_receive_pkt(rp->udp);
-	if (rc) {
-		fprintf(stderr, "cldc_udp_receive_pkt failed: %d\n", rc);
-		exit(1);
-	}
+static void timer_udp_event(struct timer *timer)
+{
+	struct run *rp = timer->userdata;
+	struct cldc_udp *udp = rp->udp;
+
+	if (udp->cb)
+		udp->cb(udp->sess, udp->cb_private);
+}
+
+static void do_event(void *private, struct cldc_session *sess,
+		     struct cldc_fh *fh, uint32_t event_mask)
+{
+	fprintf(stderr, "EVENT(0x%x)\n", event_mask);
 }
 
 static int new_sess_cb(struct cldc_call_opts *coptarg, enum cle_err_codes errc)
@@ -64,7 +82,7 @@ static int new_sess_cb(struct cldc_call_opts *coptarg, enum cle_err_codes errc)
 	memset(&copts, 0, sizeof(copts));
 	copts.cb = open_1_cb;
 	copts.private = rp;
-	rc = cldc_open(rp->udp->sess, &copts, "/cld-lock-inst",
+	rc = cldc_open(rp->udp->sess, &copts, TLNAME,
 		       COM_WRITE | COM_LOCK | COM_CREATE,
 		       CE_SESS_FAILED, &rp->fh);
 	if (rc) {
@@ -96,7 +114,7 @@ static int open_1_cb(struct cldc_call_opts *coptarg, enum cle_err_codes errc)
 	memset(&copts, 0, sizeof(copts));
 	copts.cb = write_1_cb;
 	copts.private = rp;
-	rc = cldc_put(rp->fh, &copts, rp->buf, TESTLEN);
+	rc = cldc_put(rp->fh, &copts, rp->buf, LOCKLEN);
 	if (rc) {
 		fprintf(stderr, "cldc_put call error %d\n", rc);
 		exit(1);
@@ -129,25 +147,26 @@ static int write_1_cb(struct cldc_call_opts *coptarg, enum cle_err_codes errc)
 static int lock_1_cb(struct cldc_call_opts *coptarg, enum cle_err_codes errc)
 {
 	struct run *rp = coptarg->private;
-	struct timeval tv = { 40, 0 };	/* 40s to make sure session sustains */
-	int rc;
 
 	if (errc != CLE_OK) {
 		fprintf(stderr, "first-lock failed: %d\n", errc);
 		exit(1);
 	}
 
-	rc = evtimer_add(&rp->tmr_ev, &tv);
-	if (rc) {
-		fprintf(stderr, "evtimer_add call error %d\n", rc);
-		exit(1);
-	}
+	/* Idle for 40s to verify that session sustains a protocol ping. */
+	timer_add(&rp->tmr_test, time(NULL) + 40);
 	return 0;
 }
 
-static void timer_1(int fd, short events, void *userdata)
+static void timer_test_event(struct timer *timer)
+{
+	struct run *rp = timer->userdata;
+
+	timer_1(rp);
+}
+
+static void timer_1(struct run *rp)
 {
-	struct run *rp = userdata;
 	struct cldc_call_opts copts;
 	int rc;
 
@@ -192,16 +211,15 @@ static int end_sess_cb(struct cldc_call_opts *copts, enum cle_err_codes errc)
 	}
 
 	/* session ended; success */
-	event_loopbreak();
-
+	exit(0);
 	return 0;
 }
 
 static struct run run;
 
 static struct cldc_ops ops = {
-	.timer_ctl		= cldc_levent_timer,
-	.pkt_send		= cldc_udp_pkt_send,
+	.timer_ctl		= do_timer_ctl,
+	.pkt_send		= do_pkt_send,
 	.event			= do_event,
 };
 
@@ -211,15 +229,18 @@ static int init(void)
 	int port;
 	struct cldc_call_opts copts;
 
-	memcpy(run.buf, TESTSTR, TESTLEN);
+	memcpy(run.buf, LOCKSTR, LOCKLEN);
 
-	port = cld_readport("cld.port");	/* FIXME need test.h */
+	port = cld_readport(TEST_PORTFILE_CLD);
 	if (port < 0)
 		return port;
 	if (port == 0)
 		return -1;
 
-	rc = cldc_udp_new("localhost", port, &run.udp);
+	timer_init(&run.tmr_test, "lock-timer", timer_test_event, &run);
+	timer_init(&run.tmr_udp, "udp-timer", timer_udp_event, &run);
+
+	rc = cldc_udp_new(TEST_HOST, port, &run.udp);
 	if (rc)
 		return rc;
 
@@ -227,21 +248,12 @@ static int init(void)
 	copts.cb = new_sess_cb;
 	copts.private = &run;
 	rc = cldc_new_sess(&ops, &copts, run.udp->addr, run.udp->addr_len,
-			   "testuser", "testuser", run.udp, &run.udp->sess);
+			   TEST_USER, TEST_USER_KEY, &run, &run.udp->sess);
 	if (rc)
 		return rc;
 
 	// run.udp->sess->verbose = true;
 
-	event_set(&run.udp_ev, run.udp->fd, EV_READ | EV_PERSIST,
-		  udp_event, &run);
-	evtimer_set(&run.tmr_ev, timer_1, &run);
-
-	if (event_add(&run.udp_ev, NULL) < 0) {
-		fprintf(stderr, "event_add failed\n");
-		return 1;
-	}
-
 	return 0;
 }
 
@@ -249,10 +261,9 @@ int main (int argc, char *argv[])
 {
 	g_thread_init(NULL);
 	cldc_init();
-	event_init();
 	if (init())
 		return 1;
-	event_dispatch();
+	test_loop(run.udp);
 	return 0;
 }
 
diff --git a/test/save-file-event.c b/test/save-file-event.c
index 23f1781..453f627 100644
--- a/test/save-file-event.c
+++ b/test/save-file-event.c
@@ -1,7 +1,6 @@
 
 /*
  * Create a file in CLD.
- * This version uses libevent.
  */
 #include <sys/types.h>
 #include <unistd.h>
@@ -9,19 +8,15 @@
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
-#include <event.h>
 #include <cldc.h>
-
-#define TESTSTR          "longertestdata\n"
-#define TESTLEN  (sizeof("longertestdata\n")-1)
-
-#define TFNAME     "/cld-test-inst"
+#include <libtimer.h>
+#include "test.h"
 
 #define DATAMAX  10000
 
 struct run {
 	struct cldc_udp *udp;
-	struct event udp_ev;
+	struct timer tmr_udp;
 	struct cldc_fh *fh;
 	char *fname;
 	char *buf;
@@ -35,22 +30,43 @@ static void call_close(struct run *rp);
 static int close_1_cb(struct cldc_call_opts *coptarg, enum cle_err_codes errc);
 static int end_sess_cb(struct cldc_call_opts *copts, enum cle_err_codes errc);
 
-static void do_event(void *private, struct cldc_session *sess,
-		     struct cldc_fh *fh, uint32_t event_mask)
+static bool do_timer_ctl(void *priv, bool add,
+			 int (*cb)(struct cldc_session *, void *),
+			 void *cb_priv, time_t secs)
 {
-	fprintf(stderr, "EVENT(0x%x)\n", event_mask);
+	struct run *rp = priv;
+
+	if (add) {
+		rp->udp->cb = cb;
+		rp->udp->cb_private = cb_priv;
+		timer_add(&rp->tmr_udp, time(NULL) + secs);
+	} else {
+		timer_del(&rp->tmr_udp);
+	}
+	return true;
 }
 
-static void udp_event(int fd, short events, void *userdata)
+static int do_pkt_send(void *priv, const void *addr, size_t addrlen,
+		       const void *buf, size_t buflen)
 {
-	struct run *rp = userdata;
-	int rc;
+	struct run *rp = priv;
+	return cldc_udp_pkt_send(rp->udp, addr, addrlen, buf, buflen);
+}
 
-	rc = cldc_udp_receive_pkt(rp->udp);
-	if (rc) {
-		fprintf(stderr, "cldc_udp_receive_pkt failed: %d\n", rc);
-		exit(1);
-	}
+static void timer_udp_event(struct timer *timer)
+{
+	struct run *rp = timer->userdata;
+	struct cldc_udp *udp;
+
+	udp = rp->udp;
+	if (udp->cb)
+		udp->cb(udp->sess, udp->cb_private);
+}
+
+static void do_event(void *private, struct cldc_session *sess,
+		     struct cldc_fh *fh, uint32_t event_mask)
+{
+	fprintf(stderr, "EVENT(0x%x)\n", event_mask);
 }
 
 static int new_sess_cb(struct cldc_call_opts *coptarg, enum cle_err_codes errc)
@@ -172,19 +188,15 @@ static int end_sess_cb(struct cldc_call_opts *copts, enum cle_err_codes errc)
 	}
 
 	/* session ended; success */
-	event_loopbreak();
-
+	exit(0);
 	return 0;
 }
 
 static struct run run;
-#if 0
-static char databuf[DATAMAX];
-#endif
 
 static struct cldc_ops ops = {
-	.timer_ctl		= cldc_levent_timer,
-	.pkt_send		= cldc_udp_pkt_send,
+	.timer_ctl		= do_timer_ctl,
+	.pkt_send		= do_pkt_send,
 	.event			= do_event,
 };
 
@@ -195,27 +207,18 @@ static int init(char *name)
 	struct cldc_call_opts copts;
 
 	run.fname = name;
-#if 0
-	run.buf = databuf;
-
-	rc = read(0, databuf, DATAMAX);
-	if (rc < 0) {
-		fprintf(stderr, "read error: %s\n", strerror(errno));
-		return -1;
-	}
-	run.len = rc;
-#else
 	run.buf = TESTSTR;
 	run.len = TESTLEN;
-#endif
 
-	port = cld_readport("cld.port");	/* FIXME need test.h */
+	port = cld_readport(TEST_PORTFILE_CLD);
 	if (port < 0)
 		return port;
 	if (port == 0)
 		return -1;
 
-	rc = cldc_udp_new("localhost", port, &run.udp);
+	timer_init(&run.tmr_udp, "udp-timer", timer_udp_event, &run);
+
+	rc = cldc_udp_new(TEST_HOST, port, &run.udp);
 	if (rc)
 		return rc;
 
@@ -223,43 +226,22 @@ static int init(char *name)
 	copts.cb = new_sess_cb;
 	copts.private = &run;
 	rc = cldc_new_sess(&ops, &copts, run.udp->addr, run.udp->addr_len,
-			   "testuser", "testuser", run.udp, &run.udp->sess);
+			   TEST_USER, TEST_USER_KEY, &run, &run.udp->sess);
 	if (rc)
 		return rc;
 
 	// run.udp->sess->verbose = true;
 
-	event_set(&run.udp_ev, run.udp->fd, EV_READ | EV_PERSIST,
-		  udp_event, &run);
-
-	if (event_add(&run.udp_ev, NULL) < 0) {
-		fprintf(stderr, "event_add failed\n");
-		return 1;
-	}
-
 	return 0;
 }
 
 int main(int argc, char *argv[])
 {
-#if 0
-	if (argc != 2) {
-		fprintf(stderr, "Usage: save-file-event {filename}\n");
-		return 1;
-	}
-#endif
-
 	g_thread_init(NULL);
 	cldc_init();
-	event_init();
-#if 0
-	if (init(argv[1]))
-		return 1;
-#else
 	if (init(TFNAME))
 		return 1;
-#endif
-	event_dispatch();
+	test_loop(run.udp);
 	return 0;
 }
 
diff --git a/test/test.h b/test/test.h
new file mode 100644
index 0000000..8e87684
--- /dev/null
+++ b/test/test.h
@@ -0,0 +1,25 @@
+#ifndef _CLD_TEST_H_
+#define _CLD_TEST_H_
+
+#include <stdbool.h>
+#include <cldc.h>
+
+#define TESTSTR          "longertestdata\n"
+#define TESTLEN  (sizeof("longertestdata\n")-1)
+
+#define LOCKSTR          "testlock\n"
+#define LOCKLEN  (sizeof("testlock\n")-1)
+
+#define TFNAME     "/cld-test-inst"
+#define TLNAME     "/cld-lock-inst"
+
+#define TEST_HOST "localhost"
+
+#define TEST_USER "testuser"
+#define TEST_USER_KEY "testuser"
+
+#define TEST_PORTFILE_CLD	"cld.port"
+
+extern void test_loop(struct cldc_udp *udp);
+
+#endif
diff --git a/test/util.c b/test/util.c
new file mode 100644
index 0000000..8966b9f
--- /dev/null
+++ b/test/util.c
@@ -0,0 +1,58 @@
+
+/*
+ * General utilities for CLD tests.
+ */
+#include <sys/types.h>
+#include <sys/select.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include <libtimer.h>
+#include <cldc.h>
+#include "test.h"
+
+void test_loop(struct cldc_udp *udp)
+{
+	int ufd = udp->fd;
+	fd_set rset;
+	struct timeval tm;
+	time_t tmo;
+	int rc;
+
+	for (;;) {
+		FD_ZERO(&rset);
+		FD_SET(ufd, &rset);
+
+		tmo = timers_run();
+		if (tmo) {
+			tm.tv_sec = tmo;
+			tm.tv_usec = 0;
+			rc = select(ufd + 1, &rset, NULL, NULL, &tm);
+			if (rc < 0) {
+				fprintf(stderr, "select: error\n");
+				exit(1);
+			}
+			if (rc == 0)
+				continue;
+		} else {
+			rc = select(ufd + 1, &rset, NULL, NULL, NULL);
+			if (rc <= 0) {
+				fprintf(stderr, "select: nfd %d\n", rc);
+				exit(1);
+			}
+		}
+
+		if (FD_ISSET(ufd, &rset)) {
+			rc = cldc_udp_receive_pkt(udp);
+			if (rc) {
+				fprintf(stderr,
+					"cldc_udp_receive_pkt: error %d\n", rc);
+				exit(1);
+			}
+		} else {
+			fprintf(stderr, "noevent\n");
+			exit(1);
+		}
+	}
+}
+
diff --git a/tools/Makefile.am b/tools/Makefile.am
index c171a78..2a6e1a5 100644
--- a/tools/Makefile.am
+++ b/tools/Makefile.am
@@ -5,5 +5,5 @@ INCLUDES	= -I$(top_srcdir)/include	\
 bin_PROGRAMS	= cldcli
 
 cldcli_LDADD	= ../lib/libcldc.la		\
-		  @GLIB_LIBS@ @CRYPTO_LIBS@ @EVENT_LIBS@ @ARGP_LIBS@
+		  @GLIB_LIBS@ @CRYPTO_LIBS@ @ARGP_LIBS@ @RESOLV_LIBS@
 

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [Patch 2/2] CLD: drop dependency on libevent from libcldc
  2009-11-28  9:07 ` Jeff Garzik
@ 2009-11-29  6:38   ` Pete Zaitcev
  0 siblings, 0 replies; 4+ messages in thread
From: Pete Zaitcev @ 2009-11-29  6:38 UTC (permalink / raw)
  To: Jeff Garzik; +Cc: Project Hail List

On Sat, 28 Nov 2009 04:07:32 -0500
Jeff Garzik <jeff@garzik.org> wrote:

> I would suggest using some variant of the changes below as patch #1,
> then add these other changes on top of that.  I have not committed
> these changes, pending your comments.

Thanks. I should've done it myself. Don't know what I was thinking,
I knew that these routines existed.

-- Pete

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2009-11-29  6:38 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-11-28  3:52 [Patch 2/2] CLD: drop dependency on libevent from libcldc Pete Zaitcev
2009-11-28  9:07 ` Jeff Garzik
2009-11-29  6:38   ` Pete Zaitcev
  -- strict thread matches above, loose matches on Subject: below --
2009-11-29  6:35 Pete Zaitcev

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.