-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
173 lines (161 loc) · 4.04 KB
/
main.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
package main
import (
"bufio"
"context"
"errors"
"fmt"
lib "github.com/mm-uh/agent-libgo/src"
"github.com/sirupsen/logrus"
"net"
"os"
"strconv"
"strings"
)
func LaunchServer(ip string, port string, function func(string2 string) (string, error)) {
fmt.Println("Launching server...")
// listen on all interfaces
ln, err := net.Listen("tcp", ip+":"+port)
if err != nil {
logrus.Warn("Error listen server ", ip+":"+port)
}
// accept connection on port
for {
conn, err := ln.Accept()
if err != nil {
logrus.Warn("Error establishing connection")
}
// run loop forever (or until ctrl-c)
// will listen for message to process ending in newline (\n)
message, _ := bufio.NewReader(conn).ReadString('\n')
// sample process for string received
responseMessage, err := function(message)
if err != nil {
logrus.Warn("Error reading response")
continue
}
// send new string back to client
_, err = conn.Write([]byte(responseMessage))
if err != nil {
logrus.Warn("Error sending message")
}
}
}
func main() {
name := "AdderGo"
function := "Add"
host := os.Args[1]
port := os.Args[2]
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,
}
config := lib.NewConfiguration()
config.BasePath = "http://" + host + ":" + port + "/api/v1"
api := lib.NewAPIClient(config)
defer context.Background()
logrus.Info("Getting peers")
peers, _, err := api.DefaultApi.GetPeers(context.Background())
if err != nil {
logrus.Warn("Error getting peers ", err.Error())
}
for _, peer := range peers {
fmt.Println("IP: " + peer.Ip)
fmt.Print("PORT: ")
fmt.Print(peer.Port)
}
logrus.Info("Registering Agent")
_, err = api.DefaultApi.RegisterAgent(context.Background(), agent)
if err != nil {
logrus.Warn("Error Registering agent ", err.Error())
}
logrus.Info("Getting Agent")
agentLocation, _, err := api.DefaultApi.GetAgent(context.Background(), 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(), agent.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")
}
}