-
Notifications
You must be signed in to change notification settings - Fork 0
/
examples.go
131 lines (122 loc) · 2.96 KB
/
examples.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
package main
import (
"context"
"errors"
"fmt"
lib "github.com/mm-uh/agent-libgo/src"
"github.com/sirupsen/logrus"
"strconv"
"strings"
)
func AgentMultiplier(name, function, hostP, portP string) {
test := []lib.TestCase{
{
Input: "1 2",
Output: "3",
},
{
Input: "2 3",
Output: "5",
},
}
// Handler for agent
go LaunchServer("localhost", "38090", func(message string) (string, error) {
message = message[:len(message)-1]
args := strings.Split(message, " ")
if len(args) != 2 {
return "", errors.New("unknown number of params")
}
a, err := strconv.Atoi(args[0])
if err != nil {
return "", err
}
b, err := strconv.Atoi(args[1])
if err != nil {
return "", err
}
return strconv.Itoa(a + b), nil
})
// Handler for IsAlive service
go LaunchServer("localhost", "38091", func(message string) (string, error) {
if message != "IsAlive?\n" {
return "", errors.New("unknown param, not \"IsAlive?\"")
}
return "Yes", nil
})
go LaunchServer("localhost", "38092", func(message string) (string, error) {
args := strings.Split(message, " ")
if len(args) != 2 {
return "", errors.New("unknown number of params")
}
a, err := strconv.Atoi(args[0])
if err != nil {
return "", err
}
b, err := strconv.Atoi(args[1])
if err != nil {
return "", err
}
return strconv.Itoa(a + b), nil
})
endpoints := []lib.Addr{
{
Ip: "localhost",
Port: 38090,
},
}
documentation := map[string]lib.Addr{
"localhost:38090": {
Ip: "localhost",
Port: 38092,
},
}
isAlive := map[string]lib.Addr{
"localhost:38090": {
Ip: "localhost",
Port: 38091,
},
}
//# Agent | Agent to register
//agent = lib_agent.Agent(name=name, function=function, endpoint_service=endpoint_service,
//documentation=documentation, is_alive_service=is_alive_service, test_cases=test_case)
agent := lib.Agent{
Name: name,
Function: function,
EndpointService: endpoints,
IsAliveService: isAlive,
Documentation: documentation,
TestCases: test,
}
plastform := NewPlatformWWrapper(hostP, portP)
logrus.Info("Getting peers")
err := plastform.GetPeers()
if err != nil {
logrus.Warn("Error getting peers ", err.Error())
}
for _, peer := range plastform.KnowPeers {
fmt.Println("IP: " + peer.Ip)
fmt.Print("PORT: ")
fmt.Print(peer.Port)
}
logrus.Info("Registering Agent")
err = plastform.RegisterAgent(&agent)
if err != nil {
logrus.Warn("Error Registering agent ", err.Error())
}
logrus.Info("Getting Agent")
agentLocation, err := plastform.GetAgent(agent.Name)
if err != nil {
logrus.Warn("Error Registering agent ", err.Error())
}
fmt.Println(agentLocation)
logrus.Info("Getting Agent")
var similar lib.Agent
similar, _, err = api.DefaultApi.GetSimilarAgent(context.Background(), agentLocation.Name)
if err != nil {
logrus.Warn("Error Registering agent ", err.Error())
}
_, err = fmt.Println(similar.Name)
if err != nil {
logrus.Warn("Couldn't get similar.Name")
}
}