Immediately quit routines if fwrite() fails - further writes will
[obnox/wireshark/wip.git] / gtk / mtp3_summary.c
1 /* mtp3_summary.c
2  * Routines for MTP3 Statictics summary window
3  *
4  * Copyright 2004, Michael Lum <mlum [AT] telostech.com>
5  * In association with Telos Technology Inc.
6  *
7  * Modified from gsm_map_summary.c
8  *
9  * $Id$
10  *
11  * Wireshark - Network traffic analyzer
12  * By Gerald Combs <gerald@wireshark.org>
13  * Copyright 1998 Gerald Combs
14  *
15  * This program is free software; you can redistribute it and/or
16  * modify it under the terms of the GNU General Public License
17  * as published by the Free Software Foundation; either version 2
18  * of the License, or (at your option) any later version.
19  *
20  * This program is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  * GNU General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with this program; if not, write to the Free Software
27  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
28  */
29
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
33
34 #include <gtk/gtk.h>
35 #include <string.h>
36
37 #include <wtap.h>
38
39 #include "epan/packet_info.h"
40 #include "epan/epan.h"
41 #include "epan/value_string.h"
42 #include "../stat_menu.h"
43 #include "gui_stat_menu.h"
44 #include "globals.h"
45 #include "file.h"
46 #include "summary.h"
47 #include "image/clist_ascend.xpm"
48 #include "image/clist_descend.xpm"
49 #include "dlg_utils.h"
50 #include "gui_utils.h"
51 #include "compat_macros.h"
52 #include <epan/tap.h>
53
54 #include <epan/dissectors/packet-mtp3.h>
55 #include "mtp3_stat.h"
56
57 #define SUM_STR_MAX 1024
58
59 typedef struct column_arrows {
60     GtkWidget           *table;
61     GtkWidget           *ascend_pm;
62     GtkWidget           *descend_pm;
63 } column_arrows;
64
65 #define MTP3_SUM_INIT_TABLE_NUM_COLUMNS         6
66
67 typedef struct _my_columns_t {
68     guint32             value;
69     const gchar         *strptr;
70     GtkJustification    just;
71 } my_columns_t;
72
73 static my_columns_t columns[MTP3_SUM_INIT_TABLE_NUM_COLUMNS] = {
74     { 110,      "SI",                   GTK_JUSTIFY_LEFT },
75     { 100,      "Num MSUs",             GTK_JUSTIFY_RIGHT },
76     { 100,      "MSUs/sec",             GTK_JUSTIFY_RIGHT },
77     { 100,      "Num Bytes",            GTK_JUSTIFY_RIGHT },
78     { 100,      "Bytes/MSU",            GTK_JUSTIFY_RIGHT },
79     { 100,      "Bytes/sec",            GTK_JUSTIFY_RIGHT }
80 };
81
82
83 static void
84 add_string_to_box(gchar *str, GtkWidget *box)
85 {
86   GtkWidget *lb;
87   lb = gtk_label_new(str);
88   gtk_misc_set_alignment(GTK_MISC(lb), 0.0, 0.5);
89   gtk_box_pack_start(GTK_BOX(box), lb,FALSE,FALSE, 0);
90   gtk_widget_show(lb);
91 }
92
93
94 static void
95 mtp3_sum_gtk_click_column_cb(
96     GtkCList            *clist,
97     gint                column,
98     gpointer            data)
99 {
100     column_arrows       *col_arrows = (column_arrows *) data;
101     int                 i;
102
103
104     gtk_clist_freeze(clist);
105
106     for (i=0; i < MTP3_SUM_INIT_TABLE_NUM_COLUMNS; i++)
107     {
108         gtk_widget_hide(col_arrows[i].ascend_pm);
109         gtk_widget_hide(col_arrows[i].descend_pm);
110     }
111
112     if (column == clist->sort_column)
113     {
114         if (clist->sort_type == GTK_SORT_ASCENDING)
115         {
116             clist->sort_type = GTK_SORT_DESCENDING;
117             gtk_widget_show(col_arrows[column].descend_pm);
118         }
119         else
120         {
121             clist->sort_type = GTK_SORT_ASCENDING;
122             gtk_widget_show(col_arrows[column].ascend_pm);
123         }
124     }
125     else
126     {
127         /*
128          * Columns 0 sorted in descending order by default
129          */
130         if (column == 0)
131         {
132             clist->sort_type = GTK_SORT_ASCENDING;
133             gtk_widget_show(col_arrows[column].ascend_pm);
134         }
135         else
136         {
137             clist->sort_type = GTK_SORT_DESCENDING;
138             gtk_widget_show(col_arrows[column].descend_pm);
139         }
140
141         gtk_clist_set_sort_column(clist, column);
142     }
143
144     gtk_clist_thaw(clist);
145     gtk_clist_sort(clist);
146 }
147
148
149 static gint
150 mtp3_sum_gtk_sort_column(
151     GtkCList            *clist,
152     gconstpointer       ptr1,
153     gconstpointer       ptr2)
154 {
155     const GtkCListRow   *row1 = ptr1;
156     const GtkCListRow   *row2 = ptr2;
157     char                *text1 = NULL;
158     char                *text2 = NULL;
159     int                 i1, i2;
160
161     text1 = GTK_CELL_TEXT(row1->cell[clist->sort_column])->text;
162     text2 = GTK_CELL_TEXT(row2->cell[clist->sort_column])->text;
163
164     switch (clist->sort_column)
165     {
166     case 0:
167         /* text columns */
168         return(strcmp(text1, text2));
169
170     default:
171         /* number columns */
172         i1 = strtol(text1, NULL, 0);
173         i2 = strtol(text2, NULL, 0);
174         return(i1 - i2);
175     }
176
177     g_assert_not_reached();
178
179     return(0);
180 }
181
182 static void
183 mtp3_sum_draw(
184     GtkWidget           *table,
185     double              seconds,
186     int                 *tot_num_msus_p,
187     double              *tot_num_bytes_p)
188 {
189     const char          *entries[MTP3_SUM_INIT_TABLE_NUM_COLUMNS];
190     int                 i, j;
191     int                 num_msus;
192     double              num_bytes;
193
194     *tot_num_msus_p = 0;
195     *tot_num_bytes_p = 0;
196
197     for (i=0; i < MTP3_NUM_SI_CODE; i++)
198     {
199         entries[0] = g_strdup(mtp3_service_indicator_code_short_vals[i].strptr);
200
201         j = 0;
202         num_msus = 0;
203         num_bytes = 0;
204
205         while (j < mtp3_num_used)
206         {
207             num_msus += mtp3_stat[j].si_code[i].num_msus;
208             num_bytes += mtp3_stat[j].si_code[i].size;
209
210             j++;
211         }
212
213         *tot_num_msus_p += num_msus;
214         *tot_num_bytes_p += num_bytes;
215
216         entries[1] = g_strdup_printf("%u", num_msus);
217
218         entries[2] = (seconds) ? g_strdup_printf("%.2f", num_msus/seconds) : "N/A";
219         
220         entries[3] = g_strdup_printf("%.0f", num_bytes);
221
222         entries[4] = (num_msus) ? g_strdup_printf("%.2f", num_bytes/num_msus) : "N/A";
223
224         entries[5] = (seconds) ? g_strdup_printf("%.2f", num_bytes/seconds) : "N/A";
225
226         gtk_clist_insert(GTK_CLIST(table), i, (gchar **) entries);
227     }
228
229     gtk_clist_sort(GTK_CLIST(table));
230 }
231
232
233 static void
234 mtp3_sum_gtk_sum_cb(GtkWidget *w _U_, gpointer d _U_)
235 {
236   summary_tally summary;
237   GtkWidget     *sum_open_w,
238                 *main_vb, *file_fr, *data_fr, *file_box,
239                 *data_box, *bbox, *close_bt,
240                 *tot_fr, *tot_box,
241                 *table, *column_lb, *table_fr;
242   column_arrows *col_arrows;
243
244   gchar                 string_buff[SUM_STR_MAX];
245   const char *  file_type;
246   double                seconds;
247   int                   tot_num_msus;
248   double                tot_num_bytes;
249   int                   i;
250
251   /* initialize the tally */
252   summary_fill_in(&cfile, &summary);
253
254   /* initial compututations */
255   seconds = summary.stop_time - summary.start_time;
256
257   sum_open_w = window_new(GTK_WINDOW_TOPLEVEL, "MTP3 Statistics: Summary");
258
259   /* Container for each row of widgets */
260   main_vb = gtk_vbox_new(FALSE, 3);
261   gtk_container_border_width(GTK_CONTAINER(main_vb), 5);
262   gtk_container_add(GTK_CONTAINER(sum_open_w), main_vb);
263   gtk_widget_show(main_vb);
264
265   /* File frame */
266   file_fr = gtk_frame_new("File");
267   gtk_container_add(GTK_CONTAINER(main_vb), file_fr);
268   gtk_widget_show(file_fr);
269
270   file_box = gtk_vbox_new(FALSE, 3);
271   gtk_container_add(GTK_CONTAINER(file_fr), file_box);
272   gtk_widget_show(file_box);
273
274   /* filename */
275   g_snprintf(string_buff, SUM_STR_MAX, "Name: %s", ((summary.filename) ? summary.filename : "None"));
276   add_string_to_box(string_buff, file_box);
277
278   /* length */
279   g_snprintf(string_buff, SUM_STR_MAX, "Length: %lld", summary.file_length);
280   add_string_to_box(string_buff, file_box);
281
282   /* format */
283   file_type = wtap_file_type_string(summary.encap_type);
284   g_snprintf(string_buff, SUM_STR_MAX, "Format: %s", (file_type ? file_type : "N/A"));
285   add_string_to_box(string_buff, file_box);
286
287   if (summary.has_snap) {
288     /* snapshot length */
289     g_snprintf(string_buff, SUM_STR_MAX, "Snapshot length: %u", summary.snap);
290     add_string_to_box(string_buff, file_box);
291   }
292
293   /* Data frame */
294   data_fr = gtk_frame_new("Data");
295   gtk_container_add(GTK_CONTAINER(main_vb), data_fr);
296   gtk_widget_show(data_fr);
297
298   data_box = gtk_vbox_new(FALSE, 3);
299   gtk_container_add(GTK_CONTAINER(data_fr), data_box);
300   gtk_widget_show(data_box);
301
302   /* seconds */
303   g_snprintf(string_buff, SUM_STR_MAX, "Elapsed time: %.3f seconds", summary.elapsed_time);
304   add_string_to_box(string_buff, data_box);
305
306   g_snprintf(string_buff, SUM_STR_MAX, "Between first and last packet: %.3f seconds", seconds);
307   add_string_to_box(string_buff, data_box);
308
309   /* Packet count */
310   g_snprintf(string_buff, SUM_STR_MAX, "Packet count: %i", summary.packet_count);
311   add_string_to_box(string_buff, data_box);
312
313   /* MTP3 SPECIFIC */
314   table_fr = gtk_frame_new("Service Indicator (SI) Totals");
315   gtk_container_add(GTK_CONTAINER(main_vb), table_fr);
316   gtk_widget_show(table_fr);
317
318   table = gtk_clist_new(MTP3_SUM_INIT_TABLE_NUM_COLUMNS);
319   gtk_container_add(GTK_CONTAINER(table_fr), table);
320   gtk_widget_show(table);
321
322   col_arrows =
323       (column_arrows *) g_malloc(sizeof(column_arrows) * MTP3_SUM_INIT_TABLE_NUM_COLUMNS);
324
325   for (i = 0; i < MTP3_SUM_INIT_TABLE_NUM_COLUMNS; i++)
326   {
327       col_arrows[i].table = gtk_table_new(2, 2, FALSE);
328
329       gtk_table_set_col_spacings(GTK_TABLE(col_arrows[i].table), 5);
330
331       column_lb = gtk_label_new(columns[i].strptr);
332
333       gtk_table_attach(GTK_TABLE(col_arrows[i].table), column_lb,
334           0, 1, 0, 2, GTK_SHRINK, GTK_SHRINK, 0, 0);
335
336       gtk_widget_show(column_lb);
337
338       col_arrows[i].ascend_pm = xpm_to_widget(clist_ascend_xpm);
339
340       gtk_table_attach(GTK_TABLE(col_arrows[i].table), col_arrows[i].ascend_pm,
341           1, 2, 1, 2, GTK_SHRINK, GTK_SHRINK, 0, 0);
342
343       col_arrows[i].descend_pm = xpm_to_widget(clist_descend_xpm);
344
345       gtk_table_attach(GTK_TABLE(col_arrows[i].table), col_arrows[i].descend_pm,
346           1, 2, 0, 1, GTK_SHRINK, GTK_SHRINK, 0, 0);
347
348       if (i == 0)
349       {
350           /* default column sorting */
351           gtk_widget_show(col_arrows[i].ascend_pm);
352       }
353
354       gtk_clist_set_column_justification(GTK_CLIST(table), i, columns[i].just);
355
356       gtk_clist_set_column_widget(GTK_CLIST(table), i, col_arrows[i].table);
357       gtk_widget_show(col_arrows[i].table);
358   }
359   gtk_clist_column_titles_show(GTK_CLIST(table));
360
361   gtk_clist_set_compare_func(GTK_CLIST(table), mtp3_sum_gtk_sort_column);
362   gtk_clist_set_sort_column(GTK_CLIST(table), 0);
363   gtk_clist_set_sort_type(GTK_CLIST(table), GTK_SORT_ASCENDING);
364
365   for (i = 0; i < MTP3_SUM_INIT_TABLE_NUM_COLUMNS; i++)
366   {
367       gtk_clist_set_column_width(GTK_CLIST(table), i, columns[i].value);
368   }
369
370   gtk_clist_set_shadow_type(GTK_CLIST(table), GTK_SHADOW_IN);
371   gtk_clist_column_titles_show(GTK_CLIST(table));
372
373   SIGNAL_CONNECT(table, "click-column", mtp3_sum_gtk_click_column_cb, col_arrows);
374
375   mtp3_sum_draw(table, seconds, &tot_num_msus, &tot_num_bytes);
376
377   /* Totals frame */
378   tot_fr = gtk_frame_new("Totals");
379   gtk_container_add(GTK_CONTAINER(main_vb), tot_fr);
380   gtk_widget_show(tot_fr);
381
382   tot_box = gtk_vbox_new(FALSE, 3);
383   gtk_container_add(GTK_CONTAINER(tot_fr), tot_box);
384   gtk_widget_show(tot_box);
385
386   g_snprintf(string_buff, SUM_STR_MAX, "Total MSUs: %u", tot_num_msus);
387   add_string_to_box(string_buff, tot_box);
388
389   if (seconds) {
390                 g_snprintf(string_buff, SUM_STR_MAX, "MSUs/second: %.2f", tot_num_msus/seconds);
391   }
392   else {
393                 g_snprintf(string_buff, SUM_STR_MAX, "MSUs/second: N/A");
394   }
395   add_string_to_box(string_buff, tot_box);
396
397   g_snprintf(string_buff, SUM_STR_MAX, "Total Bytes: %.0f", tot_num_bytes);
398   add_string_to_box(string_buff, tot_box);
399
400   if (tot_num_msus) {
401                 g_snprintf(string_buff, SUM_STR_MAX, "Average Bytes/MSU: %.2f", tot_num_bytes/tot_num_msus);
402   }
403   else {
404             g_snprintf(string_buff, SUM_STR_MAX, "Average Bytes/MSU: N/A");
405   }
406   add_string_to_box(string_buff, tot_box);
407
408   if (seconds) {
409           g_snprintf(string_buff, SUM_STR_MAX, "Bytes/second: %.2f", tot_num_bytes/seconds);
410   }
411   else {
412           g_snprintf(string_buff, SUM_STR_MAX, "Bytes/second: N/A");
413   }
414   add_string_to_box(string_buff, tot_box);
415
416   /* Button row. */
417   bbox = dlg_button_row_new(GTK_STOCK_CLOSE, NULL);
418   gtk_container_add(GTK_CONTAINER(main_vb), bbox);
419   gtk_widget_show(bbox);
420
421   close_bt = OBJECT_GET_DATA(bbox, GTK_STOCK_CLOSE);
422   window_set_cancel_button(sum_open_w, close_bt, window_cancel_button_cb);
423
424   SIGNAL_CONNECT(sum_open_w, "delete_event", window_delete_event_cb, NULL);
425
426   gtk_widget_show_all(sum_open_w);
427   window_present(sum_open_w);
428 }
429
430
431 void
432 register_tap_listener_gtkmtp3_summary(void)
433 {
434     register_stat_menu_item("MTP3/MSU Summary",  REGISTER_STAT_GROUP_TELEPHONY,
435         mtp3_sum_gtk_sum_cb, NULL, NULL, NULL);
436 }