-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevents.go
144 lines (117 loc) · 2.99 KB
/
events.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
package metamorph
import (
"encoding/base64"
"encoding/json"
"log"
"net/http"
"sync"
"github.com/gorilla/websocket"
)
type generalCommand struct {
Type string `json:"type"`
}
type resetCommand struct {
Type string `json:"type"`
Topics []string `json:"topics"`
}
type sendCommand struct {
Type string `json:"type"`
Topic string `json:"topic"`
Value string `json:"value"`
}
type Server struct {
connections map[*websocket.Conn]bool
chEvents chan interface{}
mutex sync.Mutex
Kafka *KafkaSystem
}
func NewServer() *Server {
return &Server{make(map[*websocket.Conn]bool), make(chan interface{}, 100), sync.Mutex{}, nil}
}
func (s *Server) SendEvent(ev interface{}) {
s.chEvents <- ev
}
type errorEv struct {
Type string `json:"type"`
Message string `json:"message"`
}
func (s *Server) SendErrorEvent(message string) interface{} {
log.Println("ERROR:", message)
return errorEv{Type: "error", Message: message}
}
func (s *Server) Start() {
go s.eventsWriter()
}
func (s *Server) EventWebSocketHandler(w http.ResponseWriter, r *http.Request) {
upgrader := websocket.Upgrader{}
c, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Println(err)
return
}
s.newConnection(c)
go s.eventsReader(c)
}
func (s *Server) eventsWriter() {
for event := range s.chEvents {
log.Printf("Broadcasting event %v\n", event)
s.mutex.Lock()
for c := range s.connections {
c.WriteJSON(event)
}
s.mutex.Unlock()
}
}
func (s *Server) eventsReader(c *websocket.Conn) {
defer c.Close()
defer s.closeConnection(c)
for {
_, p, err := c.ReadMessage()
log.Printf("Read message from Metamorph client: %v", p)
if err != nil {
log.Println("Read error:", err)
break
}
var genCommand generalCommand
err = json.Unmarshal(p, &genCommand)
if err != nil {
log.Println("Parse command type error:", err)
break
}
log.Printf("Command: %v", genCommand)
if genCommand.Type == "reset_kafka_system" {
resetCommand := resetCommand{"", []string{}}
err = json.Unmarshal(p, &resetCommand)
if err != nil {
log.Println("Could not parse reset command, error:", err)
break
}
s.Kafka.Reset(resetCommand.Topics)
} else if genCommand.Type == "send" {
sendCommand := sendCommand{}
err = json.Unmarshal(p, &sendCommand)
if err != nil {
log.Println("Could not parse send command, error:", err)
return
}
valueBytes, err := base64.StdEncoding.DecodeString(sendCommand.Value)
if err != nil {
log.Printf("Could not parse value %s as base64", sendCommand.Value)
}
log.Printf("Received send event on topic %s with value %s", sendCommand.Topic, valueBytes)
s.Kafka.Send(valueBytes, sendCommand.Topic)
}
}
}
func (s *Server) newConnection(c *websocket.Conn) {
s.mutex.Lock()
defer s.mutex.Unlock()
log.Println("New connection established.")
s.connections[c] = true
}
func (s *Server) closeConnection(c *websocket.Conn) {
s.mutex.Lock()
defer s.mutex.Unlock()
log.Println("Closing connection...")
delete(s.connections, c)
}