Skip to content

Commit

Permalink
Add string publisher
Browse files Browse the repository at this point in the history
  • Loading branch information
DownerCase committed Nov 14, 2024
1 parent b8182ea commit 46c3bfa
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 9 deletions.
8 changes: 7 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 0 additions & 7 deletions ecal/publisher/publisher.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
28 changes: 28 additions & 0 deletions ecal/string/publisher/string_publisher.go
Original file line number Diff line number Diff line change
@@ -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",
},
)
}
28 changes: 28 additions & 0 deletions ecal/string/publisher/string_publisher_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}

14 changes: 13 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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 {
Expand All @@ -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)
}
Expand Down

0 comments on commit 46c3bfa

Please sign in to comment.