ss412g | 07ef76d | 2019-08-12 17:26:40 +0300 | [diff] [blame^] | 1 | // |
| 2 | // Copyright 2019 AT&T Intellectual Property |
| 3 | // Copyright 2019 Nokia |
| 4 | // |
| 5 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | // you may not use this file except in compliance with the License. |
| 7 | // You may obtain a copy of the License at |
| 8 | // |
| 9 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | // |
| 11 | // Unless required by applicable law or agreed to in writing, software |
| 12 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | // See the License for the specific language governing permissions and |
| 15 | // limitations under the License. |
| 16 | // |
| 17 | |
| 18 | package services |
| 19 | |
| 20 | import ( |
| 21 | "e2mgr/logger" |
| 22 | "e2mgr/managers" |
| 23 | "e2mgr/models" |
| 24 | "e2mgr/rmrCgo" |
| 25 | "e2mgr/sessions" |
| 26 | "strconv" |
| 27 | "sync" |
| 28 | ) |
| 29 | |
| 30 | |
| 31 | type RmrConfig struct { |
| 32 | Port int |
| 33 | MaxMsgSize int |
| 34 | Flags int |
| 35 | Logger *logger.Logger |
| 36 | } |
| 37 | |
| 38 | func NewRmrConfig(port int, maxMsgSize int, flags int, logger *logger.Logger) *RmrConfig { |
| 39 | return &RmrConfig{port, maxMsgSize, flags, logger} |
| 40 | } |
| 41 | |
| 42 | // RmrService holds an instance of RMR messenger as well as its configuration |
| 43 | type RmrService struct { |
| 44 | config *RmrConfig |
| 45 | messenger *rmrCgo.RmrMessenger |
| 46 | e2sessions sessions.E2Sessions |
| 47 | nManager *managers.NotificationManager |
| 48 | rmrResponse chan *models.NotificationResponse |
| 49 | } |
| 50 | |
| 51 | // NewRmrService instantiates a new Rmr service instance |
| 52 | func NewRmrService(rmrConfig *RmrConfig, msrImpl rmrCgo.RmrMessenger, e2sessions sessions.E2Sessions, nManager *managers.NotificationManager, |
| 53 | rmrResponse chan *models.NotificationResponse) *RmrService { |
| 54 | |
| 55 | return &RmrService{ |
| 56 | config: rmrConfig, |
| 57 | messenger: msrImpl.Init("tcp:"+strconv.Itoa(rmrConfig.Port), rmrConfig.MaxMsgSize, rmrConfig.Flags, rmrConfig.Logger), |
| 58 | e2sessions: e2sessions, |
| 59 | nManager: nManager, |
| 60 | rmrResponse: rmrResponse, |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | func (r *RmrService) SendMessage(messageType int, messageChannel chan *models.E2RequestMessage, errorChannel chan error, |
| 65 | wg sync.WaitGroup) { |
| 66 | |
| 67 | wg.Add(1) |
| 68 | setupRequestMessage := <-messageChannel |
| 69 | e2Message := setupRequestMessage.GetMessageAsBytes(r.config.Logger) |
| 70 | |
| 71 | transactionId := []byte(setupRequestMessage.TransactionId()) |
| 72 | |
| 73 | msg := rmrCgo.NewMBuf(messageType, len(e2Message)/*r.config.MaxMsgSize*/, setupRequestMessage.RanName(), &e2Message, &transactionId) |
| 74 | |
| 75 | r.config.Logger.Debugf("#rmr_service.SendMessage - Going to send the message: %#v\n", msg) |
| 76 | _, err := (*r.messenger).SendMsg(msg, r.config.MaxMsgSize) |
| 77 | |
| 78 | errorChannel <- err |
| 79 | wg.Done() |
| 80 | } |
| 81 | |
| 82 | func (r *RmrService) SendRmrMessage(response *models.NotificationResponse) { |
| 83 | |
| 84 | msgAsBytes := response.GetMessageAsBytes(r.config.Logger) |
| 85 | transactionIdByteArr := []byte(response.RanName) |
| 86 | |
| 87 | msg := rmrCgo.NewMBuf(response.MgsType, len(msgAsBytes), response.RanName, &msgAsBytes, &transactionIdByteArr) |
| 88 | |
| 89 | r.config.Logger.Debugf("#rmr_service.SendRmrMessage - Going to send the message: %#v\n", msg) |
| 90 | |
| 91 | _, err := (*r.messenger).SendMsg(msg, r.config.MaxMsgSize) |
| 92 | |
| 93 | if err != nil { |
| 94 | r.config.Logger.Errorf("#rmr_service.SendRmrMessage - error: %#v\n", err) |
| 95 | return |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | // ListenAndHandle waits for messages coming from rmr_rcv_msg and sends it to a designated message handler |
| 100 | func (r *RmrService) ListenAndHandle() { |
| 101 | |
| 102 | for { |
| 103 | mbuf, err := (*r.messenger).RecvMsg() |
| 104 | r.config.Logger.Debugf("#rmr_service.ListenAndHandle - Going to handle received message: %#v\n", mbuf) |
| 105 | |
| 106 | // TODO: one mbuf received immediately execute goroutine |
| 107 | if err != nil { |
| 108 | continue //TODO log error |
| 109 | } |
| 110 | |
| 111 | r.nManager.HandleMessage(r.config.Logger, r.e2sessions, mbuf, r.rmrResponse) |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | func (r *RmrService) SendResponse(){ |
| 116 | for{ |
| 117 | |
| 118 | response, ok := <-r.rmrResponse |
| 119 | if !ok { |
| 120 | |
| 121 | r.config.Logger.Errorf("#rmr_service.SendResponse - channel closed") |
| 122 | break |
| 123 | } |
| 124 | |
| 125 | r.config.Logger.Debugf("#rmr_service.SendResponse - Going to send message: %#v\n", response) |
| 126 | r.SendRmrMessage(response) |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | func (r *RmrService) CloseContext() { |
| 131 | if r.config.Logger.DebugEnabled(){ |
| 132 | r.config.Logger.Debugf("#rmr_service.CloseContext - RMR is ready: %v", (*r.messenger).IsReady()) |
| 133 | (*r.messenger).Close() |
| 134 | r.config.Logger.Debugf("#rmr_service.CloseContext - RMR is ready: %v", (*r.messenger).IsReady()) |
| 135 | } |
| 136 | } |
| 137 | |