summaryrefslogtreecommitdiff
path: root/src/librygel-core/rygel-basic-management-test-ping.vala
blob: 46d6ab931521e44defca234a00d3e340efdac224 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
 * Copyright (C) 2013 Intel Corporation.
 *
 * Author: Christophe Guiraud,
 *         Jussi Kukkonen
 *
 * This file is part of Rygel.
 *
 * Rygel is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * Rygel is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

using GLib;

// Helper class for BasicManagementTestPing.
internal class Rygel.BasicManagementTestPing : BasicManagementTest {
    private const uint MAX_REPEAT_COUNT = 100;
    private const uint DEFAULT_REPEAT_COUNT = 1;
    private const uint DEFAULT_REPLY_TIMEOUT = 10000;
    private const uint MIN_REQUEST_INTERVAL_TIMEOUT = 1000;
    private const uint MAX_REQUEST_INTERVAL_TIMEOUT = 30000;
    private const uint DEFAULT_REQUEST_INTERVAL_TIMEOUT = 1000;
    private const uint MIN_DATA_BLOCK_SIZE = 20;
    private const uint MAX_DATA_BLOCK_SIZE = 2048;
    private const uint DEFAULT_DATA_BLOCK_SIZE = 32;
    private const uint MAX_DSCP = 64;
    private const uint DEFAULT_DSCP = 30;

    private enum ProcessState {
        INIT,
        STATISTICS,
        RTT,
    }

    private enum Status {
        SUCCESS,
        ERROR_CANNOT_RESOLVE_HOSTNAME,
        ERROR_INTERNAL,
        ERROR_OTHER;

        public string to_string () {
            switch (this) {
                case SUCCESS:
                    return "Success";
                case ERROR_CANNOT_RESOLVE_HOSTNAME:
                    return "Error_CannotResolveHostName";
                case ERROR_INTERNAL:
                    return "Error_Internal";
                case ERROR_OTHER:
                    return "Error_Other";
                default:
                    assert_not_reached ();
            }
        }
    }

    public string host { construct; get; default = ""; }

    private uint _repeat_count = DEFAULT_REPEAT_COUNT;
    public uint repeat_count {
        construct {
            this._repeat_count = value;
            if (this._repeat_count == 0) {
                this._repeat_count = DEFAULT_REPEAT_COUNT;
            }
        }

        get {
            return this._repeat_count;
        }
    }

    private uint _data_block_size = DEFAULT_DATA_BLOCK_SIZE;
    public uint data_block_size {
        construct {
            this._data_block_size = value;
            if (this._data_block_size == 0) {
                this._data_block_size = DEFAULT_DATA_BLOCK_SIZE;
            }
        }

        get {
            return this._data_block_size;
        }
    }

    private uint _dscp = DEFAULT_DSCP;
    public uint dscp {
        construct {
            this._dscp = value;
            if (this._dscp == 0) {
                this._dscp = DEFAULT_DSCP;
            }
        }

        get {
            return this._dscp;
        }
    }

    private uint32 _interval_time_out = DEFAULT_REQUEST_INTERVAL_TIMEOUT;
    public uint32 interval_time_out {
        construct {
            this._interval_time_out = value;
            if (this._interval_time_out == 0) {
                this._interval_time_out = DEFAULT_REQUEST_INTERVAL_TIMEOUT;
            }
        }

        get {
            return _interval_time_out;
        }
    }


    private ProcessState state;
    private Status status;
    private string additional_info;
    private uint success_count;
    private uint failure_count;
    private uint32 avg_response_time;
    private uint32 min_response_time;
    private uint32 max_response_time;

    public override string method_type {
        get {
            return "Ping";
        }
    }

    public override string results_type {
        get {
            return "GetPingResult";
        }
    }

    public BasicManagementTestPing (string host,
                                    uint repeat_count,
                                    uint32 interval_time_out,
                                    uint data_block_size,
                                    uint dscp) {
        Object (host: host,
                repeat_count: repeat_count,
                interval_time_out: interval_time_out,
                data_block_size: data_block_size,
                dscp: dscp);
    }

    public override void constructed () {
        base.constructed ();

        this.status = Status.ERROR_INTERNAL;
        this.state = ProcessState.INIT;
        this.additional_info = "";
        this.success_count = 0;
        this.failure_count = 0;
        this.avg_response_time = 0;
        this.min_response_time = 0;
        this.max_response_time = 0;

        this.command = { "ping",
                         "-c", this.repeat_count.to_string (),
                         "-W", (DEFAULT_REPLY_TIMEOUT / 1000).to_string (),
                         "-i", (this.interval_time_out / 1000).to_string (),
                         "-s", this.data_block_size.to_string (),
                         "-Q", (this.dscp >> 2).to_string (),
                         this.host };

        if (this.repeat_count > MAX_REPEAT_COUNT) {
            this.init_state = InitState.INVALID_PARAMETER;
            this.status = Status.ERROR_OTHER;
            var msg = "NumberOfRepetitions %u is not in allowed range [0, %u]";
            this.additional_info = msg.printf (this.repeat_count,
                                               MAX_REPEAT_COUNT);

        } else if (this.interval_time_out < MIN_REQUEST_INTERVAL_TIMEOUT ||
                   this.interval_time_out > MAX_REQUEST_INTERVAL_TIMEOUT) {
            this.init_state = InitState.INVALID_PARAMETER;
            this.status = Status.ERROR_OTHER;
            var msg = "Timeout %u is not in allowed range [%u, %u]";
            this.additional_info = msg.printf (this.interval_time_out,
                                               MIN_REQUEST_INTERVAL_TIMEOUT,
                                               MAX_REQUEST_INTERVAL_TIMEOUT);

        } else if (this.data_block_size < MIN_DATA_BLOCK_SIZE ||
                   this.data_block_size > MAX_DATA_BLOCK_SIZE) {
            this.init_state = InitState.INVALID_PARAMETER;
            this.status = Status.ERROR_OTHER;
            var msg = "DataBlockSize %u is not in allowed range [%u, %u]";
            this.additional_info = msg.printf (this.data_block_size,
                                               MIN_DATA_BLOCK_SIZE,
                                               MAX_DATA_BLOCK_SIZE);
        } else if (this.dscp > MAX_DSCP) {
            this.init_state = InitState.INVALID_PARAMETER;
            this.status = Status.ERROR_OTHER;
            var msg = "DSCP %u is not in allowed range [0, %u]";
            this.additional_info = msg.printf (this.dscp, MAX_DSCP);
        }
    }

    protected override bool finish_iteration () {
        if (this.init_state == InitState.SPAWN_FAILED) {
            this.status = Status.ERROR_INTERNAL;
            this.additional_info = "Failed to spawn ping";
        }

        return base.finish_iteration ();
    }

    protected override void handle_error (string line) {
        if (line.contains ("ping: unknown host")) {
            this.status = Status.ERROR_CANNOT_RESOLVE_HOSTNAME;
        } else if (line.contains ("ping:")) {
            this.status = Status.ERROR_OTHER;
            this.additional_info = line.substring ("ping:".length).strip ();
        }
    }

    protected override void handle_output (string line) {
        line.strip ();
        if (this.state == ProcessState.INIT) {
            if (line.contains ("statistics ---")) {
                this.state = ProcessState.STATISTICS;
                this.status = Status.SUCCESS;
            }
        } else if (this.state == ProcessState.STATISTICS) {
            if (line.contains ("packets transmitted")) {
                this.state = ProcessState.RTT;

                var rtt_values = line.split (", ", 3);
                uint tx = int.parse (rtt_values[0].split (" ", 3)[0]);
                uint rx = int.parse (rtt_values[1].split (" ", 3)[0]);
                this.failure_count = tx - rx;
                this.success_count = rx;
            }
        } else if (this.state == ProcessState.RTT) {
            if (line.contains ("min/avg/max")) {
                var rtt = line.split ("=", 2);
                if (rtt.length >= 2) {
                    var rtt_values = rtt[1].split ("/", 4);
                    if (rtt_values.length >= 3) {
                        this.min_response_time = (uint) Math.round
                                        (double.parse (rtt_values[0]));
                        this.avg_response_time = (uint) Math.round
                                        (double.parse (rtt_values[1]));
                        this.max_response_time = (uint) Math.round
                                        (double.parse (rtt_values[2]));
                    }
                }
            }
        }
    }

    public void get_results (out string status,
                             out string additional_info,
                             out uint success_count,
                             out uint failure_count,
                             out uint32 avg_response_time,
                             out uint32 min_response_time,
                             out uint32 max_response_time) {
        status = this.status.to_string ();
        additional_info = this.additional_info;
        success_count = this.success_count;
        failure_count = this.failure_count;
        avg_response_time = this.avg_response_time;
        min_response_time = this.min_response_time;
        max_response_time = this.max_response_time;
    }
}