Rename epan/stat.[ch] to epan/stat_cmd_args.[ch] - it only deals with
[obnox/wireshark/wip.git] / gtk / gsm_map_stat.c
1 /* gsm_map_stat.c
2  *
3  * Copyright 2004, Michael Lum <mlum [AT] telostech.com>
4  * In association with Telos Technology Inc.
5  *
6  * MUCH code modified from service_response_time_table.c.
7  *
8  * $Id$
9  *
10  * Ethereal - Network traffic analyzer
11  * By Gerald Combs <gerald@ethereal.com>
12  * Copyright 1998 Gerald Combs
13  *
14  * This program is free software; you can redistribute it and/or
15  * modify it under the terms of the GNU General Public License
16  * as published by the Free Software Foundation; either version 2
17  * of the License, or (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, write to the Free Software
26  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
27  */
28
29 /*
30  * This TAP provides statistics for GSM MAP Operations:
31  */
32
33 #ifdef HAVE_CONFIG_H
34 # include "config.h"
35 #endif
36
37 #include <gtk/gtk.h>
38 #include <string.h>
39
40 #include "epan/packet_info.h"
41 #include "epan/epan.h"
42 #include "epan/value_string.h"
43 #include <epan/stat_cmd_args.h>
44 #include "stat_menu.h"
45 #include "image/clist_ascend.xpm"
46 #include "image/clist_descend.xpm"
47 #include "simple_dialog.h"
48 #include "dlg_utils.h"
49 #include "gui_utils.h"
50 #include <epan/tap.h>
51 #include "../register.h"
52 #include "../globals.h"
53 #include "filter_dlg.h"
54 #include "compat_macros.h"
55
56 #include <epan/dissectors/packet-gsm_map.h>
57 #include "gsm_map_stat.h"
58
59 typedef struct column_arrows {
60     GtkWidget           *table;
61     GtkWidget           *ascend_pm;
62     GtkWidget           *descend_pm;
63 } column_arrows;
64
65 #define GSM_MAP_INIT_TABLE_NUM_COLUMNS          10
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[GSM_MAP_INIT_TABLE_NUM_COLUMNS] = {
74     { 40,       "ID",                   GTK_JUSTIFY_LEFT },
75     { 210,      "Operation Code",       GTK_JUSTIFY_LEFT },
76     { 60,       "Invokes",              GTK_JUSTIFY_RIGHT },
77     { 100,      "Num Bytes",            GTK_JUSTIFY_RIGHT },
78     { 80,       "Avg Bytes",            GTK_JUSTIFY_RIGHT },
79     { 60,       "RetResult",            GTK_JUSTIFY_RIGHT },
80     { 100,      "Num Bytes",            GTK_JUSTIFY_RIGHT },
81     { 80,       "Avg Bytes",            GTK_JUSTIFY_RIGHT },
82     { 100,      "Total Bytes",          GTK_JUSTIFY_RIGHT },
83     { 80,       "Avg Bytes",            GTK_JUSTIFY_RIGHT }
84 };
85
86 typedef struct _gsm_map_stat_dlg_t {
87     GtkWidget           *win;
88     GtkWidget           *scrolled_win;
89     GtkWidget           *table;
90     char                *entries[GSM_MAP_INIT_TABLE_NUM_COLUMNS];
91 } gsm_map_stat_dlg_t;
92
93 static gsm_map_stat_dlg_t       dlg;
94
95 /*
96  * used by gsm_map_summary.c
97  */
98 gsm_map_stat_t                  gsm_map_stat;
99
100
101 static void
102 gsm_map_stat_reset(
103     void                *tapdata)
104 {
105     gsm_map_stat_t      *stat_p = tapdata;
106
107     memset(stat_p, 0, sizeof(gsm_map_stat_t));
108 }
109
110
111 static int
112 gsm_map_stat_packet(
113     void                *tapdata,
114     packet_info         *pinfo _U_,
115     epan_dissect_t      *edt _U_,
116     const void          *data)
117 {
118     gsm_map_stat_t      *stat_p = tapdata;
119     const gsm_map_tap_rec_t     *data_p = data;
120
121 #if 0   /* always false because message_type is 8 bit value */
122     if (data_p->opr_code_idx > sizeof(stat_p->opr_code))
123     {
124         /*
125          * unknown message type !!!
126          */
127         return(0);
128     }
129 #endif
130
131     if (data_p->invoke)
132     {
133         stat_p->opr_code[data_p->opr_code_idx]++;
134         stat_p->size[data_p->opr_code_idx] += data_p->size;
135     }
136     else
137     {
138         stat_p->opr_code_rr[data_p->opr_code_idx]++;
139         stat_p->size_rr[data_p->opr_code_idx] += data_p->size;
140     }
141
142     return(1);
143 }
144
145
146 static void
147 gsm_map_stat_draw(
148     void                *tapdata)
149 {
150     gsm_map_stat_t      *stat_p = tapdata;
151     int                 i, j;
152     char                *strp;
153
154     if (dlg.win && tapdata)
155     {
156         i = 0;
157
158         while (gsm_map_opr_code_strings[i].strptr)
159         {
160             j = gtk_clist_find_row_from_data(GTK_CLIST(dlg.table), (gpointer) i);
161
162             strp = g_strdup_printf("%d", stat_p->opr_code[i]);
163             gtk_clist_set_text(GTK_CLIST(dlg.table), j, 2, strp);
164             g_free(strp);
165
166             strp = g_strdup_printf("%.0f", stat_p->size[i]);
167             gtk_clist_set_text(GTK_CLIST(dlg.table), j, 3, strp);
168             g_free(strp);
169
170             strp = g_strdup_printf("%.2f", stat_p->size[i]/stat_p->opr_code[i]);
171             gtk_clist_set_text(GTK_CLIST(dlg.table), j, 4, strp);
172             g_free(strp);
173
174             strp = g_strdup_printf("%u", stat_p->opr_code_rr[i]);
175             gtk_clist_set_text(GTK_CLIST(dlg.table), j, 5, strp);
176             g_free(strp);
177
178             strp = g_strdup_printf("%.0f", stat_p->size_rr[i]);
179             gtk_clist_set_text(GTK_CLIST(dlg.table), j, 6, strp);
180             g_free(strp);
181
182             strp = g_strdup_printf("%.2f", stat_p->size_rr[i]/stat_p->opr_code_rr[i]);
183             gtk_clist_set_text(GTK_CLIST(dlg.table), j, 7, strp);
184             g_free(strp);
185
186             strp = g_strdup_printf("%.0f", stat_p->size[i] + stat_p->size_rr[i]);
187             gtk_clist_set_text(GTK_CLIST(dlg.table), j, 8, strp);
188             g_free(strp);
189
190             strp = g_strdup_printf("%.2f",
191                 (stat_p->size[i] + stat_p->size_rr[i])/(stat_p->opr_code[i] + stat_p->opr_code_rr[i]));
192             gtk_clist_set_text(GTK_CLIST(dlg.table), j, 9, strp);
193             g_free(strp);
194
195             i++;
196         }
197
198         gtk_clist_sort(GTK_CLIST(dlg.table));
199     }
200 }
201
202
203 static void
204 gsm_map_stat_gtk_click_column_cb(
205     GtkCList            *clist,
206     gint                column,
207     gpointer            data)
208 {
209     column_arrows       *col_arrows = (column_arrows *) data;
210     int                 i;
211
212
213     gtk_clist_freeze(clist);
214
215     for (i=0; i < GSM_MAP_INIT_TABLE_NUM_COLUMNS; i++)
216     {
217         gtk_widget_hide(col_arrows[i].ascend_pm);
218         gtk_widget_hide(col_arrows[i].descend_pm);
219     }
220
221     if (column == clist->sort_column)
222     {
223         if (clist->sort_type == GTK_SORT_ASCENDING)
224         {
225             clist->sort_type = GTK_SORT_DESCENDING;
226             gtk_widget_show(col_arrows[column].descend_pm);
227         }
228         else
229         {
230             clist->sort_type = GTK_SORT_ASCENDING;
231             gtk_widget_show(col_arrows[column].ascend_pm);
232         }
233     }
234     else
235     {
236         /*
237          * Columns 0-1 sorted in descending order by default
238          */
239         if (column <= 1)
240         {
241             clist->sort_type = GTK_SORT_ASCENDING;
242             gtk_widget_show(col_arrows[column].ascend_pm);
243         }
244         else
245         {
246             clist->sort_type = GTK_SORT_DESCENDING;
247             gtk_widget_show(col_arrows[column].descend_pm);
248         }
249
250         gtk_clist_set_sort_column(clist, column);
251     }
252
253     gtk_clist_thaw(clist);
254     gtk_clist_sort(clist);
255 }
256
257
258 static gint
259 gsm_map_stat_gtk_sort_column(
260     GtkCList            *clist,
261     gconstpointer       ptr1,
262     gconstpointer       ptr2)
263 {
264     const GtkCListRow           *row1 = (const GtkCListRow *) ptr1;
265     const GtkCListRow           *row2 = (const GtkCListRow *) ptr2;
266     char                *text1 = NULL;
267     char                *text2 = NULL;
268     int                 i1, i2;
269
270     text1 = GTK_CELL_TEXT(row1->cell[clist->sort_column])->text;
271     text2 = GTK_CELL_TEXT(row2->cell[clist->sort_column])->text;
272
273     switch (clist->sort_column)
274     {
275     case 1:
276         /* text columns */
277         return(strcmp(text1, text2));
278
279     default:
280         /* number columns */
281         i1 = strtol(text1, NULL, 0);
282         i2 = strtol(text2, NULL, 0);
283         return(i1 - i2);
284     }
285
286     g_assert_not_reached();
287
288     return(0);
289 }
290
291
292 static void
293 gsm_map_stat_gtk_win_destroy_cb(
294     GtkWindow           *win _U_,
295     gpointer            user_data _U_)
296 {
297     memset((void *) user_data, 0, sizeof(gsm_map_stat_dlg_t));
298 }
299
300
301 static void
302 gsm_map_stat_gtk_win_create(
303     gsm_map_stat_dlg_t  *dlg_p,
304     const char          *title)
305 {
306     int                 i;
307     column_arrows       *col_arrows;
308     GtkWidget           *column_lb;
309     GtkWidget           *vbox;
310     GtkWidget           *bt_close;
311     GtkWidget           *bbox;
312
313
314     dlg_p->win = window_new(GTK_WINDOW_TOPLEVEL, title);
315     gtk_window_set_default_size(GTK_WINDOW(dlg_p->win), 560, 390);
316
317     vbox = gtk_vbox_new(FALSE, 3);
318         gtk_container_add(GTK_CONTAINER(dlg_p->win), vbox);
319     gtk_container_set_border_width(GTK_CONTAINER(vbox), 12);
320
321     dlg_p->scrolled_win = scrolled_window_new(NULL, NULL);
322     gtk_box_pack_start(GTK_BOX(vbox), dlg_p->scrolled_win, TRUE, TRUE, 0);
323
324     dlg_p->table = gtk_clist_new(GSM_MAP_INIT_TABLE_NUM_COLUMNS);
325
326     col_arrows =
327         (column_arrows *) g_malloc(sizeof(column_arrows) * GSM_MAP_INIT_TABLE_NUM_COLUMNS);
328
329     for (i = 0; i < GSM_MAP_INIT_TABLE_NUM_COLUMNS; i++)
330     {
331         col_arrows[i].table = gtk_table_new(2, 2, FALSE);
332
333         gtk_table_set_col_spacings(GTK_TABLE(col_arrows[i].table), 5);
334
335         column_lb = gtk_label_new(columns[i].strptr);
336
337         gtk_table_attach(GTK_TABLE(col_arrows[i].table), column_lb,
338             0, 1, 0, 2, GTK_SHRINK, GTK_SHRINK, 0, 0);
339
340         gtk_widget_show(column_lb);
341
342         col_arrows[i].ascend_pm = xpm_to_widget(clist_ascend_xpm);
343
344         gtk_table_attach(GTK_TABLE(col_arrows[i].table), col_arrows[i].ascend_pm,
345             1, 2, 1, 2, GTK_SHRINK, GTK_SHRINK, 0, 0);
346
347         col_arrows[i].descend_pm = xpm_to_widget(clist_descend_xpm);
348
349         gtk_table_attach(GTK_TABLE(col_arrows[i].table), col_arrows[i].descend_pm,
350             1, 2, 0, 1, GTK_SHRINK, GTK_SHRINK, 0, 0);
351
352         if (i == 0)
353         {
354             /* default column sorting */
355             gtk_widget_show(col_arrows[i].ascend_pm);
356         }
357
358         gtk_clist_set_column_justification(GTK_CLIST(dlg_p->table), i, columns[i].just);
359
360         gtk_clist_set_column_widget(GTK_CLIST(dlg_p->table), i, col_arrows[i].table);
361         gtk_widget_show(col_arrows[i].table);
362     }
363     gtk_clist_column_titles_show(GTK_CLIST(dlg_p->table));
364
365     gtk_clist_set_compare_func(GTK_CLIST(dlg_p->table), gsm_map_stat_gtk_sort_column);
366     gtk_clist_set_sort_column(GTK_CLIST(dlg_p->table), 0);
367     gtk_clist_set_sort_type(GTK_CLIST(dlg_p->table), GTK_SORT_ASCENDING);
368
369     for (i = 0; i < GSM_MAP_INIT_TABLE_NUM_COLUMNS; i++)
370     {
371         gtk_clist_set_column_width(GTK_CLIST(dlg_p->table), i, columns[i].value);
372     }
373
374     gtk_clist_set_shadow_type(GTK_CLIST(dlg_p->table), GTK_SHADOW_IN);
375     gtk_clist_column_titles_show(GTK_CLIST(dlg_p->table));
376     gtk_container_add(GTK_CONTAINER(dlg_p->scrolled_win), dlg_p->table);
377
378     SIGNAL_CONNECT(dlg_p->table, "click-column", gsm_map_stat_gtk_click_column_cb, col_arrows);
379
380     /* Button row. */
381     bbox = dlg_button_row_new(GTK_STOCK_CLOSE, NULL);
382     gtk_box_pack_start(GTK_BOX(vbox), bbox, FALSE, FALSE, 0);
383
384     bt_close = OBJECT_GET_DATA(bbox, GTK_STOCK_CLOSE);
385     window_set_cancel_button(dlg_p->win, bt_close, window_cancel_button_cb);
386
387     SIGNAL_CONNECT(dlg_p->win, "delete_event", window_delete_event_cb, NULL);
388     SIGNAL_CONNECT(dlg_p->win, "destroy", gsm_map_stat_gtk_win_destroy_cb, dlg_p);
389
390     gtk_widget_show_all(dlg_p->win);
391     window_present(dlg_p->win);
392 }
393
394
395 /*
396  * Never gets called ?
397  */
398 static void
399 gsm_map_stat_gtk_init(
400     const char          *optarg _U_)
401 {
402     /* does not appear to be called */
403 }
404
405
406 static void
407 gsm_map_stat_gtk_cb(
408     GtkWidget           *w _U_,
409     gpointer            d _U_)
410 {
411     int                 i;
412     char                str[100];
413
414
415     /*
416      * if the window is already open, bring it to front
417      */
418     if (dlg.win)
419     {
420         gdk_window_raise(dlg.win->window);
421         return;
422     }
423
424     gsm_map_stat_gtk_win_create(&dlg, "GSM MAP Operation Statistics");
425
426     i = 0;
427     while (gsm_map_opr_code_strings[i].strptr)
428     {
429         g_snprintf(str, 100, "%u", gsm_map_opr_code_strings[i].value);
430         dlg.entries[0] = g_strdup(str);
431
432         dlg.entries[1] = g_strdup(gsm_map_opr_code_strings[i].strptr);
433
434         /*
435          * set the rest of the columns
436          */
437         for (i = 2; i < GSM_MAP_INIT_TABLE_NUM_COLUMNS; i++)
438         {
439             dlg.entries[i] = g_strdup("0");
440         }
441
442         gtk_clist_insert(GTK_CLIST(dlg.table), i, dlg.entries);
443         gtk_clist_set_row_data(GTK_CLIST(dlg.table), i, (gpointer) i);
444
445         i++;
446     }
447
448     gsm_map_stat_draw(&gsm_map_stat);
449 }
450
451
452 void
453 register_tap_listener_gtkgsm_map_stat(void)
454 {
455     GString             *err_p;
456
457
458     register_stat_cmd_arg("gsm_map,", gsm_map_stat_gtk_init);
459
460     memset((void *) &gsm_map_stat, 0, sizeof(gsm_map_stat_t));
461
462     err_p =
463         register_tap_listener("gsm_map", &gsm_map_stat, NULL,
464             gsm_map_stat_reset,
465             gsm_map_stat_packet,
466             gsm_map_stat_draw);
467
468     if (err_p != NULL)
469     {
470         simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, err_p->str);
471         g_string_free(err_p, TRUE);
472
473         exit(1);
474     }
475
476     register_stat_menu_item("GSM/MAP Operation",  REGISTER_STAT_GROUP_TELEPHONY,
477         gsm_map_stat_gtk_cb, NULL, NULL, NULL);
478 }