Skip to content

Commit

Permalink
Add funcs to request free route id from visor
Browse files Browse the repository at this point in the history
  • Loading branch information
Darkren committed Aug 8, 2019
1 parent 0b23177 commit 0809098
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
22 changes: 22 additions & 0 deletions pkg/setup/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,28 @@ func (sn *Node) closeLoop(on cipher.PubKey, ld routing.LoopData) error {
return nil
}

func (sn *Node) requestRouteID(ctx context.Context, pubKey cipher.PubKey) (uint32, error) {
sn.Logger.Debugf("dialing to %s to request route ID\n", pubKey)
tr, err := sn.messenger.Dial(ctx, pubKey)
if err != nil {
return 0, fmt.Errorf("transport: %s", err)
}
defer func() {
if err := tr.Close(); err != nil {
sn.Logger.Warnf("Failed to close transport: %s", err)
}
}()

proto := NewSetupProtocol(tr)
routeID, err := RequestRouteID(proto)
if err != nil {
return 0, err
}

sn.Logger.Infof("Received route ID %d from %s", routeID, pubKey)
return routeID, nil
}

func (sn *Node) setupRule(ctx context.Context, pubKey cipher.PubKey,
rule routing.Rule) (routeID routing.RouteID, err error) {
sn.Logger.Debugf("dialing to %s to setup rule: %v\n", pubKey, rule)
Expand Down
17 changes: 17 additions & 0 deletions pkg/setup/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ const (
PacketCloseLoop
// PacketLoopClosed represents LoopClosed foundation packet.
PacketLoopClosed
// PacketRequestRouteID represents RequestRouteID foundation packet.
PacketRequestRouteID

// RespFailure represents failure response for a foundation packet.
RespFailure = 0xfe
Expand Down Expand Up @@ -99,6 +101,21 @@ func (p *Protocol) WritePacket(t PacketType, body interface{}) error {
return err
}

// RequestRouteID sends RequestRouteID request.
func RequestRouteID(p *Protocol) (uint32, error) {
if err := p.WritePacket(PacketRequestRouteID, nil); err != nil {
return 0, err
}
var res []uint32
if err := readAndDecodePacket(p, &res); err != nil {
return 0, err
}
if len(res) == 0 {
return 0, errors.New("empty response")
}
return res[0], nil
}

// AddRule sends AddRule setup request.
func AddRule(p *Protocol, rule routing.Rule) (routeID routing.RouteID, err error) {
if err = p.WritePacket(PacketAddRules, []routing.Rule{rule}); err != nil {
Expand Down

0 comments on commit 0809098

Please sign in to comment.