-
Notifications
You must be signed in to change notification settings - Fork 0
/
teomq.go
56 lines (46 loc) · 1.23 KB
/
teomq.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
// Copyright 2023-24 Kirill Scherba <[email protected]>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Teonet messages queue. This is a teonet golang packge wich creates and
// manages messages queue between message producers and consumers.
package teomq
import (
"fmt"
"os"
"os/signal"
"time"
"github.com/teonet-go/teonet"
)
var (
ConsumerHello = []byte("Consumer")
ConsumerAnswer = []byte("Connected to broker")
)
// NewTeonet creates new teonet connection and connect to teonet.
func NewTeonet(appShort string, attr ...interface{}) (teo *teonet.Teonet, err error) {
// Create teonet connection
teo, err = teonet.New(appShort, attr...)
if err != nil {
return
}
// Connect to teonet
for teo.Connect() != nil {
time.Sleep(1 * time.Second)
}
// Process Ctrl+C signal
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt)
go func() {
<-quit
fmt.Println("\nCtrl+C signal received. Exiting...")
teo.Close()
os.Exit(0)
}()
return
}
// connectToBroker connects producer or consumer to brokers peer
func ConnectToBroker(teo *teonet.Teonet, addr string) (err error) {
if err = teo.ConnectTo(addr); err != nil {
return
}
return
}