diff --git a/CMakeLists.txt b/CMakeLists.txt index 28f5a26..c2db2e5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,7 +17,13 @@ target_sources(ecal_go_publisher PRIVATE ) target_link_libraries(ecal_go_publisher PRIVATE eCAL::core) -foreach(package "ecal/publisher" "ecal" "ecal/protobuf/publisher") +# Subpackages that use cgo +set(subpackages + "ecal" + "ecal/publisher" +) + +foreach(package ${subpackages}) cmake_path(GET package STEM package_name) # Have content embedded here to evalue the package_name variable diff --git a/ecal/publisher/publisher.go b/ecal/publisher/publisher.go index 72e81ee..c67cccd 100644 --- a/ecal/publisher/publisher.go +++ b/ecal/publisher/publisher.go @@ -48,13 +48,6 @@ func New() (*Publisher, error) { }, nil } -func NewStringDataType() DataType { - return DataType{ - Name: "std::string", - Encoding: "base", - } -} - func (p *Publisher) Delete() { fmt.Println("Deleting publisher") if !p.stopped { diff --git a/ecal/string/publisher/string_publisher.go b/ecal/string/publisher/string_publisher.go new file mode 100644 index 0000000..5b36c83 --- /dev/null +++ b/ecal/string/publisher/string_publisher.go @@ -0,0 +1,28 @@ +package publisher + +import ( + "github.com/DownerCase/ecal-go/ecal/publisher" +) + +type Publisher struct { + publisher.Publisher +} + +func New() (*Publisher, error) { + pub, err := publisher.New() + return &Publisher{*pub}, err +} + +func (p *Publisher) Send(msg string) error { + p.Messages <- []byte(msg) + return nil +} + +func (p *Publisher) Create(topic string) error { + return p.Publisher.Create(topic, + publisher.DataType{ + Name: "std::string", + Encoding: "base", + }, + ) +} diff --git a/ecal/string/publisher/string_publisher_test.go b/ecal/string/publisher/string_publisher_test.go new file mode 100644 index 0000000..cb884af --- /dev/null +++ b/ecal/string/publisher/string_publisher_test.go @@ -0,0 +1,28 @@ +package publisher + +import ( + "testing" +) + +func TestProtobufPublisher(t *testing.T) { + pub, err := New() + + if err != nil { + t.Error(err) + } + defer pub.Delete() + + if err := pub.Create("testing"); err != nil { + t.Error(err) + } + + if pub.Messages == nil { + t.Error("Message channel nil") + } + + // TODO: Check datatype information + if err := pub.Send("my message"); err != nil { + t.Error("Failed to send message", err) + } +} + diff --git a/main.go b/main.go index f2695f9..8a2ae03 100644 --- a/main.go +++ b/main.go @@ -2,10 +2,12 @@ package main import ( "fmt" + "strconv" "time" "github.com/DownerCase/ecal-go/ecal" "github.com/DownerCase/ecal-go/ecal/protobuf/publisher" + string_publisher "github.com/DownerCase/ecal-go/ecal/string/publisher" "github.com/DownerCase/ecal-go/protos" ) @@ -46,7 +48,12 @@ func main() { } if pub.Create("person") != nil { - panic("Failed to Create publisher") + panic("Failed to Create protobuf publisher") + } + + string_pub, _ := string_publisher.New() + if string_pub.Create("string topic") != nil { + panic("Failed to Create string publisher") } for idx := range 100 { @@ -66,6 +73,11 @@ func main() { fmt.Println("Error: ", err) } + string_msg := "Sent " + strconv.Itoa(idx) + " messages" + if err = string_pub.Send(string_msg); err != nil { + fmt.Println("Error: ", err) + } + // Delay next iteration time.Sleep(1 * time.Second) }