-
Notifications
You must be signed in to change notification settings - Fork 46
/
basic_publisher.go
102 lines (86 loc) · 2.7 KB
/
basic_publisher.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
/*
Copyright 2016 Stanislav Liberman
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"flag"
"fmt"
"log"
"os"
"os/signal"
"syscall"
"time"
"github.com/lirm/aeron-go/aeron"
"github.com/lirm/aeron-go/aeron/atomic"
"github.com/lirm/aeron-go/aeron/logging"
"github.com/lirm/aeron-go/examples"
)
var logger = logging.MustGetLogger("basic_publisher")
var interrupt = make(chan os.Signal, 1)
func init() {
signal.Notify(interrupt, os.Interrupt)
signal.Notify(interrupt, syscall.SIGTERM)
}
func main() {
flag.Parse()
if !*examples.ExamplesConfig.LoggingOn {
logging.SetLevel(logging.INFO, "aeron")
logging.SetLevel(logging.INFO, "memmap")
logging.SetLevel(logging.INFO, "driver")
logging.SetLevel(logging.INFO, "counters")
logging.SetLevel(logging.INFO, "logbuffers")
logging.SetLevel(logging.INFO, "buffer")
logging.SetLevel(logging.INFO, "rb")
}
errorHandler := func(err error) {
logger.Warning(err)
}
to := time.Duration(time.Millisecond.Nanoseconds() * *examples.ExamplesConfig.DriverTo)
ctx := aeron.NewContext().AeronDir(*examples.ExamplesConfig.AeronPrefix).MediaDriverTimeout(to).ErrorHandler(errorHandler)
a, err := aeron.Connect(ctx)
if err != nil {
logger.Fatalf("Failed to connect to media driver: %s\n", err.Error())
}
defer a.Close()
publication, err := a.AddPublication(*examples.ExamplesConfig.Channel, int32(*examples.ExamplesConfig.StreamID))
if err != nil {
logger.Fatalf(err.Error())
}
defer publication.Close()
log.Printf("Publication found %v", publication)
for counter := 0; counter < *examples.ExamplesConfig.Messages; counter++ {
message := fmt.Sprintf("this is a message %d", counter)
srcBuffer := atomic.MakeBuffer(([]byte)(message))
ret := publication.Offer(srcBuffer, 0, int32(len(message)), nil)
switch ret {
case aeron.NotConnected:
log.Printf("%d: not connected yet", counter)
case aeron.BackPressured:
log.Printf("%d: back pressured", counter)
default:
if ret < 0 {
log.Printf("%d: Unrecognized code: %d", counter, ret)
} else {
log.Printf("%d: success!", counter)
}
}
if !publication.IsConnected() {
log.Printf("no subscribers detected")
}
select {
case <-interrupt:
return
default:
time.Sleep(time.Second)
}
}
}