diff --git a/pkg/router/router.go b/pkg/router/router.go index 9157009d92..79b4f213bf 100644 --- a/pkg/router/router.go +++ b/pkg/router/router.go @@ -44,7 +44,6 @@ type Config struct { PubKey cipher.PubKey SecKey cipher.SecKey TransportManager *transport.Manager - RoutingTable routing.Table RouteFinder rfclient.Client RouteGroupDialer setupclient.RouteGroupDialer SetupNodes []cipher.PubKey @@ -106,6 +105,13 @@ type Router interface { IntroduceRules(rules routing.EdgeRules) error Serve(context.Context) error SetupIsTrusted(cipher.PubKey) bool + + // routing table related methods + RoutesCount() int + Rules() []routing.Rule + Rule(routing.RouteID) (routing.Rule, error) + SaveRule(routing.Rule) error + DelRules([]routing.RouteID) } // Router implements visor.PacketRouter. It manages routing table by @@ -148,7 +154,7 @@ func New(n *snet.Network, config *Config) (Router, error) { logger: config.Logger, n: n, tm: config.TransportManager, - rt: config.RoutingTable, + rt: routing.NewTable(routing.DefaultConfig()), sl: sl, rfc: config.RouteFinder, rgs: make(map[routing.RouteDescriptor]*RouteGroup), @@ -644,3 +650,28 @@ func (r *router) IntroduceRules(rules routing.EdgeRules) error { } } } + +// RoutesCount returns count of the routes stored within the routing table. +func (r *router) RoutesCount() int { + return r.rt.Count() +} + +// Rules gets all the rules stored within the routing table. +func (r *router) Rules() []routing.Rule { + return r.rt.AllRules() +} + +// Rule fetches rule by the route `id`. +func (r *router) Rule(id routing.RouteID) (routing.Rule, error) { + return r.rt.Rule(id) +} + +// SaveRule stores the `rule` within the routing table. +func (r *router) SaveRule(rule routing.Rule) error { + return r.rt.SaveRule(rule) +} + +// DelRules removes rules associated with `ids` from the routing table. +func (r *router) DelRules(ids []routing.RouteID) { + r.rt.DelRules(ids) +} diff --git a/pkg/visor/config.go b/pkg/visor/config.go index 57adb2493e..a8efca7f96 100644 --- a/pkg/visor/config.go +++ b/pkg/visor/config.go @@ -130,11 +130,6 @@ func (c *Config) TransportLogStore() (transport.LogStore, error) { return transport.InMemoryTransportLogStore(), nil } -// RoutingTable returns configure routing.Table. -func (c *Config) RoutingTable() (routing.Table, error) { - return routing.NewTable(routing.DefaultConfig()), nil -} - // AppsConfig decodes AppsConfig from a local json config file. func (c *Config) AppsConfig() (map[string]AppConfig, error) { apps := make(map[string]AppConfig) diff --git a/pkg/visor/config_test.go b/pkg/visor/config_test.go index acfa363410..b695e7e18b 100644 --- a/pkg/visor/config_test.go +++ b/pkg/visor/config_test.go @@ -2,7 +2,6 @@ package visor import ( "encoding/json" - "io/ioutil" "net/http" "net/http/httptest" "os" @@ -74,18 +73,6 @@ func TestTransportLogStore(t *testing.T) { require.NotNil(t, ls) } -func TestRoutingTable(t *testing.T) { - tmpfile, err := ioutil.TempFile("", "routing") - require.NoError(t, err) - defer func() { - require.NoError(t, os.Remove(tmpfile.Name())) - }() - - conf := Config{} - _, err = conf.RoutingTable() - require.NoError(t, err) -} - func TestAppsConfig(t *testing.T) { conf := Config{Version: "1.0"} conf.Apps = []AppConfig{ diff --git a/pkg/visor/rpc.go b/pkg/visor/rpc.go index ff459d5d49..b78b68e8f2 100644 --- a/pkg/visor/rpc.go +++ b/pkg/visor/rpc.go @@ -166,7 +166,7 @@ func (r *RPC) Summary(_ *struct{}, out *Summary) error { AppProtoVersion: supportedProtocolVersion, Apps: r.visor.Apps(), Transports: summaries, - RoutesCount: r.visor.rt.Count(), + RoutesCount: r.visor.router.RoutesCount(), } return nil } @@ -354,25 +354,25 @@ func (r *RPC) DiscoverTransportByID(id *uuid.UUID, out *transport.EntryWithStatu // RoutingRules obtains all routing rules of the RoutingTable. func (r *RPC) RoutingRules(_ *struct{}, out *[]routing.Rule) error { - *out = r.visor.rt.AllRules() + *out = r.visor.router.Rules() return nil } // RoutingRule obtains a routing rule of given RouteID. func (r *RPC) RoutingRule(key *routing.RouteID, rule *routing.Rule) error { var err error - *rule, err = r.visor.rt.Rule(*key) + *rule, err = r.visor.router.Rule(*key) return err } // SaveRoutingRule saves a routing rule. func (r *RPC) SaveRoutingRule(in *routing.Rule, _ *struct{}) error { - return r.visor.rt.SaveRule(*in) + return r.visor.router.SaveRule(*in) } // RemoveRoutingRule removes a RoutingRule based on given RouteID key. func (r *RPC) RemoveRoutingRule(key *routing.RouteID, _ *struct{}) error { - r.visor.rt.DelRules([]routing.RouteID{*key}) + r.visor.router.DelRules([]routing.RouteID{*key}) return nil } @@ -391,14 +391,14 @@ type LoopInfo struct { func (r *RPC) Loops(_ *struct{}, out *[]LoopInfo) error { var loops []LoopInfo - rules := r.visor.rt.AllRules() + rules := r.visor.router.Rules() for _, rule := range rules { if rule.Type() != routing.RuleConsume { continue } fwdRID := rule.NextRouteID() - rule, err := r.visor.rt.Rule(fwdRID) + rule, err := r.visor.router.Rule(fwdRID) if err != nil { return err } diff --git a/pkg/visor/visor.go b/pkg/visor/visor.go index 4b77d202c7..a92674ba31 100644 --- a/pkg/visor/visor.go +++ b/pkg/visor/visor.go @@ -75,7 +75,6 @@ type Visor struct { router router.Router n *snet.Network tm *transport.Manager - rt routing.Table pty *dmsgpty.Host // TODO(evanlinjin): Complete. Logger *logging.MasterLogger @@ -166,17 +165,11 @@ func NewVisor(cfg *Config, logger *logging.MasterLogger, restartCtx *restart.Con return nil, fmt.Errorf("transport manager: %s", err) } - visor.rt, err = cfg.RoutingTable() - if err != nil { - return nil, fmt.Errorf("routing table: %s", err) - } - rConfig := &router.Config{ Logger: visor.Logger.PackageLogger("router"), PubKey: pk, SecKey: sk, TransportManager: visor.tm, - RoutingTable: visor.rt, RouteFinder: rfclient.NewHTTP(cfg.Routing.RouteFinder, time.Duration(cfg.Routing.RouteFinderTimeout)), SetupNodes: cfg.Routing.SetupNodes, }