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