diff options
author | sandmann <sandmann> | 2004-05-18 23:24:59 +0000 |
---|---|---|
committer | sandmann <sandmann> | 2004-05-18 23:24:59 +0000 |
commit | d3c68076e8d6a878902c8f8c12aa9e320307abad (patch) | |
tree | da6ce2138f39441f604a567073254937a4e5ce15 | |
parent | b2ec265f02f8163cd308517a7474d13465bbf1cf (diff) |
*** empty log message ***
-rw-r--r-- | src/Makefile.am | 1 | ||||
-rw-r--r-- | src/lac.h | 43 | ||||
-rw-r--r-- | src/lacactivity.c | 131 | ||||
-rw-r--r-- | src/lacaddress.c | 172 | ||||
-rw-r--r-- | src/lacinternals.h | 2 | ||||
-rwxr-xr-x | tests/Makefile.am | 2 | ||||
-rw-r--r-- | tests/connection2-test.c | 4 | ||||
-rw-r--r-- | tests/dns-test2.c | 10 | ||||
-rw-r--r-- | tests/dns-wait-test.c | 9 | ||||
-rw-r--r-- | tests/udp-test.c | 2 | ||||
-rw-r--r-- | tests/watch-test.c | 2 |
11 files changed, 77 insertions, 301 deletions
diff --git a/src/Makefile.am b/src/Makefile.am index f5f7cf2..139e44a 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -16,7 +16,6 @@ liblac_1_la_SOURCES = \ lacdns-messages.c \ lacdns-cache.c \ lacdns-nameserver.c \ - lacactivity.c \ lacaddress.c \ lacuri.c \ lacconnection.c \ @@ -28,28 +28,6 @@ G_BEGIN_DECLS /* - * Activity - */ -typedef struct LacActivityData *LacActivity; - -#if 0 -LacActivity *lac_activity_new (void); -LacActivity *lac_activity_ref (LacActivity *activity); -void lac_activity_unref (LacActivity *activity); -void lac_activity_set_timeout (LacActivity *activity, - gint timeout); -gint lac_activity_get_timeout (LacActivity *activity); -gboolean lac_activity_wait (LacActivity *activity); -void lac_activity_continue (LacActivity *activity); -void lac_activity_stop (LacActivity *activity); - -gboolean lac_activity_running (LacActivity *activity); -gboolean lac_activity_timed_out (LacActivity *activity); -gboolean lac_activity_canceled (LacActivity *activity); -#endif -void lac_activity_cancel (LacActivity activity); - -/* * Addresses */ #define LAC_DNS_ERROR lac_dns_error_quark () @@ -86,24 +64,22 @@ LacAddress *lac_address_new_from_a_b_c_d (gchar a, gchar c, gchar d); LacAddress *lac_address_new_from_string (const gchar *str); -LacActivity lac_address_new_lookup_from_name (const gchar *name, + +void lac_address_new_lookup_from_name (const gchar *name, LacAddressFunc f, gpointer data); -LacActivity lac_address_new_lookup_all_from_name (const gchar *name, +void lac_address_new_lookup_all_from_name (const gchar *name, LacAddressesFunc f, gpointer data); LacAddress *lac_address_new_from_name_wait (const gchar *name, - LacActivity *activity, GError **err); -LacActivity lac_address_new_lookup_from_localhost (LacAddressFunc f, +void lac_address_new_lookup_from_localhost (LacAddressFunc f, gpointer data); -LacAddress *lac_address_new_from_localhost_wait (LacActivity *activity, - GError **err); -LacActivity lac_address_lookup_name (const LacAddress *addr, +LacAddress *lac_address_new_from_localhost_wait (GError **err); +void lac_address_lookup_name (const LacAddress *addr, LacNameFunc f, gpointer data); gchar * lac_address_lookup_name_wait (const LacAddress *addr, - LacActivity *activity, GError **err); gchar * lac_address_to_string (const LacAddress *addr); guint lac_address_hash (gconstpointer addr); @@ -229,7 +205,6 @@ gboolean lac_fd_is_watched (gint fd); * Connection */ typedef struct _LacConnection LacConnection; -typedef struct _LacConnectionOptions LacConnectionOptions; typedef union _LacConnectionEvent LacConnectionEvent; typedef enum { @@ -346,12 +321,6 @@ gchar * lac_uri_string (const LacUri *uri); void lac_uri_free (LacUri *uri); gboolean lac_uri_equal (const LacUri *uri1, const LacUri *uri2); - -/* - * - gchar *lac_uri_get_scheme (const gchar *uri) - lac_uri_cd_up - */ /* diff --git a/src/lacactivity.c b/src/lacactivity.c deleted file mode 100644 index 2ab5eb2..0000000 --- a/src/lacactivity.c +++ /dev/null @@ -1,131 +0,0 @@ -/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- */ - -/* Lac - Library for asynchronous communication - * Copyright (C) 2003 Søren Sandmann (sandmann@daimi.au.dk) - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Library General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Library General Public License for more details. - * - * You should have received a copy of the GNU Library General Public - * License along with this library; if not, write to the - * Free Software Foundation, Inc., 59 Temple Place - Suite 330, - * Boston, MA 02111-1307, USA. - */ - -#include "lacinternals.h" - -typedef void (* Notify) (gpointer data); - -typedef struct LacActivityData LacActivityData; -struct LacActivityData { - gboolean cancel_allowed; - gboolean canceled; - - GMainLoop *main_loop; - - gint ref_count; -}; - -static void -lac_activity_destroy (LacActivity activity) -{ - if (activity->main_loop) - g_main_loop_unref (activity->main_loop); - - g_free (activity); -} - -gboolean -lac_activity_unref (LacActivity activity) -{ - if (--activity->ref_count == 0) - { - lac_activity_destroy (activity); - return TRUE; - } - - return FALSE; -} - -LacActivity -lac_activity_ref (LacActivity activity) -{ - activity->ref_count++; - return activity; -} - -void -lac_activity_disable_cancel (LacActivity activity) -{ - activity->cancel_allowed = FALSE; -} - -LacActivity -lac_activity_new (void) -{ - LacActivity activity = g_new0 (LacActivityData, 1); - - activity->cancel_allowed = TRUE; - activity->canceled = FALSE; - activity->main_loop = NULL; - activity->ref_count = 1; - - return activity; -} - -gboolean -lac_activity_canceled (LacActivity activity) -{ - return activity->canceled; -} - -gboolean -lac_activity_wait (LacActivity activity) -{ - gboolean canceled; - - activity->main_loop = g_main_loop_new (NULL, FALSE); - - lac_activity_ref (activity); - - g_main_loop_run (activity->main_loop); - canceled = lac_activity_canceled (activity); - - lac_activity_unref (activity); - - if (canceled) - return FALSE; - - return TRUE; -} - -void -lac_activity_cancel (LacActivity activity) -{ - if (!activity->cancel_allowed) - { - g_warning ("You can't cancel an activity when its callback is running\n"); - return; - } - - if (activity->canceled) - { - g_warning ("Canceling an activity twice\n"); - return; - } - - activity->canceled = TRUE; - - if (activity->main_loop && - g_main_loop_is_running (activity->main_loop)) - { - g_main_loop_quit (activity->main_loop); - } -} diff --git a/src/lacaddress.c b/src/lacaddress.c index fa040fa..1d58441 100644 --- a/src/lacaddress.c +++ b/src/lacaddress.c @@ -125,7 +125,6 @@ typedef struct AddressesLookupInfo { gchar * domain; LacAddressesFunc f; gpointer data; - LacActivity activity; } AddressesLookupInfo; static void @@ -169,14 +168,8 @@ get_addresses_callback (const DnsQueryEvent *event, gpointer data) break; } - if (!lac_activity_canceled (info->activity)) - { - lac_activity_disable_cancel (info->activity); - - info->f (addresses, info->data, err); - } + info->f (addresses, info->data, err); - lac_activity_unref (info->activity); g_free (info->domain); g_free (info); @@ -192,7 +185,7 @@ get_addresses_callback (const DnsQueryEvent *event, gpointer data) g_error_free (err); } -LacActivity +void lac_address_new_lookup_all_from_name (const gchar *name, LacAddressesFunc f, gpointer data) @@ -200,14 +193,13 @@ lac_address_new_lookup_all_from_name (const gchar *name, AddressesLookupInfo *info; LacAddress *address; GError *err = NULL; - LacActivity activity; if (!dns_config_initialize (&err)) { f (NULL, data, err); g_error_free (err); - return NULL; + return; } /* check if name is on the "a.b.c.d" form */ @@ -224,23 +216,15 @@ lac_address_new_lookup_all_from_name (const gchar *name, lac_address_free (address); g_ptr_array_free (addresses, TRUE); - return NULL; + return; } info = g_new (AddressesLookupInfo, 1); info->domain = g_strdup (name); info->f = f; info->data = data; - info->activity = activity = lac_activity_new (); - - lac_activity_ref (activity); dns_query (name, A_TYPE, get_addresses_callback, info); - - if (lac_activity_unref (activity)) - return NULL; - - return activity; } /* @@ -249,7 +233,6 @@ lac_address_new_lookup_all_from_name (const gchar *name, typedef struct { LacAddressFunc f; gpointer data; - LacActivity activity; } GetAddressInfo; static void @@ -259,44 +242,29 @@ dns_addr_callback (const GPtrArray *addresses, { GetAddressInfo *info = data; - if (!lac_activity_canceled (info->activity)) - { - lac_activity_disable_cancel (info->activity); - - if (err) - info->f (NULL, info->data, err); - else - info->f (addresses->pdata[0], info->data, err); - } + if (err) + info->f (NULL, info->data, err); + else + info->f (addresses->pdata[0], info->data, err); - lac_activity_unref (info->activity); g_free (info); } -LacActivity +void lac_address_new_lookup_from_name (const gchar *name, LacAddressFunc f, gpointer data) { GetAddressInfo *info; - LacActivity activity; - g_return_val_if_fail (name != NULL, NULL); - g_return_val_if_fail (f != NULL, NULL); + g_return_if_fail (name != NULL); + g_return_if_fail (f != NULL); info = g_new (GetAddressInfo, 1); info->f = f; info->data = data; - info->activity = activity = lac_activity_new (); - lac_activity_ref (activity); - lac_address_new_lookup_all_from_name (name, dns_addr_callback, info); - - if (lac_activity_unref (activity)) - return NULL; - - return activity; } /* @@ -306,7 +274,6 @@ typedef struct NameLookupInfo { LacAddress *address; LacNameFunc f; gpointer data; - LacActivity activity; } NameLookupInfo; static void @@ -344,15 +311,9 @@ get_name_callback (const DnsQueryEvent *event, gpointer data) break; } - if (!lac_activity_canceled (info->activity)) - { - lac_activity_disable_cancel (info->activity); - - info->f (name, info->data, err); - } + info->f (name, info->data, err); lac_address_free (info->address); - lac_activity_unref (info->activity); g_free (info); if (free_me) @@ -371,7 +332,7 @@ address_to_in_addr_arpa (const LacAddress *addr) return g_strdup_printf ("%d.%d.%d.%d.in-addr.arpa.", d, c, b, a); } -LacActivity +void lac_address_lookup_name (const LacAddress *address, LacNameFunc f, gpointer data) @@ -379,14 +340,13 @@ lac_address_lookup_name (const LacAddress *address, NameLookupInfo *info; gchar *domain_str; GError *err = NULL; - LacActivity activity; if (!dns_config_initialize (&err)) { f (NULL, data, err); g_error_free (err); - return NULL; + return; } domain_str = address_to_in_addr_arpa (address); @@ -395,17 +355,9 @@ lac_address_lookup_name (const LacAddress *address, info->address = lac_address_copy (address); info->f = f; info->data = data; - info->activity = activity = lac_activity_new (); - - lac_activity_ref (activity); dns_query (domain_str, PTR_TYPE, get_name_callback, info); g_free (domain_str); - - if (lac_activity_unref (activity)) - return NULL; - - return activity; } /* @@ -418,6 +370,7 @@ lac_address_lookup_name (const LacAddress *address, typedef struct { LacAddress *result; GError * err; + GMainLoop * loop; } GetHostByNameInfo; static void @@ -435,35 +388,35 @@ ghbn_callback (const GPtrArray *addresses, gpointer data, const GError *err) ghbn_info->result = NULL; ghbn_info->err = g_error_copy (err); } + + if (g_main_loop_is_running (ghbn_info->loop)) + g_main_loop_quit (ghbn_info->loop); } LacAddress * lac_address_new_from_name_wait (const gchar *name, - LacActivity *activity_return, GError **err) { GetHostByNameInfo ghbn_info; - LacActivity activity; - - activity = lac_address_new_lookup_all_from_name ( - name, ghbn_callback, &ghbn_info); - - if (activity_return) - *activity_return = activity; - if (activity && !lac_activity_wait (activity)) - { - /* canceled */ - return NULL; - } + ghbn_info.loop = g_main_loop_new (NULL, FALSE); + lac_address_new_lookup_all_from_name ( + name, ghbn_callback, &ghbn_info); + + g_main_loop_run (ghbn_info.loop); + + g_main_loop_unref (ghbn_info.loop); + if (ghbn_info.err) { g_propagate_error (err, ghbn_info.err); return NULL; } - - return ghbn_info.result; + else + { + return ghbn_info.result; + } } /* @@ -472,6 +425,7 @@ lac_address_new_from_name_wait (const gchar *name, typedef struct { gchar * result; GError * err; + GMainLoop * loop; } GetHostByAddrInfo; static void @@ -489,34 +443,34 @@ ghba_callback (const gchar *name, gpointer data, const GError *err) ghba_info->result = NULL; ghba_info->err = g_error_copy (err); } + + if (g_main_loop_is_running (ghba_info->loop)) + g_main_loop_quit (ghba_info->loop); } gchar * lac_address_lookup_name_wait (const LacAddress *addr, - LacActivity *activity_return, GError **err) { GetHostByAddrInfo ghba_info; - LacActivity activity; - activity = lac_address_lookup_name (addr, ghba_callback, &ghba_info); - - if (activity_return) - *activity_return = activity; + ghba_info.loop = g_main_loop_new (NULL, FALSE); - if (activity && !lac_activity_wait (activity)) - { - /* canceled */ - return NULL; - } + lac_address_lookup_name (addr, ghba_callback, &ghba_info); + g_main_loop_run (ghba_info.loop); + + g_main_loop_unref (ghba_info.loop); + if (ghba_info.err) { g_propagate_error (err, ghba_info.err); return NULL; } - - return ghba_info.result; + else + { + return ghba_info.result; + } } LacAddress * @@ -596,27 +550,27 @@ lac_address_new_from_string (const gchar *str) return addr; } -LacActivity +void lac_address_new_lookup_from_localhost (LacAddressFunc f, gpointer data) { gchar *name; - LacActivity activity; name = lac_gethostname (); - activity = lac_address_new_lookup_from_name (name, f, data); + lac_address_new_lookup_from_name (name, f, data); g_free (name); - - return activity; } typedef struct { LacAddress *result; GError *err; + GMainLoop *loop; } NewFromLocalHostInfo; static void -localhost_callback (const LacAddress *address, gpointer data, const GError *err) +localhost_callback (const LacAddress *address, + gpointer data, + const GError *err) { NewFromLocalHostInfo *info = data; @@ -630,31 +584,33 @@ localhost_callback (const LacAddress *address, gpointer data, const GError *err) info->result = NULL; info->err = g_error_copy (err); } + + if (g_main_loop_is_running (info->loop)) + g_main_loop_quit (info->loop); } LacAddress * -lac_address_new_from_localhost_wait (LacActivity *activity_return, - GError **err) +lac_address_new_from_localhost_wait (GError **err) { NewFromLocalHostInfo info; - LacActivity activity; - activity = - lac_address_new_lookup_from_localhost (localhost_callback, &info); + info.loop = g_main_loop_new (NULL, FALSE); - if (!lac_activity_wait (activity)) - { - /* canceled */ - return NULL; - } + lac_address_new_lookup_from_localhost (localhost_callback, &info); + g_main_loop_run (info.loop); + + g_main_loop_unref (info.loop); + if (info.err) { g_propagate_error (err, info.err); return NULL; } - - return info.result; + else + { + return info.result; + } } guint diff --git a/src/lacinternals.h b/src/lacinternals.h index 97cad07..52fea0b 100644 --- a/src/lacinternals.h +++ b/src/lacinternals.h @@ -24,6 +24,7 @@ #include "lac.h" +#if 0 /* * Activity */ @@ -36,6 +37,7 @@ LacActivity lac_activity_ref (LacActivity activity); LacActivity lac_activity_new (void); gboolean lac_activity_wait (LacActivity activity); gboolean lac_activity_canceled (LacActivity activity); +#endif /* * Addresses diff --git a/tests/Makefile.am b/tests/Makefile.am index a18e4c2..eb9c277 100755 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -4,7 +4,6 @@ TESTPROGRAMS = \ dns-test \ dns-test-async \ dns-test2 \ - dns-wait-test \ rdns-test-async \ uri-test \ http-test \ @@ -28,7 +27,6 @@ simple_test_LDADD = $(top_builddir)/src/liblac-1.la dns_test_LDADD = $(top_builddir)/src/liblac-1.la dns_test_async_LDADD = $(top_builddir)/src/liblac-1.la dns_test2_LDADD = $(top_builddir)/src/liblac-1.la -dns_wait_test_LDADD = $(top_builddir)/src/liblac-1.la rdns_test_async_LDADD = $(top_builddir)/src/liblac-1.la uri_test_LDADD = $(top_builddir)/src/liblac-1.la connection_test_LDADD = $(top_builddir)/src/liblac-1.la diff --git a/tests/connection2-test.c b/tests/connection2-test.c index c2bc370..898436a 100644 --- a/tests/connection2-test.c +++ b/tests/connection2-test.c @@ -28,7 +28,7 @@ int main (int argc, char *argv[]) { LacAddress *addr = - lac_address_new_from_name_wait ("www.toyota.co.jp", NULL, NULL); + lac_address_new_from_name_wait ("www.toyota.co.jp", NULL); int fd; guchar blah[60000]; GTimer *timer; @@ -42,7 +42,7 @@ main (int argc, char *argv[]) g_assert (lac_connect (fd, addr, 80, NULL)); -#define line1 "GET / HTTP/1.1\r\n" +#define line1 "GET /index.html HTTP/1.1\r\n" #define line2 "host: www.toyota.co.jp\r\n\r\n" timer = g_timer_new (); diff --git a/tests/dns-test2.c b/tests/dns-test2.c index c70b91b..3233cf0 100644 --- a/tests/dns-test2.c +++ b/tests/dns-test2.c @@ -95,17 +95,9 @@ main (int argc, char *argv[]) { for (i = 1; i < argc; ++i) { - LacActivity activity; ++n_outstanding; - activity = lac_address_new_lookup_all_from_name ( + lac_address_new_lookup_all_from_name ( argv[i], callback, argv[i]); - - if (i == 3 || i == 4) - { - g_print ("canceling\n"); - lac_activity_cancel (activity); - } - } } else diff --git a/tests/dns-wait-test.c b/tests/dns-wait-test.c index 224368c..43cb289 100644 --- a/tests/dns-wait-test.c +++ b/tests/dns-wait-test.c @@ -24,15 +24,6 @@ #include <netinet/tcp.h> #include <string.h> -static gboolean -timeout (gpointer data) -{ - LacActivity activity = data; - g_print ("hej\n"); - lac_activity_cancel (activity); - return FALSE; -} - int main (int argc, char *argv[]) { diff --git a/tests/udp-test.c b/tests/udp-test.c index d91254d..37bb08c 100644 --- a/tests/udp-test.c +++ b/tests/udp-test.c @@ -34,7 +34,7 @@ main (int argc, char *argv[]) GError *err = NULL; int fd; - addr = lac_address_new_from_localhost_wait (NULL, NULL); + addr = lac_address_new_from_localhost_wait (NULL); if (!addr) { g_print ("can't find local IP address \n"); diff --git a/tests/watch-test.c b/tests/watch-test.c index 91ccf42..362ff10 100644 --- a/tests/watch-test.c +++ b/tests/watch-test.c @@ -85,7 +85,7 @@ main (int argc, char *argv[]) GError *err = NULL; int fd; - addr = lac_address_new_from_name_wait ("www.daimi.au.dk.", NULL, NULL); + addr = lac_address_new_from_name_wait ("www.daimi.au.dk.", NULL); g_print ("address is: %s\n", lac_address_to_string (addr)); fd = lac_socket_tcp (NULL); if (!lac_connect (fd, addr, 80, &err)) |