From a28605b9edf8169ae31ae8fa3da00dd27f1227de Mon Sep 17 00:00:00 2001 From: DownerCase Date: Mon, 13 Jan 2025 10:27:02 +0000 Subject: [PATCH] Update Go code for new datatypes --- cmd/monitor/common.go | 2 +- cmd/monitor/page_services_detailed.go | 6 ++-- cmd/monitor/page_services_main.go | 16 +++++++--- cmd/monitor/page_topics_detailed.go | 4 +-- cmd/monitor/page_topics_main.go | 14 ++++++--- cmd/monitor/page_topics_messages.go | 4 +-- ecal/monitoring/callback.go | 44 +++++++++++++-------------- ecal/monitoring/monitoring.go | 13 +++----- ecal/registration/registration.go | 2 +- ecal/types.go | 2 +- 10 files changed, 56 insertions(+), 51 deletions(-) diff --git a/cmd/monitor/common.go b/cmd/monitor/common.go index 03af39c..b5c1299 100644 --- a/cmd/monitor/common.go +++ b/cmd/monitor/common.go @@ -54,7 +54,7 @@ func getTopicMonitoring(topicType TopicType) []monitoring.TopicMon { return nil } -func getTopicFromID(topicType TopicType, id string) (monitoring.TopicMon, error) { +func getTopicFromID(topicType TopicType, id uint64) (monitoring.TopicMon, error) { topicList := getTopicMonitoring(topicType) for _, topic := range topicList { if topic.TopicID == id { diff --git a/cmd/monitor/page_services_detailed.go b/cmd/monitor/page_services_detailed.go index 65fa6a7..5d58c58 100644 --- a/cmd/monitor/page_services_detailed.go +++ b/cmd/monitor/page_services_detailed.go @@ -12,7 +12,7 @@ import ( type ModelServiceDetailed struct { table table.Model - ID string + ID uint64 IsServer bool } @@ -24,7 +24,7 @@ func NewDetailedServiceModel() *ModelServiceDetailed { return &ModelServiceDetailed{ table: NewTable(cols), - ID: "", + ID: 0, } } @@ -97,7 +97,7 @@ func getMethodRows(b monitoring.ServiceBase) []table.Row { for _, method := range b.Methods { rows = append(rows, table.Row{ method.Name, - fmt.Sprintf("%s -> %s (Called x%v)", method.RequestType.Type, method.ResponseType.Type, method.CallCount), + fmt.Sprintf("%v -> %v (Called x%v)", method.RequestType.Name, method.ResponseType.Name, method.CallCount), }) } diff --git a/cmd/monitor/page_services_main.go b/cmd/monitor/page_services_main.go index 820256f..dc2991e 100644 --- a/cmd/monitor/page_services_main.go +++ b/cmd/monitor/page_services_main.go @@ -1,6 +1,7 @@ package main import ( + "fmt" "strconv" "github.com/charmbracelet/bubbles/table" @@ -39,13 +40,18 @@ func (m *ModelServicesMain) View() string { return baseStyle.Render(m.table.View()) + "\n" + m.table.HelpView() } -func (m *ModelServicesMain) GetSelectedID() (string, bool, error) { +func (m *ModelServicesMain) GetSelectedID() (uint64, bool, error) { row := m.table.SelectedRow() if row == nil { - return "", false, errEmptyTable + return 0, false, errEmptyTable } - return row[0], row[1] == "S", nil + id, err := strconv.ParseUint(row[0], 10, 64) + if err != nil { + err = fmt.Errorf("services - GetSelectedID() %w", err) + } + + return id, row[1] == "S", err } func (m *ModelServicesMain) updateTable(msg tea.Msg) tea.Cmd { @@ -78,14 +84,14 @@ func serviceToRow(service monitoring.ServiceBase) table.Row { func clientToRow(client monitoring.ClientMon) table.Row { return append( - []string{client.ID, "C"}, + []string{strconv.FormatUint(client.ID, 10), "C"}, serviceToRow(client.ServiceBase)..., ) } func serverToRow(server monitoring.ServerMon) table.Row { return append( - []string{server.ID, "S"}, + []string{strconv.FormatUint(server.ID, 10), "S"}, serviceToRow(server.ServiceBase)..., ) } diff --git a/cmd/monitor/page_topics_detailed.go b/cmd/monitor/page_topics_detailed.go index 1cff566..3ab750b 100644 --- a/cmd/monitor/page_topics_detailed.go +++ b/cmd/monitor/page_topics_detailed.go @@ -10,7 +10,7 @@ import ( type ModelTopicDetailed struct { table table.Model - id string `exhaustruct:"optional"` + id uint64 `exhaustruct:"optional"` topicType TopicType `exhaustruct:"optional"` } @@ -25,7 +25,7 @@ func NewDetailedModel() *ModelTopicDetailed { } } -func (m *ModelTopicDetailed) ShowTopic(topicID string, topicType TopicType) { +func (m *ModelTopicDetailed) ShowTopic(topicID uint64, topicType TopicType) { m.id = topicID m.topicType = topicType m.updateDetailedTable(nil) diff --git a/cmd/monitor/page_topics_main.go b/cmd/monitor/page_topics_main.go index b12cd96..4060eeb 100644 --- a/cmd/monitor/page_topics_main.go +++ b/cmd/monitor/page_topics_main.go @@ -1,6 +1,7 @@ package main import ( + "fmt" "strconv" "strings" @@ -122,10 +123,10 @@ func (m *ModelTopicsMain) View() string { return baseStyle.Render(m.table.View()) + "\n" + m.help.View(m.keymap) } -func (m *ModelTopicsMain) GetSelectedID() (string, TopicType, error) { +func (m *ModelTopicsMain) GetSelectedID() (uint64, TopicType, error) { row := m.table.SelectedRow() if row == nil { - return "", 0, errEmptyTable + return 0, 0, errEmptyTable } var topicType TopicType @@ -137,7 +138,12 @@ func (m *ModelTopicsMain) GetSelectedID() (string, TopicType, error) { topicType = topicTypePublisher } - return row[0], topicType, nil + id, err := strconv.ParseUint(row[0], 10, 64) + if err != nil { + err = fmt.Errorf("topics - GetSelectedID(): %w", err) + } + + return id, topicType, err } func (m *ModelTopicsMain) updateTopicsTable(msg tea.Msg) { @@ -168,7 +174,7 @@ func (m *ModelTopicsMain) updateTopicsTable(msg tea.Msg) { func topicToRow(topic monitoring.TopicMon) table.Row { return []string{ - topic.TopicID, + strconv.FormatUint(topic.TopicID, 10), strings.ToUpper(topic.Direction[0:1]), topic.TopicName, topic.Datatype.Name, diff --git a/cmd/monitor/page_topics_messages.go b/cmd/monitor/page_topics_messages.go index 20655a3..f56b3e8 100644 --- a/cmd/monitor/page_topics_messages.go +++ b/cmd/monitor/page_topics_messages.go @@ -18,7 +18,7 @@ type ModelTopicMessages struct { viewport viewport.Model mon monitoring.TopicMon topicType TopicType - topicID string + topicID uint64 subscriber *subscriber.Subscriber msg []byte deserializer func([]byte) string @@ -76,7 +76,7 @@ func (m *ModelTopicMessages) Refresh() { m.mon, _ = getTopicFromID(m.topicType, m.topicID) } -func (m *ModelTopicMessages) ShowTopic(topicID string, topicType TopicType) { +func (m *ModelTopicMessages) ShowTopic(topicID uint64, topicType TopicType) { if m.topicID != topicID { m.topicType = topicType m.topicID = topicID diff --git a/ecal/monitoring/callback.go b/ecal/monitoring/callback.go index aba0a3f..af65918 100644 --- a/ecal/monitoring/callback.go +++ b/ecal/monitoring/callback.go @@ -10,22 +10,26 @@ import ( "github.com/DownerCase/ecal-go/ecal" ) +func copyToDatatype(datatype C.struct_CDatatype) ecal.DataType { + return ecal.DataType{ + Name: C.GoString(datatype.name), + Encoding: C.GoString(datatype.encoding), + } +} + func copyToTopicMons(ctopics []C.struct_CTopicMon) []TopicMon { topics := make([]TopicMon, len(ctopics)) for idx, pub := range ctopics { topics[idx] = TopicMon{ - TopicID: C.GoString(pub.topic_id), - RegistrationClock: int32(pub.registration_clock), - TopicName: C.GoString(pub.topic_name), - DataClock: int64(pub.data_clock), - DataFreq: int32(pub.data_freq), - TopicSize: int32(pub.topic_size), - UnitName: C.GoString(pub.unit_name), - Direction: C.GoString(pub.direction), - Datatype: ecal.DataType{ - Name: C.GoString(pub.datatype.name), - Encoding: C.GoString(pub.datatype.encoding), - }, + TopicID: uint64(pub.topic_id), + RegistrationClock: int32(pub.registration_clock), + TopicName: C.GoString(pub.topic_name), + DataClock: int64(pub.data_clock), + DataFreq: int32(pub.data_freq), + TopicSize: int32(pub.topic_size), + UnitName: C.GoString(pub.unit_name), + Direction: C.GoString(pub.direction), + Datatype: copyToDatatype(pub.datatype), ConnectionsLocal: int32(pub.connections_local), ConnectionsExternal: int32(pub.connections_external), MessageDrops: int32(pub.message_drops), @@ -59,16 +63,10 @@ func copyToMethodMons(cmethods []C.struct_CMethodMon) []MethodMon { methods := make([]MethodMon, len(cmethods)) for idx, cmethod := range cmethods { methods[idx] = MethodMon{ - Name: C.GoString(cmethod.name), - RequestType: methodType{ - Type: C.GoString(cmethod.request_name), - Descriptor: C.GoString(cmethod.request_desc), - }, - ResponseType: methodType{ - Type: C.GoString(cmethod.response_name), - Descriptor: C.GoString(cmethod.response_desc), - }, - CallCount: int64(cmethod.call_count), + Name: C.GoString(cmethod.name), + RequestType: copyToDatatype(cmethod.req_datatype), + ResponseType: copyToDatatype(cmethod.resp_datatype), + CallCount: int64(cmethod.call_count), } } return methods @@ -77,7 +75,7 @@ func copyToMethodMons(cmethods []C.struct_CMethodMon) []MethodMon { func copyToServiceBase(cbase C.struct_CServiceCommon) ServiceBase { return ServiceBase{ Name: C.GoString(cbase.name), - ID: C.GoString(cbase.id), + ID: uint64(cbase.id), RegistrationClock: int32(cbase.registration_clock), HostName: C.GoString(cbase.host_name), Process: C.GoString(cbase.process_name), diff --git a/ecal/monitoring/monitoring.go b/ecal/monitoring/monitoring.go index 7a792c8..d34d60c 100644 --- a/ecal/monitoring/monitoring.go +++ b/ecal/monitoring/monitoring.go @@ -60,7 +60,7 @@ type TopicMon struct { // pid int32 // process_name string UnitName string - TopicID string + TopicID uint64 TopicName string Direction string Datatype ecal.DataType @@ -91,21 +91,16 @@ type ProcessMon struct { RuntimeVersion string // eCAL Version in use } -type methodType struct { - Type string - Descriptor string -} - type MethodMon struct { Name string - RequestType methodType - ResponseType methodType + RequestType ecal.DataType + ResponseType ecal.DataType CallCount int64 } type ServiceBase struct { Name string - ID string + ID uint64 Methods []MethodMon RegistrationClock int32 // registration heart beat HostName string diff --git a/ecal/registration/registration.go b/ecal/registration/registration.go index 0faf6c7..41b3d92 100644 --- a/ecal/registration/registration.go +++ b/ecal/registration/registration.go @@ -58,7 +58,7 @@ func RemSubscriberCallback(token CallbackToken) { func toTopicID(id *C.struct_CTopicId) ecal.TopicID { return ecal.TopicID{ TopicID: ecal.EntityID{ - EntityID: C.GoString(id.topic_id.entity_id), + EntityID: uint64(id.topic_id.entity_id), ProcessID: int32(id.topic_id.process_id), HostName: C.GoString(id.topic_id.host_name), }, diff --git a/ecal/types.go b/ecal/types.go index 35c3e07..8f565ab 100644 --- a/ecal/types.go +++ b/ecal/types.go @@ -1,7 +1,7 @@ package ecal type EntityID struct { - EntityID string + EntityID uint64 ProcessID int32 HostName string }