Add text import capability, like text2pcap, directly into the GUI.
[obnox/wireshark/wip.git] / gtk / megaco_stat.c
1 /* megaco_stat.c
2  * megaco-statistics for Wireshark
3  * Copyright 2003 Lars Roland
4  * Copyright 2008, Ericsson AB
5  * By Balint Reczey <balint.reczey@ericsson.com>
6  *
7  * $Id$
8  *
9  * Wireshark - Network traffic analyzer
10  * By Gerald Combs <gerald@wireshark.org>
11  * Copyright 1998 Gerald Combs
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
26  */
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #ifdef HAVE_SYS_TYPES_H
33 # include <sys/types.h>
34 #endif
35
36 #include <string.h>
37
38 #include <gtk/gtk.h>
39
40 #include <epan/packet_info.h>
41 #include <epan/epan.h>
42 #include <epan/value_string.h>
43 #include <epan/tap.h>
44 #include "epan/gcp.h"
45 #include <epan/prefs-int.h>
46
47 #include "../timestats.h"
48 #include "../simple_dialog.h"
49 #include "../file.h"
50 #include "../stat_menu.h"
51
52 #include "gtk/gui_stat_util.h"
53 #include "gtk/dlg_utils.h"
54 #include "gtk/tap_dfilter_dlg.h"
55 #include "gtk/gui_utils.h"
56 #include "gtk/main.h"
57
58 #include "tap-megaco-common.h"
59
60
61
62 static void
63 megacostat_reset(void *pms)
64 {
65         megacostat_t *ms=(megacostat_t *)pms;
66         int i;
67
68         for(i=0;i<NUM_TIMESTATS;i++) {
69                 ms->rtd[i].num=0;
70                 ms->rtd[i].min_num=0;
71                 ms->rtd[i].max_num=0;
72                 ms->rtd[i].min.secs=0;
73                 ms->rtd[i].min.nsecs=0;
74                 ms->rtd[i].max.secs=0;
75                 ms->rtd[i].max.nsecs=0;
76                 ms->rtd[i].tot.secs=0;
77                 ms->rtd[i].tot.nsecs=0;
78         }
79
80         ms->open_req_num=0;
81         ms->disc_rsp_num=0;
82         ms->req_dup_num=0;
83         ms->rsp_dup_num=0;
84 }
85
86 static void
87 megacostat_draw(void *pms)
88 {
89         megacostat_t *ms=(megacostat_t *)pms;
90         int i;
91         char str[3][256];
92         GtkListStore *store;
93         GtkTreeIter iter;
94
95         /* clear list before printing */
96         store = GTK_LIST_STORE(gtk_tree_view_get_model(ms->table));
97         gtk_list_store_clear(store);
98
99         for(i=0;i<NUM_TIMESTATS;i++) {
100                 /* nothing seen, nothing to do */
101                 if(ms->rtd[i].num==0){
102                         continue;
103                 }
104
105                 g_snprintf(str[0], sizeof(char[256]), "%8.2f msec", nstime_to_msec(&(ms->rtd[i].min)));
106                 g_snprintf(str[1], sizeof(char[256]), "%8.2f msec", nstime_to_msec(&(ms->rtd[i].max)));
107                 g_snprintf(str[2], sizeof(char[256]), "%8.2f msec", get_average(&(ms->rtd[i].tot), ms->rtd[i].num));
108                 gtk_list_store_append(store, &iter);
109                 gtk_list_store_set(store, &iter,
110                         0, val_to_str(i,megaco_message_type,"Other"),
111                         1, ms->rtd[i].num,
112                         2, str[0],
113                         3, str[1],
114                         4, str[2],
115                         5, ms->rtd[i].min_num,
116                         6, ms->rtd[i].max_num,
117                         -1);
118         }
119 }
120
121 static void
122 win_destroy_cb(GtkWindow *win _U_, gpointer data)
123 {
124         megacostat_t *ms=(megacostat_t *)data;
125
126         protect_thread_critical_region();
127         remove_tap_listener(ms);
128         unprotect_thread_critical_region();
129
130         if(ms->filter){
131                 g_free(ms->filter);
132                 ms->filter=NULL;
133         }
134         g_free(ms);
135 }
136
137 static const stat_column titles[]={
138         {G_TYPE_STRING, LEFT, "Type" },
139         {G_TYPE_UINT, RIGHT,   "Messages" },
140         {G_TYPE_STRING, RIGHT, "Min SRT" },
141         {G_TYPE_STRING, RIGHT, "Max SRT" },
142         {G_TYPE_STRING, RIGHT, "Avg SRT" },
143         {G_TYPE_UINT, RIGHT,  "Min in Frame" },
144         {G_TYPE_UINT, RIGHT,  "Max in Frame" }
145 };
146
147 static void
148 gtk_megacostat_init(const char *optarg, void *userdata _U_)
149 {
150         megacostat_t *ms;
151         GString *error_string;
152         GtkWidget *bt_close;
153         GtkWidget *bbox;
154         pref_t *megaco_ctx_track,*h248_ctx_track;
155
156         megaco_ctx_track = prefs_find_preference(prefs_find_module("megaco"),"ctx_info");
157         h248_ctx_track = prefs_find_preference(prefs_find_module("h248"),"ctx_info");
158
159         if (!*megaco_ctx_track->varp.boolp || !*h248_ctx_track->varp.boolp) {
160                 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "%s", "Track Context option at Protocols -> MEGACO and Protocols -> H248 preferences has to be set to true to enable measurement of service reponse times.");
161                 return;
162         }
163
164         ms=g_malloc(sizeof(megacostat_t));
165
166         if(strncmp(optarg,"megaco,srt,",11) == 0){
167                 ms->filter=g_strdup(optarg+11);
168         } else {
169                 ms->filter=NULL;
170         }
171
172         megacostat_reset(ms);
173
174         ms->win = dlg_window_new("MEGACO SRT");  /* transient_for top_level */
175         gtk_window_set_destroy_with_parent (GTK_WINDOW(ms->win), TRUE);
176
177         gtk_window_set_default_size(GTK_WINDOW(ms->win), 550, 150);
178
179         ms->vbox=gtk_vbox_new(FALSE, 3);
180
181         init_main_stat_window(ms->win, ms->vbox, "MEGACO Service Response Time (SRT) Statistics", ms->filter);
182
183         /* init a scrolled window*/
184         ms->scrolled_window = scrolled_window_new(NULL, NULL);
185
186         ms->table = create_stat_table(ms->scrolled_window, ms->vbox, 7, titles);
187
188         error_string=register_tap_listener("megaco", ms, ms->filter, 0, megacostat_reset, megacostat_packet, megacostat_draw);
189         if(error_string){
190                 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "%s", error_string->str);
191                 g_string_free(error_string, TRUE);
192                 g_free(ms->filter);
193                 g_free(ms);
194                 return;
195         }
196
197         /* Button row. */
198         bbox = dlg_button_row_new(GTK_STOCK_CLOSE, NULL);
199         gtk_box_pack_start(GTK_BOX(ms->vbox), bbox, FALSE, FALSE, 0);
200
201         bt_close = g_object_get_data(G_OBJECT(bbox), GTK_STOCK_CLOSE);
202         window_set_cancel_button(ms->win, bt_close, window_cancel_button_cb);
203
204         g_signal_connect(ms->win, "delete_event", G_CALLBACK(window_delete_event_cb), NULL);
205         g_signal_connect(ms->win, "destroy", G_CALLBACK(win_destroy_cb), ms);
206
207         gtk_widget_show_all(ms->win);
208         window_present(ms->win);
209
210         cf_retap_packets(&cfile);
211         gdk_window_raise(ms->win->window);
212 }
213
214 static tap_dfilter_dlg megaco_srt_dlg = {
215         "MEGACO Service Response Time (SRT) Statistics",
216         "megaco,srt",
217         gtk_megacostat_init,
218         -1
219 };
220
221 void
222 register_tap_listener_gtkmegacostat(void)
223 {
224         /* We don't register this tap, if we don't have the megaco plugin loaded.*/
225         if (find_tap_id("megaco")) {
226                 register_dfilter_stat(&megaco_srt_dlg, "MEGACO",
227                     REGISTER_STAT_GROUP_RESPONSE_TIME);
228         }
229 }