Simplified API. Correct Router initialization. Debug printing.
This commit is contained in:
		| @@ -4,31 +4,36 @@ import ( | |||||||
| 	"fmt" | 	"fmt" | ||||||
| 	"strings" | 	"strings" | ||||||
|  |  | ||||||
| 	"github.com/google/uuid" |  | ||||||
| 	"github.com/micro/go-micro/registry" | 	"github.com/micro/go-micro/registry" | ||||||
| 	"github.com/micro/go-micro/registry/gossip" | 	"github.com/micro/go-micro/registry/gossip" | ||||||
|  | 	"github.com/olekukonko/tablewriter" | ||||||
| ) | ) | ||||||
|  |  | ||||||
| type router struct { | type router struct { | ||||||
| 	opts  Options | 	opts Options | ||||||
| 	goss  registry.Registry | 	goss registry.Registry | ||||||
| 	table Table |  | ||||||
| 	id    uuid.UUID |  | ||||||
| } | } | ||||||
|  |  | ||||||
| func newRouter(opts ...Option) Router { | func newRouter(opts ...Option) Router { | ||||||
| 	// TODO: figure out how to supply gossip registry options | 	// set default options | ||||||
| 	r := &router{ | 	options := Options{ | ||||||
| 		goss:  gossip.NewRegistry(), | 		Table: NewTable(), | ||||||
| 		table: NewTable(), |  | ||||||
| 		id:    uuid.New(), |  | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	for _, o := range opts { | 	for _, o := range opts { | ||||||
| 		o(&r.opts) | 		o(&options) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	// TODO: need to start some gossip.Registry watch here | 	goss := gossip.NewRegistry( | ||||||
|  | 		gossip.Address(options.GossipAddr), | ||||||
|  | 	) | ||||||
|  |  | ||||||
|  | 	r := &router{ | ||||||
|  | 		opts: options, | ||||||
|  | 		goss: goss, | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	// TODO: start gossip.Registry watch here | ||||||
|  |  | ||||||
| 	return r | 	return r | ||||||
| } | } | ||||||
| @@ -46,37 +51,9 @@ func (r *router) Options() Options { | |||||||
| 	return r.opts | 	return r.opts | ||||||
| } | } | ||||||
|  |  | ||||||
| // Add adds new entry into routing table with given options. |  | ||||||
| // It returns error if the entry could not be added. |  | ||||||
| func (r *router) Add(e Route) error { |  | ||||||
| 	return r.table.Add(e) |  | ||||||
| } |  | ||||||
|  |  | ||||||
| // Remove removes entry from the routing table. |  | ||||||
| // It returns error if either the entry could not be removed or it does not exist. |  | ||||||
| func (r *router) Remove(e Route) error { |  | ||||||
| 	return r.table.Remove(e) |  | ||||||
| } |  | ||||||
|  |  | ||||||
| // Update updates an entry in the router's routing table |  | ||||||
| // It returns error if the entry was not found or it failed to be updated. |  | ||||||
| func (r *router) Update(opts ...RouteOption) error { |  | ||||||
| 	return r.table.Update(opts...) |  | ||||||
| } |  | ||||||
|  |  | ||||||
| // Lookup makes a query lookup and returns all matching entries |  | ||||||
| func (r *router) Lookup(q Query) ([]*Route, error) { |  | ||||||
| 	return nil, ErrNotImplemented |  | ||||||
| } |  | ||||||
|  |  | ||||||
| // Table returns routing table | // Table returns routing table | ||||||
| func (r *router) Table() Table { | func (r *router) Table() Table { | ||||||
| 	return r.table | 	return r.opts.Table | ||||||
| } |  | ||||||
|  |  | ||||||
| // Network returns router's micro network |  | ||||||
| func (r *router) Network() string { |  | ||||||
| 	return r.opts.Network |  | ||||||
| } | } | ||||||
|  |  | ||||||
| // Address returns router's bind address | // Address returns router's bind address | ||||||
| @@ -84,21 +61,29 @@ func (r *router) Address() string { | |||||||
| 	return r.opts.Address | 	return r.opts.Address | ||||||
| } | } | ||||||
|  |  | ||||||
|  | // Network returns router's micro network | ||||||
|  | func (r *router) Network() string { | ||||||
|  | 	return r.opts.NetworkAddr | ||||||
|  | } | ||||||
|  |  | ||||||
| // String prints debugging information about router | // String prints debugging information about router | ||||||
| func (r *router) String() string { | func (r *router) String() string { | ||||||
| 	sb := &strings.Builder{} | 	sb := &strings.Builder{} | ||||||
|  |  | ||||||
| 	s := fmt.Sprintf("Router ID: %s\n", r.id.String()) | 	table := tablewriter.NewWriter(sb) | ||||||
| 	sb.WriteString(s) | 	table.SetHeader([]string{"ID", "Address", "Gossip", "Network", "Table"}) | ||||||
|  |  | ||||||
| 	s = fmt.Sprintf("Router Local Address: %s\n", r.opts.Address) | 	data := []string{ | ||||||
| 	sb.WriteString(s) | 		r.opts.ID, | ||||||
|  | 		r.opts.Address, | ||||||
|  | 		r.opts.GossipAddr, | ||||||
|  | 		r.opts.NetworkAddr, | ||||||
|  | 		fmt.Sprintf("%d", r.opts.Table.Size()), | ||||||
|  | 	} | ||||||
|  | 	table.Append(data) | ||||||
|  |  | ||||||
| 	s = fmt.Sprintf("Router Network Address: %s\n", r.opts.Network) | 	// render table into sb | ||||||
| 	sb.WriteString(s) | 	table.Render() | ||||||
|  |  | ||||||
| 	s = fmt.Sprintf("Routing table size: %d\n", r.opts.Table.Size()) |  | ||||||
| 	sb.WriteString(s) |  | ||||||
|  |  | ||||||
| 	return sb.String() | 	return sb.String() | ||||||
| } | } | ||||||
|   | |||||||
| @@ -16,9 +16,8 @@ type RouteOptions struct { | |||||||
| 	DestAddr string | 	DestAddr string | ||||||
| 	// Hop is the next route hop | 	// Hop is the next route hop | ||||||
| 	Hop Router | 	Hop Router | ||||||
| 	// SrcAddr defines local routing address | 	// Network defines micro network | ||||||
| 	// On local networkss, this will be the address of local router | 	Network string | ||||||
| 	SrcAddr string |  | ||||||
| 	// Metric is route cost metric | 	// Metric is route cost metric | ||||||
| 	Metric int | 	Metric int | ||||||
| 	// Policy defines route addition policy | 	// Policy defines route addition policy | ||||||
| @@ -39,10 +38,10 @@ func Hop(r Router) RouteOption { | |||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  |  | ||||||
| // SrcAddr sets source address | // Network sets micro network | ||||||
| func SrcAddr(a string) RouteOption { | func Network(n string) RouteOption { | ||||||
| 	return func(o *RouteOptions) { | 	return func(o *RouteOptions) { | ||||||
| 		o.SrcAddr = a | 		o.Network = n | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -6,10 +6,14 @@ import ( | |||||||
|  |  | ||||||
| // Options allows to set Router options | // Options allows to set Router options | ||||||
| type Options struct { | type Options struct { | ||||||
|  | 	// ID is router ID | ||||||
|  | 	ID string | ||||||
| 	// Address is router address | 	// Address is router address | ||||||
| 	Address string | 	Address string | ||||||
| 	// Network defines micro network address | 	// GossipAddr is router gossip address | ||||||
| 	Network string | 	GossipAddr string | ||||||
|  | 	// NetworkAddr defines micro network address | ||||||
|  | 	NetworkAddr string | ||||||
| 	// RIB is Routing Information Base | 	// RIB is Routing Information Base | ||||||
| 	RIB RIB | 	RIB RIB | ||||||
| 	// Table is routing table | 	// Table is routing table | ||||||
| @@ -18,6 +22,13 @@ type Options struct { | |||||||
| 	Context context.Context | 	Context context.Context | ||||||
| } | } | ||||||
|  |  | ||||||
|  | // ID sets Router ID | ||||||
|  | func ID(id string) Option { | ||||||
|  | 	return func(o *Options) { | ||||||
|  | 		o.ID = id | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  |  | ||||||
| // Address allows to set router address | // Address allows to set router address | ||||||
| func Address(a string) Option { | func Address(a string) Option { | ||||||
| 	return func(o *Options) { | 	return func(o *Options) { | ||||||
| @@ -25,10 +36,17 @@ func Address(a string) Option { | |||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  |  | ||||||
| // Network allows to set router network | // GossipAddress allows to set router address | ||||||
| func Network(n string) Option { | func GossipAddress(a string) Option { | ||||||
| 	return func(o *Options) { | 	return func(o *Options) { | ||||||
| 		o.Network = n | 		o.GossipAddr = a | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  |  | ||||||
|  | // NetworkAddr allows to set router network | ||||||
|  | func NetworkAddr(n string) Option { | ||||||
|  | 	return func(o *Options) { | ||||||
|  | 		o.NetworkAddr = n | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -7,19 +7,11 @@ type Router interface { | |||||||
| 	Init(...Option) error | 	Init(...Option) error | ||||||
| 	// Options returns Router options | 	// Options returns Router options | ||||||
| 	Options() Options | 	Options() Options | ||||||
| 	// Add adds new entry into routing table |  | ||||||
| 	Add(Route) error |  | ||||||
| 	// Remove removes entry from the routing table |  | ||||||
| 	Remove(Route) error |  | ||||||
| 	// Update updates entry in the routing table |  | ||||||
| 	Update(...RouteOption) error |  | ||||||
| 	// Lookup queries the routing table and returns matching entries |  | ||||||
| 	Lookup(Query) ([]*Route, error) |  | ||||||
| 	// Table returns routing table | 	// Table returns routing table | ||||||
| 	Table() Table | 	Table() Table | ||||||
| 	// Address returns the router bind adddress | 	// Address returns router gossip adddress | ||||||
| 	Address() string | 	Address() string | ||||||
| 	// Network returns router's micro network bind address | 	// Network returns micro network address | ||||||
| 	Network() string | 	Network() string | ||||||
| 	// String implemens fmt.Stringer interface | 	// String implemens fmt.Stringer interface | ||||||
| 	String() string | 	String() string | ||||||
| @@ -42,15 +34,5 @@ type QueryOption func(*QueryOptions) | |||||||
|  |  | ||||||
| // NewRouter creates new Router and returns it | // NewRouter creates new Router and returns it | ||||||
| func NewRouter(opts ...Option) Router { | func NewRouter(opts ...Option) Router { | ||||||
| 	// set default options |  | ||||||
| 	ropts := Options{ |  | ||||||
| 		// Default table |  | ||||||
| 		Table: NewTable(), |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	for _, o := range opts { |  | ||||||
| 		o(&ropts) |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	return newRouter(opts...) | 	return newRouter(opts...) | ||||||
| } | } | ||||||
|   | |||||||
| @@ -137,28 +137,31 @@ func (t *table) String() string { | |||||||
|  |  | ||||||
| 	// create nice table printing structure | 	// create nice table printing structure | ||||||
| 	table := tablewriter.NewWriter(sb) | 	table := tablewriter.NewWriter(sb) | ||||||
| 	table.SetHeader([]string{"Dest", "Hop", "Src", "Metric"}) | 	table.SetHeader([]string{"Service", "Gateway", "Network", "Metric"}) | ||||||
|  |  | ||||||
| 	for _, route := range t.m { | 	for _, route := range t.m { | ||||||
| 		strRoute := []string{ | 		strRoute := []string{ | ||||||
| 			route.Options().DestAddr, | 			route.Options().DestAddr, | ||||||
| 			route.Options().Hop.Address(), | 			route.Options().Hop.Address(), | ||||||
| 			fmt.Sprintf("%d", route.Options().SrcAddr), | 			route.Options().Network, | ||||||
| 			fmt.Sprintf("%d", route.Options().Metric), | 			fmt.Sprintf("%d", route.Options().Metric), | ||||||
| 		} | 		} | ||||||
| 		table.Append(strRoute) | 		table.Append(strRoute) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|  | 	// render table into sb | ||||||
|  | 	table.Render() | ||||||
|  |  | ||||||
| 	return sb.String() | 	return sb.String() | ||||||
| } | } | ||||||
|  |  | ||||||
| func (t *table) hash(r Route) uint64 { | func (t *table) hash(r Route) uint64 { | ||||||
| 	srcAddr := r.Options().SrcAddr |  | ||||||
| 	destAddr := r.Options().DestAddr | 	destAddr := r.Options().DestAddr | ||||||
| 	routerAddr := r.Options().Hop.Address() | 	routerAddr := r.Options().Hop.Address() | ||||||
|  | 	network := r.Options().Network | ||||||
|  |  | ||||||
| 	t.h.Reset() | 	t.h.Reset() | ||||||
| 	t.h.Write([]byte(srcAddr + destAddr + routerAddr)) | 	t.h.Write([]byte(destAddr + routerAddr + network)) | ||||||
|  |  | ||||||
| 	return t.h.Sum64() | 	return t.h.Sum64() | ||||||
| } | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user