blob: 4b5ae480b7ef3cf0ce383749470c0b6c8d80d161 [file] [log] [blame]
Juha Hyttinenb31b13f2020-03-18 10:25:30 +02001/*
2==================================================================================
3 Copyright (c) 2019 AT&T Intellectual Property.
4 Copyright (c) 2019 Nokia
5
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17==================================================================================
18*/
19
20package xapptweaks
21
22import (
23 "strconv"
24 "strings"
25)
26
27//-----------------------------------------------------------------------------
28//
29//-----------------------------------------------------------------------------
30type RmrEndpoint struct {
31 Addr string // xapp addr
32 Port uint16 // xapp port
33}
34
35func (endpoint RmrEndpoint) String() string {
36 return endpoint.Addr + ":" + strconv.FormatUint(uint64(endpoint.Port), 10)
37}
38
39func (endpoint *RmrEndpoint) Equal(ep *RmrEndpoint) bool {
40 if (endpoint.Addr == ep.Addr) &&
41 (endpoint.Port == ep.Port) {
42 return true
43 }
44 return false
45}
46
47func (endpoint *RmrEndpoint) GetAddr() string {
48 return endpoint.Addr
49}
50
51func (endpoint *RmrEndpoint) GetPort() uint16 {
52 return endpoint.Port
53}
54
55func (endpoint *RmrEndpoint) Set(src string) bool {
56 elems := strings.Split(src, ":")
57 if len(elems) == 2 {
58 srcAddr := elems[0]
59 srcPort, err := strconv.ParseUint(elems[1], 10, 16)
60 if err == nil {
61 endpoint.Addr = srcAddr
62 endpoint.Port = uint16(srcPort)
63 return true
64 }
65 }
66 return false
67}
68
69//-----------------------------------------------------------------------------
70//
71//-----------------------------------------------------------------------------
72type RmrEndpointList struct {
73 Endpoints []RmrEndpoint
74}
75
76func (eplist *RmrEndpointList) String() string {
77 valuesText := eplist.StringList()
78 return strings.Join(valuesText, ",")
79}
80
81func (eplist *RmrEndpointList) StringList() []string {
82 tmpList := eplist.Endpoints
83 valuesText := []string{}
84 for i := range tmpList {
85 valuesText = append(valuesText, tmpList[i].String())
86 }
87 return valuesText
88}
89
90func (eplist *RmrEndpointList) Size() int {
91 return len(eplist.Endpoints)
92}
93
94func (eplist *RmrEndpointList) AddEndpoint(ep *RmrEndpoint) bool {
95 for i := range eplist.Endpoints {
96 if eplist.Endpoints[i].Equal(ep) {
97 return false
98 }
99 }
100 eplist.Endpoints = append(eplist.Endpoints, *ep)
101 return true
102}
103
104func (eplist *RmrEndpointList) DelEndpoint(ep *RmrEndpoint) bool {
105 for i := range eplist.Endpoints {
106 if eplist.Endpoints[i].Equal(ep) {
107 eplist.Endpoints[i] = eplist.Endpoints[len(eplist.Endpoints)-1]
108 eplist.Endpoints[len(eplist.Endpoints)-1] = RmrEndpoint{"", 0}
109 eplist.Endpoints = eplist.Endpoints[:len(eplist.Endpoints)-1]
110 return true
111 }
112 }
113 return false
114}
115
116func (eplist *RmrEndpointList) DelEndpoints(otheplist *RmrEndpointList) bool {
117 var retval bool = false
118 for i := range otheplist.Endpoints {
119 if eplist.DelEndpoint(&otheplist.Endpoints[i]) {
120 retval = true
121 }
122 }
123 return retval
124}
125
126func (eplist *RmrEndpointList) HasEndpoint(ep *RmrEndpoint) bool {
127 for i := range eplist.Endpoints {
128 if eplist.Endpoints[i].Equal(ep) {
129 return true
130 }
131 }
132 return false
133}
134
135func NewRmrEndpoint(src string) *RmrEndpoint {
136 ep := &RmrEndpoint{}
137 if ep.Set(src) == false {
138 return nil
139 }
140 return ep
141}