forked from posteo/go-agentx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist_handler.go
71 lines (57 loc) · 1.91 KB
/
list_handler.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
// Copyright 2018 The agentx authors
// Licensed under the LGPLv3 with static-linking exception.
// See LICENCE file for details.
package agentx
import (
"bytes"
"sort"
"github.com/posteo/go-agentx/pdu"
"github.com/posteo/go-agentx/value"
)
// ListHandler is a helper that takes a list of oids and implements
// a default behaviour for that list.
type ListHandler struct {
oids sort.StringSlice
items map[string]*ListItem
}
// Add adds a list item for the provided oid and returns it.
func (l *ListHandler) Add(oid string) *ListItem {
if l.items == nil {
l.items = make(map[string]*ListItem)
}
l.oids = append(l.oids, oid)
l.oids.Sort()
item := &ListItem{}
l.items[oid] = item
return item
}
// Get tries to find the provided oid and returns the corresponding value.
func (l *ListHandler) Get(oid value.OID) (value.OID, pdu.VariableType, interface{}, error) {
if l.items == nil {
return nil, pdu.VariableTypeNoSuchObject, nil, nil
}
item, ok := l.items[oid.String()]
if ok {
return oid, item.Type, item.Value, nil
}
return nil, pdu.VariableTypeNoSuchObject, nil, nil
}
// GetNext tries to find the value that follows the provided oid and returns it.
func (l *ListHandler) GetNext(from value.OID, includeFrom bool, to value.OID) (value.OID, pdu.VariableType, interface{}, error) {
if l.items == nil {
return nil, pdu.VariableTypeNoSuchObject, nil, nil
}
fromOID, toOID := from.String(), to.String()
for _, oid := range l.oids {
if oidWithin(oid, fromOID, includeFrom, toOID) {
return l.Get(value.MustParseOID(oid))
}
}
return nil, pdu.VariableTypeNoSuchObject, nil, nil
}
func oidWithin(oid string, from string, includeFrom bool, to string) bool {
oidBytes, fromBytes, toBytes := []byte(oid), []byte(from), []byte(to)
fromCompare := bytes.Compare(fromBytes, oidBytes)
toCompare := bytes.Compare(toBytes, oidBytes)
return (fromCompare == -1 || (fromCompare == 0 && includeFrom)) && (toCompare == 1)
}