forked from gtfierro/wave
-
Notifications
You must be signed in to change notification settings - Fork 2
/
locationschemes.go
80 lines (70 loc) · 2.04 KB
/
locationschemes.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
77
78
79
80
package iapi
import (
"fmt"
"github.com/immesys/asn1"
"github.com/immesys/wave/serdes"
"golang.org/x/crypto/sha3"
)
func LocationSchemeInstanceFor(e *asn1.External) LocationSchemeInstance {
//fmt.Printf("LOC SHEM INSTANCE\n")
//spew.Dump(e)
lsurl, ok := e.Content.(serdes.LocationURL)
if ok {
return &LocationSchemeInstanceURL{
SerdesForm: &lsurl,
}
}
return &UnsupportedLocationSchemeInstance{}
}
var _ LocationSchemeInstance = &UnsupportedLocationSchemeInstance{}
type UnsupportedLocationSchemeInstance struct {
}
func (ls *UnsupportedLocationSchemeInstance) Supported() bool {
return false
}
func (ls *UnsupportedLocationSchemeInstance) Equal(rhs LocationSchemeInstance) bool {
return false
}
func (ls *UnsupportedLocationSchemeInstance) CanonicalForm() *asn1.External {
panic("CanonicalForm called on unsupported location scheme instance")
}
func (ls *UnsupportedLocationSchemeInstance) IdHash() [32]byte {
return [32]byte{}
}
var _ LocationSchemeInstance = &LocationSchemeInstanceURL{}
type LocationSchemeInstanceURL struct {
SerdesForm *serdes.LocationURL
idhash []byte
}
func (ls *LocationSchemeInstanceURL) CanonicalForm() *asn1.External {
ex := asn1.NewExternal(*ls.SerdesForm)
return &ex
}
func (ls *LocationSchemeInstanceURL) Supported() bool {
return true
}
func (ls *LocationSchemeInstanceURL) Equal(rhs LocationSchemeInstance) bool {
rhurl, ok := rhs.(*LocationSchemeInstanceURL)
if !ok {
return false
}
return rhurl.SerdesForm.Value == ls.SerdesForm.Value && rhurl.SerdesForm.Version == ls.SerdesForm.Version
}
func (ls *LocationSchemeInstanceURL) IdHash() [32]byte {
if ls.idhash == nil {
h := sha3.New256()
h.Write([]byte("LocationSchemeInstanceURL"))
h.Write([]byte(fmt.Sprintf("%04d:%s", ls.SerdesForm.Version, ls.SerdesForm.Value)))
ls.idhash = h.Sum(nil)
}
rv := [32]byte{}
copy(rv[:], ls.idhash)
return rv
}
func NewLocationSchemeInstanceURL(url string, version int) LocationSchemeInstance {
sf := &serdes.LocationURL{
Value: url,
Version: version,
}
return &LocationSchemeInstanceURL{SerdesForm: sf}
}