Skip to content

Commit

Permalink
Rename app.Packet's Addr -> Loop
Browse files Browse the repository at this point in the history
  • Loading branch information
Darkren committed Jul 8, 2019
1 parent ebe92f3 commit 106b36c
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 29 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ require (
go.etcd.io/bbolt v1.3.3
golang.org/x/crypto v0.0.0-20190621222207-cc06ce4a13d4
golang.org/x/net v0.0.0-20190620200207-3b0461eec859
golang.org/x/text v0.3.2 // indirect
)

// Uncomment for tests with alternate branches of 'dmsg'
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,13 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACk
golang.org/x/crypto v0.0.0-20190621222207-cc06ce4a13d4 h1:ydJNl0ENAG67pFbB+9tfhiL2pYqLhfoaZFw/cjLhY4A=
golang.org/x/crypto v0.0.0-20190621222207-cc06ce4a13d4/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859 h1:R/3boaszxrf1GEUWTVDzSKVwLmSJpwZ1yqXm8j0v2QI=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20181205085412-a5c9d58dba9a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
Expand All @@ -135,6 +137,7 @@ golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190627182818-9947fec5c3ab/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
gopkg.in/alecthomas/kingpin.v2 v2.2.6 h1:jMFz6MfLP0/4fUyZle81rXUoxOBFi19VUFKVDOQfozc=
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
Expand Down
12 changes: 6 additions & 6 deletions pkg/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ func (app *App) handleProto() {
}
}

func (app *App) serveConn(addr *routing.Loop, conn io.ReadWriteCloser) {
func (app *App) serveConn(loop *routing.Loop, conn io.ReadWriteCloser) {
defer conn.Close()

for {
Expand All @@ -181,17 +181,17 @@ func (app *App) serveConn(addr *routing.Loop, conn io.ReadWriteCloser) {
break
}

packet := &Packet{Addr: addr, Payload: buf[:n]}
packet := &Packet{Loop: loop, Payload: buf[:n]}
if err := app.proto.Send(FrameSend, packet, nil); err != nil {
break
}
}

app.mu.Lock()
if _, ok := app.conns[*addr]; ok {
app.proto.Send(FrameClose, &addr, nil) // nolint: errcheck
if _, ok := app.conns[*loop]; ok {
app.proto.Send(FrameClose, &loop, nil) // nolint: errcheck
}
delete(app.conns, *addr)
delete(app.conns, *loop)
app.mu.Unlock()
}

Expand All @@ -202,7 +202,7 @@ func (app *App) forwardPacket(data []byte) error {
}

app.mu.Lock()
conn := app.conns[*packet.Addr]
conn := app.conns[*packet.Loop]
app.mu.Unlock()

if conn == nil {
Expand Down
8 changes: 4 additions & 4 deletions pkg/app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,10 @@ func TestAppWrite(t *testing.T) {

packet := &Packet{}
require.NoError(t, json.Unmarshal(<-dataCh, packet))
assert.Equal(t, rpk, packet.Addr.Remote.PubKey)
assert.Equal(t, routing.Port(3), packet.Addr.Remote.Port)
assert.Equal(t, routing.Port(2), packet.Addr.Local.Port)
assert.Equal(t, lpk, packet.Addr.Local.PubKey)
assert.Equal(t, rpk, packet.Loop.Remote.PubKey)
assert.Equal(t, routing.Port(3), packet.Loop.Remote.Port)
assert.Equal(t, routing.Port(2), packet.Loop.Local.Port)
assert.Equal(t, lpk, packet.Loop.Local.PubKey)
assert.Equal(t, []byte("foo"), packet.Payload)

require.NoError(t, proto.Close())
Expand Down
2 changes: 1 addition & 1 deletion pkg/app/packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ import "github.com/skycoin/skywire/pkg/routing"

// Packet represents message exchanged between App and Node.
type Packet struct {
Addr *routing.Loop `json:"addr"`
Loop *routing.Loop `json:"loop"`
Payload []byte `json:"payload"`
}
2 changes: 1 addition & 1 deletion pkg/router/app_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func TestAppManagerForward(t *testing.T) {

lpk, _ := cipher.GenerateKeyPair()
rpk, _ := cipher.GenerateKeyPair()
packet := &app.Packet{Payload: []byte("foo"), Addr: &routing.Loop{Local: routing.Addr{PubKey: lpk, Port: 2}, Remote: routing.Addr{PubKey: rpk, Port: 3}}}
packet := &app.Packet{Payload: []byte("foo"), Loop: &routing.Loop{Local: routing.Addr{PubKey: lpk, Port: 2}, Remote: routing.Addr{PubKey: rpk, Port: 3}}}
err := proto.Send(app.FrameSend, packet, nil)
require.NoError(t, err)
assert.Equal(t, packet, inPacket)
Expand Down
16 changes: 8 additions & 8 deletions pkg/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ func (r *Router) forwardPacket(payload []byte, rule routing.Rule) error {
func (r *Router) consumePacket(payload []byte, rule routing.Rule) error {
raddr := &routing.Addr{PubKey: rule.RemotePK(), Port: rule.RemotePort()}

p := &app.Packet{Addr: &routing.Loop{Local: routing.Addr{Port: rule.LocalPort()}, Remote: *raddr}, Payload: payload}
p := &app.Packet{Loop: &routing.Loop{Local: routing.Addr{Port: rule.LocalPort()}, Remote: *raddr}, Payload: payload}
b, _ := r.pm.Get(rule.LocalPort()) // nolint: errcheck
if err := b.conn.Send(app.FrameSend, p, nil); err != nil {
return err
Expand All @@ -230,11 +230,11 @@ func (r *Router) consumePacket(payload []byte, rule routing.Rule) error {
}

func (r *Router) forwardAppPacket(appConn *app.Protocol, packet *app.Packet) error {
if packet.Addr.Remote.PubKey == r.config.PubKey {
if packet.Loop.Remote.PubKey == r.config.PubKey {
return r.forwardLocalAppPacket(packet)
}

l, err := r.pm.GetLoop(packet.Addr.Local.Port, &packet.Addr.Remote)
l, err := r.pm.GetLoop(packet.Loop.Local.Port, &packet.Loop.Remote)
if err != nil {
return err
}
Expand All @@ -245,21 +245,21 @@ func (r *Router) forwardAppPacket(appConn *app.Protocol, packet *app.Packet) err
}

p := routing.MakePacket(l.routeID, packet.Payload)
r.Logger.Infof("Forwarded App packet from LocalPort %d using route ID %d", packet.Addr.Local.Port, l.routeID)
r.Logger.Infof("Forwarded App packet from LocalPort %d using route ID %d", packet.Loop.Local.Port, l.routeID)
_, err = tr.Write(p)
return err
}

func (r *Router) forwardLocalAppPacket(packet *app.Packet) error {
b, err := r.pm.Get(packet.Addr.Remote.Port)
b, err := r.pm.Get(packet.Loop.Remote.Port)
if err != nil {
return nil
}

p := &app.Packet{
Addr: &routing.Loop{
Local: routing.Addr{Port: packet.Addr.Remote.Port},
Remote: routing.Addr{PubKey: packet.Addr.Remote.PubKey, Port: packet.Addr.Local.Port},
Loop: &routing.Loop{
Local: routing.Addr{Port: packet.Loop.Remote.Port},
Remote: routing.Addr{PubKey: packet.Loop.Remote.PubKey, Port: packet.Loop.Local.Port},
},
Payload: packet.Payload,
}
Expand Down
16 changes: 8 additions & 8 deletions pkg/router/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ func TestRouterApp(t *testing.T) {
require.NoError(t, r.pm.SetLoop(6, raddr, &loop{tr.Entry.ID, 4}))

tr2 := m2.Transport(tr.Entry.ID)
go proto.Send(app.FrameSend, &app.Packet{Addr: &routing.Loop{Local: routing.Addr{Port: 6}, Remote: *raddr}, Payload: []byte("bar")}, nil) // nolint: errcheck
go proto.Send(app.FrameSend, &app.Packet{Loop: &routing.Loop{Local: routing.Addr{Port: 6}, Remote: *raddr}, Payload: []byte("bar")}, nil) // nolint: errcheck

packet := make(routing.Packet, 9)
_, err = tr2.Read(packet)
Expand All @@ -214,9 +214,9 @@ func TestRouterApp(t *testing.T) {

var aPacket app.Packet
require.NoError(t, json.Unmarshal(<-dataCh, &aPacket))
assert.Equal(t, pk2, aPacket.Addr.Remote.PubKey)
assert.Equal(t, routing.Port(5), aPacket.Addr.Remote.Port)
assert.Equal(t, routing.Port(6), aPacket.Addr.Local.Port)
assert.Equal(t, pk2, aPacket.Loop.Remote.PubKey)
assert.Equal(t, routing.Port(5), aPacket.Loop.Remote.Port)
assert.Equal(t, routing.Port(6), aPacket.Loop.Local.Port)
assert.Equal(t, []byte("foo"), aPacket.Payload)

require.NoError(t, r.Close())
Expand Down Expand Up @@ -258,16 +258,16 @@ func TestRouterLocalApp(t *testing.T) {
return nil, nil
})

go proto1.Send(app.FrameSend, &app.Packet{Addr: &routing.Loop{Local: routing.Addr{Port: 5}, Remote: routing.Addr{PubKey: pk, Port: 6}}, Payload: []byte("foo")}, nil) // nolint: errcheck
go proto1.Send(app.FrameSend, &app.Packet{Loop: &routing.Loop{Local: routing.Addr{Port: 5}, Remote: routing.Addr{PubKey: pk, Port: 6}}, Payload: []byte("foo")}, nil) // nolint: errcheck

time.Sleep(100 * time.Millisecond)

packet := &app.Packet{}
require.NoError(t, json.Unmarshal(<-dataCh, packet))
require.NoError(t, err)
assert.Equal(t, pk, packet.Addr.Remote.PubKey)
assert.Equal(t, routing.Port(5), packet.Addr.Remote.Port)
assert.Equal(t, routing.Port(6), packet.Addr.Local.Port)
assert.Equal(t, pk, packet.Loop.Remote.PubKey)
assert.Equal(t, routing.Port(5), packet.Loop.Remote.Port)
assert.Equal(t, routing.Port(6), packet.Loop.Local.Port)
assert.Equal(t, []byte("foo"), packet.Payload)

require.NoError(t, r.Close())
Expand Down

0 comments on commit 106b36c

Please sign in to comment.