Skip to content

Commit

Permalink
refactor: Merge types into core (#35)
Browse files Browse the repository at this point in the history
* refactor: Merge types package to core

* registration: remove type alias
  • Loading branch information
DownerCase authored Dec 19, 2024
1 parent 583d176 commit 2fb3656
Show file tree
Hide file tree
Showing 15 changed files with 33 additions and 55 deletions.
22 changes: 7 additions & 15 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,16 @@ find_package(eCAL CONFIG REQUIRED)
add_library(utils INTERFACE)
target_include_directories(utils INTERFACE .)

add_library(ecal_go_types)
target_sources(ecal_go_types PRIVATE
./ecal/types/types.h
./ecal/types/types.hpp
./ecal/types/types.cpp
)
target_include_directories(ecal_go_types
PUBLIC ./ecal/types
)
target_link_libraries(ecal_go_types PRIVATE eCAL::core)

add_library(ecal_go_core)
target_sources(ecal_go_core PRIVATE
./ecal/core.h
./ecal/core.cpp
./ecal/types.h
./ecal/types.hpp
./ecal/types.cpp
)
target_link_libraries(ecal_go_core PRIVATE eCAL::core)
target_include_directories(ecal_go_core PUBLIC ./ecal)

add_library(ecal_go_publisher)
target_sources(ecal_go_publisher PRIVATE
Expand All @@ -43,26 +36,25 @@ target_sources(ecal_go_logging PRIVATE
./ecal/logging/logging.h
./ecal/logging/logging.cpp
)
target_link_libraries(ecal_go_logging PRIVATE eCAL::core ecal_go_types)
target_link_libraries(ecal_go_logging PRIVATE eCAL::core ecal_go_core)

add_library(ecal_go_registration)
target_sources(ecal_go_registration PRIVATE
./ecal/registration/registration.h
./ecal/registration/registration.cpp
)
target_link_libraries(ecal_go_registration PRIVATE eCAL::core ecal_go_types)
target_link_libraries(ecal_go_registration PRIVATE eCAL::core ecal_go_core)

add_library(ecal_go_monitoring)
target_sources(ecal_go_monitoring PRIVATE
./ecal/monitoring/monitoring.h
./ecal/monitoring/monitoring.cpp
)
target_link_libraries(ecal_go_monitoring PRIVATE eCAL::core ecal_go_types)
target_link_libraries(ecal_go_monitoring PRIVATE eCAL::core ecal_go_core)

# Subpackages that use cgo
set(subpackages
"ecal"
"ecal/types"
"ecal/publisher"
"ecal/subscriber"
"ecal/logging"
Expand Down
2 changes: 1 addition & 1 deletion ecal/logging/logging.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package logging

// #cgo CPPFLAGS: -I${SRCDIR}/../types
// #cgo CPPFLAGS: -I${SRCDIR}/..
// #include <ecal/ecal_log_level.h>
// #include "logging.h"
// void GoLog(enum eCAL_Logging_eLogLevel level, _GoString_ msg) {
Expand Down
1 change: 0 additions & 1 deletion ecal/logging/logging_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

"github.com/DownerCase/ecal-go/ecal"
"github.com/DownerCase/ecal-go/ecal/logging"
_ "github.com/DownerCase/ecal-go/ecal/types"
"github.com/DownerCase/ecal-go/internal/ecaltest"
)

Expand Down
4 changes: 2 additions & 2 deletions ecal/monitoring/callback.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"runtime/cgo"
"unsafe"

"github.com/DownerCase/ecal-go/ecal/types"
"github.com/DownerCase/ecal-go/ecal"
)

func copyToTopicMons(ctopics []C.struct_CTopicMon) []TopicMon {
Expand All @@ -22,7 +22,7 @@ func copyToTopicMons(ctopics []C.struct_CTopicMon) []TopicMon {
TopicSize: int32(pub.topic_size),
UnitName: C.GoString(pub.unit_name),
Direction: C.GoString(pub.direction),
Datatype: types.DataType{
Datatype: ecal.DataType{
Name: C.GoString(pub.datatype.name),
Encoding: C.GoString(pub.datatype.encoding),
},
Expand Down
6 changes: 3 additions & 3 deletions ecal/monitoring/monitoring.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package monitoring
//#cgo LDFLAGS: -lecal_core
//#include <ecal/ecal_process_severity.h>
//#include "monitoring.h"
//#cgo CPPFLAGS: -I${SRCDIR}/../types
//#cgo CPPFLAGS: -I${SRCDIR}/..
import "C"

import (
"runtime/cgo"
"strconv"

"github.com/DownerCase/ecal-go/ecal/types"
"github.com/DownerCase/ecal-go/ecal"
)

type MonitorEntity uint
Expand Down Expand Up @@ -63,7 +63,7 @@ type TopicMon struct {
TopicID string
TopicName string
Direction string
Datatype types.DataType
Datatype ecal.DataType
// TODO: transport layer
TopicSize int32 // Size of messages (bytes)
ConnectionsLocal int32
Expand Down
4 changes: 2 additions & 2 deletions ecal/publisher/publisher.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ import (
"runtime/cgo"
"unsafe"

"github.com/DownerCase/ecal-go/ecal/types"
"github.com/DownerCase/ecal-go/ecal"
)

var ErrFailedNew = errors.New("failed to create new publisher")

type DataType = types.DataType
type DataType = ecal.DataType

type Publisher struct {
Messages chan []byte
Expand Down
4 changes: 2 additions & 2 deletions ecal/publisher/publisher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import (
"fmt"
"testing"

"github.com/DownerCase/ecal-go/ecal"
"github.com/DownerCase/ecal-go/ecal/publisher"
"github.com/DownerCase/ecal-go/ecal/types"
testutilpublisher "github.com/DownerCase/ecal-go/internal/ecaltest/testutil_publisher"
)

func TestNewPublishers(t *testing.T) {
for i := range 100 {
ptr, err := publisher.New(fmt.Sprintf("testPubTopic-%v", i), types.DataType{})
ptr, err := publisher.New(fmt.Sprintf("testPubTopic-%v", i), ecal.DataType{})
if err != nil {
t.Error(err)
}
Expand Down
28 changes: 8 additions & 20 deletions ecal/registration/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package registration
// #cgo LDFLAGS: -lecal_core
//#include "registration.h"
//#include "types.h"
// #cgo CPPFLAGS: -I${SRCDIR}/../types
// #cgo CPPFLAGS: -I${SRCDIR}/..
import "C"

import (
"log"
"runtime/cgo"

"github.com/DownerCase/ecal-go/ecal/types"
"github.com/DownerCase/ecal-go/ecal"
)

type Event uint8
Expand All @@ -20,24 +20,12 @@ const (
EntityDeleted Event = 1
)

type QualityFlags uint8

type (
EntityID = types.EntityID
TopicID = types.TopicID
)

type CallbackToken struct {
ecalToken uint
goHandle cgo.Handle
}

type QualityTopicInfo struct {
Datatype types.DataType
QualityFlags QualityFlags
}

func AddPublisherEventCallback(callback func(TopicID, Event)) CallbackToken {
func AddPublisherEventCallback(callback func(ecal.TopicID, Event)) CallbackToken {
handle := cgo.NewHandle(callback)
ecalToken := C.AddPublisherEventCallback(C.uintptr_t(handle))
token := CallbackToken{
Expand All @@ -52,7 +40,7 @@ func RemPublisherCallback(token CallbackToken) {
token.goHandle.Delete()
}

func AddSubscriberEventCallback(callback func(TopicID, Event)) CallbackToken {
func AddSubscriberEventCallback(callback func(ecal.TopicID, Event)) CallbackToken {
handle := cgo.NewHandle(callback)
ecalToken := C.AddSubscriberEventCallback(C.uintptr_t(handle))
token := CallbackToken{
Expand All @@ -67,9 +55,9 @@ func RemSubscriberCallback(token CallbackToken) {
token.goHandle.Delete()
}

func toTopicID(id *C.struct_CTopicId) TopicID {
return TopicID{
TopicID: EntityID{
func toTopicID(id *C.struct_CTopicId) ecal.TopicID {
return ecal.TopicID{
TopicID: ecal.EntityID{
EntityID: C.GoString(id.topic_id.entity_id),
ProcessID: int32(id.topic_id.process_id),
HostName: C.GoString(id.topic_id.host_name),
Expand All @@ -81,7 +69,7 @@ func toTopicID(id *C.struct_CTopicId) TopicID {
//export goTopicEventCallback
func goTopicEventCallback(handle C.uintptr_t, id C.struct_CTopicId, event C.uint8_t) {
h := cgo.Handle(handle)
f, ok := h.Value().(func(TopicID, Event))
f, ok := h.Value().(func(ecal.TopicID, Event))
if !ok {
log.Panic("Invalid handle passed to registration callback")
}
Expand Down
4 changes: 2 additions & 2 deletions ecal/subscriber/subscriber.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"time"
"unsafe"

"github.com/DownerCase/ecal-go/ecal/types"
"github.com/DownerCase/ecal-go/ecal"
)

var (
Expand All @@ -34,7 +34,7 @@ type Subscriber struct {
Deserialize func(unsafe.Pointer, int) any
}

type DataType = types.DataType
type DataType = ecal.DataType

func New(topic string, datatype DataType) (*Subscriber, error) {
sub := &Subscriber{
Expand Down
File renamed without changes.
4 changes: 1 addition & 3 deletions ecal/types/types.go → ecal/types.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
package types

import "C"
package ecal

type EntityID struct {
EntityID string
Expand Down
File renamed without changes.
File renamed without changes.
7 changes: 4 additions & 3 deletions internal/ecaltest/regtest/test_registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"
"time"

"github.com/DownerCase/ecal-go/ecal"
"github.com/DownerCase/ecal-go/ecal/registration"
)

Expand All @@ -14,11 +15,11 @@ const (

type Callback struct {
Event registration.Event
ID registration.TopicID
ID ecal.TopicID
}

func EventCallback(topic string, channel chan Callback) func(registration.TopicID, registration.Event) {
return func(id registration.TopicID, event registration.Event) {
func EventCallback(topic string, channel chan Callback) func(ecal.TopicID, registration.Event) {
return func(id ecal.TopicID, event registration.Event) {
if id.TopicName == topic {
channel <- Callback{event, id}
}
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,6 @@ func receiveMessages(s *subscriber.Subscriber) {
}
}

func registrationLogger(id registration.TopicID, _ registration.Event) {
func registrationLogger(id ecal.TopicID, _ registration.Event) {
fmt.Println("Received registration sample:", id) //nolint:forbidigo
}

0 comments on commit 2fb3656

Please sign in to comment.