1f8f82463ca4b162615bca3a1c382741b2e2b00c
[govpp.git] / proxy / proxy.go
1 //  Copyright (c) 2019 Cisco and/or its affiliates.
2 //
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:
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
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.
14
15 package proxy
16
17 import (
18         "log"
19         "net"
20         "net/http"
21         "net/rpc"
22
23         "git.fd.io/govpp.git/adapter"
24         "git.fd.io/govpp.git/core"
25 )
26
27 // Server defines a proxy server that serves client requests to stats and binapi.
28 type Server struct {
29         rpc *rpc.Server
30
31         statsConn  *core.StatsConnection
32         binapiConn *core.Connection
33 }
34
35 func NewServer() *Server {
36         return &Server{
37                 rpc: rpc.NewServer(),
38         }
39 }
40
41 func (p *Server) ConnectStats(stats adapter.StatsAPI) error {
42         var err error
43         p.statsConn, err = core.ConnectStats(stats)
44         if err != nil {
45                 return err
46         }
47         return nil
48 }
49
50 func (p *Server) DisconnectStats() {
51         if p.statsConn != nil {
52                 p.statsConn.Disconnect()
53         }
54 }
55
56 func (p *Server) ConnectBinapi(binapi adapter.VppAPI) error {
57         var err error
58         p.binapiConn, err = core.Connect(binapi)
59         if err != nil {
60                 return err
61         }
62         return nil
63 }
64
65 func (p *Server) DisconnectBinapi() {
66         if p.binapiConn != nil {
67                 p.binapiConn.Disconnect()
68         }
69 }
70
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 {
75                         panic(err)
76                 }
77         }
78         if p.binapiConn != nil {
79                 ch, err := p.binapiConn.NewAPIChannel()
80                 if err != nil {
81                         panic(err)
82                 }
83                 binapiRPC := NewBinapiRPC(ch)
84                 if err := p.rpc.Register(binapiRPC); err != nil {
85                         panic(err)
86                 }
87         }
88
89         p.rpc.HandleHTTP(rpc.DefaultRPCPath, rpc.DefaultDebugPath)
90
91         l, e := net.Listen("tcp", addr)
92         if e != nil {
93                 log.Fatal("listen error:", e)
94         }
95         defer l.Close()
96
97         log.Printf("proxy serving on: %v", addr)
98
99         if err := http.Serve(l, nil); err != nil {
100                 log.Fatalln(err)
101         }
102 }