/
update.go
105 lines (88 loc) · 2.64 KB
/
update.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
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
// Copyright 2016 The Vanadium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package discovery
import (
"fmt"
"time"
"v.io/v23/context"
"v.io/v23/discovery"
)
// update is an implementation of discovery.Update.
type update struct {
ad discovery.Advertisement
hash AdHash
dirAddrs []string
status AdStatus
lost bool
timestamp time.Time
}
func (u *update) IsLost() bool { return u.lost }
//nolint:golint // API change required.
func (u *update) Id() discovery.AdId { return u.ad.Id }
func (u *update) InterfaceName() string { return u.ad.InterfaceName }
func (u *update) Addresses() []string {
addrs := make([]string, len(u.ad.Addresses))
copy(addrs, u.ad.Addresses)
return addrs
}
func (u *update) Attribute(name string) string { return u.ad.Attributes[name] }
func (u *update) Attachment(ctx *context.T, name string) <-chan discovery.DataOrError {
ch := make(chan discovery.DataOrError, 1)
if data, ok := u.ad.Attachments[name]; ok {
var r discovery.DataOrError
r.Data = make([]byte, len(data))
copy(r.Data, data)
ch <- r
close(ch)
return ch
}
if u.status == AdPartiallyReady {
go u.fetchAttachment(ctx, name, ch)
} else {
close(ch)
}
return ch
}
func (u *update) fetchAttachment(ctx *context.T, name string, ch chan<- discovery.DataOrError) {
defer close(ch)
dir := newDirClient(u.dirAddrs)
data, err := dir.GetAttachment(ctx, u.ad.Id, name)
ch <- discovery.DataOrError{Data: data, Error: err}
}
func (u *update) Advertisement() discovery.Advertisement {
ad := discovery.Advertisement{
Id: u.ad.Id,
InterfaceName: u.ad.InterfaceName,
}
ad.Addresses = make([]string, len(u.ad.Addresses))
copy(ad.Addresses, u.ad.Addresses)
ad.Attributes = make(discovery.Attributes, len(u.ad.Attributes))
for k, v := range u.ad.Attributes {
ad.Attributes[k] = v
}
ad.Attachments = make(discovery.Attachments, len(u.ad.Attachments))
for k, v := range u.ad.Attachments {
data := make([]byte, len(v))
copy(data, v)
ad.Attachments[k] = data
}
return ad
}
func (u *update) Timestamp() time.Time {
return u.timestamp
}
func (u *update) String() string {
return fmt.Sprintf("{%v %s %s %v %v}", u.lost, u.ad.Id, u.ad.InterfaceName, u.ad.Addresses, u.ad.Attributes)
}
// NewUpdate returns a new update with the given advertisement information.
func NewUpdate(adinfo *AdInfo) discovery.Update {
return &update{
ad: adinfo.Ad,
hash: adinfo.Hash,
dirAddrs: adinfo.DirAddrs,
status: adinfo.Status,
lost: adinfo.Lost,
timestamp: time.Unix(adinfo.TimestampNs/1e6, adinfo.TimestampNs%1e6),
}
}