1 // Copyright (c) 2019 Cisco and/or its affiliates.
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at:
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
24 "git.fd.io/govpp.git/adapter"
27 // Server defines a proxy server that serves client requests to stats and binapi.
35 func NewServer() (*Server, error) {
38 statsRPC: &StatsRPC{},
39 binapiRPC: &BinapiRPC{},
42 if err := srv.rpc.Register(srv.statsRPC); err != nil {
46 if err := srv.rpc.Register(srv.binapiRPC); err != nil {
53 func (p *Server) ConnectStats(stats adapter.StatsAPI) error {
54 return p.statsRPC.connect(stats)
57 func (p *Server) DisconnectStats() {
58 p.statsRPC.disconnect()
61 func (p *Server) ConnectBinapi(binapi adapter.VppAPI) error {
62 return p.binapiRPC.connect(binapi)
65 func (p *Server) DisconnectBinapi() {
66 p.binapiRPC.disconnect()
69 func (p *Server) ServeHTTP(w http.ResponseWriter, req *http.Request) {
70 p.rpc.ServeHTTP(w, req)
73 func (p *Server) ServeCodec(codec rpc.ServerCodec) {
74 p.rpc.ServeCodec(codec)
77 func (p *Server) ServeConn(conn io.ReadWriteCloser) {
81 func (p *Server) ListenAndServe(addr string) error {
82 p.rpc.HandleHTTP(rpc.DefaultRPCPath, rpc.DefaultDebugPath)
84 l, e := net.Listen("tcp", addr)
86 return fmt.Errorf("listen failed: %v", e)
90 log.Printf("proxy serving on: %v", addr)
92 return http.Serve(l, nil)