-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsielink.go
92 lines (80 loc) · 2.42 KB
/
sielink.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
/*
* Copyright (c) 2017 by Farsight Security, Inc.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package sielink
import (
"fmt"
"github.com/golang/protobuf/proto"
)
// ProtocolVersion is the version of the protocol implemented
// in this version of the package.
var ProtocolVersion uint32 = 1
// SupportedVersions lists the protocol versions this version of
// the package can interoperate with.
var SupportedVersions = []uint32{ProtocolVersion}
//go:generate protoc --go_out=. sielink.proto
// A Link is the basic interface to a collection of sielink connections.
type Link interface {
// Send sends a payload on the Link, blocking if there is no available
// connection, returning an error if the Link is closing.
Send(*Payload) error
// Receive returns a channel from which the caller can read the next
// payload received on any connection. If all connections have announced
// their intention to cease sending, this channel is closed.
Receive() <-chan *Payload
// Close closes all connections involved in the Link
Close() error
}
// The Error() method allows an Alert to be returned and handled as an error.
func (a *Alert) Error() string {
return fmt.Sprintf("Remote host reported %s: %s",
a.GetLevel().String(), a.GetMessage())
}
// RecordLinkLoss resets the LinkLoss counters after adding their values
// to the PathLoss counters.
func (p *Payload) RecordLinkLoss() {
pl := p.GetPathLoss()
if pl == nil {
return
}
ll := p.GetLinkLoss()
if ll == nil {
p.LinkLoss = pl
p.PathLoss = nil
return
}
*p.PathLoss.Bytes += ll.GetBytes()
*p.PathLoss.Payloads += ll.GetPayloads()
p.LinkLoss.Reset()
}
// RecordDiscard updates the link Loss counters to record the
// discarding of the supplied payload.
func (p *Payload) RecordDiscard(disc *Payload) {
if p.LinkLoss == nil {
p.LinkLoss = &LossCounter{
Bytes: proto.Uint64(uint64(len(disc.Data))),
Payloads: proto.Uint64(1),
}
return
}
*p.LinkLoss.Bytes += uint64(len(disc.Data))
*p.LinkLoss.Payloads++
}
// GetDestination returns the destination site of the Path.
func (p *Path) GetDestination() uint32 {
if p.Site != nil {
return p.Site[0]
}
return 0
}
// GetNexthop returns the Next site on the path.
func (p *Path) GetNexthop() uint32 {
if p.Site != nil {
return p.Site[len(p.Site)-1]
}
return 0
}