Skip to content

Commit

Permalink
Move setup.LoopData -> routing.LoopData
Browse files Browse the repository at this point in the history
  • Loading branch information
Darkren committed Jul 9, 2019
1 parent 833a581 commit 023f560
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 31 deletions.
6 changes: 3 additions & 3 deletions 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 := setup.LoopData{}
ld := routing.LoopData{}
if err := json.Unmarshal(data, &ld); err != nil {
return err
}
Expand Down Expand Up @@ -190,8 +190,8 @@ func (rm *routeManager) confirmLoop(data []byte) error {
}

func (rm *routeManager) loopClosed(data []byte) error {
ld := &setup.LoopData{}
if err := json.Unmarshal(data, ld); err != nil {
var ld routing.LoopData
if err := json.Unmarshal(data, &ld); err != nil {
return err
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/router/route_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func TestRouteManagerConfirmLoop(t *testing.T) {
rule = routing.ForwardRule(time.Now(), 3, uuid.New())
require.NoError(t, rt.SetRule(1, rule))

ld := &setup.LoopData{
ld := &routing.LoopData{
Loop: routing.Loop{
Remote: routing.Addr{
PubKey: pk,
Expand Down Expand Up @@ -195,7 +195,7 @@ func TestRouteManagerLoopClosed(t *testing.T) {
rule = routing.ForwardRule(time.Now(), 3, uuid.New())
require.NoError(t, rt.SetRule(1, rule))

ld := &setup.LoopData{
ld := &routing.LoopData{
Loop: routing.Loop{
Remote: routing.Addr{
PubKey: pk,
Expand Down
2 changes: 1 addition & 1 deletion pkg/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ func (r *Router) closeLoop(appConn *app.Protocol, loop *routing.Loop) error {
}
defer tr.Close()

ld := &setup.LoopData{Loop: *loop}
ld := &routing.LoopData{Loop: *loop}
if err := setup.CloseLoop(proto, ld); err != nil {
return fmt.Errorf("route setup: %s", 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 @@ -342,7 +342,7 @@ func TestRouterSetup(t *testing.T) {
appRouteID, err := setup.AddRule(sProto, routing.AppRule(time.Now().Add(time.Hour), 0, pk2, 1, 2))
require.NoError(t, err)

err = setup.ConfirmLoop(sProto, &setup.LoopData{
err = setup.ConfirmLoop(sProto, &routing.LoopData{
Loop: routing.Loop{
Remote: routing.Addr{
PubKey: pk2,
Expand Down Expand Up @@ -384,7 +384,7 @@ func TestRouterSetup(t *testing.T) {
appRouteID, err := setup.AddRule(sProto, routing.AppRule(time.Now().Add(time.Hour), 0, pk2, 3, 4))
require.NoError(t, err)

err = setup.ConfirmLoop(sProto, &setup.LoopData{
err = setup.ConfirmLoop(sProto, &routing.LoopData{
Loop: routing.Loop{
Remote: routing.Addr{
PubKey: pk2,
Expand Down Expand Up @@ -422,7 +422,7 @@ func TestRouterSetup(t *testing.T) {
require.NotNil(t, rule)
assert.Equal(t, routing.RuleApp, rule.Type())

require.NoError(t, setup.LoopClosed(sProto, &setup.LoopData{
require.NoError(t, setup.LoopClosed(sProto, &routing.LoopData{
Loop: routing.Loop{
Remote: routing.Addr{
PubKey: pk2,
Expand Down Expand Up @@ -614,7 +614,7 @@ func TestRouterCloseLoop(t *testing.T) {
return
}

ld := &setup.LoopData{}
ld := &routing.LoopData{}
if err := json.Unmarshal(data, ld); err != nil {
errCh <- err
return
Expand Down Expand Up @@ -706,8 +706,8 @@ func TestRouterCloseLoopOnAppClose(t *testing.T) {
return
}

ld := &setup.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
6 changes: 6 additions & 0 deletions pkg/routing/loop.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,9 @@ func (l *LoopDescriptor) String() string {
return fmt.Sprintf("lport: %d. rport: %d. routes: %s/%s. expire at %s",
l.Local.Port, l.Remote.Port, l.Forward, l.Reverse, l.Expiry)
}

// LoopData stores loop confirmation request data.
type LoopData struct {
Loop Loop `json:"loop"`
RouteID RouteID `json:"resp-rid,omitempty"`
}
12 changes: 6 additions & 6 deletions pkg/setup/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func (sn *Node) createLoop(l *routing.LoopDescriptor) error {
initiator := l.Initiator()
responder := l.Responder()

ldR := &LoopData{
ldR := &routing.LoopData{
Loop: routing.Loop{
Remote: routing.Addr{PubKey: initiator, Port: l.Local.Port},
Local: routing.Addr{PubKey: responder, Port: l.Remote.Port},
Expand All @@ -133,7 +133,7 @@ func (sn *Node) createLoop(l *routing.LoopDescriptor) error {
return fmt.Errorf("loop connect: %s", err)
}

ldI := &LoopData{
ldI := &routing.LoopData{
Loop: routing.Loop{
Remote: routing.Addr{
PubKey: responder,
Expand Down Expand Up @@ -221,13 +221,13 @@ func (sn *Node) serveTransport(tr transport.Transport) error {
err = sn.createLoop(loop)
}
case PacketCloseLoop:
ld := &LoopData{}
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")
}
err = sn.closeLoop(ld.Loop.Remote.PubKey, &LoopData{
err = sn.closeLoop(ld.Loop.Remote.PubKey, &routing.LoopData{
Loop: routing.Loop{
Remote: routing.Addr{
PubKey: remote,
Expand All @@ -252,7 +252,7 @@ func (sn *Node) serveTransport(tr transport.Transport) error {
return proto.WritePacket(RespSuccess, nil)
}

func (sn *Node) connectLoop(on cipher.PubKey, ld *LoopData) error {
func (sn *Node) connectLoop(on cipher.PubKey, ld *routing.LoopData) error {
tr, err := sn.tm.CreateTransport(context.Background(), on, dmsg.Type, false)
if err != nil {
return fmt.Errorf("transport: %s", err)
Expand All @@ -268,7 +268,7 @@ func (sn *Node) connectLoop(on cipher.PubKey, ld *LoopData) error {
return nil
}

func (sn *Node) closeLoop(on cipher.PubKey, ld *LoopData) error {
func (sn *Node) closeLoop(on cipher.PubKey, ld *routing.LoopData) error {
tr, err := sn.tm.CreateTransport(context.Background(), on, dmsg.Type, false)
if err != nil {
return fmt.Errorf("transport: %s", err)
Expand Down
8 changes: 4 additions & 4 deletions pkg/setup/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ func TestCloseLoop(t *testing.T) {
require.NoError(t, err)

proto := NewSetupProtocol(tr)
require.NoError(t, CloseLoop(proto, &LoopData{
require.NoError(t, CloseLoop(proto, &routing.LoopData{
Loop: routing.Loop{
Remote: routing.Addr{
PubKey: pk3,
Expand Down Expand Up @@ -354,7 +354,7 @@ func (n *mockNode) serveTransport(tr transport.Transport) error {
}
}
case PacketConfirmLoop:
ld := LoopData{}
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 All @@ -365,8 +365,8 @@ func (n *mockNode) serveTransport(tr transport.Transport) error {
}
}
case PacketLoopClosed:
ld := &LoopData{}
json.Unmarshal(data, ld) // nolint: errcheck
var ld routing.LoopData
json.Unmarshal(data, &ld) // nolint: errcheck
for routeID, rule := range n.rules {
if rule.Type() == routing.RuleApp && rule.RemotePK() == ld.Loop.Remote.PubKey &&
rule.RemotePort() == ld.Loop.Remote.Port && rule.LocalPort() == ld.Loop.Local.Port {
Expand Down
12 changes: 3 additions & 9 deletions pkg/setup/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,6 @@ const (
RespSuccess = 0xff
)

// LoopData stores loop confirmation request data.
type LoopData struct {
Loop routing.Loop `json:"loop"`
RouteID routing.RouteID `json:"resp-rid,omitempty"`
}

// Protocol defines routes setup protocol.
type Protocol struct {
rw io.ReadWriter
Expand Down Expand Up @@ -146,23 +140,23 @@ func CreateLoop(p *Protocol, l *routing.LoopDescriptor) error {
}

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

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

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

0 comments on commit 023f560

Please sign in to comment.