-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.go
216 lines (195 loc) · 6.73 KB
/
helper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package core
import (
"fmt"
"sort"
"strconv"
"strings"
"github.com/stas-makutin/howeve/api"
)
var errorCodeToName map[api.ErrorCode]string = map[api.ErrorCode]string{
api.ErrorUnknownProtocol: "Unknown Protocol",
api.ErrorUnknownTransport: "Unknown Transport",
api.ErrorInvalidProtocolTransport: "Invalid Protocol's Transport",
api.ErrorUnknownParameter: "Unknown Parameter",
api.ErrorInvalidParameterValue: "Invalid Parameter Value",
api.ErrorNoRequiredParameter: "No Required Parameter",
api.ErrorNoDiscovery: "No Discovery",
api.ErrorDiscoveryBusy: "Discovery Busy",
api.ErrorNoDiscoveryID: "No Discovery ID",
api.ErrorDiscoveryPending: "Discovery Pending",
api.ErrorDiscoveryFailed: "Discovery Failed",
api.ErrorServiceNoKey: "Service No Key",
api.ErrorServiceNoID: "Service No ID",
api.ErrorServiceExists: "Service Exists",
api.ErrorServiceAliasExists: "Service Alias Exists",
api.ErrorServiceInitialize: "Service Initialize",
api.ErrorServiceKeyNotExists: "Service Key Not Exists",
api.ErrorServiceAliasNotExists: "Service Alias Not Exists",
api.ErrorServiceStatusBad: "Service Status Bad",
api.ErrorServiceBadPayload: "Service Bad Payload",
api.ErrorServiceSendBusy: "Service Send Busy",
api.ErrorOtherError: "Other Error",
}
func ApiErrorName(code api.ErrorCode) string {
name, ok := errorCodeToName[code]
if ok {
return name
}
return "Unknown"
}
type ProtocolsInfoWrapper struct {
Info *api.ProtocolInfoEntry
transportIDs map[api.TransportIdentifier]*api.ProtocolTransportInfoEntry
}
type ProtocolsWrapper struct {
Protocols []*api.ProtocolInfoEntry
protocolIDs map[api.ProtocolIdentifier]*ProtocolsInfoWrapper
}
func NewProtocolsWrapper(protocols *api.ProtocolInfoResult) *ProtocolsWrapper {
result := &ProtocolsWrapper{}
if protocols != nil {
result.Protocols = protocols.Protocols
result.protocolIDs = make(map[api.ProtocolIdentifier]*ProtocolsInfoWrapper)
for _, protocol := range protocols.Protocols {
p := &ProtocolsInfoWrapper{Info: protocol, transportIDs: make(map[api.TransportIdentifier]*api.ProtocolTransportInfoEntry)}
result.protocolIDs[protocol.ID] = p
for _, transport := range protocol.Transports {
p.transportIDs[transport.ID] = transport
}
}
}
return result
}
func (pw *ProtocolsWrapper) ProtocolAndTransport(protocolID api.ProtocolIdentifier, transportID api.TransportIdentifier) (protocol *api.ProtocolInfoEntry, transport *api.ProtocolTransportInfoEntry) {
if p, ok := pw.protocolIDs[protocolID]; ok {
protocol = p.Info
if t, ok := p.transportIDs[transportID]; ok {
transport = t
}
}
return
}
func (pw *ProtocolsWrapper) ProtocolAndTransportNames(protocolID api.ProtocolIdentifier, transportID api.TransportIdentifier) (protocolName, transportName string) {
pi, ti := pw.ProtocolAndTransport(protocolID, transportID)
if pi != nil {
protocolName = pi.Name
}
if ti != nil {
transportName = ti.Name
}
return
}
func (pw *ProtocolsWrapper) ProtocolAndTransportFullNames(protocolID api.ProtocolIdentifier, transportID api.TransportIdentifier) (protocolName, transportName string) {
protocolName = strconv.Itoa(int(protocolID))
transportName = strconv.Itoa(int(transportID))
pi, ti := pw.ProtocolAndTransport(protocolID, transportID)
if pi != nil && pi.Name != "" {
protocolName = pi.Name + " (" + protocolName + ")"
}
if ti != nil && ti.Name != "" {
transportName = ti.Name + " (" + transportName + ")"
}
return
}
func ArrangeServices(services []api.ListServicesEntry) {
sort.SliceStable(services, func(i, j int) bool {
if services[i].Protocol < services[j].Protocol {
return true
}
if services[i].Protocol == services[j].Protocol {
if services[i].Transport < services[j].Transport {
return true
}
if services[i].Transport == services[j].Transport {
if c := strings.Compare(services[i].Alias, services[j].Alias); c > 0 {
return true
} else if c == 0 {
return strings.Compare(services[i].Entry, services[j].Entry) < 0
}
}
}
return false
})
}
type ServiceEntryData struct {
api.ServiceKey
Alias string
Params Parameters
}
func NewServiceEntryData(protocols []*api.ProtocolInfoEntry) *ServiceEntryData {
result := &ServiceEntryData{}
if len(protocols) > 0 {
protocol := protocols[0]
result.Protocol = protocol.ID
if len(protocol.Transports) > 0 {
result.Transport = protocol.Transports[0].ID
}
}
return result
}
func (sed *ServiceEntryData) ChangeProtocol(protocols []*api.ProtocolInfoEntry, index int) bool {
if index < 0 || index >= len(protocols) {
return false
}
protocol := protocols[index]
if sed.Protocol == protocol.ID {
return false
}
transportNotSupported := true
for _, t := range protocol.Transports {
if t.ID == sed.Transport {
transportNotSupported = false
break
}
}
sed.Protocol = protocol.ID
if transportNotSupported && len(protocol.Transports) > 0 {
sed.Transport = protocol.Transports[0].ID
}
return true
}
func (sed *ServiceEntryData) ChangeTransport(protocols *ProtocolsWrapper, index int) bool {
if index < 0 || len(protocols.Protocols) == 0 {
return false
}
piw, ok := protocols.protocolIDs[sed.Protocol]
if !ok {
return false
}
if index >= len(piw.Info.Transports) {
return false
}
transportID := piw.Info.Transports[index].ID
if transportID == sed.Transport {
return false
}
sed.Transport = transportID
return true
}
func (sed *ServiceEntryData) ValidateEntryAndAlias(services []api.ListServicesEntry, protocols *ProtocolsWrapper) (entryMessage, aliasMessage string) {
for _, s := range services {
if s.Alias != "" && s.Alias == sed.Alias {
aliasMessage = fmt.Sprintf("Service with alias '%s' already exists", s.Alias)
}
if s.Protocol == sed.Protocol && s.Transport == sed.Transport && s.Entry == sed.Entry {
protocol, transport := sed.ProtocolAndTransport(protocols)
if protocol == nil || transport == nil {
entryMessage = "Data integrity error - try to refresh the page"
} else {
entryMessage = fmt.Sprintf(
"Service with '%s' entry already exists for %s (%v) protocol and %s (%v) transport",
s.Entry,
protocol.Name, s.Protocol,
transport.Name, s.Transport,
)
}
}
if aliasMessage != "" && entryMessage != "" {
break
}
}
return
}
func (sed *ServiceEntryData) ProtocolAndTransport(protocols *ProtocolsWrapper) (*api.ProtocolInfoEntry, *api.ProtocolTransportInfoEntry) {
return protocols.ProtocolAndTransport(sed.Protocol, sed.Transport)
}