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

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