refactor and cleanup some router code
This commit is contained in:
@@ -2,7 +2,6 @@ package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"sync"
|
||||
@@ -19,7 +18,6 @@ type svc struct {
|
||||
callOpts []client.CallOption
|
||||
router pb.RouterService
|
||||
table *table
|
||||
status *router.Status
|
||||
exit chan bool
|
||||
errChan chan error
|
||||
advertChan chan *router.Advert
|
||||
@@ -43,16 +41,9 @@ func NewRouter(opts ...router.Option) router.Router {
|
||||
cli = options.Client
|
||||
}
|
||||
|
||||
// set the status to Stopped
|
||||
status := &router.Status{
|
||||
Code: router.Stopped,
|
||||
Error: nil,
|
||||
}
|
||||
|
||||
// NOTE: should we have Client/Service option in router.Options?
|
||||
s := &svc{
|
||||
opts: options,
|
||||
status: status,
|
||||
router: pb.NewRouterService(router.DefaultName, cli),
|
||||
}
|
||||
|
||||
@@ -98,12 +89,6 @@ func (s *svc) Table() router.Table {
|
||||
func (s *svc) Start() error {
|
||||
s.Lock()
|
||||
defer s.Unlock()
|
||||
|
||||
s.status = &router.Status{
|
||||
Code: router.Running,
|
||||
Error: nil,
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -169,21 +154,16 @@ func (s *svc) Advertise() (<-chan *router.Advert, error) {
|
||||
s.Lock()
|
||||
defer s.Unlock()
|
||||
|
||||
switch s.status.Code {
|
||||
case router.Running, router.Advertising:
|
||||
stream, err := s.router.Advertise(context.Background(), &pb.Request{}, s.callOpts...)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed getting advert stream: %s", err)
|
||||
}
|
||||
// create advertise and event channels
|
||||
advertChan := make(chan *router.Advert)
|
||||
go s.advertiseEvents(advertChan, stream)
|
||||
return advertChan, nil
|
||||
case router.Stopped:
|
||||
return nil, fmt.Errorf("not running")
|
||||
stream, err := s.router.Advertise(context.Background(), &pb.Request{}, s.callOpts...)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed getting advert stream: %s", err)
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("error: %s", s.status.Error)
|
||||
// create advertise and event channels
|
||||
advertChan := make(chan *router.Advert)
|
||||
go s.advertiseEvents(advertChan, stream)
|
||||
|
||||
return advertChan, nil
|
||||
}
|
||||
|
||||
// Process processes incoming adverts
|
||||
@@ -220,55 +200,6 @@ func (s *svc) Process(advert *router.Advert) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Status returns router status
|
||||
func (s *svc) Status() router.Status {
|
||||
s.Lock()
|
||||
defer s.Unlock()
|
||||
|
||||
// check if its stopped
|
||||
select {
|
||||
case <-s.exit:
|
||||
return router.Status{
|
||||
Code: router.Stopped,
|
||||
Error: nil,
|
||||
}
|
||||
default:
|
||||
// don't block
|
||||
}
|
||||
|
||||
// check the remote router
|
||||
rsp, err := s.router.Status(context.Background(), &pb.Request{}, s.callOpts...)
|
||||
if err != nil {
|
||||
return router.Status{
|
||||
Code: router.Error,
|
||||
Error: err,
|
||||
}
|
||||
}
|
||||
|
||||
code := router.Running
|
||||
var serr error
|
||||
|
||||
switch rsp.Status.Code {
|
||||
case "running":
|
||||
code = router.Running
|
||||
case "advertising":
|
||||
code = router.Advertising
|
||||
case "stopped":
|
||||
code = router.Stopped
|
||||
case "error":
|
||||
code = router.Error
|
||||
}
|
||||
|
||||
if len(rsp.Status.Error) > 0 {
|
||||
serr = errors.New(rsp.Status.Error)
|
||||
}
|
||||
|
||||
return router.Status{
|
||||
Code: code,
|
||||
Error: serr,
|
||||
}
|
||||
}
|
||||
|
||||
// Remote router cannot be stopped
|
||||
func (s *svc) Stop() error {
|
||||
s.Lock()
|
||||
|
||||
Reference in New Issue
Block a user