Add support for PARAM_ENUM tap parameters, and use the tap_param_dlg
[obnox/wireshark/wip.git] / gtk / afp_stat.c
1 /* afp_stat.c
2  * Based on
3  * smb_stat   2003 Ronnie Sahlberg
4  *
5  * $Id$
6  *
7  * Wireshark - Network traffic analyzer
8  * By Gerald Combs <gerald@wireshark.org>
9  * Copyright 1998 Gerald Combs
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24  */
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #ifdef HAVE_SYS_TYPES_H
31 # include <sys/types.h>
32 #endif
33
34 #include <string.h>
35
36 #include <gtk/gtk.h>
37
38 #include <epan/packet_info.h>
39 #include <epan/epan.h>
40 #include <epan/value_string.h>
41 #include <epan/tap.h>
42 #include <epan/dissectors/packet-afp.h>
43
44 #include "../timestats.h"
45 #include "../simple_dialog.h"
46 #include "../file.h"
47 #include "../stat_menu.h"
48
49 #include "gtk/gui_utils.h"
50 #include "gtk/dlg_utils.h"
51 #include "gtk/service_response_time_table.h"
52 #include "gtk/tap_param_dlg.h"
53 #include "gtk/main.h"
54
55
56 /* used to keep track of the statistics for an entire program interface */
57 typedef struct _afpstat_t {
58         GtkWidget *win;
59         srt_stat_table afp_srt_table;
60 } afpstat_t;
61
62 static void
63 afpstat_set_title(afpstat_t *ss)
64 {
65         char *title;
66
67         title = g_strdup_printf("AFP Service Response Time statistics: %s",
68             cf_get_display_name(&cfile));
69         gtk_window_set_title(GTK_WINDOW(ss->win), title);
70         g_free(title);
71 }
72
73 static void
74 afpstat_reset(void *pss)
75 {
76         afpstat_t *ss=(afpstat_t *)pss;
77
78         reset_srt_table_data(&ss->afp_srt_table);
79         afpstat_set_title(ss);
80 }
81
82 static int
83 afpstat_packet(void *pss, packet_info *pinfo, epan_dissect_t *edt _U_, const void *prv)
84 {
85         afpstat_t *ss=(afpstat_t *)pss;
86         const afp_request_val *request_val=prv;
87
88         /* if we havnt seen the request, just ignore it */
89         if(!request_val){
90                 return 0;
91         }
92
93         add_srt_table_data(&ss->afp_srt_table, request_val->command, &request_val->req_time, pinfo);
94
95         return 1;
96 }
97
98
99
100 static void
101 afpstat_draw(void *pss)
102 {
103         afpstat_t *ss=(afpstat_t *)pss;
104
105         draw_srt_table_data(&ss->afp_srt_table);
106 }
107
108
109 static void
110 win_destroy_cb(GtkWindow *win _U_, gpointer data)
111 {
112         afpstat_t *ss=(afpstat_t *)data;
113
114         protect_thread_critical_region();
115         remove_tap_listener(ss);
116         unprotect_thread_critical_region();
117
118         free_srt_table_data(&ss->afp_srt_table);
119         g_free(ss);
120 }
121
122
123 static void
124 gtk_afpstat_init(const char *optarg, void *userdata _U_)
125 {
126         afpstat_t *ss;
127         const char *filter=NULL;
128         GtkWidget *label;
129         char *filter_string;
130         GString *error_string;
131         int i;
132         GtkWidget *vbox;
133         GtkWidget *bbox;
134         GtkWidget *close_bt;
135
136         if(!strncmp(optarg,"afp,srt,",8)){
137                 filter=optarg+8;
138         } else {
139                 filter=NULL;
140         }
141
142         ss=g_malloc(sizeof(afpstat_t));
143
144         ss->win=dlg_window_new("afp-stat");  /* transient_for top_level */
145         gtk_window_set_destroy_with_parent (GTK_WINDOW(ss->win), TRUE);
146         gtk_window_set_default_size(GTK_WINDOW(ss->win), 550, 600);
147         afpstat_set_title(ss);
148
149         vbox=gtk_vbox_new(FALSE, 3);
150         gtk_container_add(GTK_CONTAINER(ss->win), vbox);
151         gtk_container_set_border_width(GTK_CONTAINER(vbox), 12);
152
153         label=gtk_label_new("AFP Service Response Time statistics");
154         gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
155
156         filter_string = g_strdup_printf("Filter: %s", filter ? filter : "");
157         label=gtk_label_new(filter_string);
158         gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
159         g_free(filter_string);
160         gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
161
162         label=gtk_label_new("AFP Commands");
163         gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
164
165         /* We must display TOP LEVEL Widget before calling init_srt_table() */
166         gtk_widget_show_all(ss->win);
167
168         init_srt_table(&ss->afp_srt_table, 256, vbox, "afp.command");
169         for(i=0;i<256;i++){
170                 init_srt_table_row(&ss->afp_srt_table, i, val_to_str_ext(i, &CommandCode_vals_ext, "Unknown(%u)"));
171         }
172
173
174         error_string=register_tap_listener("afp", ss, filter, 0, afpstat_reset, afpstat_packet, afpstat_draw);
175         if(error_string){
176                 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "%s", error_string->str);
177                 g_string_free(error_string, TRUE);
178                 g_free(ss);
179                 return;
180         }
181
182         /* Button row. */
183         bbox = dlg_button_row_new(GTK_STOCK_CLOSE, NULL);
184         gtk_box_pack_end(GTK_BOX(vbox), bbox, FALSE, FALSE, 0);
185
186         close_bt = g_object_get_data(G_OBJECT(bbox), GTK_STOCK_CLOSE);
187         window_set_cancel_button(ss->win, close_bt, window_cancel_button_cb);
188
189         g_signal_connect(ss->win, "delete_event", G_CALLBACK(window_delete_event_cb), NULL);
190         g_signal_connect(ss->win, "destroy", G_CALLBACK(win_destroy_cb), ss);
191
192         gtk_widget_show_all(ss->win);
193         window_present(ss->win);
194
195         cf_retap_packets(&cfile);
196         gdk_window_raise(ss->win->window);
197 }
198
199 static tap_param afp_stat_params[] = {
200         { PARAM_FILTER, "Filter", NULL }
201 };
202
203 static tap_param_dlg afp_stat_dlg = {
204         "AFP SRT Statistics",
205         "afp,srt",
206         gtk_afpstat_init,
207         -1,
208         G_N_ELEMENTS(afp_stat_params),
209         afp_stat_params
210 };
211
212 void
213 register_tap_listener_gtkafpstat(void)
214 {
215         register_dfilter_stat(&afp_stat_dlg, "AFP",
216             REGISTER_STAT_GROUP_RESPONSE_TIME);
217 }