From 873bfcc73ce9de46604997808150f4836457548a Mon Sep 17 00:00:00 2001 From: Asim Aslam Date: Wed, 31 Jul 2019 16:46:55 +0100 Subject: [PATCH] Process/Stop router --- network/default.go | 107 ++++++++++++++++++++++++++++----------------- 1 file changed, 67 insertions(+), 40 deletions(-) diff --git a/network/default.go b/network/default.go index 838c35f5..415824a9 100644 --- a/network/default.go +++ b/network/default.go @@ -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 {