@@ -13,15 +13,17 @@ import (
1313 "github.com/Azure/azure-container-networking/log"
1414)
1515
16- // Listener object
16+ // Listener represents an HTTP listener.
1717type Listener struct {
1818 protocol string
1919 localAddress string
20+ endpoints []string
21+ active bool
2022 l net.Listener
2123 mux * http.ServeMux
2224}
2325
24- // Creates a new Listener.
26+ // NewListener creates a new Listener.
2527func NewListener (protocol string , localAddress string ) (* Listener , error ) {
2628 listener := Listener {
2729 protocol : protocol ,
@@ -59,16 +61,17 @@ func (listener *Listener) Start(errChan chan error) error {
5961 errChan <- http .Serve (listener .l , listener .mux )
6062 }()
6163
64+ listener .active = true
6265 return nil
6366}
6467
65- // Stops listening for requests from libnetwork .
68+ // Stop stops listening for requests.
6669func (listener * Listener ) Stop () {
67-
68- // Succeed early if no socket was requested.
69- if listener .localAddress == "" {
70+ // Ignore if not active.
71+ if ! listener .active {
7072 return
7173 }
74+ listener .active = false
7275
7376 // Stop servicing requests.
7477 listener .l .Close ()
@@ -81,17 +84,27 @@ func (listener *Listener) Stop() {
8184 log .Printf ("[Listener] Stopped listening on %s" , listener .localAddress )
8285}
8386
84- // Returns the HTTP mux for the listener.
87+ // GetMux returns the HTTP mux for the listener.
8588func (listener * Listener ) GetMux () * http.ServeMux {
8689 return listener .mux
8790}
8891
89- // Registers a protocol handler.
92+ // GetEndpoints returns the list of registered protocol endpoints.
93+ func (listener * Listener ) GetEndpoints () []string {
94+ return listener .endpoints
95+ }
96+
97+ // AddEndpoint registers a protocol endpoint.
98+ func (listener * Listener ) AddEndpoint (endpoint string ) {
99+ listener .endpoints = append (listener .endpoints , endpoint )
100+ }
101+
102+ // AddHandler registers a protocol handler.
90103func (listener * Listener ) AddHandler (path string , handler func (http.ResponseWriter , * http.Request )) {
91104 listener .mux .HandleFunc (path , handler )
92105}
93106
94- // Decodes JSON payload.
107+ // Decode receives and decodes JSON payload to a request .
95108func (listener * Listener ) Decode (w http.ResponseWriter , r * http.Request , request interface {}) error {
96109 var err error
97110
@@ -108,7 +121,7 @@ func (listener *Listener) Decode(w http.ResponseWriter, r *http.Request, request
108121 return err
109122}
110123
111- // Encodes JSON payload.
124+ // Encode encodes and sends a response as JSON payload.
112125func (listener * Listener ) Encode (w http.ResponseWriter , response interface {}) error {
113126 err := json .NewEncoder (w ).Encode (response )
114127 if err != nil {
@@ -117,8 +130,3 @@ func (listener *Listener) Encode(w http.ResponseWriter, response interface{}) er
117130 }
118131 return err
119132}
120-
121- // Sends an error response.
122- func (listener * Listener ) SendError (w http.ResponseWriter , errMessage string ) {
123- json .NewEncoder (w ).Encode (map [string ]string {"Err" : errMessage })
124- }
0 commit comments