diff --git a/router/default.go b/router/default.go index 53c65618..1eefd644 100644 --- a/router/default.go +++ b/router/default.go @@ -1,27 +1,32 @@ package router import ( + "github.com/google/uuid" "github.com/micro/go-micro/registry" "github.com/micro/go-micro/registry/gossip" ) type router struct { - opts Options - goss registry.Registry - t Table + opts Options + goss registry.Registry + table Table + id uuid.UUID } func newRouter(opts ...Option) Router { // TODO: figure out how to supply gossip registry options r := &router{ - goss: gossip.NewRegistry(), - t: NewTable(), + goss: gossip.NewRegistry(), + table: NewTable(), + id: uuid.New(), } for _, o := range opts { o(&r.opts) } + // TODO: need to start some gossip.Registry watch here + return r } @@ -63,10 +68,15 @@ func (r *router) Lookup(q Query) ([]*Entry, error) { // Table returns routing table func (r *router) Table() Table { - return nil + return r.table } -// Address returns router's network address +// Network returns router's micro network +func (r *router) Network() string { + return r.opts.Network +} + +// Address returns router's bind address func (r *router) Address() string { return r.opts.Address } diff --git a/router/options.go b/router/options.go index 4db04ab7..277e1a30 100644 --- a/router/options.go +++ b/router/options.go @@ -8,6 +8,8 @@ import ( type Options struct { // Address is router address Address string + // Network defines micro network address + Network string // RIB is Routing Information Base RIB RIB // Table is routing table @@ -16,13 +18,6 @@ type Options struct { Context context.Context } -// RIBase allows to configure RIB -func RIBase(r RIB) Option { - return func(o *Options) { - o.RIB = r - } -} - // Address allows to set router address func Address(a string) Option { return func(o *Options) { @@ -30,6 +25,20 @@ func Address(a string) Option { } } +// Network allows to set router network +func Network(n string) Option { + return func(o *Options) { + o.Network = n + } +} + +// RIBase allows to configure RIB +func RIBase(r RIB) Option { + return func(o *Options) { + o.RIB = r + } +} + // RoutingTable allows to specify custom routing table func RoutingTable(t Table) Option { return func(o *Options) { diff --git a/router/router.go b/router/router.go index f8f94cce..72af0979 100644 --- a/router/router.go +++ b/router/router.go @@ -24,6 +24,8 @@ type Router interface { Table() Table // Address is Router adddress Address() string + // Network defines network router is in + Network() string // String implemens fmt.Stringer interface String() string } diff --git a/router/table.go b/router/table.go index 15933e8e..1f2de4d3 100644 --- a/router/table.go +++ b/router/table.go @@ -1,6 +1,9 @@ package router -import "errors" +import ( + "errors" + "sync" +) var ( // DefaultRouter returns default micro router @@ -29,8 +32,8 @@ type Table interface { // Entry is micro network routing table entry type Entry struct { - // DestAddr is destination address - DestAddr string + // Addr is destination address + Addr string // NetID is micro network ID NetID string // Hop is the next route hop @@ -44,6 +47,7 @@ type Entry struct { type table struct { // m stores routing table map m map[string][]Entry + sync.RWMutex } // NewTable creates new routing table and returns it