Added network ID option. Added mutex to routing table.
This commit is contained in:
parent
e4311c3a10
commit
d7f0db04ec
@ -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
|
||||
}
|
||||
|
@ -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) {
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user