Process/Stop router
This commit is contained in:
parent
7884e889f4
commit
873bfcc73c
@ -22,46 +22,9 @@ type network struct {
|
||||
exit chan bool
|
||||
}
|
||||
|
||||
func (n *network) Name() string {
|
||||
return n.options.Name
|
||||
}
|
||||
|
||||
// Implements the server.ServeRequest method.
|
||||
func (n *network) ServeRequest(ctx context.Context, req server.Request, rsp server.Response) error {
|
||||
// If we're being called then execute our handlers
|
||||
if req.Service() == n.options.Name {
|
||||
return n.handler.ServeRequest(ctx, req, rsp)
|
||||
}
|
||||
|
||||
// execute the proxy
|
||||
return n.options.Proxy.ServeRequest(ctx, req, rsp)
|
||||
}
|
||||
|
||||
func (n *network) Connect() error {
|
||||
return n.options.Server.Start()
|
||||
}
|
||||
|
||||
func (n *network) Close() error {
|
||||
n.Lock()
|
||||
defer n.Unlock()
|
||||
|
||||
// check if we're connected
|
||||
if !n.connected {
|
||||
return nil
|
||||
}
|
||||
|
||||
advertChan, err := n.options.Router.Advertise()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
go n.run(advertChan)
|
||||
|
||||
// stop the server
|
||||
return n.options.Server.Stop()
|
||||
}
|
||||
|
||||
func (n *network) run(advertChan <-chan *router.Advert) {
|
||||
// process processes router advertisements and randomly sends the advert
|
||||
// to a node in the network. Over a period of time the routers should converge.
|
||||
func (n *network) process(advertChan <-chan *router.Advert) {
|
||||
for {
|
||||
select {
|
||||
// process local adverts and randomly fire them at other nodes
|
||||
@ -95,6 +58,70 @@ func (n *network) run(advertChan <-chan *router.Advert) {
|
||||
}
|
||||
}
|
||||
}
|
||||
func (n *network) Name() string {
|
||||
return n.options.Name
|
||||
}
|
||||
|
||||
// Implements the server.ServeRequest method.
|
||||
func (n *network) ServeRequest(ctx context.Context, req server.Request, rsp server.Response) error {
|
||||
// If we're being called then execute our handlers
|
||||
if req.Service() == n.options.Name {
|
||||
return n.handler.ServeRequest(ctx, req, rsp)
|
||||
}
|
||||
|
||||
// execute the proxy
|
||||
return n.options.Proxy.ServeRequest(ctx, req, rsp)
|
||||
}
|
||||
|
||||
func (n *network) Connect() error {
|
||||
n.Lock()
|
||||
defer n.Unlock()
|
||||
|
||||
// check if we're connected
|
||||
if !n.connected {
|
||||
return nil
|
||||
}
|
||||
|
||||
// start advertising
|
||||
advertChan, err := n.options.Router.Advertise()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// process the adverts
|
||||
go n.process(advertChan)
|
||||
|
||||
// start the server
|
||||
if err := n.options.Server.Start(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// set connected to true
|
||||
n.connected = true
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (n *network) Close() error {
|
||||
n.Lock()
|
||||
defer n.Unlock()
|
||||
|
||||
// check if we're connected
|
||||
if !n.connected {
|
||||
return nil
|
||||
}
|
||||
|
||||
// set connected to false
|
||||
n.connected = false
|
||||
|
||||
// stop the router
|
||||
if err := n.options.Router.Stop(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// stop the server
|
||||
return n.options.Server.Stop()
|
||||
}
|
||||
|
||||
// newNetwork returns a new network node
|
||||
func newNetwork(opts ...Option) Network {
|
||||
|
Loading…
x
Reference in New Issue
Block a user