16bc2b76a68f5a3955bd5b2a783527d466349eae
[obnox/wireshark/wip.git] / gtk / ansi_map_stat.c
1 /* ansi_map_stat.c
2  *
3  * Copyright 2003, 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 ANSI MAP:
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 "tap_menu.h"
44 #include "image/clist_ascend.xpm"
45 #include "image/clist_descend.xpm"
46 #include "simple_dialog.h"
47 #include "dlg_utils.h"
48 #include <epan/tap.h>
49 #include "../register.h"
50 #include "../globals.h"
51 #include "filter_dlg.h"
52 #include "compat_macros.h"
53 #include <epan/dissectors/packet-ansi_map.h>
54 #include "ui_util.h"
55
56
57 typedef struct column_arrows {
58     GtkWidget           *table;
59     GtkWidget           *ascend_pm;
60     GtkWidget           *descend_pm;
61 } column_arrows;
62
63 typedef struct _my_columns_t {
64     guint32             value;
65     gchar               *strptr;
66     GtkJustification    just;
67 } my_columns_t;
68
69 #define ANSI_MAP_INIT_TABLE_NUM_COLUMNS         5
70
71 static my_columns_t columns[ANSI_MAP_INIT_TABLE_NUM_COLUMNS] = {
72     { 60,       "Op Code",              GTK_JUSTIFY_LEFT },
73     { 290,      "Operation Name",       GTK_JUSTIFY_LEFT },
74     { 50,       "Count",                GTK_JUSTIFY_RIGHT },
75     { 100,      "Total Bytes",          GTK_JUSTIFY_RIGHT },
76     { 50,       "Average Bytes",        GTK_JUSTIFY_RIGHT }
77 };
78
79 typedef struct _ansi_map_stat_dlg_t {
80     GtkWidget           *win;
81     GtkWidget           *scrolled_win;
82     GtkWidget           *table;
83     char                *entries[ANSI_MAP_INIT_TABLE_NUM_COLUMNS];
84 } ansi_map_stat_dlg_t;
85
86 typedef struct _ansi_map_stat_t {
87     int                 message_type[ANSI_MAP_MAX_NUM_MESSAGE_TYPES];
88     double              size[ANSI_MAP_MAX_NUM_MESSAGE_TYPES];
89 } ansi_map_stat_t;
90
91 static ansi_map_stat_dlg_t      dlg;
92 static ansi_map_stat_t          stat;
93
94
95 static void
96 ansi_map_stat_reset(
97     void                *tapdata)
98 {
99     ansi_map_stat_t     *stat_p = tapdata;
100
101     memset(stat_p, 0, sizeof(ansi_map_stat_t));
102 }
103
104
105 static int
106 ansi_map_stat_packet(
107     void                *tapdata,
108     packet_info         *pinfo _U_,
109     epan_dissect_t      *edt _U_,
110     const void          *data)
111 {
112     ansi_map_stat_t     *stat_p = tapdata;
113     const ansi_map_tap_rec_t    *data_p = data;
114
115 #if 0   /* always false because message_type is 8 bit value */
116     if (data_p->message_type >= ANSI_MAP_MAX_NUM_MESSAGE_TYPES)
117     {
118         /*
119          * unknown PDU type !!!
120          */
121         return(0);
122     }
123 #endif
124
125     stat_p->message_type[data_p->message_type]++;
126     stat_p->size[data_p->message_type] += data_p->size;
127
128     return(1);
129 }
130
131
132 static void
133 ansi_map_stat_draw(
134     void                *tapdata)
135 {
136     ansi_map_stat_t     *stat_p = tapdata;
137     int                 i, j;
138     char                *strp;
139
140     if (dlg.win && tapdata)
141     {
142         i = 0;
143
144         while (ansi_map_opr_code_strings[i].strptr)
145         {
146             j = gtk_clist_find_row_from_data(GTK_CLIST(dlg.table), (gpointer) i);
147
148             strp = g_strdup_printf("%d",
149                     stat_p->message_type[ansi_map_opr_code_strings[i].value]);
150             gtk_clist_set_text(GTK_CLIST(dlg.table), j, 2, strp);
151             g_free(strp);
152
153             strp = g_strdup_printf("%.0f", stat_p->size[ansi_map_opr_code_strings[i].value]);
154             gtk_clist_set_text(GTK_CLIST(dlg.table), j, 3, strp);
155             g_free(strp);
156
157             strp = g_strdup_printf("%.2f", stat_p->size[ansi_map_opr_code_strings[i].value]/stat_p->message_type[ansi_map_opr_code_strings[i].value]);
158             gtk_clist_set_text(GTK_CLIST(dlg.table), j, 4, strp);
159             g_free(strp);
160
161             i++;
162         }
163
164         gtk_clist_sort(GTK_CLIST(dlg.table));
165     }
166 }
167
168
169 static void
170 ansi_map_stat_gtk_click_column_cb(
171     GtkCList            *clist,
172     gint                column,
173     gpointer            data)
174 {
175     column_arrows       *col_arrows = (column_arrows *) data;
176     int                 i;
177
178
179     gtk_clist_freeze(clist);
180
181     for (i=0; i < ANSI_MAP_INIT_TABLE_NUM_COLUMNS; i++)
182     {
183         gtk_widget_hide(col_arrows[i].ascend_pm);
184         gtk_widget_hide(col_arrows[i].descend_pm);
185     }
186
187     if (column == clist->sort_column)
188     {
189         if (clist->sort_type == GTK_SORT_ASCENDING)
190         {
191             clist->sort_type = GTK_SORT_DESCENDING;
192             gtk_widget_show(col_arrows[column].descend_pm);
193         }
194         else
195         {
196             clist->sort_type = GTK_SORT_ASCENDING;
197             gtk_widget_show(col_arrows[column].ascend_pm);
198         }
199     }
200     else
201     {
202         /*
203          * Columns 0-1 sorted in descending order by default
204          */
205         if (column <= 1)
206         {
207             clist->sort_type = GTK_SORT_ASCENDING;
208             gtk_widget_show(col_arrows[column].ascend_pm);
209         }
210         else
211         {
212             clist->sort_type = GTK_SORT_DESCENDING;
213             gtk_widget_show(col_arrows[column].descend_pm);
214         }
215
216         gtk_clist_set_sort_column(clist, column);
217     }
218
219     gtk_clist_thaw(clist);
220     gtk_clist_sort(clist);
221 }
222
223
224 static gint
225 ansi_map_stat_gtk_sort_column(
226     GtkCList            *clist,
227     gconstpointer       ptr1,
228     gconstpointer       ptr2)
229 {
230     const GtkCListRow   *row1 = ptr1;
231     const GtkCListRow   *row2 = ptr2;
232     char                *text1 = NULL;
233     char                *text2 = NULL;
234     int                 i1, i2;
235
236     text1 = GTK_CELL_TEXT(row1->cell[clist->sort_column])->text;
237     text2 = GTK_CELL_TEXT(row2->cell[clist->sort_column])->text;
238
239     switch (clist->sort_column)
240     {
241     case 1:
242         /* text columns */
243         return(strcmp(text1, text2));
244
245     default:
246         /* number columns */
247         i1 = strtol(text1, NULL, 0);
248         i2 = strtol(text2, NULL, 0);
249         return(i1 - i2);
250     }
251
252     g_assert_not_reached();
253
254     return(0);
255 }
256
257
258 static void
259 ansi_map_stat_gtk_win_destroy_cb(
260     GtkWindow           *win _U_,
261     gpointer            user_data _U_)
262 {
263     memset((void *) user_data, 0, sizeof(ansi_map_stat_dlg_t));
264 }
265
266
267 static void
268 ansi_map_stat_gtk_win_create(
269     ansi_map_stat_dlg_t *dlg_p,
270     char                *title)
271 {
272     int                 i;
273     column_arrows       *col_arrows;
274     GtkWidget           *column_lb;
275     GtkWidget           *vbox;
276     GtkWidget           *bt_close;
277     GtkWidget           *bbox;
278
279
280     dlg_p->win = window_new(GTK_WINDOW_TOPLEVEL, title);
281     gtk_window_set_default_size(GTK_WINDOW(dlg_p->win), 500, 450);
282
283     vbox = gtk_vbox_new(FALSE, 3);
284         gtk_container_add(GTK_CONTAINER(dlg_p->win), vbox);
285     gtk_container_set_border_width(GTK_CONTAINER(vbox), 12);
286
287     dlg_p->scrolled_win = scrolled_window_new(NULL, NULL);
288     gtk_box_pack_start(GTK_BOX(vbox), dlg_p->scrolled_win, TRUE, TRUE, 0);
289
290     dlg_p->table = gtk_clist_new(ANSI_MAP_INIT_TABLE_NUM_COLUMNS);
291
292     col_arrows =
293         (column_arrows *) g_malloc(sizeof(column_arrows) * ANSI_MAP_INIT_TABLE_NUM_COLUMNS);
294
295     for (i = 0; i < ANSI_MAP_INIT_TABLE_NUM_COLUMNS; i++)
296     {
297         col_arrows[i].table = gtk_table_new(2, 2, FALSE);
298
299         gtk_table_set_col_spacings(GTK_TABLE(col_arrows[i].table), 5);
300
301         column_lb = gtk_label_new(columns[i].strptr);
302
303         gtk_table_attach(GTK_TABLE(col_arrows[i].table), column_lb,
304             0, 1, 0, 2, GTK_SHRINK, GTK_SHRINK, 0, 0);
305
306         gtk_widget_show(column_lb);
307
308         col_arrows[i].ascend_pm = xpm_to_widget(clist_ascend_xpm);
309         /*    gtk_pixmap_new(ascend_pm, ascend_bm); */
310
311         gtk_table_attach(GTK_TABLE(col_arrows[i].table), col_arrows[i].ascend_pm,
312             1, 2, 1, 2, GTK_SHRINK, GTK_SHRINK, 0, 0);
313
314         col_arrows[i].descend_pm = xpm_to_widget(clist_descend_xpm);
315         /*    gtk_pixmap_new(descend_pm, descend_bm); */
316
317         gtk_table_attach(GTK_TABLE(col_arrows[i].table), col_arrows[i].descend_pm,
318             1, 2, 0, 1, GTK_SHRINK, GTK_SHRINK, 0, 0);
319
320         if (i == 0)
321         {
322             /* default column sorting */
323             gtk_widget_show(col_arrows[i].ascend_pm);
324         }
325
326         gtk_clist_set_column_justification(GTK_CLIST(dlg_p->table), i, columns[i].just);
327
328         gtk_clist_set_column_widget(GTK_CLIST(dlg_p->table), i, col_arrows[i].table);
329         gtk_widget_show(col_arrows[i].table);
330     }
331     gtk_clist_column_titles_show(GTK_CLIST(dlg_p->table));
332
333     gtk_clist_set_compare_func(GTK_CLIST(dlg_p->table), ansi_map_stat_gtk_sort_column);
334     gtk_clist_set_sort_column(GTK_CLIST(dlg_p->table), 0);
335     gtk_clist_set_sort_type(GTK_CLIST(dlg_p->table), GTK_SORT_ASCENDING);
336
337     for (i = 0; i < ANSI_MAP_INIT_TABLE_NUM_COLUMNS; i++)
338     {
339         gtk_clist_set_column_width(GTK_CLIST(dlg_p->table), i, columns[i].value);
340     }
341
342     gtk_clist_set_shadow_type(GTK_CLIST(dlg_p->table), GTK_SHADOW_IN);
343     gtk_clist_column_titles_show(GTK_CLIST(dlg_p->table));
344     gtk_container_add(GTK_CONTAINER(dlg_p->scrolled_win), dlg_p->table);
345
346     SIGNAL_CONNECT(dlg_p->table, "click-column", ansi_map_stat_gtk_click_column_cb, col_arrows);
347
348     /* Button row. */
349     bbox = dlg_button_row_new(GTK_STOCK_CLOSE, NULL);
350         gtk_box_pack_start(GTK_BOX(vbox), bbox, FALSE, FALSE, 0);
351
352     bt_close = OBJECT_GET_DATA(bbox, GTK_STOCK_CLOSE);
353
354     window_set_cancel_button(dlg_p->win, bt_close, window_cancel_button_cb);
355
356     SIGNAL_CONNECT(dlg_p->win, "delete_event", window_delete_event_cb, NULL);
357     SIGNAL_CONNECT(dlg_p->win, "destroy", ansi_map_stat_gtk_win_destroy_cb, dlg_p);
358
359     gtk_widget_show_all(dlg_p->win);
360     window_present(dlg_p->win);
361 }
362
363
364 /*
365  * Never gets called ?
366  */
367 static void
368 ansi_map_stat_gtk_init(
369     char                *optarg)
370 {
371     /* does not appear to be called */
372
373     optarg = optarg;
374 }
375
376
377 static void
378 ansi_map_stat_gtk_cb(
379     GtkWidget           *w _U_,
380     gpointer            d _U_)
381 {
382     int                 i;
383     char                str[100];
384
385
386     /*
387      * if the window is already open, bring it to front
388      */
389     if (dlg.win)
390     {
391         gdk_window_raise(dlg.win->window);
392         return;
393     }
394
395     ansi_map_stat_gtk_win_create(&dlg, "ANSI MAP Operation Statistics");
396
397     i = 0;
398     while (ansi_map_opr_code_strings[i].strptr)
399     {
400         g_snprintf(str, 100, "0x%02x", ansi_map_opr_code_strings[i].value);
401         dlg.entries[0] = g_strdup(str);
402
403         dlg.entries[1] = g_strdup(ansi_map_opr_code_strings[i].strptr);
404
405         dlg.entries[2] = g_strdup("0");
406
407         dlg.entries[3] = g_strdup("0");
408
409         dlg.entries[4] = g_strdup("0");
410
411         gtk_clist_insert(GTK_CLIST(dlg.table), i, dlg.entries);
412         gtk_clist_set_row_data(GTK_CLIST(dlg.table), i, (gpointer) i);
413
414         i++;
415     }
416
417     ansi_map_stat_draw(NULL);
418 }
419
420
421 void
422 register_tap_listener_gtkansi_map_stat(void)
423 {
424     GString             *err_p;
425
426
427     register_ethereal_tap("ansi_map,", ansi_map_stat_gtk_init);
428
429     memset((void *) &stat, 0, sizeof(ansi_map_stat_t));
430
431     err_p =
432         register_tap_listener("ansi_map", &stat, NULL,
433             ansi_map_stat_reset,
434             ansi_map_stat_packet,
435             ansi_map_stat_draw);
436
437     if (err_p != NULL)
438     {
439         simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, err_p->str);
440         g_string_free(err_p, TRUE);
441
442         exit(1);
443     }
444
445     register_tap_menu_item("ANSI/MAP Operation", REGISTER_TAP_GROUP_NONE,
446         ansi_map_stat_gtk_cb, NULL, NULL, NULL);
447 }