-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
114 lines (91 loc) · 2.89 KB
/
main.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
package main
import (
"fmt"
"time"
"github.com/DownerCase/ecal-go/ecal"
"github.com/DownerCase/ecal-go/ecal/logging"
"github.com/DownerCase/ecal-go/ecal/protobuf/publisher"
"github.com/DownerCase/ecal-go/ecal/registration"
string_publisher "github.com/DownerCase/ecal-go/ecal/string/publisher"
"github.com/DownerCase/ecal-go/ecal/string/subscriber"
"github.com/DownerCase/ecal-go/protos"
)
func main() {
// eCAL version as string and semantic version components
fmt.Println(ecal.GetVersionString())
fmt.Println(ecal.GetVersion())
// Initialize eCAL with default config, the unit name "Go eCAL",
// and the Publisher, Subscriber and Logging components enabled
initResult := ecal.Initialize(
ecal.NewConfig(),
"Go eCAL!",
ecal.C_Publisher|ecal.C_Subscriber|ecal.C_Logging,
)
// Enable all logging levels in the console
logging.SetConsoleFilter(logging.LevelAll)
// Log a message
logging.Log(logging.LevelInfo, "Initialized: ", initResult)
defer ecal.Finalize() // Shutdown eCAL at the end of the program
registration.AddPublisherEventCallback(registrationLogger)
// Change the unit name
logging.Debug("Changed name:", ecal.SetUnitName("Go demo"))
// Check if the eCAL system is Ok.
// Other eCAL programs can send a message to cause ecal.Ok() to return false
// Typically used as a condition to terminate daemon-style programs
logging.Infof("eCAL ok: %t", ecal.Ok())
// Create new protobuf publisher
pub, err := publisher.New[protos.Person]()
if err != nil {
panic("Failed to make new publisher")
}
defer pub.Delete() // Don't forget to delete the publisher when done!
person := &protos.Person{
Id: 0, Name: "John", Email: "[email protected]",
Dog: &protos.Dog{Name: "Pluto"},
House: &protos.House{Rooms: 5},
}
if pub.Create("person") != nil {
panic("Failed to Create protobuf publisher")
}
string_pub, _ := string_publisher.New()
if string_pub.Create("string topic") != nil {
panic("Failed to Create string publisher")
}
sub, _ := subscriber.New()
if sub.Create("string topic") != nil {
panic("Failed to Create string subscriber")
}
go receiveMessages(sub)
for idx := range 100 {
// Check if program has been requested to stop
if !ecal.Ok() {
logging.Warn("eCAL.Ok() is false; shutting down")
return
}
logging.Info("Sending message ", idx)
// Update message to send
person.Id = int32(idx)
// Serialize and send protobuf message
if err := pub.Send(person); err != nil {
logging.Error(err)
}
if err = string_pub.Send("Message ", idx); err != nil {
logging.Error(err)
}
// Delay next iteration
time.Sleep(1 * time.Second)
}
}
func receiveMessages(s *subscriber.Subscriber) {
for {
msg, err := s.Receive(2 * time.Second)
if err == nil {
fmt.Println("Received:", msg)
} else {
fmt.Println(err)
}
}
}
func registrationLogger(id registration.TopicId, event registration.Event) {
fmt.Println("Received registration sample:", id)
}