aboutsummaryrefslogtreecommitdiff
path: root/src/plugins/stats/functions.c
blob: eb897abf996626a3023793c4df666037bc0b2696 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
/*
     This file is part of GNUnet
     (C) 2004, 2005, 2006, 2008 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., 51 Franklin Street, Fifth Floor,
     Boston, MA 02110-1301, USA.

*/

#include "platform.h"
#include "gnunetgtk_common.h"
#include <GNUnet/gnunet_util_cron.h>
#include <GNUnet/gnunet_stats_lib.h>
#include <GNUnet/gnunet_getoption_lib.h>
#include <GNUnet/gnunet_protocols.h>
#include "functions.h"

#define FUNCTIONS_DEBUG GNUNET_NO

static StatPair *lastStatValues;

static unsigned int lsv_size;

static struct GNUNET_ClientServerConnection *sock;

static struct GNUNET_Mutex *lock;

static long connectionGoal;

static unsigned long long banddown;

static unsigned long long bandup;

static struct GNUNET_GE_Context *ectx;

static struct GNUNET_GC_Configuration *cfg;

static struct GNUNET_CronManager *cron;

static int
getStatValue (unsigned long long *value,
              unsigned long long *lvalue,
              GNUNET_CronTime * dtime, const char *optName, int monotone)
{
  unsigned int i;

  *value = 0;
  if (lvalue != NULL)
    *lvalue = 0;
  for (i = 0; i < lsv_size; i++)
    {
      if (0 == strcmp (optName, lastStatValues[i].statName))
        {
          *value = lastStatValues[i].value;
          if (lvalue != NULL)
            *lvalue = lastStatValues[i].lvalue;
          if (dtime != NULL)
            *dtime = lastStatValues[i].delta;
          if ((monotone == GNUNET_YES) && (lvalue != NULL)
              && (*lvalue > *value))
            return GNUNET_SYSERR;       /* gnunetd restart? */
          return GNUNET_OK;
        }
    }
#if FUNCTIONS_DEBUG
  GNUNET_GE_LOG (ectx,
                 GNUNET_GE_ERROR | GNUNET_GE_DEVELOPER | GNUNET_GE_ADMIN |
                 GNUNET_GE_USER | GNUNET_GE_BULK,
                 "Statistic not found: `%s'\n", optName);
#endif
  return GNUNET_SYSERR;
}

static void
updateConnectionGoal (void *unused)
{
  char *cmh;
  char *availableDown;
  char *availableUp;

  GNUNET_mutex_lock (lock);
  cmh =
    GNUNET_get_daemon_configuration_value (sock, "gnunetd",
                                           "connection-max-hosts");
  availableDown =
    GNUNET_get_daemon_configuration_value (sock, "LOAD",
                                           "MAXNETDOWNBPSTOTAL");
  availableUp =
    GNUNET_get_daemon_configuration_value (sock, "LOAD", "MAXNETUPBPSTOTAL");
  GNUNET_mutex_unlock (lock);
  if (cmh == NULL)
    connectionGoal = 0;
  else
    connectionGoal = atol (cmh);
  if (availableDown == NULL)
    banddown = 0;
  else
    banddown = atol (availableDown);
  if (availableUp == NULL)
    bandup = 0;
  else
    bandup = atol (availableUp);

  GNUNET_free_non_null (cmh);
  GNUNET_free_non_null (availableDown);
  GNUNET_free_non_null (availableUp);
}

static int
getConnectedNodesStat (const void *closure, gfloat ** data)
{
  unsigned long long val;

  if (connectionGoal == 0)
    return GNUNET_SYSERR;
  if (GNUNET_OK !=
      getStatValue (&val, NULL, NULL, "# of connected peers", GNUNET_NO))
    return GNUNET_SYSERR;
  data[0][0] = ((gfloat) val) / connectionGoal;
  return GNUNET_OK;
}

static int
getLoadStat (const void *closure, gfloat ** data)
{
  unsigned long long valc;
  unsigned long long vali;
  unsigned long long valu;
  unsigned long long vald;

  if (GNUNET_OK !=
      getStatValue (&valc, NULL, NULL, "% of allowed cpu load", GNUNET_NO))
    return GNUNET_SYSERR;
  if (GNUNET_OK !=
      getStatValue (&vali, NULL, NULL, "% of allowed io load", GNUNET_NO))
    return GNUNET_SYSERR;
  if (GNUNET_OK != getStatValue (&valu,
                                 NULL, NULL, "% of allowed network load (up)",
                                 GNUNET_NO))
    return GNUNET_SYSERR;
  if (GNUNET_OK != getStatValue (&vald,
                                 NULL, NULL,
                                 "% of allowed network load (down)",
                                 GNUNET_NO))
    return GNUNET_SYSERR;
  data[0][0] = (gfloat) valc / 100.0;
  data[0][1] = (gfloat) vali / 100.0;
  data[0][2] = (gfloat) valu / 100.0;
  data[0][3] = (gfloat) vald / 100.0;
  return GNUNET_OK;
}

static int
getQuotaStat (const void *closure, gfloat ** data)
{
  unsigned long long allowed;
  unsigned long long have;

  data[0][0] = 0;
  data[0][1] = 0;
  if ((GNUNET_OK == getStatValue (&allowed,
                                  NULL, NULL, "# bytes allowed in datastore",
                                  GNUNET_NO)) &&
      (allowed != 0) &&
      (GNUNET_OK ==
       getStatValue (&have, NULL, NULL, "# bytes in datastore", GNUNET_NO)))
    data[0][0] = ((gfloat) have) / allowed;
  if ((GNUNET_OK == getStatValue (&allowed,
                                  NULL, NULL, "# max bytes allowed in dstore",
                                  GNUNET_NO)) &&
      (allowed != 0) &&
      (GNUNET_OK ==
       getStatValue (&have, NULL, NULL, "# bytes in dstore", GNUNET_NO)))
    data[0][1] = ((gfloat) have) / allowed;
  return GNUNET_OK;
}

static int
getTrafficRecvStats (const void *closure, gfloat ** data)
{
  unsigned long long total;
  unsigned long long noise;
  unsigned long long content;
  unsigned long long queries;
  unsigned long long hellos;
  unsigned long long rlimit;
  unsigned long long ltotal;
  unsigned long long lnoise;
  unsigned long long lcontent;
  unsigned long long lqueries;
  unsigned long long lhellos;
  unsigned long long lrlimit;
  GNUNET_CronTime dtime;
  char *buffer;

  if (GNUNET_OK !=
      getStatValue (&total, &ltotal, &dtime, "# bytes received", GNUNET_YES))
    return GNUNET_SYSERR;
  if (GNUNET_OK != getStatValue (&noise,
                                 &lnoise, NULL, "# bytes of noise received",
                                 GNUNET_YES))
    return GNUNET_SYSERR;
  buffer = GNUNET_malloc (512);
  GNUNET_snprintf (buffer, 512, "# bytes received of type %d",
                   GNUNET_P2P_PROTO_GAP_RESULT);
  if (GNUNET_OK !=
      getStatValue (&content, &lcontent, NULL, buffer, GNUNET_YES))
    {
      content = 0;
      lcontent = 0;
    }
  GNUNET_snprintf (buffer, 512, "# bytes received of type %d",
                   GNUNET_P2P_PROTO_HELLO);
  if (GNUNET_OK != getStatValue (&hellos, &lhellos, NULL, buffer, GNUNET_YES))
    {
      hellos = 0;
      lhellos = 0;
    }
  GNUNET_snprintf (buffer, 512, "# bytes received of type %d",
                   GNUNET_P2P_PROTO_GAP_QUERY);
  if (GNUNET_OK !=
      getStatValue (&queries, &lqueries, NULL, buffer, GNUNET_YES))
    {
      queries = 0;
      lqueries = 0;
    }
  if (GNUNET_OK != getStatValue (&rlimit,
                                 &lrlimit,
                                 NULL,
                                 "# total bytes per second receive limit",
                                 GNUNET_NO))
    {
      rlimit = 0;
      lrlimit = 0;
    }
  GNUNET_free (buffer);
  if (banddown == 0)
    return GNUNET_SYSERR;

  total -= ltotal;
  noise -= lnoise;
  queries -= lqueries;
  content -= lcontent;
  hellos -= lhellos;
  if (banddown <= 0)
    {
      data[0][0] = 0.0;
      data[0][1] = 0.0;
      data[0][2] = 0.0;
      data[0][3] = 0.0;
      data[0][4] = 0.0;
      data[0][5] = 0.0;
      return GNUNET_OK;
    }
  data[0][0] = ((gfloat) noise) / (banddown * dtime / GNUNET_CRON_SECONDS);     /* red */
  data[0][1] = ((gfloat) (content + noise)) / (banddown * dtime / GNUNET_CRON_SECONDS); /* green */
  data[0][2] = ((gfloat) (queries + content + noise)) / (banddown * dtime / GNUNET_CRON_SECONDS);       /* yellow */
  data[0][3] = ((gfloat) (queries + content + noise + hellos)) / (banddown * dtime / GNUNET_CRON_SECONDS);      /* blue */
  data[0][4] = ((gfloat) total) / (banddown * dtime / GNUNET_CRON_SECONDS);     /* gray */
  data[0][5] = (gfloat) rlimit / banddown;      /* magenta */
#if 0
  printf ("I: %f %f %f %f\n", data[0][0], data[0][1], data[0][2]);
#endif
  return GNUNET_OK;
}

static int
getTrafficSendStats (const void *closure, gfloat ** data)
{
  unsigned long long total;
  unsigned long long noise;
  unsigned long long content;
  unsigned long long queries;
  unsigned long long hellos;
  unsigned long long slimit;
  unsigned long long ltotal;
  unsigned long long lnoise;
  unsigned long long lcontent;
  unsigned long long lqueries;
  unsigned long long lhellos;
  unsigned long long lslimit;
  GNUNET_CronTime dtime;
  char *buffer;

  if (GNUNET_OK != getStatValue (&total,
                                 &ltotal, &dtime, "# bytes transmitted",
                                 GNUNET_YES))
    return GNUNET_SYSERR;
  if (GNUNET_OK !=
      getStatValue (&noise, &lnoise, NULL, "# bytes noise sent", GNUNET_YES))
    return GNUNET_SYSERR;
  buffer = GNUNET_malloc (512);
  GNUNET_snprintf (buffer,
                   512, "# bytes transmitted of type %d",
                   GNUNET_P2P_PROTO_GAP_RESULT);
  if (GNUNET_OK !=
      getStatValue (&content, &lcontent, NULL, buffer, GNUNET_YES))
    {
      content = 0;
      lcontent = 0;
    }
  GNUNET_snprintf (buffer,
                   512, "# bytes transmitted of type %d",
                   GNUNET_P2P_PROTO_GAP_QUERY);
  if (GNUNET_OK !=
      getStatValue (&queries, &lqueries, NULL, buffer, GNUNET_YES))
    {
      queries = 0;
      lqueries = 0;
    }
  GNUNET_snprintf (buffer, 512, "# bytes transmitted of type %d",
                   GNUNET_P2P_PROTO_HELLO);
  if (GNUNET_OK != getStatValue (&hellos, &lhellos, NULL, buffer, GNUNET_YES))
    {
      queries = 0;
      lqueries = 0;
    }
  if (GNUNET_OK != getStatValue (&slimit,
                                 &lslimit,
                                 NULL, "# total bytes per second send limit",
                                 GNUNET_NO))
    {
      slimit = 0;
      lslimit = 0;
    }
  GNUNET_free (buffer);
  if (bandup == 0)
    return GNUNET_SYSERR;
  total -= ltotal;
  noise -= lnoise;
  queries -= lqueries;
  content -= lcontent;
  hellos -= lhellos;
  if (bandup <= 0)
    {
      data[0][0] = 0.0;
      data[0][1] = 0.0;
      data[0][2] = 0.0;
      data[0][3] = 0.0;
      data[0][4] = 0.0;
      data[0][5] = 0.0;
      return GNUNET_OK;
    }
  data[0][0] = ((gfloat) noise) / (bandup * dtime / GNUNET_CRON_SECONDS);       /* red */
  data[0][1] = ((gfloat) (noise + content)) / (bandup * dtime / GNUNET_CRON_SECONDS);   /* green */
  data[0][2] = ((gfloat) (noise + content + queries)) / (bandup * dtime / GNUNET_CRON_SECONDS); /* yellow */
  data[0][3] = ((gfloat) (noise + content + queries + hellos)) / (bandup * dtime / GNUNET_CRON_SECONDS);        /* blue */
  data[0][4] = ((gfloat) total) / (bandup * dtime / GNUNET_CRON_SECONDS);       /* grey */
  data[0][5] = ((gfloat) slimit) / bandup;      /* magenta */
#if 0
  printf ("O: %f %f %f %f\n", data[0][0], data[0][1], data[0][2], data[0][3]);
#endif
  return GNUNET_OK;
}

static int
getTrustStats (const void *closure, gfloat ** data)
{
  unsigned long long spent;
  unsigned long long earned;
  unsigned long long awarded;
  unsigned long long lspent;
  unsigned long long learned;
  unsigned long long lawarded;
  unsigned long long max;

  if (GNUNET_OK != getStatValue (&spent,
                                 &lspent, NULL, "# trust spent", GNUNET_YES))
    return GNUNET_SYSERR;
  if (GNUNET_OK != getStatValue (&earned,
                                 &learned,
                                 NULL, "# trust earned", GNUNET_YES))
    return GNUNET_SYSERR;
  if (GNUNET_OK != getStatValue (&awarded,
                                 &lawarded,
                                 NULL, "# gap total trust awarded",
                                 GNUNET_YES))
    return GNUNET_SYSERR;
  max = spent;
  if (earned > max)
    max = earned;
  if (awarded > max)
    max = awarded;
  data[0][0] = 0.0;
  if (max > 0)
    {
      data[0][0] = (1.0 * spent) / max;
      data[0][1] = (1.0 * earned) / max;
      data[0][2] = (1.0 * awarded) / max;
    }
  else
    {
      data[0][0] = 0.0;
      data[0][1] = 0.0;
      data[0][2] = 0.0;
    }
  return GNUNET_OK;
}

static int
getEffectivenessStats (const void *closure, gfloat ** data)
{
  unsigned long long total;     /* total number of queries passed on to remote */
  unsigned long long success;   /* responses forwarded (including local and remote) */
  unsigned long long ltotal;
  unsigned long long lsuccess;

  if (GNUNET_OK != getStatValue (&total,
                                 &ltotal,
                                 NULL,
                                 "# gap requests total sent", GNUNET_YES))
    return GNUNET_SYSERR;
  if (GNUNET_OK != getStatValue (&success,
                                 &lsuccess,
                                 NULL, "# gap routes succeeded", GNUNET_YES))
    return GNUNET_SYSERR;
  if (total > 0)
    data[0][0] = 1.0 * success / total;
  else
    data[0][0] = 0.0;
  return GNUNET_OK;
}


static int
statsProcessor (const char *optName, unsigned long long value, void *data)
{
  /**
   * Keep track of last match (or, more precisely, position
   * of next expected match) since 99.99% of the time we
   * go over the same stats in the same order and thus
   * this will predict correctly).
   */
  static unsigned int last;
  GNUNET_CronTime *delta = data;
  unsigned int j;
  unsigned int found;

  if (last >= lsv_size)
    last = 0;
  j = last;
  found = -1;
  if ((j < lsv_size) && (0 == strcmp (optName, lastStatValues[j].statName)))
    found = j;
  if (found == (unsigned int) -1)
    {
      for (j = 0; j < lsv_size; j++)
        {
          if (0 == strcmp (optName, lastStatValues[j].statName))
            {
              found = j;
              break;
            }
        }
    }
  if (found == (unsigned int) -1)
    {
      found = lsv_size;
      GNUNET_array_grow (lastStatValues, lsv_size, lsv_size + 1);
      lastStatValues[found].statName = GNUNET_strdup (optName);
    }
  lastStatValues[found].lvalue = lastStatValues[found].value;
  lastStatValues[found].value = value;
  lastStatValues[found].delta = *delta;
  last = found + 1;
  return GNUNET_OK;
}

struct UpdateClosure
{
  GNUNET_CronTime delta;
  int is_running;
};

/*
 * Update the status bar indicator about daemon and connexions status
 */
static void *
updateDaemonStatus (void *cls)
{
  struct UpdateClosure *uc = cls;
  static gboolean once = TRUE;
  static long long last_status = -5;
  unsigned long long connected_peers;
  char *label;
  static GtkWidget *statusConnexionsLabel;
  static GtkWidget *statusConnexionsPic;
#if GTK_CHECK_VERSION (2,10,0)
  static GtkStatusIcon *trayIcon;
#endif

  if (once)
    {
      statusConnexionsLabel =
        glade_xml_get_widget (GNUNET_GTK_get_main_glade_XML (),
                              "statusConnexionsLabel");
      statusConnexionsPic =
        glade_xml_get_widget (GNUNET_GTK_get_main_glade_XML (),
                              "statusConnexionsPic");
#if GTK_CHECK_VERSION (2,10,0)
      GtkIconSet *iconSet;
      GtkIconSource *iconSource;
      char *iconPath;
      char *finalPath;
      char *instDir;
      char *dataDir;

      trayIcon = GNUNET_GTK_get_trayIcon ();

      instDir = GNUNET_get_installation_path (GNUNET_IPK_DATADIR);
      dataDir = g_strconcat (instDir, "/../gnunet-gtk/", NULL);
#if MINGW
      finalPath = GNUNET_malloc (_MAX_PATH + 1);
      plibc_conv_to_win_path (dataDir, finalPath);
#else
      finalPath = GNUNET_strdup (dataDir);
#endif
      GNUNET_free (instDir);
      g_free (dataDir);
      iconSource = gtk_icon_source_new ();

      iconSet = gtk_icon_set_new ();
      iconPath =
        g_strconcat (finalPath, "gnunet-gtk-status-unknown.svg", NULL);
      gtk_icon_source_set_filename (iconSource, iconPath);
      g_free (iconPath);
      gtk_icon_source_set_size_wildcarded (iconSource, TRUE);
      gtk_icon_set_add_source (iconSet, iconSource);
      iconPath =
        g_strconcat (finalPath, "gnunet-gtk-status-unknown.png", NULL);
      gtk_icon_source_set_filename (iconSource, iconPath);
      g_free (iconPath);
      gtk_icon_source_set_size_wildcarded (iconSource, FALSE);
      gtk_icon_set_add_source (iconSet, iconSource);

      gtk_icon_factory_add (GNUNET_GTK_get_iconFactory (),
                            "gnunet-gtk-status-unknown", iconSet);
      gtk_icon_set_unref (iconSet);

      iconSet = gtk_icon_set_new ();
      iconPath =
        g_strconcat (finalPath, "gnunet-gtk-status-nodaemon.svg", NULL);
      gtk_icon_source_set_filename (iconSource, iconPath);
      g_free (iconPath);
      gtk_icon_source_set_size_wildcarded (iconSource, TRUE);
      gtk_icon_set_add_source (iconSet, iconSource);
      iconPath =
        g_strconcat (finalPath, "gnunet-gtk-status-nodaemon.png", NULL);
      gtk_icon_source_set_filename (iconSource, iconPath);
      g_free (iconPath);
      gtk_icon_source_set_size_wildcarded (iconSource, FALSE);
      gtk_icon_set_add_source (iconSet, iconSource);

      gtk_icon_factory_add (GNUNET_GTK_get_iconFactory (),
                            "gnunet-gtk-status-nodaemon", iconSet);
      gtk_icon_set_unref (iconSet);

      iconSet = gtk_icon_set_new ();
      iconPath =
        g_strconcat (finalPath, "gnunet-gtk-status-disconnected.svg", NULL);
      gtk_icon_source_set_filename (iconSource, iconPath);
      g_free (iconPath);
      gtk_icon_source_set_size_wildcarded (iconSource, TRUE);
      gtk_icon_set_add_source (iconSet, iconSource);
      iconPath =
        g_strconcat (finalPath, "gnunet-gtk-status-disconnected.png", NULL);
      gtk_icon_source_set_filename (iconSource, iconPath);
      g_free (iconPath);
      gtk_icon_source_set_size_wildcarded (iconSource, FALSE);
      gtk_icon_set_add_source (iconSet, iconSource);

      gtk_icon_factory_add (GNUNET_GTK_get_iconFactory (),
                            "gnunet-gtk-status-disconnected", iconSet);
      gtk_icon_set_unref (iconSet);


      gtk_icon_source_free (iconSource);
      GNUNET_free (finalPath);
#endif

      once = FALSE;
    }
  if (uc->is_running)
    {
      if (GNUNET_OK !=
          getStatValue (&connected_peers, NULL, NULL,
                        "# of connected peers", GNUNET_NO))
        {
          if (last_status != -1)
            {
              GNUNET_GTK_add_log_entry (_
                                        ("WARNING: Failed to obtain connection statistics from gnunetd.\n"));
              gtk_label_set_text (GTK_LABEL (statusConnexionsLabel),
                                  _("Unknown status"));
              gtk_image_set_from_stock (GTK_IMAGE (statusConnexionsPic),
                                        GTK_STOCK_DIALOG_ERROR, 1);
#if GTK_CHECK_VERSION (2,10,0)
              gtk_status_icon_set_tooltip (trayIcon, _("Unknown status"));
              gtk_status_icon_set_from_stock (trayIcon,
                                              "gnunet-gtk-status-unknown");
#endif
              last_status = -1;
            }
        }
      else if (connected_peers > 0)
        {
          if (last_status != (long long) connected_peers)
            {
              if (connected_peers == 1)
                {
                  gtk_label_set_text (GTK_LABEL (statusConnexionsLabel),
                                      _("Connected to 1 peer"));
#if GTK_CHECK_VERSION (2,10,0)
                  gtk_status_icon_set_tooltip (trayIcon,
                                               _
                                               ("GNUnet - Connected to 1 peer"));
#endif
                }
              else
                {
                  label = g_strdup_printf (_("Connected to %Lu peers"),
                                           connected_peers);
                  gtk_label_set_text (GTK_LABEL (statusConnexionsLabel),
                                      label);
                  g_free (label);
#if GTK_CHECK_VERSION (2,10,0)
                  label =
                    g_strdup_printf (_("GNUnet - Connected to %Lu peers"),
                                     connected_peers);
                  gtk_status_icon_set_tooltip (trayIcon, label);
                  g_free (label);
#endif
                }
#if GTK_CHECK_VERSION (2,10,0)
              gtk_status_icon_set_from_stock (trayIcon,
                                              "gnunet-gtk-status-connected");
#endif
              gtk_image_set_from_stock (GTK_IMAGE (statusConnexionsPic),
                                        GTK_STOCK_NETWORK, 1);
              last_status = (long long) connected_peers;
            }
        }
      else if (last_status != 0)
        {
          gtk_label_set_markup (GTK_LABEL (statusConnexionsLabel),
                                _("<b>Disconnected</b>"));
          gtk_image_set_from_stock (GTK_IMAGE (statusConnexionsPic),
                                    GTK_STOCK_DISCONNECT, 1);
#if GTK_CHECK_VERSION (2,10,0)
          gtk_status_icon_set_tooltip (trayIcon, _("GNUnet - Disconnected"));
          gtk_status_icon_set_from_stock (trayIcon,
                                          "gnunet-gtk-status-disconnected");
#endif
          last_status = 0;
        }
    }
  else if (last_status != -2)
    {
      gtk_label_set_markup (GTK_LABEL (statusConnexionsLabel),
                            _("<b>Daemon not running</b>"));
      gtk_image_set_from_stock (GTK_IMAGE (statusConnexionsPic),
                                GTK_STOCK_NO, 1);
#if GTK_CHECK_VERSION (2,10,0)
      gtk_status_icon_set_tooltip (trayIcon,
                                   _("GNUnet - Daemon not running"));
      gtk_status_icon_set_from_stock (trayIcon, "gnunet-gtk-status-nodaemon");
#endif
      last_status = -2;
    }
  return NULL;
}

/**
 * Cron-job that updates all stat values.
 */
static void
updateStatValues (void *unused)
{
  static GNUNET_CronTime lastUpdate;
  GNUNET_CronTime now;
  struct UpdateClosure uc;

  now = GNUNET_get_time ();
  uc.delta = now - lastUpdate;
  uc.is_running = GNUNET_OK == GNUNET_test_daemon_running (ectx, cfg);
  GNUNET_mutex_lock (lock);
  if (GNUNET_OK ==
      GNUNET_STATS_get_statistics (ectx, sock, &statsProcessor, &uc.delta))
    lastUpdate = now;
  GNUNET_GTK_save_call (&updateDaemonStatus, (void *) &uc);
  GNUNET_mutex_unlock (lock);
}


StatEntry stats[] = {
  {
   gettext_noop ("Connectivity"),
   gettext_noop ("# connected nodes (100% = connection table size)"),
   &getConnectedNodesStat,
   NULL,
   1,
   GNUNET_NO,
   }
  ,
  {
   gettext_noop ("System load"),
   gettext_noop
   ("CPU load (red), IO load (green), Network upload (yellow), Network download (blue)"),
   &getLoadStat,
   NULL,
   4,
   GNUNET_NO,
   }
  ,
  {
   gettext_noop ("Datastore capacity"),
   gettext_noop ("Persistent file-sharing data (red) and DHT cache (green)"),
   &getQuotaStat,
   NULL,
   2,
   GNUNET_NO,
   }
  ,
  {
   gettext_noop ("Inbound Traffic"),
   gettext_noop
   ("Noise (red), Content (green), Queries (yellow), Hellos (blue), other (gray), limit (magenta)"),
   &getTrafficRecvStats,
   NULL,
   6,
   5,
   }
  ,
  {
   gettext_noop ("Outbound Traffic"),
   gettext_noop
   ("Noise (red), Content (green), Queries (yellow), Hellos (blue), other (gray), limit (magenta)"),
   &getTrafficSendStats,
   NULL,
   6,
   5,
   }
  ,
  {
   gettext_noop ("Trust"),
   gettext_noop ("Spent (red), Earned (green) and Awarded (yellow)"),
   &getTrustStats,
   NULL,
   3,
   GNUNET_NO,
   }
  ,
  {
   gettext_noop ("Routing Effectiveness"),
   gettext_noop ("Average (red) effectiveness (100% = perfect)"),
   &getEffectivenessStats,
   NULL,
   1,
   GNUNET_NO,
   }
  ,
  {
   NULL,
   NULL,
   NULL,
   NULL,
   0,
   GNUNET_NO,
   }
  ,
};

static unsigned long long UPDATE_INTERVAL;

void
init_functions (struct GNUNET_GE_Context *e,
                struct GNUNET_GC_Configuration *c)
{
  ectx = e;
  cfg = c;
  GNUNET_GC_get_configuration_value_number (cfg,
                                            "GNUNET-GTK",
                                            "STATS-INTERVAL",
                                            1,
                                            99 * GNUNET_CRON_YEARS,
                                            30 * GNUNET_CRON_SECONDS,
                                            &UPDATE_INTERVAL);
  sock = GNUNET_client_connection_create (ectx, cfg);
  lock = GNUNET_mutex_create (GNUNET_NO);
  cron = GNUNET_GTK_get_cron_manager ();
  GNUNET_cron_add_job (cron,
                       &updateStatValues, UPDATE_INTERVAL, UPDATE_INTERVAL,
                       NULL);
  GNUNET_cron_add_job (cron, &updateConnectionGoal, 5 * GNUNET_CRON_SECONDS,
                       5 * GNUNET_CRON_MINUTES, NULL);
  updateStatValues (NULL);
}

void
done_functions ()
{
  unsigned int i;

  GNUNET_cron_del_job (cron, &updateConnectionGoal, 5 * GNUNET_CRON_MINUTES,
                       NULL);
  GNUNET_cron_del_job (cron, &updateStatValues, UPDATE_INTERVAL, NULL);
  GNUNET_mutex_destroy (lock);
  GNUNET_client_connection_destroy (sock);
  for (i = 0; i < lsv_size; i++)
    GNUNET_free (lastStatValues[i].statName);
  GNUNET_array_grow (lastStatValues, lsv_size, 0);
  sock = NULL;
}


/* end of functions.c */