-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxmpptoamqp.go
332 lines (269 loc) · 6.29 KB
/
xmpptoamqp.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
package xmpptoamqp
import (
"crypto/tls"
"strings"
"sync"
log "github.com/Sirupsen/logrus"
"github.com/golang/protobuf/proto"
"github.com/mattn/go-xmpp"
"github.com/streadway/amqp"
)
var waitGroup sync.WaitGroup
type AmqpService struct {
connection *amqp.Connection
channel *amqp.Channel
xmppCient *xmpp.Client
close chan bool
SendChatCommands chan Chat
ReceivedChatEvents chan Chat
}
func NewAmqpService(uri, server, user, password *string) (amqpService *AmqpService) {
amqpService = &AmqpService{
connection: nil,
channel: nil,
xmppCient: connectToXmpp(server, user, password),
close: make(chan bool, 1),
SendChatCommands: make(chan Chat, 4),
ReceivedChatEvents: make(chan Chat, 4),
}
var err error
log.WithFields(log.Fields{
"uri": *uri,
}).Info("Connecting to AMQP")
amqpService.connection, err = amqp.Dial(*uri)
failOnError(err, "AMQP connection error")
log.Info("Acquire AMQP channel")
amqpService.channel, err = amqpService.connection.Channel()
failOnError(err, "AMQP channel error")
return amqpService
}
func (as *AmqpService) ExchangeDeclare(exchange, kind *string) {
if len(*exchange) > 0 {
log.WithFields(log.Fields{
"exchange": *exchange,
"kind": *kind,
}).Info("Declaring AMQP exchange")
err := as.channel.ExchangeDeclare(
*exchange,
*kind,
false,
true,
false,
false,
nil,
)
failOnError(err, "AMQP exchange error")
}
}
func (as *AmqpService) ConsumeSendCommands(exchange, queue, tag *string) {
waitGroup.Add(1)
defer waitGroup.Done()
log.WithFields(log.Fields{
"queue": *queue,
}).Info("Declaring AMQP send queue")
amqpQueue, err := as.channel.QueueDeclare(
*queue,
false,
true,
false,
false,
nil,
)
failOnError(err, "AMQP send queue error")
log.WithFields(log.Fields{
"messages": amqpQueue.Messages,
"consumers": amqpQueue.Consumers,
}).Info("Declared AMQP send queue")
log.WithFields(log.Fields{
"queue": *queue,
"exchange": *exchange,
}).Info("Binding AMQP queue")
err = as.channel.QueueBind(
*queue,
*queue,
*exchange,
false,
nil,
)
failOnError(err, "AMQP send queue bind")
log.WithFields(log.Fields{
"tag": *tag,
}).Info("Starting AMQP consumer")
deliveries, err := as.channel.Consume(
*queue,
*tag,
false,
false,
false,
false,
nil,
)
failOnError(err, "AMQP queue consume")
for {
select {
case <-as.close:
log.Info("AMQP consumer received close signal")
return
case delivery := <-deliveries:
go as.handle(&delivery)
}
}
}
func (as *AmqpService) ForwardChatReceivedEvents(exchange *string) {
waitGroup.Add(1)
defer waitGroup.Done()
for {
select {
case <-as.close:
return
case chat := <-as.ReceivedChatEvents:
err := as.channel.Publish(
*exchange,
"",
false,
false,
amqp.Publishing{
ContentType: "application/vnd.google.protobuf",
Body: []byte(chat.String()),
})
failOnError(err, "Failed to publish a message")
}
}
}
func (as *AmqpService) handle(delivery *amqp.Delivery) {
defer delivery.Ack(true)
log.WithFields(log.Fields{
"id": delivery.MessageId,
"when": delivery.Timestamp,
}).Info("AMQP delivery received")
var chat Chat
err := proto.Unmarshal(delivery.Body, &chat)
if err != nil {
log.WithFields(log.Fields{
"error": err,
}).Warn("Unable to unmarshall delivery from AMQP")
} else {
as.SendChatCommands <- chat
}
}
func (as *AmqpService) Send(exchange, key, user, text *string) {
chat := &Chat{
User: proto.String(*user),
Text: proto.String(*text),
}
data, err := proto.Marshal(chat)
failOnError(err, "Serialization failure")
log.WithFields(log.Fields{
"exchange": *exchange,
"key": *key,
"data": data,
}).Info("sending")
err = as.channel.Publish(*exchange, *key, false, false, amqp.Publishing{
Headers: amqp.Table{},
ContentType: "application/vnd.google.protobuf",
Body: data,
DeliveryMode: amqp.Transient,
Priority: 0,
})
failOnError(err, "Publish failure")
}
/*
func (as *AmqpService) FakeReply() {
for sendChatCommand := range as.SendChatCommands {
reply := fmt.Sprint("Reply to ", *sendChatCommand.Text)
receivedChatEvent := Chat{
User: sendChatCommand.User,
Text: &reply,
}
log.WithFields(log.Fields{
"original": *sendChatCommand.Text,
"new": reply,
}).Info("Replying")
as.ReceivedChatEvents <- receivedChatEvent
}
}
*/
func (as *AmqpService) Close() {
as.close <- true
as.close <- true
as.close <- true
waitGroup.Wait()
}
func connectToXmpp(server, user, password *string) (xmppClient *xmpp.Client) {
if server == nil {
return nil
}
xmpp.DefaultConfig = tls.Config{
ServerName: serverName(*server),
InsecureSkipVerify: true,
}
options := xmpp.Options{
Host: *server,
User: *user,
Password: *password,
NoTLS: false,
Debug: false,
Session: false,
Status: "xa",
StatusMessage: "Xmppbot",
}
log.WithFields(log.Fields{
"server": *server,
"user": *user,
"password": *password,
}).Info("Connecting to XMPP")
xmppClient, err := options.NewClient()
failOnError(err, "XMPP connection error")
return xmppClient
}
func (as *AmqpService) ReceiveXmpp() {
for {
chat, err := as.xmppCient.Recv()
failOnError(err, "XMPP receive error")
switch v := chat.(type) {
case xmpp.Chat:
chatReceivedEvent := Chat{
User: &v.Remote,
Text: &v.Text,
}
if len(*chatReceivedEvent.Text) > 0 {
log.WithFields(log.Fields{
"from": *chatReceivedEvent.User,
"text": *chatReceivedEvent.Text,
}).Info("XMPP chat received")
as.ReceivedChatEvents <- chatReceivedEvent
}
}
}
}
func (as *AmqpService) SendXmpp() {
waitGroup.Add(1)
defer waitGroup.Done()
for {
select {
case <-as.close:
return
case chat := <-as.SendChatCommands:
_, err := as.xmppCient.Send(xmpp.Chat{
Remote: chat.GetUser(),
Type: "chat",
Text: chat.GetText(),
})
if err != nil {
log.WithFields(log.Fields{
"error": err,
}).Warn("Unable to send to XMPP")
}
}
}
}
func serverName(host string) string {
return strings.Split(host, ":")[0]
}
func failOnError(err error, message string) {
if err != nil {
log.WithFields(log.Fields{
"error": err,
}).Fatal(message)
}
}