-
Notifications
You must be signed in to change notification settings - Fork 0
/
wrapper.go
246 lines (209 loc) · 5.55 KB
/
wrapper.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package main
import (
"context"
"errors"
"github.com/mm-uh/agent-libgo/src"
platform "github.com/mm-uh/go-agent-platform/src"
"github.com/sirupsen/logrus"
"math/rand"
"net"
"strconv"
)
type AgentWrapper struct {
Name string
NodeAddr src.Addr
IsAliveAddr src.Addr
DocumentationAddr src.Addr
}
func NewAgentWrapper(agentName string, addresses []src.Addr) *AgentWrapper {
if len(addresses) != 3 {
return &AgentWrapper{}
}
return &AgentWrapper{
Name: agentName,
NodeAddr: addresses[0],
IsAliveAddr: addresses[1],
DocumentationAddr: addresses[2],
}
}
func (agent *AgentWrapper) SendToAgent(wrapper *PlatformWrapper, request string) (string, error) {
if !agent.refreshAgent(wrapper, false) {
return "", nil
}
response, err := platform.MakeRequest(getEndpoint(agent.NodeAddr, 0), request)
if err != nil {
return "", nil
}
return response, nil
}
func getEndpoint(addr src.Addr, plus int) string {
port := strconv.Itoa(int(addr.Port) + plus)
return addr.Ip + port
}
func (agent *AgentWrapper) IsAlive(wrapper *PlatformWrapper) bool {
return platform.NodeIsAlive(getEndpoint(agent.IsAliveAddr, 0))
}
func (agent *AgentWrapper) GetDocumentation(wrapper *PlatformWrapper) (string, error) {
if !agent.refreshAgent(wrapper, false) {
return "", nil
}
response, err := platform.MakeRequest(getEndpoint(agent.DocumentationAddr, 0), "Doc")
if err != nil {
return "", nil
}
return response, nil
}
func (agent *AgentWrapper) refreshAgent(wrapper *PlatformWrapper, forced bool) bool {
port := strconv.Itoa(int(agent.IsAliveAddr.Port))
if isOpen(agent.IsAliveAddr.Ip, port) && !forced {
return true
}
tmpAgent, err := wrapper.GetAgent(agent.Name)
if err != nil {
return false
}
*agent = *tmpAgent
return true
}
type PlatformWrapper struct {
Host string
Port string
ApiClient *src.APIClient
KnowPeers []src.Addr
}
func NewPlatformWWrapper(host, port string) *PlatformWrapper {
wrapper := PlatformWrapper{
Host: host,
Port: port,
KnowPeers: make([]src.Addr, 0),
}
config := src.NewConfiguration()
config.BasePath = "http://" + host + ":" + port + "/api/v1"
wrapper.ApiClient = src.NewAPIClient(config)
_ = wrapper.GetPeers()
return &wrapper
}
func Union(a, b []src.Addr) []src.Addr {
m := make(map[src.Addr]bool)
for _, item := range a {
m[item] = true
}
for _, item := range b {
if _, ok := m[item]; !ok {
a = append(a, item)
}
}
return a
}
func (wrapper *PlatformWrapper) GetPeers() error {
defer context.Background()
tmpPeers, _, err := wrapper.ApiClient.DefaultApi.GetPeers(context.Background())
if err != nil {
logrus.Warn("Could't get peers")
return err
}
wrapper.KnowPeers = Union(wrapper.KnowPeers, tmpPeers)
err = wrapper.UpdatePeers()
if err != nil {
logrus.Warn("Could't update peers")
return err
}
return nil
}
func (wrapper *PlatformWrapper) UpdatePeers() error {
defer context.Background()
if len(wrapper.KnowPeers) < 1 {
return nil
}
for i := 0; i < 10; i++ {
rnd := rand.Intn(len(wrapper.KnowPeers))
port := strconv.Itoa(int(wrapper.KnowPeers[rnd].Port + 1000))
if !isOpen(wrapper.KnowPeers[rnd].Ip, port) {
continue
}
config := src.NewConfiguration()
config.Host = "http://" + wrapper.KnowPeers[rnd].Ip + ":" + port + "/api/v1"
apiClient := src.NewAPIClient(config)
tmpPeers, _, err := apiClient.DefaultApi.GetPeers(context.Background())
if err != nil {
logrus.Warn("Could't get peers")
continue
}
wrapper.KnowPeers = Union(wrapper.KnowPeers, tmpPeers)
}
return nil
}
func (wrapper *PlatformWrapper) UpdateApi() error {
_ = wrapper.GetPeers()
if !isOpen(wrapper.Host, wrapper.Port) {
for _, node := range wrapper.KnowPeers {
port := strconv.Itoa(int(node.Port + 1000))
if !isOpen(node.Ip, port) {
wrapper.Port = port
wrapper.Host = node.Ip
config := src.NewConfiguration()
config.BasePath = "http://" + wrapper.Host + ":" + port + "/api/v1"
wrapper.ApiClient = src.NewAPIClient(config)
return nil
}
}
return errors.New("couldn't get any platform available")
}
return nil
}
func (wrapper *PlatformWrapper) GetAgent(agentName string) (*AgentWrapper, error) {
defer context.Background()
err := wrapper.UpdateApi()
if err != nil {
return &AgentWrapper{}, err
}
agent, _, err := wrapper.ApiClient.DefaultApi.GetAgent(context.Background(), agentName)
if err != nil {
return &AgentWrapper{}, err
}
return NewAgentWrapper(agentName, agent), nil
}
func (wrapper *PlatformWrapper) GetSimilar(agentName string) (*AgentWrapper, error) {
defer context.Background()
err := wrapper.UpdateApi()
if err != nil {
return &AgentWrapper{}, err
}
agent, _, err := wrapper.ApiClient.DefaultApi.GetSimilarAgent(context.Background(), agentName)
if err != nil {
return &AgentWrapper{}, err
}
return NewAgentWrapper(agentName, agent), nil
}
func (wrapper *PlatformWrapper) RegisterAgent(agent *src.Agent) error {
defer context.Background()
err := wrapper.UpdateApi()
if err != nil {
return err
}
_, err = wrapper.ApiClient.DefaultApi.RegisterAgent(context.Background(), *agent)
if err != nil {
return err
}
return nil
}
func (wrapper *PlatformWrapper) GetAllAgents() ([]string, error) {
defer context.Background()
err := wrapper.UpdateApi()
if err != nil {
return nil, err
}
agents, _, err := wrapper.ApiClient.DefaultApi.GetAgentsNames(context.Background())
if err != nil {
return nil, err
}
return agents, nil
}
func isOpen(host, port string) bool {
ln, err := net.Listen("tcp", host+":"+port)
if err != nil {
return false
}
_ = ln.Close()
return true
}