2019-07-25 23:19:05 +01:00
|
|
|
package router
|
2019-06-18 18:33:05 +01:00
|
|
|
|
2019-06-13 23:28:47 +02:00
|
|
|
// QueryOption sets routing table query options
|
2019-06-13 12:09:49 +01:00
|
|
|
type QueryOption func(*QueryOptions)
|
|
|
|
|
2019-06-13 23:28:47 +02:00
|
|
|
// QueryOptions are routing table query options
|
2019-06-06 23:29:24 +01:00
|
|
|
type QueryOptions struct {
|
2019-07-09 15:45:42 +01:00
|
|
|
// Service is destination service name
|
|
|
|
Service string
|
|
|
|
// Gateway is route gateway
|
|
|
|
Gateway string
|
2019-06-19 21:22:14 +01:00
|
|
|
// Network is network address
|
2019-06-12 22:30:42 +01:00
|
|
|
Network string
|
2019-06-06 23:29:24 +01:00
|
|
|
}
|
|
|
|
|
2019-07-09 15:45:42 +01:00
|
|
|
// QueryService sets destination address
|
|
|
|
func QueryService(s string) QueryOption {
|
2019-06-06 23:29:24 +01:00
|
|
|
return func(o *QueryOptions) {
|
2019-07-09 15:45:42 +01:00
|
|
|
o.Service = s
|
2019-06-06 23:29:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-09 15:45:42 +01:00
|
|
|
// QueryGateway sets route gateway
|
|
|
|
func QueryGateway(g string) QueryOption {
|
2019-06-06 23:29:24 +01:00
|
|
|
return func(o *QueryOptions) {
|
2019-07-09 15:45:42 +01:00
|
|
|
o.Gateway = g
|
2019-06-06 23:29:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-09 15:45:42 +01:00
|
|
|
// QueryNetwork sets route network address
|
|
|
|
func QueryNetwork(n string) QueryOption {
|
2019-06-16 23:09:59 +01:00
|
|
|
return func(o *QueryOptions) {
|
2019-07-09 15:45:42 +01:00
|
|
|
o.Network = n
|
2019-06-19 21:22:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-13 23:28:47 +02:00
|
|
|
// Query is routing table query
|
2019-06-06 23:29:24 +01:00
|
|
|
type Query interface {
|
|
|
|
// Options returns query options
|
|
|
|
Options() QueryOptions
|
|
|
|
}
|
|
|
|
|
2019-06-13 23:28:47 +02:00
|
|
|
// query is a basic implementation of Query
|
2019-06-06 23:29:24 +01:00
|
|
|
type query struct {
|
|
|
|
opts QueryOptions
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewQuery creates new query and returns it
|
|
|
|
func NewQuery(opts ...QueryOption) Query {
|
2019-06-12 22:30:42 +01:00
|
|
|
// default options
|
|
|
|
qopts := QueryOptions{
|
2019-07-09 15:45:42 +01:00
|
|
|
Service: "*",
|
|
|
|
Gateway: "*",
|
|
|
|
Network: "*",
|
2019-06-12 22:30:42 +01:00
|
|
|
}
|
2019-06-06 23:29:24 +01:00
|
|
|
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&qopts)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &query{
|
|
|
|
opts: qopts,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Options returns query options
|
|
|
|
func (q *query) Options() QueryOptions {
|
|
|
|
return q.opts
|
|
|
|
}
|
2019-06-26 16:03:19 +01:00
|
|
|
|
|
|
|
// String prints routing table query in human readable form
|
|
|
|
func (q query) String() string {
|
2019-07-09 16:17:18 +01:00
|
|
|
return "query"
|
2019-06-26 16:03:19 +01:00
|
|
|
}
|