Skip to content

Commit

Permalink
More minor improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Darkren committed Jul 9, 2019
1 parent d712081 commit be9dc86
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 34 deletions.
14 changes: 7 additions & 7 deletions pkg/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,12 @@ func (app *App) Accept() (net.Conn, error) {
laddr := addrs[0]
raddr := addrs[1]

addr := &routing.Loop{Local: *laddr, Remote: *raddr}
loop := routing.Loop{Local: *laddr, Remote: *raddr}
conn, out := net.Pipe()
app.mu.Lock()
app.conns[*addr] = conn
app.conns[loop] = conn
app.mu.Unlock()
go app.serveConn(addr, conn)
go app.serveConn(&loop, conn)
return newAppConn(out, laddr, raddr), nil
}

Expand All @@ -136,12 +136,12 @@ func (app *App) Dial(raddr *routing.Addr) (net.Conn, error) {
if err != nil {
return nil, err
}
addr := &routing.Loop{Local: laddr, Remote: *raddr}
loop := routing.Loop{Local: laddr, Remote: *raddr}
conn, out := net.Pipe()
app.mu.Lock()
app.conns[*addr] = conn
app.conns[loop] = conn
app.mu.Unlock()
go app.serveConn(addr, conn)
go app.serveConn(&loop, conn)
return newAppConn(out, &laddr, raddr), nil
}

Expand Down Expand Up @@ -228,7 +228,7 @@ func (app *App) closeConn(data []byte) error {
}

func (app *App) confirmLoop(data []byte) error {
addrs := [2]*routing.Addr{}
var addrs [2]*routing.Addr
if err := json.Unmarshal(data, &addrs); err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/router/app_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ func TestAppManagerSetupLoop(t *testing.T) {
proto := app.NewProtocol(in)
go proto.Serve(nil) // nolint: errcheck

var laddr *routing.Addr
var laddr routing.Addr
pk, _ := cipher.GenerateKeyPair()
raddr := &routing.Addr{PubKey: pk, Port: 3}
err := proto.Send(app.FrameCreateLoop, raddr, &laddr)
raddr := routing.Addr{PubKey: pk, Port: 3}
err := proto.Send(app.FrameCreateLoop, &raddr, &laddr)
require.NoError(t, err)
assert.Equal(t, raddr, laddr)

Expand Down
2 changes: 1 addition & 1 deletion pkg/router/loop_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ func (ll *loopList) remove(addr *routing.Addr) {
}

func (ll *loopList) dropAll() []routing.Addr {
r := make([]routing.Addr, 0)
ll.Lock()
r := make([]routing.Addr, 0, len(ll.loops))
for addr := range ll.loops {
r = append(r, addr)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/router/route_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func (rm *routeManager) deleteRoutingRules(data []byte) ([]routing.RouteID, erro
}

func (rm *routeManager) confirmLoop(data []byte) error {
ld := routing.LoopData{}
var ld routing.LoopData
if err := json.Unmarshal(data, &ld); err != nil {
return err
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,10 @@ 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()}
laddr := routing.Addr{Port: rule.LocalPort()}
raddr := routing.Addr{PubKey: rule.RemotePK(), Port: rule.RemotePort()}

p := &app.Packet{Loop: &routing.Loop{Local: routing.Addr{Port: rule.LocalPort()}, Remote: *raddr}, Payload: payload}
p := &app.Packet{Loop: &routing.Loop{Local: laddr, 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 Down
12 changes: 6 additions & 6 deletions pkg/router/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ func TestRouterSetup(t *testing.T) {
assert.Equal(t, tr.Entry.ID, loop.trID)
assert.Equal(t, routing.RouteID(2), loop.routeID)

addrs := [2]*routing.Addr{}
var addrs [2]*routing.Addr
require.NoError(t, json.Unmarshal(<-dataCh, &addrs))
require.NoError(t, err)
assert.Equal(t, pk1, addrs[0].PubKey)
Expand Down Expand Up @@ -407,7 +407,7 @@ func TestRouterSetup(t *testing.T) {
assert.Equal(t, tr.Entry.ID, l.trID)
assert.Equal(t, routing.RouteID(2), l.routeID)

addrs := [2]*routing.Addr{}
var addrs [2]*routing.Addr
require.NoError(t, json.Unmarshal(<-dataCh, &addrs))
require.NoError(t, err)
assert.Equal(t, pk1, addrs[0].PubKey)
Expand Down Expand Up @@ -503,8 +503,8 @@ func TestRouterSetupLoop(t *testing.T) {
return
}

l := &routing.LoopDescriptor{}
if err := json.Unmarshal(data, l); err != nil {
var l routing.LoopDescriptor
if err := json.Unmarshal(data, &l); err != nil {
errCh <- err
return
}
Expand Down Expand Up @@ -614,8 +614,8 @@ func TestRouterCloseLoop(t *testing.T) {
return
}

ld := &routing.LoopData{}
if err := json.Unmarshal(data, ld); err != nil {
var ld routing.LoopData
if err := json.Unmarshal(data, &ld); err != nil {
errCh <- err
return
}
Expand Down
10 changes: 5 additions & 5 deletions pkg/setup/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,13 +222,13 @@ func (sn *Node) serveTransport(tr transport.Transport) error {
startTime := time.Now()
switch sp {
case PacketCreateLoop:
loop := &routing.LoopDescriptor{}
if err = json.Unmarshal(data, loop); err == nil {
err = sn.createLoop(loop)
var loopDp routing.LoopDescriptor
if err = json.Unmarshal(data, &loopDp); err == nil {
err = sn.createLoop(&loopDp)
}
case PacketCloseLoop:
ld := &routing.LoopData{}
if err = json.Unmarshal(data, ld); err == nil {
var ld routing.LoopData
if err = json.Unmarshal(data, &ld); err == nil {
remote, ok := sn.tm.Remote(tr.Edges())
if !ok {
return errors.New("configured PubKey not found in edges")
Expand Down
2 changes: 1 addition & 1 deletion pkg/setup/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ func (n *mockNode) serveTransport(tr transport.Transport) error {
}
}
case PacketConfirmLoop:
ld := routing.LoopData{}
var ld routing.LoopData
json.Unmarshal(data, &ld) // nolint: errcheck
for _, rule := range n.rules {
if rule.Type() == routing.RuleApp && rule.RemotePK() == ld.Loop.Remote.PubKey &&
Expand Down
16 changes: 8 additions & 8 deletions pkg/setup/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,32 +132,32 @@ func DeleteRule(p *Protocol, routeID routing.RouteID) error {
}

// CreateLoop sends CreateLoop setup request.
func CreateLoop(p *Protocol, l *routing.LoopDescriptor) error {
if err := p.WritePacket(PacketCreateLoop, l); err != nil {
func CreateLoop(p *Protocol, ld *routing.LoopDescriptor) error {
if err := p.WritePacket(PacketCreateLoop, ld); err != nil {
return err
}
return readAndDecodePacket(p, nil) // TODO: data race.
}

// ConfirmLoop sends ConfirmLoop setup request.
func ConfirmLoop(p *Protocol, l *routing.LoopData) error {
if err := p.WritePacket(PacketConfirmLoop, l); err != nil {
func ConfirmLoop(p *Protocol, ld *routing.LoopData) error {
if err := p.WritePacket(PacketConfirmLoop, ld); err != nil {
return err
}
return readAndDecodePacket(p, nil)
}

// CloseLoop sends CloseLoop setup request.
func CloseLoop(p *Protocol, l *routing.LoopData) error {
if err := p.WritePacket(PacketCloseLoop, l); err != nil {
func CloseLoop(p *Protocol, ld *routing.LoopData) error {
if err := p.WritePacket(PacketCloseLoop, ld); err != nil {
return err
}
return readAndDecodePacket(p, nil)
}

// LoopClosed sends LoopClosed setup request.
func LoopClosed(p *Protocol, l *routing.LoopData) error {
if err := p.WritePacket(PacketLoopClosed, l); err != nil {
func LoopClosed(p *Protocol, ld *routing.LoopData) error {
if err := p.WritePacket(PacketLoopClosed, ld); err != nil {
return err
}
return readAndDecodePacket(p, nil)
Expand Down

0 comments on commit be9dc86

Please sign in to comment.