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" |
ss412g | 07ef76d | 2019-08-12 17:26:40 +0300 | [diff] [blame] | 22 | "e2mgr/models" |
| 23 | "e2mgr/rmrCgo" |
| 24 | "e2mgr/sessions" |
| 25 | "strconv" |
| 26 | "sync" |
| 27 | ) |
| 28 | |
ss412g | 07ef76d | 2019-08-12 17:26:40 +0300 | [diff] [blame] | 29 | type RmrConfig struct { |
| 30 | Port int |
| 31 | MaxMsgSize int |
| 32 | Flags int |
| 33 | Logger *logger.Logger |
| 34 | } |
| 35 | |
| 36 | func NewRmrConfig(port int, maxMsgSize int, flags int, logger *logger.Logger) *RmrConfig { |
| 37 | return &RmrConfig{port, maxMsgSize, flags, logger} |
| 38 | } |
| 39 | |
| 40 | // RmrService holds an instance of RMR messenger as well as its configuration |
| 41 | type RmrService struct { |
rh362j | f9e31bb | 2019-08-15 15:37:32 +0300 | [diff] [blame^] | 42 | Config *RmrConfig |
| 43 | Messenger *rmrCgo.RmrMessenger |
| 44 | E2sessions sessions.E2Sessions |
| 45 | RmrResponse chan *models.NotificationResponse |
ss412g | 07ef76d | 2019-08-12 17:26:40 +0300 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | // NewRmrService instantiates a new Rmr service instance |
rh362j | f9e31bb | 2019-08-15 15:37:32 +0300 | [diff] [blame^] | 49 | func NewRmrService(rmrConfig *RmrConfig, msrImpl rmrCgo.RmrMessenger, e2sessions sessions.E2Sessions, rmrResponse chan *models.NotificationResponse) *RmrService { |
ss412g | 07ef76d | 2019-08-12 17:26:40 +0300 | [diff] [blame] | 50 | |
| 51 | return &RmrService{ |
rh362j | f9e31bb | 2019-08-15 15:37:32 +0300 | [diff] [blame^] | 52 | Config: rmrConfig, |
| 53 | Messenger: msrImpl.Init("tcp:"+strconv.Itoa(rmrConfig.Port), rmrConfig.MaxMsgSize, rmrConfig.Flags, rmrConfig.Logger), |
| 54 | E2sessions: e2sessions, |
| 55 | RmrResponse: rmrResponse, |
ss412g | 07ef76d | 2019-08-12 17:26:40 +0300 | [diff] [blame] | 56 | } |
| 57 | } |
| 58 | |
| 59 | func (r *RmrService) SendMessage(messageType int, messageChannel chan *models.E2RequestMessage, errorChannel chan error, |
| 60 | wg sync.WaitGroup) { |
| 61 | |
| 62 | wg.Add(1) |
| 63 | setupRequestMessage := <-messageChannel |
rh362j | f9e31bb | 2019-08-15 15:37:32 +0300 | [diff] [blame^] | 64 | e2Message := setupRequestMessage.GetMessageAsBytes(r.Config.Logger) |
ss412g | 07ef76d | 2019-08-12 17:26:40 +0300 | [diff] [blame] | 65 | |
| 66 | transactionId := []byte(setupRequestMessage.TransactionId()) |
| 67 | |
rh362j | f9e31bb | 2019-08-15 15:37:32 +0300 | [diff] [blame^] | 68 | msg := rmrCgo.NewMBuf(messageType, len(e2Message) /*r.config.MaxMsgSize*/, setupRequestMessage.RanName(), &e2Message, &transactionId) |
ss412g | 07ef76d | 2019-08-12 17:26:40 +0300 | [diff] [blame] | 69 | |
rh362j | f9e31bb | 2019-08-15 15:37:32 +0300 | [diff] [blame^] | 70 | r.Config.Logger.Debugf("#rmr_service.SendMessage - Going to send the message: %#v\n", msg) |
| 71 | _, err := (*r.Messenger).SendMsg(msg, r.Config.MaxMsgSize) |
ss412g | 07ef76d | 2019-08-12 17:26:40 +0300 | [diff] [blame] | 72 | |
| 73 | errorChannel <- err |
| 74 | wg.Done() |
| 75 | } |
| 76 | |
rh362j | f9e31bb | 2019-08-15 15:37:32 +0300 | [diff] [blame^] | 77 | func (r *RmrService) SendRmrMessage(response *models.NotificationResponse) error { |
ss412g | 07ef76d | 2019-08-12 17:26:40 +0300 | [diff] [blame] | 78 | |
rh362j | f9e31bb | 2019-08-15 15:37:32 +0300 | [diff] [blame^] | 79 | msgAsBytes := response.GetMessageAsBytes(r.Config.Logger) |
ss412g | 07ef76d | 2019-08-12 17:26:40 +0300 | [diff] [blame] | 80 | transactionIdByteArr := []byte(response.RanName) |
| 81 | |
| 82 | msg := rmrCgo.NewMBuf(response.MgsType, len(msgAsBytes), response.RanName, &msgAsBytes, &transactionIdByteArr) |
| 83 | |
rh362j | f9e31bb | 2019-08-15 15:37:32 +0300 | [diff] [blame^] | 84 | _, err := (*r.Messenger).SendMsg(msg, r.Config.MaxMsgSize) |
ss412g | 07ef76d | 2019-08-12 17:26:40 +0300 | [diff] [blame] | 85 | |
| 86 | if err != nil { |
rh362j | f9e31bb | 2019-08-15 15:37:32 +0300 | [diff] [blame^] | 87 | r.Config.Logger.Errorf("#rmr_service.SendRmrMessage - error: %#v\n", err) |
| 88 | return err |
ss412g | 07ef76d | 2019-08-12 17:26:40 +0300 | [diff] [blame] | 89 | } |
rh362j | f9e31bb | 2019-08-15 15:37:32 +0300 | [diff] [blame^] | 90 | return nil |
ss412g | 07ef76d | 2019-08-12 17:26:40 +0300 | [diff] [blame] | 91 | } |
| 92 | |
rh362j | f9e31bb | 2019-08-15 15:37:32 +0300 | [diff] [blame^] | 93 | func (r *RmrService) SendResponse() { |
ss412g | 07ef76d | 2019-08-12 17:26:40 +0300 | [diff] [blame] | 94 | for { |
ss412g | 07ef76d | 2019-08-12 17:26:40 +0300 | [diff] [blame] | 95 | |
rh362j | f9e31bb | 2019-08-15 15:37:32 +0300 | [diff] [blame^] | 96 | response, ok := <-r.RmrResponse |
ss412g | 07ef76d | 2019-08-12 17:26:40 +0300 | [diff] [blame] | 97 | if !ok { |
| 98 | |
rh362j | f9e31bb | 2019-08-15 15:37:32 +0300 | [diff] [blame^] | 99 | r.Config.Logger.Errorf("#rmr_service.SendResponse - channel closed") |
ss412g | 07ef76d | 2019-08-12 17:26:40 +0300 | [diff] [blame] | 100 | break |
| 101 | } |
| 102 | |
rh362j | f9e31bb | 2019-08-15 15:37:32 +0300 | [diff] [blame^] | 103 | r.Config.Logger.Debugf("#rmr_service.SendResponse - Going to send message: %#v\n", response) |
ss412g | 07ef76d | 2019-08-12 17:26:40 +0300 | [diff] [blame] | 104 | r.SendRmrMessage(response) |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | func (r *RmrService) CloseContext() { |
rh362j | f9e31bb | 2019-08-15 15:37:32 +0300 | [diff] [blame^] | 109 | if r.Config.Logger.DebugEnabled() { |
| 110 | r.Config.Logger.Debugf("#rmr_service.CloseContext - RMR is ready: %v", (*r.Messenger).IsReady()) |
| 111 | (*r.Messenger).Close() |
| 112 | r.Config.Logger.Debugf("#rmr_service.CloseContext - RMR is ready: %v", (*r.Messenger).IsReady()) |
ss412g | 07ef76d | 2019-08-12 17:26:40 +0300 | [diff] [blame] | 113 | } |
| 114 | } |