-
Notifications
You must be signed in to change notification settings - Fork 23
/
rstats.go
76 lines (63 loc) · 1.83 KB
/
rstats.go
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
package irtt
import (
"encoding/json"
"fmt"
)
// ReceivedStats selects what information to gather about received packets.
type ReceivedStats int
// ReceivedStats constants.
const (
ReceivedStatsNone ReceivedStats = 0x00
ReceivedStatsCount ReceivedStats = 0x01
ReceivedStatsWindow ReceivedStats = 0x02
ReceivedStatsBoth ReceivedStats = ReceivedStatsCount | ReceivedStatsWindow
)
var rss = [...]string{"none", "count", "window", "both"}
func (rs ReceivedStats) String() string {
if int(rs) < 0 || int(rs) >= len(rss) {
return fmt.Sprintf("ReceivedStats:%d", rs)
}
return rss[rs]
}
// ReceivedStatsFromInt returns a ReceivedStats value from its int constant.
func ReceivedStatsFromInt(v int) (ReceivedStats, error) {
if v < int(ReceivedStatsNone) || v > int(ReceivedStatsBoth) {
return ReceivedStatsNone, Errorf(InvalidReceivedStatsInt,
"invalid ReceivedStats int: %d", v)
}
return ReceivedStats(v), nil
}
// MarshalJSON implements the json.Marshaler interface.
func (rs ReceivedStats) MarshalJSON() ([]byte, error) {
return json.Marshal(rs.String())
}
// ParseReceivedStats returns a ReceivedStats value from its string.
func ParseReceivedStats(s string) (ReceivedStats, error) {
for i, v := range rss {
if v == s {
return ReceivedStats(i), nil
}
}
return ReceivedStatsNone, Errorf(InvalidReceivedStatsString,
"invalid ReceivedStats string: %s", s)
}
// Lost indicates the lost status of a packet.
type Lost int
// Lost constants.
const (
LostTrue Lost = iota
LostDown
LostUp
LostFalse
)
var lsts = [...]string{"true", "true_down", "true_up", "false"}
func (l Lost) String() string {
if int(l) < 0 || int(l) >= len(lsts) {
return fmt.Sprintf("Lost:%d", l)
}
return lsts[l]
}
// MarshalJSON implements the json.Marshaler interface.
func (l Lost) MarshalJSON() ([]byte, error) {
return json.Marshal(l.String())
}