Skip to content

Commit edb23bc

Browse files
committed
connect: update transfer path logic
1 parent c854b5c commit edb23bc

2 files changed

Lines changed: 72 additions & 50 deletions

File tree

connect.go

Lines changed: 57 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ const MaxMultihopLength = 8
2020
// v2: 2025-05-28 to optimize memory usage. Breaks compatibility with v1
2121
//
2222
// Most clients need to be able to read v2 before we turn this on.
23-
//
24-
// TODO migrate to 2
2523
const DefaultProtocolVersion = 2
2624

2725
// id for message to/from the platform
@@ -31,13 +29,17 @@ var ControlId = Id{}
3129
// TODO SourceTransferPath, DestinationTransferPath
3230
// TODO this would avoid the need to check the "masks"
3331

34-
// there are three types of transfer paths:
35-
// 1. a full path, which can have either source id and destination id, or stream id
36-
// 2. a source, which can have either source id or stream id.
32+
// there are four types of transfer paths:
33+
// 1. a full path, which will have source id, destination id, and optional stream id
34+
// 2. a full path without stream, which will have source id and/or destination id, but no stream id.
35+
// This is called the "local mask".
36+
// 3. a source, which will have source id and optional stream id.
3737
// This is called the "source mask".
38-
// 3. a destination, which can have either destination id or stream id.
38+
// 4. a destination, which will have destination id and optional stream id.
3939
// This is called the "destination mask".
40-
//
40+
// Normally a local mask should be stored in the protobuf message transfer path,
41+
// and the destination mask should be used to match routes.
42+
4143
// comparable
4244
type TransferPath struct {
4345
SourceId Id
@@ -64,11 +66,9 @@ func StreamId(streamId Id) TransferPath {
6466
}
6567

6668
func NewTransferPath(sourceId Id, destinationId Id, streamId Id) (path TransferPath) {
69+
path.SourceId = sourceId
70+
path.DestinationId = destinationId
6771
path.StreamId = streamId
68-
if (path.StreamId == Id{}) {
69-
path.SourceId = sourceId
70-
path.DestinationId = destinationId
71-
}
7272
return
7373
}
7474

@@ -87,55 +87,49 @@ func TransferPathFromBytes(
8787
destinationIdBytes []byte,
8888
streamIdBytes []byte,
8989
) (path TransferPath, err error) {
90-
if streamIdBytes != nil {
91-
path.StreamId, err = IdFromBytes(streamIdBytes)
90+
if sourceIdBytes != nil {
91+
path.SourceId, err = IdFromBytes(sourceIdBytes)
9292
if err != nil {
9393
return
9494
}
9595
}
96-
if (path.StreamId == Id{}) {
97-
if sourceIdBytes != nil {
98-
path.SourceId, err = IdFromBytes(sourceIdBytes)
99-
if err != nil {
100-
return
101-
}
96+
if destinationIdBytes != nil {
97+
path.DestinationId, err = IdFromBytes(destinationIdBytes)
98+
if err != nil {
99+
return
102100
}
103-
if destinationIdBytes != nil {
104-
path.DestinationId, err = IdFromBytes(destinationIdBytes)
105-
if err != nil {
106-
return
107-
}
101+
}
102+
if streamIdBytes != nil {
103+
path.StreamId, err = IdFromBytes(streamIdBytes)
104+
if err != nil {
105+
return
108106
}
109107
}
110108
return
111109
}
112110

113111
func (self TransferPath) IsControlSource() bool {
114-
return self.IsSourceMask() && !self.IsStream() && self.SourceId == ControlId
112+
return self.SourceId == ControlId && (self.DestinationId == Id{}) && (self.StreamId == Id{})
115113
}
116114

117115
func (self TransferPath) IsControlDestination() bool {
118-
return self.IsDestinationMask() && !self.IsStream() && self.DestinationId == ControlId
116+
return self.DestinationId == ControlId && (self.SourceId == Id{}) && (self.StreamId == Id{})
119117
}
120118

121119
func (self TransferPath) IsStream() bool {
122120
return self.StreamId != Id{}
123121
}
124122

125123
func (self TransferPath) IsSourceMask() bool {
126-
if self.IsStream() {
127-
return self.SourceId == Id{} && self.DestinationId == Id{}
128-
} else {
129-
return self.DestinationId == Id{}
130-
}
124+
return self.DestinationId == Id{}
131125
}
132126

133127
func (self TransferPath) IsDestinationMask() bool {
134-
if self.IsStream() {
135-
return self.SourceId == Id{} && self.DestinationId == Id{}
136-
} else {
137-
return self.SourceId == Id{}
138-
}
128+
return self.SourceId == Id{}
129+
}
130+
131+
func (self TransferPath) IsLocalMask() bool {
132+
return self.StreamId == Id{}
139133
}
140134

141135
func (self TransferPath) SourceMask() TransferPath {
@@ -152,6 +146,13 @@ func (self TransferPath) DestinationMask() TransferPath {
152146
}
153147
}
154148

149+
func (self TransferPath) LocalMask() TransferPath {
150+
return TransferPath{
151+
SourceId: self.SourceId,
152+
DestinationId: self.DestinationId,
153+
}
154+
}
155+
155156
func (self TransferPath) Reverse() TransferPath {
156157
return TransferPath{
157158
SourceId: self.DestinationId,
@@ -161,41 +162,50 @@ func (self TransferPath) Reverse() TransferPath {
161162
}
162163

163164
func (self TransferPath) AddSource(sourceId Id) TransferPath {
164-
if self.IsStream() {
165-
return self
166-
}
167165
return TransferPath{
168166
SourceId: sourceId,
169167
DestinationId: self.DestinationId,
168+
StreamId: self.StreamId,
170169
}
171170
}
172171

173172
func (self TransferPath) AddDestination(destinationId Id) TransferPath {
174-
if self.IsStream() {
175-
return self
176-
}
177173
return TransferPath{
178174
SourceId: self.SourceId,
179175
DestinationId: destinationId,
176+
StreamId: self.StreamId,
180177
}
181178
}
182179

183180
func (self TransferPath) String() string {
181+
var spart string
182+
var dpart string
184183
if (self.StreamId != Id{}) {
185-
return fmt.Sprintf("s(%s)", self.StreamId)
184+
spart = fmt.Sprintf("s(%s)", self.StreamId)
185+
}
186+
if (self.SourceId != Id{}) || (self.DestinationId != Id{}) {
187+
dpart = fmt.Sprintf("%s->%s", self.SourceId, self.DestinationId)
188+
}
189+
if spart != "" && dpart != "" {
190+
return fmt.Sprintf("%s %s", spart, dpart)
191+
} else if spart != "" {
192+
return spart
186193
} else {
187-
return fmt.Sprintf("%s->%s", self.SourceId, self.DestinationId)
194+
return dpart
188195
}
189196
}
190197

191198
func (self TransferPath) ToProtobuf() *protocol.TransferPath {
192199
protoTransferPath := &protocol.TransferPath{}
193-
if self.IsStream() {
194-
protoTransferPath.StreamId = self.StreamId.Bytes()
195-
} else {
200+
if (self.SourceId != Id{}) {
196201
protoTransferPath.SourceId = self.SourceId.Bytes()
202+
}
203+
if (self.DestinationId != Id{}) {
197204
protoTransferPath.DestinationId = self.DestinationId.Bytes()
198205
}
206+
if (self.StreamId != Id{}) {
207+
protoTransferPath.StreamId = self.StreamId.Bytes()
208+
}
199209
return protoTransferPath
200210
}
201211

connect_test.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ func TestTransferPath(t *testing.T) {
8686
assert.Equal(t, path.IsStream(), false)
8787
assert.Equal(t, path.IsSourceMask(), true)
8888
assert.Equal(t, path.IsDestinationMask(), true)
89+
assert.Equal(t, path.IsLocalMask(), true)
8990
assert.Equal(t, path.IsControlSource(), true)
9091
assert.Equal(t, path.IsControlDestination(), true)
9192

@@ -95,6 +96,7 @@ func TestTransferPath(t *testing.T) {
9596
assert.Equal(t, path.IsStream(), false)
9697
assert.Equal(t, path.IsSourceMask(), true)
9798
assert.Equal(t, path.IsDestinationMask(), false)
99+
assert.Equal(t, path.IsLocalMask(), true)
98100
assert.Equal(t, path.IsControlSource(), false)
99101
assert.Equal(t, path.IsControlDestination(), false)
100102

@@ -103,6 +105,7 @@ func TestTransferPath(t *testing.T) {
103105
assert.Equal(t, path.IsStream(), false)
104106
assert.Equal(t, path.IsSourceMask(), true)
105107
assert.Equal(t, path.IsDestinationMask(), false)
108+
assert.Equal(t, path.IsLocalMask(), true)
106109
assert.Equal(t, path.IsControlSource(), false)
107110
assert.Equal(t, path.IsControlDestination(), false)
108111

@@ -112,6 +115,7 @@ func TestTransferPath(t *testing.T) {
112115
assert.Equal(t, path.IsStream(), false)
113116
assert.Equal(t, path.IsSourceMask(), false)
114117
assert.Equal(t, path.IsDestinationMask(), false)
118+
assert.Equal(t, path.IsLocalMask(), true)
115119
assert.Equal(t, path.IsControlSource(), false)
116120
assert.Equal(t, path.IsControlDestination(), false)
117121

@@ -121,6 +125,7 @@ func TestTransferPath(t *testing.T) {
121125
assert.Equal(t, path.IsStream(), false)
122126
assert.Equal(t, path.IsSourceMask(), false)
123127
assert.Equal(t, path.IsDestinationMask(), true)
128+
assert.Equal(t, path.IsLocalMask(), true)
124129
assert.Equal(t, path.IsControlSource(), false)
125130
assert.Equal(t, path.IsControlDestination(), false)
126131

@@ -129,15 +134,17 @@ func TestTransferPath(t *testing.T) {
129134
assert.Equal(t, path.IsStream(), false)
130135
assert.Equal(t, path.IsSourceMask(), false)
131136
assert.Equal(t, path.IsDestinationMask(), true)
137+
assert.Equal(t, path.IsLocalMask(), true)
132138
assert.Equal(t, path.IsControlSource(), false)
133139
assert.Equal(t, path.IsControlDestination(), false)
134140

135141
path, err = TransferPathFromBytes(a.Bytes(), b.Bytes(), c.Bytes())
136142
assert.Equal(t, err, nil)
137-
assert.Equal(t, path, TransferPath{StreamId: c})
143+
assert.NotEqual(t, path, TransferPath{StreamId: c})
138144
assert.Equal(t, path.IsStream(), true)
139-
assert.Equal(t, path.IsSourceMask(), true)
140-
assert.Equal(t, path.IsDestinationMask(), true)
145+
assert.Equal(t, path.IsSourceMask(), false)
146+
assert.Equal(t, path.IsDestinationMask(), false)
147+
assert.Equal(t, path.IsLocalMask(), false)
141148
assert.Equal(t, path.IsControlSource(), false)
142149
assert.Equal(t, path.IsControlDestination(), false)
143150

@@ -147,6 +154,7 @@ func TestTransferPath(t *testing.T) {
147154
assert.Equal(t, path.IsStream(), true)
148155
assert.Equal(t, path.IsSourceMask(), true)
149156
assert.Equal(t, path.IsDestinationMask(), true)
157+
assert.Equal(t, path.IsLocalMask(), false)
150158
assert.Equal(t, path.IsControlSource(), false)
151159
assert.Equal(t, path.IsControlDestination(), false)
152160

@@ -155,6 +163,7 @@ func TestTransferPath(t *testing.T) {
155163
assert.Equal(t, path.IsStream(), true)
156164
assert.Equal(t, path.IsSourceMask(), true)
157165
assert.Equal(t, path.IsDestinationMask(), true)
166+
assert.Equal(t, path.IsLocalMask(), false)
158167
assert.Equal(t, path.IsControlSource(), false)
159168
assert.Equal(t, path.IsControlDestination(), false)
160169

@@ -163,12 +172,15 @@ func TestTransferPath(t *testing.T) {
163172
path = NewTransferPath(a, b, Id{})
164173
assert.Equal(t, path.IsSourceMask(), false)
165174
assert.Equal(t, path.IsDestinationMask(), false)
175+
assert.Equal(t, path.IsLocalMask(), true)
166176
s := path.SourceMask()
167177
assert.Equal(t, s.IsSourceMask(), true)
168178
assert.Equal(t, s.IsDestinationMask(), false)
179+
assert.Equal(t, s.IsLocalMask(), true)
169180
d := path.DestinationMask()
170181
assert.Equal(t, d.IsSourceMask(), false)
171182
assert.Equal(t, d.IsDestinationMask(), true)
183+
assert.Equal(t, d.IsLocalMask(), true)
172184

173185
assert.Equal(t, path.Reverse(), TransferPath{SourceId: b, DestinationId: a})
174186
}

0 commit comments

Comments
 (0)