[Patch 2/2] CLD: drop dependency on libevent from libcldc

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxxxx>

---
 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@
 
--
To unsubscribe from this list: send the line "unsubscribe hail-devel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Fedora Clound]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]     [XFree86]

  Powered by Linux