giftcurs-commits
[Top][All Lists]
Advanced

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

[giFTcurs-commits] giFTcurs/src get.c gift.c gift.h protocol.c pro...


From: Christian Häggström
Subject: [giFTcurs-commits] giFTcurs/src get.c gift.c gift.h protocol.c pro...
Date: Mon, 08 Sep 2003 13:47:33 -0400

CVSROOT:        /cvsroot/giftcurs
Module name:    giFTcurs
Branch:         
Changes by:     Christian Häggström <address@hidden>    03/09/08 13:47:33

Modified files:
        src            : get.c gift.c gift.h protocol.c protocol.h 
                         test_gift.c ui_main.c 

Log message:
        Try to reconnect _before_ allocating an ID

Patches:
Index: giFTcurs/src/get.c
diff -u giFTcurs/src/get.c:1.205 giFTcurs/src/get.c:1.206
--- giFTcurs/src/get.c:1.205    Tue Aug 19 10:56:09 2003
+++ giFTcurs/src/get.c  Mon Sep  8 13:47:23 2003
@@ -18,7 +18,7 @@
  * along with giFTcurs; if not, write to the Free Software Foundation,
  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307,  USA.
  *
- * $Id: get.c,v 1.205 2003/08/19 14:56:09 weinholt Exp $
+ * $Id: get.c,v 1.206 2003/09/08 17:47:23 saturn Exp $
  */
 #include "giftcurs.h"
 
@@ -178,18 +178,12 @@
 {
        ntree *packet = NULL;
 
-       t->search_id = gift_new_id();
-
-       interface_append_int(&packet, "LOCATE", t->search_id);
        interface_append(&packet, "query", t->hash);
        if (t->protocol)
                interface_append(&packet, "protocol", t->protocol);
-       gift_register_id(t->search_id, (EventCallback) 
download_incoming_source_handler, t);
-       if (gift_write(&packet) < 0) {
-               t->search_id = 0;
-               gift_unregister_id(t->search_id);
+       t->search_id = gift_write_register(&packet, "LOCATE", (EventCallback) 
download_incoming_source_handler, t);
+       if (t->search_id == 0)
                return -1;
-       }
        g_message(_("Started a source search for '%s'."), t->filename);
 
        return 0;
Index: giFTcurs/src/gift.c
diff -u giFTcurs/src/gift.c:1.206 giFTcurs/src/gift.c:1.207
--- giFTcurs/src/gift.c:1.206   Fri Jul 25 20:47:40 2003
+++ giFTcurs/src/gift.c Mon Sep  8 13:47:23 2003
@@ -18,7 +18,7 @@
  * along with giFTcurs; if not, write to the Free Software Foundation,
  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307,  USA.
  *
- * $Id: gift.c,v 1.206 2003/07/26 00:47:40 weinholt Exp $
+ * $Id: gift.c,v 1.207 2003/09/08 17:47:23 saturn Exp $
  */
 #include "giftcurs.h"
 
@@ -62,9 +62,11 @@
 {
        guint id = bitmap_find_unset(id_bitmap, sizeof id_bitmap);
 
-       if (G_UNLIKELY(id == -1))
-               /* FIXME: no more id's, the crowds go wild */ ;
-       g_assert(id != 0);
+       if (G_UNLIKELY(id == -1)) {
+               g_message(_("Run out of id's, please cancel some searches."));
+               return -1;
+       }
+       g_assert(id > 0);
 
        bitmap_set(id_bitmap, sizeof id_bitmap, id, TRUE);
        return id;
@@ -72,9 +74,9 @@
 
 static void gift_claimed_id(gift_id id)
 {
-       static gboolean have_warned;
-
        if (id < 32768 && !bitmap_get(id_bitmap, sizeof id_bitmap, id)) {
+               static gboolean have_warned;
+
                if (!have_warned)
                        g_message(_("The daemon used the reserved ID %u, 
upgrade the daemon."), id);
                have_warned = TRUE;
@@ -105,10 +107,24 @@
        return TRUE;
 }
 
+static void sanity_check_leftover_id(void)
+{
+#ifndef NDEBUG
+       int i;
+
+       for (i = 0; i < callbacks.num; i++) {
+               event_callback *foo = list_index(&callbacks, i);
+
+               if (foo->command && atoi(foo->command))
+                       abort();
+       }
+#endif
+}
+
 static int gift_attach(void)
 {
        ntree *packet = NULL;
-       int fd, i;
+       int fd;
 
        if (gift_stream)
                return 1;
@@ -123,15 +139,9 @@
        /* Clear the ID bitmap and rework it from any stuff that called
           gift_new_id() before us. */
        memset(id_bitmap, 0, sizeof id_bitmap);
-       bitmap_set(id_bitmap, sizeof id_bitmap, 0, TRUE);       /* ID 0 is 
reserved, or something. */
-       for (i = 0; i < callbacks.num; i++) {
-               event_callback *foo = list_index(&callbacks, i);
-               gift_id id;
-
-               if (!foo->command || !(id = atoi(foo->command)))
-                       continue;
-               bitmap_set(id_bitmap, sizeof id_bitmap, id, TRUE);
-       }
+       /* ID 0 is used internally to denote that an id is not assigned. */
+       bitmap_set(id_bitmap, sizeof id_bitmap, 0, TRUE);
+       sanity_check_leftover_id();
 
        gift_stream = g_io_channel_unix_new(fd);
 
@@ -162,6 +172,9 @@
 
        gift_unregister(NULL, (EventCallback) evil_id_macrophage, NULL);
 
+       /* sanity check */
+       sanity_check_leftover_id();
+
        g_io_channel_shutdown(gift_stream, FALSE, NULL);
        g_io_channel_unref(gift_stream);
        gift_stream = NULL;
@@ -187,6 +200,35 @@
        g_io_channel_flush(gift_stream, NULL);
        g_free(data);
        return 0;
+}
+
+int gift_write_register(ntree **packet, const char *command, EventCallback cb, 
void *udata)
+{
+       /* Allocates an id, writes a packet, and registers the callback
+        * if successful. Have the advantage that it tests the connection
+        * before allocating an id, in case a reconnect occurs
+        */
+       char *data;
+       gift_id id;
+
+       g_assert(*packet);
+
+       if (gift_attach() < 0 || (id = gift_new_id()) <= 0) {
+               interface_free(*packet);
+               *packet = NULL;
+               return 0;
+       }
+
+       interface_prepend_int(packet, command, id);
+       data = interface_construct(*packet);
+       interface_free(*packet);
+       *packet = NULL;
+       DEBUG("<= %s", data);
+       g_io_channel_write_chars(gift_stream, data, -1, NULL, NULL);
+       g_io_channel_flush(gift_stream, NULL);
+       g_free(data);
+       gift_register_id(id, cb, udata);
+       return id;
 }
 
 void gift_cleanup(void)
Index: giFTcurs/src/gift.h
diff -u giFTcurs/src/gift.h:1.79 giFTcurs/src/gift.h:1.80
--- giFTcurs/src/gift.h:1.79    Fri Jul 25 20:47:40 2003
+++ giFTcurs/src/gift.h Mon Sep  8 13:47:23 2003
@@ -18,7 +18,7 @@
  * along with giFTcurs; if not, write to the Free Software Foundation,
  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307,  USA.
  *
- * $Id: gift.h,v 1.79 2003/07/26 00:47:40 weinholt Exp $
+ * $Id: gift.h,v 1.80 2003/09/08 17:47:23 saturn Exp $
  */
 #ifndef _GIFT_H
 #define _GIFT_H
@@ -28,8 +28,6 @@
 typedef guint16 gift_id;
 typedef int (*EventCallback) (void *param, void *udata);
 
-/* Get the next available id. */
-gift_id gift_new_id(void);
 int gift_emit(const char *command, void *param);
 
 /* this needs to be called upon startup/exit */
@@ -37,6 +35,10 @@
 void gift_cleanup(void);
 
 int gift_write(ntree **packet);
+
+/* Starts a query with the next available id. Registers the callback to
+ * listen on responses with that id. Returns the new id. */
+int gift_write_register(ntree **packet, const char *command, EventCallback cb, 
void *udata);
 
 void gift_register(const char *command, EventCallback, void *udata);
 void gift_register_id(gift_id id, EventCallback, void *udata);
Index: giFTcurs/src/protocol.c
diff -u giFTcurs/src/protocol.c:1.49 giFTcurs/src/protocol.c:1.50
--- giFTcurs/src/protocol.c:1.49        Fri Aug  1 10:00:00 2003
+++ giFTcurs/src/protocol.c     Mon Sep  8 13:47:23 2003
@@ -18,7 +18,7 @@
  * along with giFTcurs; if not, write to the Free Software Foundation,
  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307,  USA.
  *
- * $Id: protocol.c,v 1.49 2003/08/01 14:00:00 weinholt Exp $
+ * $Id: protocol.c,v 1.50 2003/09/08 17:47:23 saturn Exp $
  */
 #include "giftcurs.h"
 
@@ -39,6 +39,7 @@
        ntree *next;
        ntree *subtree;
        unsigned is_integer:1;
+       unsigned appended:1;
 };
 
 static char *get_unescaped_until(char **packet, char limiter)
@@ -191,7 +192,7 @@
        if (!key)
                return;
 
-       if (key->next)
+       if (key->appended && key->next)
                build(key->next, s);
 
        escape(s, key->name);
@@ -210,6 +211,9 @@
                g_string_append_c(s, '}');
        }
        g_string_append_c(s, ' ');
+
+       if (!key->appended && key->next)
+               build(key->next, s);
 }
 
 char *interface_construct(ntree *tree)
@@ -254,7 +258,7 @@
        return !(tree && tree->subtree);
 }
 
-void interface_append(ntree **packet, const char *key_name, const char *value)
+void interface_prepend(ntree **packet, const char *key_name, const char *value)
 {
        ntree *key;
 
@@ -269,10 +273,22 @@
        *packet = key;
 }
 
+void interface_append(ntree **packet, const char *key_name, const char *value)
+{
+       interface_prepend(packet, key_name, value);
+       (*packet)->appended = TRUE;
+}
+
+void interface_prepend_int(ntree **packet, const char *key_name, unsigned int 
value)
+{
+       interface_prepend(packet, key_name, (const char *) 
GINT_TO_POINTER(value));
+       (*packet)->is_integer = TRUE;
+}
+
 void interface_append_int(ntree **packet, const char *key_name, unsigned int 
value)
 {
-       interface_append(packet, key_name, (const char *) 
GINT_TO_POINTER(value));
-       (*packet)->is_integer = 1;
+       interface_prepend_int(packet, key_name, value);
+       (*packet)->appended = TRUE;
 }
 
 void interface_foreach(ntree *key, PForEachFunc func, void *udata)
Index: giFTcurs/src/protocol.h
diff -u giFTcurs/src/protocol.h:1.18 giFTcurs/src/protocol.h:1.19
--- giFTcurs/src/protocol.h:1.18        Tue Jul 22 19:58:37 2003
+++ giFTcurs/src/protocol.h     Mon Sep  8 13:47:23 2003
@@ -18,7 +18,7 @@
  * along with giFTcurs; if not, write to the Free Software Foundation,
  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307,  USA.
  *
- * $Id: protocol.h,v 1.18 2003/07/22 23:58:37 weinholt Exp $
+ * $Id: protocol.h,v 1.19 2003/09/08 17:47:23 saturn Exp $
  */
 #ifndef _PROTOCOL_H
 #define _PROTOCOL_H
@@ -40,6 +40,8 @@
  */
 void interface_append(ntree **, const char *key, const char *value);
 void interface_append_int(ntree **packet, const char *key_name, unsigned int 
value);
+void interface_prepend(ntree **, const char *key, const char *value);
+void interface_prepend_int(ntree **packet, const char *key_name, unsigned int 
value);
 
 /* Get the name or value of an item */
 const char *interface_lookup(ntree *, const char *key);
Index: giFTcurs/src/test_gift.c
diff -u giFTcurs/src/test_gift.c:1.1 giFTcurs/src/test_gift.c:1.2
--- giFTcurs/src/test_gift.c:1.1        Fri Aug  1 23:03:57 2003
+++ giFTcurs/src/test_gift.c    Mon Sep  8 13:47:23 2003
@@ -18,7 +18,7 @@
  * along with giFTcurs; if not, write to the Free Software Foundation,
  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307,  USA.
  *
- * $Id: test_gift.c,v 1.1 2003/08/02 03:03:57 weinholt Exp $
+ * $Id: test_gift.c,v 1.2 2003/09/08 17:47:23 saturn Exp $
  */
 #include "giftcurs.h"
 
@@ -28,6 +28,7 @@
 #include <unistd.h>
 
 #include "gift.h"
+gift_id gift_new_id(void);             /* need to import this too */
 
 #define PASS                   0
 #define FAIL                   1
Index: giFTcurs/src/ui_main.c
diff -u giFTcurs/src/ui_main.c:1.344 giFTcurs/src/ui_main.c:1.345
--- giFTcurs/src/ui_main.c:1.344        Sat Aug 23 16:01:44 2003
+++ giFTcurs/src/ui_main.c      Mon Sep  8 13:47:23 2003
@@ -18,7 +18,7 @@
  * along with giFTcurs; if not, write to the Free Software Foundation,
  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307,  USA.
  *
- * $Id: ui_main.c,v 1.344 2003/08/23 20:01:44 weinholt Exp $
+ * $Id: ui_main.c,v 1.345 2003/09/08 17:47:23 saturn Exp $
  */
 #include "giftcurs.h"
 
@@ -244,20 +244,15 @@
        search_realm.sel = 6;           /* BROWSE */
        q = prepare_search(1);
 
-       q->id = gift_new_id();
-
-       interface_append_int(&packet, "BROWSE", q->id);
        interface_append(&packet, "query", user);
        if (node)
                interface_append(&packet, "node", node);
-       gift_register_id(q->id, (EventCallback) search_result_item_handler, q);
-       if (gift_write(&packet) < 0) {
+       q->id = gift_write_register(&packet, "BROWSE",
+                       (EventCallback) search_result_item_handler, q);
+       if (!q->id)
                g_message(_("Couldn't start search!"));
-               gift_unregister_id(q->id);
-               q->id = 0;
-       } else {
+       else
                g_message(_("Retrieving file list..."));
-       }
        ui_update(my_screen_nr | 0200);
 }
 
@@ -288,9 +283,6 @@
        t = names + search_realm.sel;
        q = prepare_search(t->format);
 
-       q->id = gift_new_id();
-
-       interface_append_int(&packet, t->command, q->id);
        parse_typed_query(q->search_term, &includes, &excludes, &protocols);
        interface_append(&packet, "query", includes);
        if (excludes && excludes[0])
@@ -299,11 +291,10 @@
                interface_append(&packet, "realm", t->realm);
        if (protocols && protocols[0])
                interface_append(&packet, "protocol", protocols);
-       gift_register_id(q->id, (EventCallback) search_result_item_handler, q);
-       if (gift_write(&packet) < 0) {
+
+       q->id = gift_write_register(&packet, t->command, (EventCallback) 
search_result_item_handler, q);
+       if (!q->id) {
                g_message(_("Couldn't start search!"));
-               gift_unregister_id(q->id);
-               q->id = 0;
        } else {
                g_message(_("Searching..."));
        }




reply via email to

[Prev in Thread] Current Thread [Next in Thread]