-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
68 lines (57 loc) · 1.17 KB
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package gnmi
import (
"fmt"
"net"
"github.com/freeconf/restconf/device"
pb_gnmi "github.com/openconfig/gnmi/proto/gnmi"
"google.golang.org/grpc"
)
var Version = "0.0.0"
type ServerOpts struct {
Port string
}
type Server struct {
opts ServerOpts
grpcServer *grpc.Server
listener net.Listener
driver *driver
device *device.Local
}
func NewServer(d *device.Local) *Server {
s := &Server{device: d}
if err := d.Add("fc-gnmi", Manage(s)); err != nil {
panic(err)
}
return s
}
func (s *Server) Options() ServerOpts {
return s.opts
}
func (s *Server) Apply(opts ServerOpts) error {
var err error
if s.grpcServer != nil {
s.grpcServer.GracefulStop()
s.grpcServer = nil
}
if s.listener != nil {
s.listener.Close()
s.listener = nil
}
s.grpcServer = grpc.NewServer()
s.driver = newDriver(s.device)
pb_gnmi.RegisterGNMIServer(s.grpcServer, s.driver)
s.listener, err = net.Listen("tcp", opts.Port)
if err != nil {
return err
}
s.opts = opts
s.start()
return nil
}
func (s *Server) start() {
go func() {
if err := s.grpcServer.Serve(s.listener); err != nil {
panic(fmt.Sprintf("error starting or stopping gRPC server. %s", err))
}
}()
}