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.
23 "git.fd.io/govpp.git/adapter"
24 "git.fd.io/govpp.git/core"
27 // Server defines a proxy server that serves client requests to stats and binapi.
31 statsConn *core.StatsConnection
32 binapiConn *core.Connection
35 func NewServer() *Server {
41 func (p *Server) ConnectStats(stats adapter.StatsAPI) error {
43 p.statsConn, err = core.ConnectStats(stats)
50 func (p *Server) DisconnectStats() {
51 if p.statsConn != nil {
52 p.statsConn.Disconnect()
56 func (p *Server) ConnectBinapi(binapi adapter.VppAPI) error {
58 p.binapiConn, err = core.Connect(binapi)
65 func (p *Server) DisconnectBinapi() {
66 if p.binapiConn != nil {
67 p.binapiConn.Disconnect()
71 func (p *Server) ListenAndServe(addr string) {
72 if p.statsConn != nil {
73 statsRPC := NewStatsRPC(p.statsConn)
74 if err := p.rpc.Register(statsRPC); err != nil {
78 if p.binapiConn != nil {
79 ch, err := p.binapiConn.NewAPIChannel()
83 binapiRPC := NewBinapiRPC(ch)
84 if err := p.rpc.Register(binapiRPC); err != nil {
89 p.rpc.HandleHTTP(rpc.DefaultRPCPath, rpc.DefaultDebugPath)
91 l, e := net.Listen("tcp", addr)
93 log.Fatal("listen error:", e)
97 log.Printf("proxy serving on: %v", addr)
99 if err := http.Serve(l, nil); err != nil {