-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgolang-ecal_sample.go
141 lines (128 loc) · 2.9 KB
/
golang-ecal_sample.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
package main
import (
"fmt"
"google.golang.org/protobuf/proto"
"log"
"os"
"time"
"github.com/Blutkoete/golang-ecal/ecal"
"github.com/Blutkoete/golang-ecal/pbexample"
)
func minimalSnd() {
pub, pubChannel, err := ecal.PublisherCreate("Hello", "base:std::string", "", true)
if err != nil {
log.Fatal(err)
}
defer pub.Destroy()
count := 0
for ecal.Ok() {
message := ecal.Message{Content: []byte(fmt.Sprintf("Hello World from Go (%d)", count)),
Timestamp: -1}
count += 1
select {
case pubChannel <- message:
log.Printf("Sent \"%s\"\n", message.Content)
case <-time.After(time.Second):
}
<-time.After(250 * time.Millisecond)
}
}
func minimalRec() {
sub, subChannel, err := ecal.SubscriberCreate("Hello", "base:std::string", "", true, 1024)
if err != nil {
log.Fatal(err)
}
defer sub.Destroy()
for ecal.Ok() {
select {
case message := <-subChannel:
log.Printf("Received \"%s\"\n", message.Content)
case <-time.After(time.Second):
}
}
}
func personSnd() {
pub, pubChannel, err := ecal.PublisherCreate("person", "proto:pb.People.Person", "", true)
if err != nil {
log.Fatal(err)
}
defer pub.Destroy()
count := 0
for ecal.Ok() {
person := &pbexample.Person{Id: (int32)(count),
Name: "Max",
Email: "[email protected]",
Dog: &pbexample.Dog{Name: "Brandy"},
House: &pbexample.House{Rooms: 4},
}
message := ecal.Message{Timestamp: -1}
message.Content, err = proto.Marshal(person)
if err != nil {
log.Fatal(err)
}
count += 1
select {
case pubChannel <- message:
log.Printf("Sent \"%s\"\n", person)
case <-time.After(time.Second):
}
<-time.After(250 * time.Millisecond)
}
}
func personRec() {
sub, subChannel, err := ecal.SubscriberCreate("person", "proto:pb.People.Person", "", true, 1024)
if err != nil {
log.Fatal(err)
}
defer sub.Destroy()
for ecal.Ok() {
select {
case message := <-subChannel:
person := &pbexample.Person{}
err = proto.Unmarshal(message.Content, person)
if err != nil {
log.Fatal(err)
}
log.Printf("Received \"%s\"\n", person)
case <-time.After(time.Second):
}
}
}
func main() {
var mode string
if len(os.Args) <= 1 {
log.Print("No sample type given. Assuming \"minimal_snd\".\n")
mode = "minimal_snd"
} else {
switch os.Args[1] {
case "minimal_snd":
mode = os.Args[1]
case "minimal_rec":
mode = os.Args[1]
case "person_snd":
mode = os.Args[1]
case "person_rec":
mode = os.Args[1]
default:
log.Printf("Unknown sample type \"%s\". Assuming \"minimal_snd\".\n", os.Args[1])
mode = "minimal_snd"
}
}
err := ecal.Initialize(os.Args, fmt.Sprintf("golang-ecal_sample_%s", mode), ecal.InitDefault)
if err != nil {
log.Fatal(err)
}
defer ecal.Finalize(ecal.InitAll)
switch mode {
case "minimal_snd":
minimalSnd()
case "minimal_rec":
minimalRec()
case "person_snd":
personSnd()
case "person_rec":
personRec()
default:
log.Fatalf("Unknown mode \"%s\".", mode)
}
}