Add Address method. Start and Stop router/server.

This commit is contained in:
Milos Gajdos 2019-08-21 19:16:18 +01:00
parent 6c1f1d66f7
commit a09d5d2e9a
No known key found for this signature in database
GPG Key ID: 8B31058CC55DFD4F
2 changed files with 28 additions and 7 deletions

View File

@ -6,25 +6,25 @@ import (
"github.com/golang/protobuf/proto" "github.com/golang/protobuf/proto"
"github.com/micro/go-micro/client" "github.com/micro/go-micro/client"
rtr "github.com/micro/go-micro/client/selector/router"
"github.com/micro/go-micro/proxy" "github.com/micro/go-micro/proxy"
"github.com/micro/go-micro/router" "github.com/micro/go-micro/router"
pb "github.com/micro/go-micro/router/proto" pb "github.com/micro/go-micro/router/proto"
"github.com/micro/go-micro/server" "github.com/micro/go-micro/server"
"github.com/micro/go-micro/transport" "github.com/micro/go-micro/transport"
"github.com/micro/go-micro/tunnel" "github.com/micro/go-micro/tunnel"
tr "github.com/micro/go-micro/tunnel/transport" trn "github.com/micro/go-micro/tunnel/transport"
"github.com/micro/go-micro/util/log" "github.com/micro/go-micro/util/log"
) )
var ( var (
// ControlChannel is the name of the tunnel channel for passing contron message // ControlChannel is the name of the tunnel channel for passing contron message
ControlChannel = "control-msg" ControlChannel = "control"
) )
// network implements Network interface // network implements Network interface
type network struct { type network struct {
// options configure the network // options configure the network
// TODO: we might end up embedding
options Options options Options
// rtr is network router // rtr is network router
router.Router router.Router
@ -59,7 +59,7 @@ func newNetwork(opts ...Option) Network {
// create tunnel client with tunnel transport // create tunnel client with tunnel transport
tunTransport := transport.NewTransport( tunTransport := transport.NewTransport(
tr.WithTunnel(options.Tunnel), trn.WithTunnel(options.Tunnel),
) )
// srv is network server // srv is network server
@ -70,6 +70,11 @@ func newNetwork(opts ...Option) Network {
// client is network client // client is network client
client := client.NewClient( client := client.NewClient(
client.Transport(tunTransport), client.Transport(tunTransport),
client.Selector(
rtr.NewSelector(
rtr.WithRouter(options.Router),
),
),
) )
return &network{ return &network{
@ -89,7 +94,7 @@ func (n *network) Name() string {
// Address returns network bind address // Address returns network bind address
func (n *network) Address() string { func (n *network) Address() string {
return n.options.Address return n.Tunnel.Address()
} }
func (n *network) resolveNodes() ([]string, error) { func (n *network) resolveNodes() ([]string, error) {
@ -272,7 +277,11 @@ func (n *network) Connect() error {
// keep resolving network nodes // keep resolving network nodes
go n.resolve() go n.resolve()
// TODO: do we assume the router has been started? // start the router
if err := n.options.Router.Start(); err != nil {
return err
}
// start advertising routes // start advertising routes
advertChan, err := n.options.Router.Advertise() advertChan, err := n.options.Router.Advertise()
if err != nil { if err != nil {
@ -284,6 +293,11 @@ func (n *network) Connect() error {
// process routes // process routes
go n.process(client) go n.process(client)
// start the server
if err := n.srv.Start(); err != nil {
return err
}
// set connected to true // set connected to true
n.connected = true n.connected = true
@ -291,6 +305,11 @@ func (n *network) Connect() error {
} }
func (n *network) close() error { func (n *network) close() error {
// stop the server
if err := n.srv.Stop(); err != nil {
return err
}
// stop the router // stop the router
if err := n.Router.Stop(); err != nil { if err := n.Router.Stop(); err != nil {
return err return err

View File

@ -13,7 +13,7 @@ var (
DefaultName = "go.micro.network" DefaultName = "go.micro.network"
// DefaultAddress is default network address // DefaultAddress is default network address
DefaultAddress = ":0" DefaultAddress = ":0"
// ResolveTime ddefines the time we periodically resolve network nodes // ResolveTime ddefines the time to periodically resolve network nodes
ResolveTime = 1 * time.Minute ResolveTime = 1 * time.Minute
) )
@ -21,6 +21,8 @@ var (
type Network interface { type Network interface {
// Name of the network // Name of the network
Name() string Name() string
// Address returns network bind address
Address() string
// Connect starts the resolver and tunnel server // Connect starts the resolver and tunnel server
Connect() error Connect() error
// Close stops the tunnel and resolving // Close stops the tunnel and resolving