Skip to content

Commit

Permalink
Add transports when RouteGroup's get saved
Browse files Browse the repository at this point in the history
  • Loading branch information
nkryuchkov committed Oct 2, 2019
1 parent a092b4e commit e31643d
Show file tree
Hide file tree
Showing 8 changed files with 238 additions and 26 deletions.
2 changes: 1 addition & 1 deletion pkg/app/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const (
// FrameSend represents Send frame type.
FrameSend
// FrameClose represents Close frame type
FrameClose // TODO: decide whether this needs to be removed
FrameClose // TODO(nkryuchkov): decide whether this needs to be removed

// FrameFailure represents frame type for failed requests.
FrameFailure = 0xfe
Expand Down
2 changes: 1 addition & 1 deletion pkg/router/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

type Gateway struct {
logger *logging.Logger
router *router // TODO: move part of Router methods to Gateway
router *router // TODO(nkryuchkov): move part of Router methods to Gateway
}

func NewGateway(router *router) *Gateway {
Expand Down
23 changes: 11 additions & 12 deletions pkg/router/route_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ func (r *RouteGroup) Write(p []byte) (n int, err error) {
defer r.mu.Unlock()

if len(r.tps) == 0 {
return 0, errors.New("no transports") // TODO: proper error
return 0, errors.New("no transports") // TODO(nkryuchkov): proper error
}
if len(r.fwd) == 0 {
return 0, errors.New("no rules") // TODO: proper error
return 0, errors.New("no rules") // TODO(nkryuchkov): proper error
}

tp := r.tps[0]
Expand All @@ -97,13 +97,12 @@ func (r *RouteGroup) Write(p []byte) (n int, err error) {
return len(p), nil
}

// Close closes a RouteGroup:
// - Send Close packet for all ForwardRules.
// - Delete all rules (ForwardRules and ConsumeRules) from routing table.
// - Close all go channels.
func (r *RouteGroup) Close() error {
/*
Closing the RouteGroup
Send Close packet for all ForwardRules.
Delete all rules (ForwardRules and ConsumeRules) from routing table.
Close all go channels.
*/

r.mu.Lock()
defer r.mu.Unlock()

Expand All @@ -125,7 +124,7 @@ func (r *RouteGroup) Close() error {
}
r.rt.DelRules(routeIDs)

// TODO: close readCh
close(r.readCh) // TODO(nkryuchkov): close readCh properly

return nil
}
Expand All @@ -138,17 +137,17 @@ func (r *RouteGroup) RemoteAddr() net.Addr {
return r.desc.Dst()
}

// TODO: implement
// TODO(nkryuchkov): implement
func (r *RouteGroup) SetDeadline(t time.Time) error {
return nil
}

// TODO: implement
// TODO(nkryuchkov): implement
func (r *RouteGroup) SetReadDeadline(t time.Time) error {
return nil
}

// TODO: implement
// TODO(nkryuchkov): implement
func (r *RouteGroup) SetWriteDeadline(t time.Time) error {
return nil
}
18 changes: 10 additions & 8 deletions pkg/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (

const (
// DefaultRouteKeepAlive is the default expiration interval for routes
DefaultRouteKeepAlive = 2 * time.Hour // TODO: change
DefaultRouteKeepAlive = 2 * time.Hour // TODO(nkryuchkov): change
acceptSize = 1024

minHops = 0
Expand Down Expand Up @@ -149,10 +149,6 @@ func New(n *snet.Network, config *Config) (*router, error) {
// - Save to routing.Table and internal RouteGroup map.
// - Return RouteGroup if successful.
func (r *router) DialRoutes(ctx context.Context, rPK cipher.PubKey, lPort, rPort routing.Port, opts *DialOptions) (*RouteGroup, error) {
if opts == nil {
opts = DefaultDialOptions
}

lPK := r.conf.PubKey
forwardDesc := routing.NewRouteDescriptor(lPK, rPK, lPort, rPort)

Expand Down Expand Up @@ -262,14 +258,17 @@ func (r *router) saveRouteGroupRules(rules routing.EdgeRules) *RouteGroup {
rg = NewRouteGroup(r.rt, rules.Desc)
r.rgs[rules.Desc] = rg
}

rg.fwd = append(rg.fwd, rules.Forward)
rg.rvs = append(rg.fwd, rules.Reverse)

// TODO: fill transports
tp := r.tm.Transport(rules.Forward.NextTransportID())
rg.tps = append(rg.tps, tp)

return rg
}

// TODO: handle other packet types
// TODO(nkryuchkov): handle other packet types
func (r *router) handleTransportPacket(ctx context.Context, packet routing.Packet) error {
rule, err := r.GetRule(packet.RouteID())
if err != nil {
Expand Down Expand Up @@ -378,7 +377,10 @@ func (r *router) RemoveRouteDescriptor(desc routing.RouteDescriptor) {
}

func (r *router) fetchBestRoutes(source, destination cipher.PubKey, opts *DialOptions) (fwd routing.Path, rev routing.Path, err error) {
// TODO: use opts
// TODO(nkryuchkov): use opts
if opts == nil {
opts = DefaultDialOptions
}

r.logger.Infof("Requesting new routes from %s to %s", source, destination)

Expand Down
4 changes: 2 additions & 2 deletions pkg/routing/packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func MakeDataPacket(id RouteID, payload []byte) Packet {
}

// MakeClosePacket constructs a new ClosePacket.
func MakeClosePacket(id RouteID, code CloseCode) Packet { // TODO: use
func MakeClosePacket(id RouteID, code CloseCode) Packet {
packet := make([]byte, PacketHeaderSize+1)

packet[PacketTypeOffset] = byte(ClosePacket)
Expand All @@ -99,7 +99,7 @@ func MakeClosePacket(id RouteID, code CloseCode) Packet { // TODO: use
}

// MakeKeepAlivePacket constructs a new KeepAlivePacket.
func MakeKeepAlivePacket(id RouteID) Packet { // TODO: use
func MakeKeepAlivePacket(id RouteID) Packet { // TODO(nkryuchkov): use it
packet := make([]byte, PacketHeaderSize)

packet[PacketTypeOffset] = byte(KeepAlivePacket)
Expand Down
4 changes: 2 additions & 2 deletions pkg/setup/idreservoir.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ func (rm RulesMap) String() string {
return string(jb)
}

// TODO: fix comment, refactor
// GenerateRules2 generates rules for a given route.
// TODO(nkryuchkov): fix comment, refactor
// GenerateRules generates rules for a given route.
// The outputs are as follows:
// - a map that relates a slice of routing rules to a given visor's public key.
// - an error (if any).
Expand Down
26 changes: 26 additions & 0 deletions vendor/github.com/skycoin/dmsg/addr.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

185 changes: 185 additions & 0 deletions vendor/github.com/skycoin/dmsg/frame.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit e31643d

Please sign in to comment.