gnunet-svn
[Top][All Lists]
Advanced

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

[GNUnet-SVN] r16757 - gnunet-gtk/src/peerinfo


From: gnunet
Subject: [GNUnet-SVN] r16757 - gnunet-gtk/src/peerinfo
Date: Sun, 11 Sep 2011 13:48:40 +0200

Author: grothoff
Date: 2011-09-11 13:48:40 +0200 (Sun, 11 Sep 2011)
New Revision: 16757

Added:
   gnunet-gtk/src/peerinfo/gnunet-peerinfo-gtk-flags.c
   gnunet-gtk/src/peerinfo/gnunet-peerinfo-gtk-flags.h
Modified:
   gnunet-gtk/src/peerinfo/Makefile.am
   gnunet-gtk/src/peerinfo/gnunet-peerinfo-gtk.c
Log:
adding flag support

Modified: gnunet-gtk/src/peerinfo/Makefile.am
===================================================================
--- gnunet-gtk/src/peerinfo/Makefile.am 2011-09-11 11:37:02 UTC (rev 16756)
+++ gnunet-gtk/src/peerinfo/Makefile.am 2011-09-11 11:48:40 UTC (rev 16757)
@@ -11,12 +11,13 @@
 
 gnunet_peerinfo_gtk_SOURCES = \
   gnunet-peerinfo-gtk.c \
-  gnunet-peerinfo-gtk-about.c
+  gnunet-peerinfo-gtk-about.c \
+  gnunet-peerinfo-gtk-flags.c gnunet-peerinfo-gtk-flags.h
 gnunet_peerinfo_gtk_LDADD = \
   $(top_builddir)/src/lib/libgnunetgtk.la \
   @GTK_LIBS@ \
   @GLADE_LIBS@ @GNUNET_LIBS@ \
-  -lgnunetutil -lgnunetpeerinfo \
+  -lgnunetutil -lgnunetpeerinfo -lgnunetcore -lgnunettransport \
   $(INTLLIBS) 
 gnunet_peerinfo_gtk_LDFLAGS = \
   -export-dynamic 
\ No newline at end of file

Added: gnunet-gtk/src/peerinfo/gnunet-peerinfo-gtk-flags.c
===================================================================
--- gnunet-gtk/src/peerinfo/gnunet-peerinfo-gtk-flags.c                         
(rev 0)
+++ gnunet-gtk/src/peerinfo/gnunet-peerinfo-gtk-flags.c 2011-09-11 11:48:40 UTC 
(rev 16757)
@@ -0,0 +1,144 @@
+/*
+     This file is part of GNUnet.
+     (C) 2007, 2011 Christian Grothoff (and other contributing authors)
+
+     GNUnet is free software; you can redistribute it and/or modify
+     it under the terms of the GNU General Public License as published
+     by the Free Software Foundation; either version 2, or (at your
+     option) any later version.
+
+     GNUnet 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
+     General Public License for more details.
+
+     You should have received a copy of the GNU General Public License
+     along with GNUnet; see the file COPYING.  If not, write to the
+     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+     Boston, MA 02111-1307, USA.
+*/
+
+/**
+ * @file src/peerinfo/gnunet-peerinfo-gtk-flags.c
+ * @brief flag lookup
+ * @author Christian Grothoff
+ */
+#include "gnunet_gtk.h"
+#include "gnunet-peerinfo-gtk-flags.h"
+
+/**
+ * Entry we keep for each flag.
+ */
+struct Flag
+{
+  /**
+   * This is a linked list.
+   */
+  struct Flag *next;
+
+  /**
+   * This is a linked list.
+   */
+  struct Flag *prev;
+
+  /**
+   * Associated country code.
+   */
+  char *cc;
+
+  /**
+   * Flag image.
+   */
+  GdkPixbuf *flag;
+};
+
+
+/**
+ * Head of the DLL of loaded flags.
+ */
+static struct Flag *flags_head;
+
+/**
+ * Tail of the DLL of loaded flags.
+ */
+static struct Flag *flags_tail;
+
+
+/**
+ * Lookup the flag image for the given country code
+ *
+ * @return NULL on error
+ */
+GdkPixbuf *
+GNUNET_PEERINFO_GTK_get_flag (const char *cc)
+{
+  GdkPixbuf *flagBuf;
+  char *mcc;
+  char *dir;
+  char *fn;
+  size_t i;
+  struct Flag *pos;
+
+  if (NULL == cc)
+    return NULL;
+  if ((0 == strcasecmp (cc, "edu")) ||
+      (0 == strcasecmp (cc, "com")) ||
+      (0 == strcasecmp (cc, "net")) ||
+      (0 == strcasecmp (cc, "org")) ||
+      (0 == strcasecmp (cc, "gov")) || (0 == strcasecmp (cc, "mil")))
+    cc = "us";
+  if (0 == strcasecmp (cc, "uk"))
+    cc = "gb";
+  if (strlen (cc) > 2)
+    return NULL;
+  pos = flags_head;
+  while (pos != NULL)
+    {
+      if (0 == strcmp (pos->cc, cc))
+       return pos->flag;
+      pos = pos->next;
+    }
+  mcc = GNUNET_strdup (cc);
+  for (i = 0; i < strlen (mcc); i++)
+    mcc[i] = tolower (mcc[i]);
+  dir = GNUNET_GTK_installation_get_path (GNUNET_OS_IPK_DATADIR);
+  GNUNET_asprintf (&fn,
+                  "%sgnunet-gtk%sflags%s%s.png",
+                  dir,
+                  DIR_SEPARATOR_STR,
+                  DIR_SEPARATOR_STR,
+                  mcc);
+  GNUNET_free (dir);
+  flagBuf = gdk_pixbuf_new_from_file (fn, NULL);
+  pos = GNUNET_malloc (sizeof (struct Flag));
+  pos->cc = mcc;
+  pos->flag = flagBuf;
+  GNUNET_CONTAINER_DLL_insert (flags_head,
+                              flags_tail,
+                              pos);
+  return flagBuf;
+}
+
+
+/**
+ * Deallocate all cached flags.
+ */ 
+void
+GNUNET_PEERINFO_GTK_flags_shutdown ()
+{
+  struct Flag *flag;
+
+  while (NULL != (flag = flags_head))
+    {
+      GNUNET_CONTAINER_DLL_remove (flags_head,
+                                  flags_tail,
+                                  flag);
+      if (flag->flag != NULL)
+        g_object_unref (flag->flag);
+      GNUNET_free (flag->cc);
+      GNUNET_free (flag);
+    }
+}
+
+
+/* end of gnunet-peerinfo-gtk-flags.c */

Added: gnunet-gtk/src/peerinfo/gnunet-peerinfo-gtk-flags.h
===================================================================
--- gnunet-gtk/src/peerinfo/gnunet-peerinfo-gtk-flags.h                         
(rev 0)
+++ gnunet-gtk/src/peerinfo/gnunet-peerinfo-gtk-flags.h 2011-09-11 11:48:40 UTC 
(rev 16757)
@@ -0,0 +1,50 @@
+/*
+     This file is part of GNUnet.
+     (C) 2007, 2011 Christian Grothoff (and other contributing authors)
+
+     GNUnet is free software; you can redistribute it and/or modify
+     it under the terms of the GNU General Public License as published
+     by the Free Software Foundation; either version 2, or (at your
+     option) any later version.
+
+     GNUnet 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
+     General Public License for more details.
+
+     You should have received a copy of the GNU General Public License
+     along with GNUnet; see the file COPYING.  If not, write to the
+     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+     Boston, MA 02111-1307, USA.
+*/
+
+/**
+ * @file src/peerinfo/gnunet-peerinfo-gtk-flags.h
+ * @brief flag lookup
+ * @author Christian Grothoff
+ */
+#ifndef GNUNET_PEERINFO_GTK_FLAGS_H
+#define GNUNET_PEERINFO_GTK_FLAGS_H
+
+#include "gnunet_gtk.h"
+#include <ctype.h>
+#include <gdk/gdk.h>
+
+/**
+ * Lookup the flag image for the given country code
+ *
+ * @return NULL on error
+ */
+GdkPixbuf *
+GNUNET_PEERINFO_GTK_get_flag (const char *cc);
+
+
+
+/**
+ * Deallocate all cached flags.
+ */ 
+void
+GNUNET_PEERINFO_GTK_flags_shutdown (void);
+
+
+#endif

Modified: gnunet-gtk/src/peerinfo/gnunet-peerinfo-gtk.c
===================================================================
--- gnunet-gtk/src/peerinfo/gnunet-peerinfo-gtk.c       2011-09-11 11:37:02 UTC 
(rev 16756)
+++ gnunet-gtk/src/peerinfo/gnunet-peerinfo-gtk.c       2011-09-11 11:48:40 UTC 
(rev 16757)
@@ -25,15 +25,60 @@
  */
 #include "gnunet_gtk.h"
 #include <gnunet/gnunet_peerinfo_service.h>
+#include <gnunet/gnunet_transport_service.h>
+#include <gnunet/gnunet_core_service.h>
+#include "gnunet-peerinfo-gtk-flags.h"
 
+
 /**
+ * Information we track for each peer outside of the model.
+ */
+struct PeerInfo
+{
+  /**
+   * Reference to the peer in the view.
+   */
+  GtkTreeRowReference *rr;
+
+  /**
+   * Handle to an active lookup for addresses of this peer, or NULL.
+   */
+  struct GNUNET_TRANSPORT_PeerAddressLookupContext *palc;
+
+  /**
+   * Location where we store all addresses that were found during the 'palc' 
iteration.
+   */
+  char *palc_accumulator;
+
+  /**
+   * Total number of addresses found.
+   */
+  guint palc_counter;
+  
+};
+
+
+/**
  * Handle to our main loop.
  */
 static struct GNUNET_GTK_MainLoop *ml;
 
+/**
+ * Handle for our notifications from peerinfo about new peers.
+ */
 static struct GNUNET_PEERINFO_NotifyContext *pnc;
 
 /**
+ * Handle to core service.
+ */
+static struct GNUNET_CORE_Handle *core;
+
+/**
+ * Map of peer identities to the respective PeerInfo for our view.
+ */
+static struct GNUNET_CONTAINER_MultiHashMap *peer2info;
+
+/**
  * Should gnunet-peerinfo-gtk start in tray mode?
  */
 static int tray_only;
@@ -48,6 +93,7 @@
   return GNUNET_GTK_main_loop_get_configuration (ml);
 }
 
+
 /**
  * Get an object from the main window.
  *
@@ -62,6 +108,35 @@
 
 
 /**
+ * Function called on each entry in the 'peer2info' map 
+ * to free the associated path.
+ *
+ * @param cls unused
+ * @param key peer identity
+ * @param value the 'struct PeerInfo'
+ * @return GNUNET_OK (continue to iterate)
+ */
+static int
+free_paths (void *cls,
+           const GNUNET_HashCode *key,
+           void *value)
+{
+  struct PeerInfo *info = value;
+
+  if (NULL != info->palc)
+    {
+      GNUNET_TRANSPORT_peer_address_lookup_cancel (info->palc);
+      info->palc = NULL;
+      GNUNET_free (info->palc_accumulator);
+      info->palc_accumulator = NULL;
+    }
+  gtk_tree_row_reference_free (info->rr);
+  GNUNET_free (info);
+  return GNUNET_OK;
+}
+
+
+/**
  * Task run on shutdown.
  *
  * @param cls unused
@@ -73,10 +148,83 @@
 {
   GNUNET_PEERINFO_notify_cancel (pnc);
   pnc = NULL;
+  if (NULL != core)
+  {
+    GNUNET_CORE_disconnect (core);
+    core = NULL;
+  }
+  GNUNET_CONTAINER_multihashmap_iterate (peer2info,
+                                        &free_paths,
+                                        NULL);
+  GNUNET_CONTAINER_multihashmap_destroy (peer2info);
+  peer2info = NULL;
+  GNUNET_PEERINFO_GTK_flags_shutdown ();
 }
 
 
 /**
+ * Function to call with a binary format of an address
+ *
+ * @param cls the 'struct PeerInfo' for which this is a valid address
+ * @param address NULL on error, otherwise 0-terminated printable UTF-8 string
+ */
+static void
+peer_address_cb (void *cls,
+                const char *address)
+{
+  struct PeerInfo *info = cls;
+  char *tmp;
+  GtkListStore *ls;
+  GtkTreeModel *tm;
+  GtkTreeIter iter;
+  GtkTreePath *path;
+  const char *colon;
+  const char *dot;
+  char *country;
+
+  if (NULL == address)
+    {
+      /* last address, store information in model */
+      country = NULL;      
+      colon = strstr (info->palc_accumulator, ":");
+      if (NULL != colon)
+       {
+         for (dot = colon - 1; dot != info->palc_accumulator; dot--)
+           if ('.' == *dot)
+             break;
+         if ('.' == *dot)
+           country = GNUNET_strndup (&dot[1], (colon - dot) - 1);
+       }
+      ls = GTK_LIST_STORE (get_object ("GNUNET_PEERINFO_GTK_list_store"));
+      tm = GTK_TREE_MODEL (ls);
+      path = gtk_tree_row_reference_get_path (info->rr);
+      GNUNET_assert (NULL != path);
+      GNUNET_assert (TRUE ==
+                    gtk_tree_model_get_iter (tm, &iter, path));
+      gtk_tree_path_free (path);
+      gtk_list_store_set (ls, &iter, 
+                         1, info->palc_counter,
+                         2, country,
+                         3, GNUNET_PEERINFO_GTK_get_flag (country),
+                         6, &info->palc_accumulator[1],
+                         -1);      
+      GNUNET_free_non_null (country);
+      info->palc = NULL;
+      GNUNET_free (info->palc_accumulator);
+      info->palc_accumulator = NULL;
+      return;
+    }
+  GNUNET_asprintf (&tmp,
+                  "%s|%s",
+                  info->palc_accumulator,
+                  address);
+  GNUNET_free (info->palc_accumulator);
+  info->palc_accumulator = tmp;
+  info->palc_counter++;
+}
+
+
+/**
  * Function called for peers that we know about.
  *
  * @param cls closure
@@ -93,46 +241,124 @@
   GtkListStore *ls;
   GtkTreeModel *tm;
   GtkTreeIter iter;
-  int found;
-  gchar *pid;
   const char *npid;
   struct GNUNET_CRYPTO_HashAsciiEncoded enc;
+  struct PeerInfo *info;
+  GtkTreePath *path;
 
-  GNUNET_CRYPTO_hash_to_enc (&peer->hashPubKey, &enc);
-  npid = (const char *) &enc;
   ls = GTK_LIST_STORE (get_object ("GNUNET_PEERINFO_GTK_list_store"));
+  if (NULL == ls)
+    {
+      GNUNET_break (0);
+      return;
+    }
   tm = GTK_TREE_MODEL (ls);
-  found = GNUNET_NO;
-  if (TRUE == gtk_tree_model_get_iter_first (tm, &iter))
-  {
-    do
+  info = GNUNET_CONTAINER_multihashmap_get (peer2info,
+                                           &peer->hashPubKey);
+  if (NULL == info)
     {
-      pid = NULL;
-      gtk_tree_model_get (tm, &iter, 0, &pid, -1);
-      if (pid != NULL)
-      {
-        if (0 == strcmp (pid, npid))
-        {
-          found = GNUNET_YES;
-          g_free (pid);
-          break;
-        }
-      }
-      g_free (pid);
+      GNUNET_CRYPTO_hash_to_enc (&peer->hashPubKey, &enc);  
+      npid = (const char *) &enc;
+      gtk_list_store_append (ls, &iter);
+      gtk_list_store_set (ls, &iter, 
+                         0, npid, 
+                         1, 0 /* number of known addresses */ ,
+                         2, "" /* country name */ ,
+                         3, NULL /* country flag */ ,
+                         4, (guint64) 0 /* bandwidth-in */ ,
+                         5, (guint64) 0 /* bandwidth-out */ ,
+                         6, "" /* addresses as strings */,
+                         -1);
+      path = gtk_tree_model_get_path (tm, &iter);
+      info = GNUNET_malloc (sizeof (struct PeerInfo));     
+      info->rr = gtk_tree_row_reference_new (tm, path);
+      GNUNET_assert (NULL != info->rr);
+      gtk_tree_path_free (path);
+      GNUNET_CONTAINER_multihashmap_put (peer2info,
+                                        &peer->hashPubKey,
+                                        info,
+                                        
GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
     }
-    while ((found == GNUNET_NO) &&
-           (TRUE == gtk_tree_model_iter_next (tm, &iter)));
-  }
-  if (found == GNUNET_NO)
-    gtk_list_store_append (ls, &iter);
+  if (NULL == info->palc)
+    {
+      info->palc_accumulator = GNUNET_strdup ("|");
+      info->palc_counter = 0;
+      info->palc = GNUNET_TRANSPORT_peer_address_lookup (get_configuration(),
+                                                        peer,
+                                                        
GNUNET_TIME_UNIT_MINUTES,
+                                                        &peer_address_cb,
+                                                        info);      
+    }
+
+}
+
+
+/**
+ * Function called after GNUNET_CORE_connect has succeeded
+ * (or failed for good).  Note that the private key of the
+ * peer is intentionally not exposed here; if you need it,
+ * your process should try to read the private key file
+ * directly (which should work if you are authorized...).
+ *
+ * @param cls closure
+ * @param server handle to the server, NULL if we failed
+ * @param my_identity ID of this peer, NULL if we failed
+ * @param publicKey public key of this peer, NULL if we failed
+ */
+static void 
+init_cb (void *cls,
+        struct GNUNET_CORE_Handle * server,
+        const struct GNUNET_PeerIdentity *
+        my_identity,
+        const struct
+        GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded
+        * publicKey)
+{
+  /* FIXME: should probably do something to my 'own' entry
+     in the peerinfo list to make it stand out */
+}
+
+
+/**
+ * Method called whenever a given peer has a status change.
+ *
+ * @param cls closure
+ * @param peer peer identity this notification is about
+ * @param timeout absolute time when this peer will time out
+ *        unless we see some further activity from it
+ * @param bandwidth_in available amount of inbound bandwidth
+ * @param bandwidth_out available amount of outbound bandwidth
+ * @param atsi performance data for the connection
+ */
+static void 
+status_cb (void *cls,
+          const struct GNUNET_PeerIdentity * peer,
+          struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
+          struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
+          struct GNUNET_TIME_Absolute timeout,
+          const struct GNUNET_TRANSPORT_ATS_Information* atsi)
+{
+  struct PeerInfo *info;
+  GtkListStore *ls;
+  GtkTreeModel *tm;
+  GtkTreeIter iter;
+  GtkTreePath *path;
+ 
+  info = GNUNET_CONTAINER_multihashmap_get (peer2info,
+                                           &peer->hashPubKey);
+  if (NULL == info)
+    return; /* should rarely happen... */
+  ls = GTK_LIST_STORE (get_object ("GNUNET_PEERINFO_GTK_list_store"));
+  tm = GTK_TREE_MODEL (ls);
+  path = gtk_tree_row_reference_get_path (info->rr);
+  GNUNET_assert (NULL != path);
+  GNUNET_assert (TRUE ==
+                gtk_tree_model_get_iter (tm, &iter, path));
+  gtk_tree_path_free (path);
   gtk_list_store_set (ls, &iter, 
-                     0, npid, 
-                     1, 0 /* number of known addresses */ ,
-                      2, "" /* country name */ ,
-                      3, NULL /* country flag */ ,
-                     4, 0 /* bandwidth */ ,
-                     5, "" /* addresses as strings */,
-                      -1);
+                     4, (guint64) ntohl (bandwidth_in.value__),
+                     5, (guint64) ntohl (bandwidth_out.value__),
+                     -1);      
 }
 
 
@@ -160,6 +386,7 @@
 
   GNUNET_GTK_set_icon_search_path ();
   GNUNET_GTK_setup_nls ();
+  peer2info = GNUNET_CONTAINER_multihashmap_create (256);
   pnc = GNUNET_PEERINFO_notify (get_configuration(), 
                                &peerinfo_processor, NULL);
   if (pnc == NULL)
@@ -167,6 +394,14 @@
     fprintf (stderr, _("Failed to initialize communication with peerinfo 
service!\n"));
     exit (1);
   }
+  core = GNUNET_CORE_connect (get_configuration(),
+                             1, NULL,
+                             &init_cb,
+                             NULL, NULL,
+                             &status_cb,
+                             NULL, GNUNET_NO,
+                             NULL, GNUNET_NO,
+                             NULL);
 
   /* setup main window */
   main_window =




reply via email to

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