-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
159 lines (131 loc) · 2.92 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
package main
import (
"encoding/json"
"fmt"
"net/http"
"os"
"time"
"github.com/gorilla/websocket"
)
var connection *websocket.Conn
var tasks chan *Payload = make(chan *Payload, 1024)
type TaskMessage struct {
MessageType string
Payload Payload
}
type Payload struct {
IntentType string
TaskType string
TaskUUID string
Config interface{}
Input Input
}
type Input struct {
Board []int8 `json:"board"`
Size int `json:"size"`
}
func main() {
connection, err := registerWorker()
if err != nil {
fmt.Println(err)
return
}
go listen(connection)
go execute(tasks, connection)
select {}
}
func registerWorker() (*websocket.Conn, error) {
var dialer websocket.Dialer
connection, _, err := dialer.Dial("ws://"+os.Args[1]+":2216/workers/register/?workertype=cloud", make(http.Header))
if err != nil {
return nil, err
}
return connection, nil
}
func listen(connection *websocket.Conn) {
for {
fmt.Println("Message received")
_, buffer, err := connection.ReadMessage()
if err != nil {
return
}
// Try to read message into JSON
// var message map[string]interface{}
var message TaskMessage
err = json.Unmarshal(buffer, &message)
if err != nil {
fmt.Println(err)
}
if message.MessageType == "Intent" {
payload := message.Payload
tasks <- &payload
}
}
}
func sendMessage(message *map[string]interface{}, connection *websocket.Conn) {
go func() {
// Convert to buffer
buffer, err := json.Marshal(message)
if err != nil {
fmt.Println(err)
}
connection.WriteMessage(websocket.TextMessage, buffer)
}()
}
func execute(tasks chan *Payload, connection *websocket.Conn) {
for {
task := <-tasks
if task.TaskType == "GOL" {
output, start, end := gol(task.Input.Size, task.Input.Board)
sendMessage(&map[string]interface{}{
"MessageType": "WorkResponse",
"Output": output,
"start": start,
"end": end,
"device": "cloudworker",
}, connection)
} else {
fmt.Println("Unknown task")
}
}
}
func gol(size int, board []int8) ([]int8, int64, int64) {
start := time.Now().UnixNano() / int64(time.Millisecond)
result := make([]int8, size*size)
for i := 0; i < size*size; i++ {
x := i % size
y := i / size
x1 := (x + size - 1) % size
x2 := x
x3 := (x + size + 1) % size
y1 := ((y + size - 1) % size) * size
y2 := y * size
y3 := ((y + size + 1) % size) * size
var count int8 = 0
count += board[x1+y1]
count += board[x2+y1]
count += board[x3+y1]
count += board[x1+y2]
count += board[x3+y2]
count += board[x1+y3]
count += board[x2+y3]
count += board[x3+y3]
if board[x2+y2] == 1 {
if count < 2 {
result[x2+y2] = 0
} else if count > 3 {
result[x2+y2] = 0
} else {
result[x2+y2] = 1
}
} else {
if count == 3 {
result[x2+y2] = 1
} else {
result[x2+y2] = 0
}
}
}
end := time.Now().UnixNano() / int64(time.Millisecond)
return result, start, end
}