Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 3 | |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package afrouter |
| 18 | |
| 19 | // This file implements the ArouterPoxy struct and its |
| 20 | // functions. The ArouterProxy is the top level object |
| 21 | // for the affinity router. |
| 22 | |
| 23 | import ( |
| 24 | "github.com/opencord/voltha-go/common/log" |
| 25 | ) |
| 26 | |
| 27 | // String names for display in error messages. |
Scott Baker | 4989fe9 | 2019-10-09 17:03:06 -0700 | [diff] [blame] | 28 | var arProxy *ArouterProxy |
Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 29 | |
| 30 | type ArouterProxy struct { |
| 31 | servers map[string]*server // Defined in handler.go |
| 32 | api *ArouterApi |
| 33 | } |
| 34 | |
| 35 | // Create the routing proxy |
| 36 | func NewArouterProxy(conf *Configuration) (*ArouterProxy, error) { |
| 37 | arProxy = &ArouterProxy{servers: make(map[string]*server)} |
| 38 | // Create all the servers listed in the configuration |
| 39 | for _, s := range conf.Servers { |
| 40 | if ns, err := newServer(&s); err != nil { |
| 41 | log.Error("Configuration failed") |
| 42 | return nil, err |
| 43 | } else { |
| 44 | arProxy.servers[ns.Name()] = ns |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | // TODO: The API is not mandatory, check if it's even in the config before |
| 49 | // trying to create it. If it isn't then don't bother but log a warning. |
| 50 | if api, err := newApi(&conf.Api, arProxy); err != nil { |
| 51 | return nil, err |
| 52 | } else { |
| 53 | arProxy.api = api |
| 54 | } |
| 55 | |
| 56 | return arProxy, nil |
| 57 | } |
| 58 | |
| 59 | // Start serving |
| 60 | func (ap *ArouterProxy) ListenAndServe() error { |
| 61 | |
| 62 | for _, srvr := range ap.servers { |
| 63 | ap.serve(srvr) |
| 64 | } |
| 65 | ap.api.serve() |
| 66 | |
| 67 | // Just wait until we're done which only happens |
| 68 | // on a signal or an error. |
| 69 | err := <-doneChan |
| 70 | |
| 71 | return err |
| 72 | } |
| 73 | |
| 74 | func (ap *ArouterProxy) serve(srvr *server) { |
| 75 | |
| 76 | // Start a serving thread |
| 77 | go func() { |
| 78 | srvr.running = true |
| 79 | if err := srvr.proxyServer.Serve(srvr.proxyListener); err != nil { |
| 80 | srvr.running = false |
| 81 | log.Error(err) |
| 82 | errChan <- err |
| 83 | } |
| 84 | }() |
| 85 | } |