d6000e67fe670c4922332425b0cea70ab31341cb
[obnox/wireshark/wip.git] / gtk / camel_srt.c
1 /* camel_srt.c
2  * camel Service Response Time statistics for Wireshark
3  * Copyright 2006 Florent Drouin (based on h225_ras_srt.c from Lars Roland)
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 #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/packet.h>
42 #include <epan/asn1.h>
43 #include <epan/camel-persistentdata.h>
44
45 #include "../timestats.h"
46 #include "../simple_dialog.h"
47 #include "../file.h"
48 #include "../stat_menu.h"
49
50 #include "gtk/main.h"
51 #include "gtk/dlg_utils.h"
52 #include "gtk/gui_utils.h"
53 #include "gtk/gui_stat_util.h"
54 #include "gtk/tap_dfilter_dlg.h"
55 #include "gtk/service_response_time_table.h"
56
57
58 /* used to keep track of the statistics for an entire program interface */
59 struct camelsrt_t {
60   GtkWidget *win;
61   srt_stat_table camel_srt_table;
62 };
63
64 static void camelsrt_set_title(struct camelsrt_t * p_camelsrt);
65 static void camelsrt_reset(void *phs);
66 static int camelsrt_packet(void *phs,
67                            packet_info *pinfo _U_,
68                            epan_dissect_t *edt _U_,
69                            const void *phi);
70
71 static void camelsrt_draw(void *phs);
72 static void win_destroy_cb(GtkWindow *win _U_, gpointer data);
73 static void gtk_camelsrt_init(const char *optarg, void *userdata _U_);
74 void register_tap_listener_gtk_camelsrt(void);
75
76 /*
77  *
78  */
79 static void camelsrt_set_title(struct camelsrt_t * p_camelsrt)
80 {
81   char * title;
82   title = g_strdup_printf("CAMEL Service Response Time statistics: %s",
83                           cf_get_display_name(&cfile));
84   gtk_window_set_title(GTK_WINDOW(p_camelsrt->win), title);
85   g_free(title);
86 }
87
88 static void camelsrt_reset(void *phs)
89 {
90   struct camelsrt_t *hs=(struct camelsrt_t *)phs;
91   reset_srt_table_data(&hs->camel_srt_table);
92   camelsrt_set_title(hs);
93 }
94
95 /*
96  * Count the delta time between Request and Response
97  * As we can make several measurement per message, we use a boolean array for the category
98  * Then, if the measurement is provided, check if it is valid, and update the table
99  */
100 static int camelsrt_packet(void *phs,
101                            packet_info *pinfo _U_,
102                            epan_dissect_t *edt _U_,
103                            const void *phi)
104 {
105   struct camelsrt_t *hs=(struct camelsrt_t *)phs;
106   const struct camelsrt_info_t * pi=phi;
107   int i;
108
109   for (i=1; i<NB_CAMELSRT_CATEGORY; i++) {
110     if ( pi->bool_msginfo[i] &&
111          pi->msginfo[i].is_delta_time
112          && pi->msginfo[i].request_available
113          && !pi->msginfo[i].is_duplicate ) {
114
115       add_srt_table_data(&hs->camel_srt_table, i, &pi->msginfo[i].req_time, pinfo);
116
117     }
118   } /* category */
119   return 1;
120 }
121
122
123 static void camelsrt_draw(void *phs)
124 {
125   struct camelsrt_t *hs=(struct camelsrt_t *)phs;
126   draw_srt_table_data(&hs->camel_srt_table);
127 }
128
129 /*
130  * Routine for Display
131  */
132 static void win_destroy_cb(GtkWindow *win _U_, gpointer data)
133 {
134   struct camelsrt_t *hs=(struct camelsrt_t *)data;
135
136   protect_thread_critical_region();
137   remove_tap_listener(hs);
138   unprotect_thread_critical_region();
139
140   free_srt_table_data(&hs->camel_srt_table);
141   g_free(hs);
142 }
143
144 static void gtk_camelsrt_init(const char *optarg, void *userdata _U_)
145 {
146   struct camelsrt_t * p_camelsrt;
147   const char *filter=NULL;
148
149   GtkWidget *cmd_label;
150   GtkWidget *main_label;
151   GtkWidget *filter_label;
152   char *filter_string;
153   GString *error_string;
154   GtkWidget *vbox;
155   GtkWidget *bbox;
156   GtkWidget *close_bt;
157   int i;
158
159   if(strncmp(optarg,"camel,srt,",10) == 0){
160     filter=optarg+10;
161   } else {
162     filter=NULL;
163   }
164
165   p_camelsrt=g_malloc(sizeof(struct camelsrt_t));
166
167   p_camelsrt->win= dlg_window_new("camel-srt");  /* transient_for top_level */
168   gtk_window_set_destroy_with_parent (GTK_WINDOW(p_camelsrt->win), TRUE);
169
170   gtk_window_set_default_size(GTK_WINDOW(p_camelsrt->win), 550, 400);
171   camelsrt_set_title(p_camelsrt);
172
173   vbox=gtk_vbox_new(FALSE, 3);
174   gtk_container_add(GTK_CONTAINER(p_camelsrt->win), vbox);
175   gtk_container_set_border_width(GTK_CONTAINER(vbox), 12);
176
177   main_label=gtk_label_new("CAMEL Service Response Time statistics");
178   gtk_box_pack_start(GTK_BOX(vbox), main_label, FALSE, FALSE, 0);
179   gtk_widget_show(main_label);
180
181   filter_string = g_strdup_printf("Filter: %s",filter ? filter : "");
182   filter_label=gtk_label_new(filter_string);
183   g_free(filter_string);
184   gtk_label_set_line_wrap(GTK_LABEL(filter_label), TRUE);
185   gtk_box_pack_start(GTK_BOX(vbox), filter_label, FALSE, FALSE, 0);
186   gtk_widget_show(filter_label);
187
188   cmd_label=gtk_label_new("CAMEL Commands");
189   gtk_box_pack_start(GTK_BOX(vbox), cmd_label, FALSE, FALSE, 0);
190   gtk_widget_show(cmd_label);
191
192   /* We must display TOP LEVEL Widget before calling init_srt_table() */
193   gtk_widget_show_all(p_camelsrt->win);
194
195   init_srt_table(&p_camelsrt->camel_srt_table, NB_CAMELSRT_CATEGORY, vbox, NULL);
196   for(i=0 ;i<NB_CAMELSRT_CATEGORY; i++) {
197     init_srt_table_row(&p_camelsrt->camel_srt_table, i,
198                        val_to_str(i,camelSRTtype_naming,"Unknown"));
199   }
200
201   error_string=register_tap_listener("CAMEL",
202                                      p_camelsrt,
203                                      filter,
204                                      0,
205                                      camelsrt_reset,
206                                      camelsrt_packet,
207                                      camelsrt_draw);
208
209   if(error_string){
210     simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "%s", error_string->str);
211     g_string_free(error_string, TRUE);
212     g_free(p_camelsrt);
213     return;
214   }
215
216   /* Button row. */
217   bbox = dlg_button_row_new(GTK_STOCK_CLOSE, NULL);
218   gtk_box_pack_end(GTK_BOX(vbox), bbox, FALSE, FALSE, 0);
219
220   close_bt = g_object_get_data(G_OBJECT(bbox), GTK_STOCK_CLOSE);
221   window_set_cancel_button(p_camelsrt->win, close_bt, window_cancel_button_cb);
222
223   g_signal_connect(p_camelsrt->win, "delete_event", G_CALLBACK(window_delete_event_cb), NULL);
224   g_signal_connect(p_camelsrt->win, "destroy", G_CALLBACK(win_destroy_cb), p_camelsrt);
225
226   gtk_widget_show_all(p_camelsrt->win);
227   window_present(p_camelsrt->win);
228   cf_retap_packets(&cfile);
229   gdk_window_raise(p_camelsrt->win->window);
230
231 }
232
233 static tap_param camel_srt_params[] = {
234   { PARAM_FILTER, "Filter", NULL }
235 };
236
237 static tap_param_dlg camel_srt_dlg = {
238   "CAMEL Service Response Time",
239   "camel,srt",
240   gtk_camelsrt_init,
241   -1,
242   G_N_ELEMENTS(camel_srt_params),
243   camel_srt_params
244 };
245
246 void /* Next line mandatory */
247 register_tap_listener_gtk_camelsrt(void)
248 {
249   register_dfilter_stat(&camel_srt_dlg, "CAMEL",
250                         REGISTER_STAT_GROUP_RESPONSE_TIME);
251 }