From b48c5613dd62f99854bfa2d87b62838b4b0d14b7 Mon Sep 17 00:00:00 2001 From: FUJITA Tomonori Date: Thu, 8 Nov 2018 10:44:02 +0900 Subject: server: Add PeerTable API go native API that corresponds to MonitorTable gRPC API. Signed-off-by: FUJITA Tomonori --- pkg/server/grpc_server.go | 60 +++++++---------------------------------------- 1 file changed, 8 insertions(+), 52 deletions(-) (limited to 'pkg/server/grpc_server.go') diff --git a/pkg/server/grpc_server.go b/pkg/server/grpc_server.go index b8a1f9e8..2b4fe02b 100644 --- a/pkg/server/grpc_server.go +++ b/pkg/server/grpc_server.go @@ -171,62 +171,18 @@ func (s *Server) ListPath(r *api.ListPathRequest, stream api.GobgpApi_ListPathSe } func (s *Server) MonitorTable(arg *api.MonitorTableRequest, stream api.GobgpApi_MonitorTableServer) error { - if arg == nil { - return fmt.Errorf("invalid request") - } - w, err := func() (*watcher, error) { - switch arg.Type { - case api.Resource_GLOBAL: - return s.bgpServer.watch(watchBestPath(arg.Current)), nil - case api.Resource_ADJ_IN: - if arg.PostPolicy { - return s.bgpServer.watch(watchPostUpdate(arg.Current)), nil - } - return s.bgpServer.watch(watchUpdate(arg.Current)), nil - default: - return nil, fmt.Errorf("unsupported resource type: %v", arg.Type) - } - }() + tm, err := s.bgpServer.NewTableMonitor(arg) if err != nil { - return nil + return err } - return func() error { - defer func() { w.Stop() }() + defer tm.Close() - sendPath := func(pathList []*table.Path) error { - for _, path := range pathList { - f := bgp.AfiSafiToRouteFamily(uint16(arg.Family.Afi), uint8(arg.Family.Safi)) - if path == nil || (arg.Family != nil && f != path.GetRouteFamily()) { - continue - } - if err := stream.Send(&api.MonitorTableResponse{Path: toPathApi(path, nil)}); err != nil { - return err - } - } - return nil - } - - for ev := range w.Event() { - switch msg := ev.(type) { - case *watchEventBestPath: - if err := sendPath(func() []*table.Path { - if len(msg.MultiPathList) > 0 { - l := make([]*table.Path, 0) - for _, p := range msg.MultiPathList { - l = append(l, p...) - } - return l - } else { - return msg.PathList - } - }()); err != nil { - return err - } - case *watchEventUpdate: - if err := sendPath(msg.PathList); err != nil { - return err - } + for v := range tm.Inbox { + if err := stream.Send(&api.MonitorTableResponse{ + Path: v, + }); err != nil { + return err } } return nil -- cgit v1.2.3