Skip to content

Commit

Permalink
added Loops RPC endpoint for node
Browse files Browse the repository at this point in the history
  • Loading branch information
林志宇 committed Apr 7, 2019
1 parent 0189db1 commit 0b2cb1f
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 95 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
.idea/

/skywire.json
/*-config.json
/apps/
/skywire/
/local/
Expand Down
52 changes: 30 additions & 22 deletions pkg/node/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,28 +34,6 @@ type RPC struct {
node *Node
}

// RPCClient represents a RPC Client implementation.
type RPCClient interface {
Summary() (*Summary, error)

Apps() ([]*AppState, error)
StartApp(appName string) error
StopApp(appName string) error
SetAutoStart(appName string, autostart bool) error

TransportTypes() ([]string, error)
Transports(types []string, pks []cipher.PubKey, logs bool) ([]*TransportSummary, error)
Transport(tid uuid.UUID) (*TransportSummary, error)
AddTransport(remote cipher.PubKey, tpType string, public bool, timeout time.Duration) (*TransportSummary, error)
RemoveTransport(tid uuid.UUID) error

RoutingRules() ([]*RoutingEntry, error)
RoutingRule(key routing.RouteID) (routing.Rule, error)
AddRoutingRule(rule routing.Rule) (routing.RouteID, error)
SetRoutingRule(key routing.RouteID, rule routing.Rule) error
RemoveRoutingRule(key routing.RouteID) error
}

/*
<<< NODE SUMMARY >>>
*/
Expand Down Expand Up @@ -273,3 +251,33 @@ func (r *RPC) RemoveRoutingRule(key *routing.RouteID, _ *struct{}) error {
<<< LOOPS MANAGEMENT >>>
>>> TODO(evanlinjin): Implement.
*/

// LoopInfo is a human-understandable representation of a loop.
type LoopInfo struct {
AppRule routing.Rule
FwdRule routing.Rule
}

// Loops retrieves loops via rules of the routing table.
func (r *RPC) Loops(_ *struct{}, out *[]LoopInfo) error {
var loops []LoopInfo
err := r.node.rt.RangeRules(func(_ routing.RouteID, rule routing.Rule) (next bool) {
if rule.Type() == routing.RuleApp {
loops = append(loops, LoopInfo{AppRule: rule})
}
return true
})
if err != nil {
return err
}
for i, l := range loops {
fwdRID := l.AppRule.RouteID()
rule, err := r.node.rt.Rule(fwdRID)
if err != nil {
return err
}
loops[i].FwdRule = rule
}
*out = loops
return nil
}
73 changes: 67 additions & 6 deletions pkg/node/rpc_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,30 @@ import (
"github.com/skycoin/skywire/pkg/transport"
)

// RPCClient represents a RPC Client implementation.
type RPCClient interface {
Summary() (*Summary, error)

Apps() ([]*AppState, error)
StartApp(appName string) error
StopApp(appName string) error
SetAutoStart(appName string, autostart bool) error

TransportTypes() ([]string, error)
Transports(types []string, pks []cipher.PubKey, logs bool) ([]*TransportSummary, error)
Transport(tid uuid.UUID) (*TransportSummary, error)
AddTransport(remote cipher.PubKey, tpType string, public bool, timeout time.Duration) (*TransportSummary, error)
RemoveTransport(tid uuid.UUID) error

RoutingRules() ([]*RoutingEntry, error)
RoutingRule(key routing.RouteID) (routing.Rule, error)
AddRoutingRule(rule routing.Rule) (routing.RouteID, error)
SetRoutingRule(key routing.RouteID, rule routing.Rule) error
RemoveRoutingRule(key routing.RouteID) error

Loops() ([]LoopInfo, error)
}

// RPCClient provides methods to call an RPC Server.
// It implements RPCClient
type rpcClient struct {
Expand Down Expand Up @@ -137,6 +161,13 @@ func (rc *rpcClient) RemoveRoutingRule(key routing.RouteID) error {
return rc.Call("RemoveRoutingRule", &key, &struct{}{})
}

// Loops calls Loops.
func (rc *rpcClient) Loops() ([]LoopInfo, error) {
var loops []LoopInfo
err := rc.Call("Loops", &struct{}{}, &loops)
return loops, err
}

// MockRPCClient mocks RPCClient.
type mockRPCClient struct {
s *Summary
Expand Down Expand Up @@ -164,19 +195,26 @@ func NewMockRPCClient(r *rand.Rand, maxTps int, maxRules int) (cipher.PubKey, RP
ruleExp := time.Now().Add(time.Hour * 24)
for i := 0; i < r.Intn(maxRules+1); i++ {
remotePK, _ := cipher.GenerateKeyPair()
var rule routing.Rule
if r.Int()%2 == 0 {
var lpRaw, rpRaw [2]byte
r.Read(lpRaw[:])
r.Read(rpRaw[:])
lp := binary.BigEndian.Uint16(lpRaw[:])
rp := binary.BigEndian.Uint16(rpRaw[:])
rule = routing.AppRule(ruleExp, routing.RouteID(r.Uint32()), remotePK, rp, lp)
revRID := routing.RouteID(r.Uint32())
rule := routing.AppRule(ruleExp, revRID, remotePK, rp, lp)
if _, err := rt.AddRule(rule); err != nil {
panic(err)
}
revRule := routing.ForwardRule(ruleExp, routing.RouteID(r.Uint32()), uuid.New())
if _, err := rt.AddRule(revRule); err != nil {
panic(err)
}
} else {
rule = routing.ForwardRule(ruleExp, routing.RouteID(r.Uint32()), uuid.New())
}
if _, err := rt.AddRule(rule); err != nil {
panic(err)
rule := routing.ForwardRule(ruleExp, routing.RouteID(r.Uint32()), uuid.New())
if _, err := rt.AddRule(rule); err != nil {
panic(err)
}
}
}
return localPK, &mockRPCClient{
Expand Down Expand Up @@ -368,3 +406,26 @@ func (mc *mockRPCClient) SetRoutingRule(key routing.RouteID, rule routing.Rule)
func (mc *mockRPCClient) RemoveRoutingRule(key routing.RouteID) error {
return mc.rt.DeleteRules(key)
}

// Loops implements RPCClient.
func (mc *mockRPCClient) Loops() ([]LoopInfo, error) {
var loops []LoopInfo
err := mc.rt.RangeRules(func(_ routing.RouteID, rule routing.Rule) (next bool) {
if rule.Type() == routing.RuleApp {
loops = append(loops, LoopInfo{AppRule: rule})
}
return true
})
if err != nil {
return nil, err
}
for i, l := range loops {
fwdRID := l.AppRule.RouteID()
rule, err := mc.rt.Rule(fwdRID)
if err != nil {
return nil, err
}
loops[i].FwdRule = rule
}
return loops, nil
}
2 changes: 1 addition & 1 deletion pkg/router/loop_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type loop struct {
type loopList struct {
sync.Mutex

loops map[app.Addr]*loop
loops map[app.Addr]*loop // key: remote address (pk+port), value: forwarding transport and route ID.
}

func newLoopList() *loopList {
Expand Down
6 changes: 3 additions & 3 deletions pkg/router/port_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,13 @@ func (pm *portManager) Get(port uint16) (*portBind, error) {
return b, nil
}

func (pm *portManager) GetLoop(port uint16, raddr *app.Addr) (*loop, error) {
b, err := pm.Get(port)
func (pm *portManager) GetLoop(localPort uint16, remoteAddr *app.Addr) (*loop, error) {
b, err := pm.Get(localPort)
if err != nil {
return nil, err
}

l := b.loops.get(raddr)
l := b.loops.get(remoteAddr)
if l == nil {
return nil, errors.New("unknown loop")
}
Expand Down
4 changes: 3 additions & 1 deletion pkg/routing/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ const (
RuleForward
)

// Rule represents a rule in a RoutingTable.
// Rule represents a routing rule.
// There are two types of routing rules; App and Forward.
//
type Rule []byte

// Expiry returns rule's expiration time.
Expand Down
62 changes: 0 additions & 62 deletions skywire-config.json

This file was deleted.

0 comments on commit 0b2cb1f

Please sign in to comment.