forked from scionproto/scion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
path_mgmt.go
123 lines (109 loc) · 3.1 KB
/
path_mgmt.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Copyright 2017 ETH Zurich
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package path_mgmt
import (
"fmt"
"strings"
//log "github.com/inconshreveable/log15"
"github.com/scionproto/scion/go/lib/common"
"github.com/scionproto/scion/go/proto"
)
// union represents the contents of the unnamed capnp union.
type union struct {
Which proto.PathMgmt_Which
SegReq *SegReq
SegReply *SegReply
SegReg *SegReg
SegSync *SegSync
RevInfo *RevInfo
IFStateReq *IFStateReq `capnp:"ifStateReq"`
IFStateInfos *IFStateInfos `capnp:"ifStateInfos"`
}
func (u *union) set(c proto.Cerealizable) error {
switch p := c.(type) {
case *SegReq:
u.Which = proto.PathMgmt_Which_segReq
u.SegReq = p
case *SegReply:
u.Which = proto.PathMgmt_Which_segReply
u.SegReply = p
case *SegReg:
u.Which = proto.PathMgmt_Which_segReg
u.SegReg = p
case *SegSync:
u.Which = proto.PathMgmt_Which_segSync
u.SegSync = p
case *RevInfo:
u.Which = proto.PathMgmt_Which_revInfo
u.RevInfo = p
case *IFStateReq:
u.Which = proto.PathMgmt_Which_ifStateReq
u.IFStateReq = p
case *IFStateInfos:
u.Which = proto.PathMgmt_Which_ifStateInfos
u.IFStateInfos = p
default:
return common.NewBasicError("Unsupported path mgmt union type (set)", nil,
"type", common.TypeOf(c))
}
return nil
}
func (u *union) get() (proto.Cerealizable, error) {
switch u.Which {
case proto.PathMgmt_Which_segReq:
return u.SegReq, nil
case proto.PathMgmt_Which_segReply:
return u.SegReply, nil
case proto.PathMgmt_Which_segReg:
return u.SegReg, nil
case proto.PathMgmt_Which_segSync:
return u.SegSync, nil
case proto.PathMgmt_Which_revInfo:
return u.RevInfo, nil
case proto.PathMgmt_Which_ifStateReq:
return u.IFStateReq, nil
case proto.PathMgmt_Which_ifStateInfos:
return u.IFStateInfos, nil
}
return nil, common.NewBasicError("Unsupported path mgmt union type (get)", nil, "type", u.Which)
}
var _ proto.Cerealizable = (*Pld)(nil)
type Pld struct {
union
*Data
}
// NewPld creates a new path mgmt payload, containing the supplied Cerealizable instance.
func NewPld(u proto.Cerealizable, d *Data) (*Pld, error) {
p := &Pld{Data: d}
return p, p.union.set(u)
}
func (p *Pld) Union() (proto.Cerealizable, error) {
return p.union.get()
}
func (p *Pld) ProtoId() proto.ProtoIdType {
return proto.PathMgmt_TypeID
}
func (p *Pld) String() string {
desc := []string{"PathMgmt: Union:"}
u, err := p.Union()
if err != nil {
desc = append(desc, err.Error())
} else {
desc = append(desc, fmt.Sprintf("%+v", u))
}
return strings.Join(desc, " ")
}
type Data struct {
// For passing any future non-union data.
}