From 26ed22d29e5b86b0ac3a60b81b50a25563205578 Mon Sep 17 00:00:00 2001 From: NovaFell Date: Tue, 3 Dec 2024 14:47:47 -0600 Subject: [PATCH 1/2] SelectEndpoint returns error --- client.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/client.go b/client.go index f3a5329f..0307f00a 100644 --- a/client.go +++ b/client.go @@ -85,9 +85,9 @@ func GetEndpoints(ctx context.Context, endpoint string, opts ...Option) ([]*ua.E // security policy and security mode. policy and mode can be omitted so that // only one of them has to match. // todo(fs): should this function return an error? -func SelectEndpoint(endpoints []*ua.EndpointDescription, policy string, mode ua.MessageSecurityMode) *ua.EndpointDescription { +func SelectEndpoint(endpoints []*ua.EndpointDescription, policy string, mode ua.MessageSecurityMode) (*ua.EndpointDescription, error) { if len(endpoints) == 0 { - return nil + return nil, errors.Errorf("no endpoints available") } sort.Sort(sort.Reverse(bySecurityLevel(endpoints))) @@ -95,26 +95,26 @@ func SelectEndpoint(endpoints []*ua.EndpointDescription, policy string, mode ua. // don't care -> return highest security level if policy == "" && mode == ua.MessageSecurityModeInvalid { - return endpoints[0] + return endpoints[0], nil } for _, p := range endpoints { // match only security mode if policy == "" && p.SecurityMode == mode { - return p + return p, nil } // match only security policy if p.SecurityPolicyURI == policy && mode == ua.MessageSecurityModeInvalid { - return p + return p, nil } // match both if p.SecurityPolicyURI == policy && p.SecurityMode == mode { - return p + return p, nil } } - return nil + return nil, errors.Errorf("no matching endpoint found for policy %s and mode %s", policy, mode) } type bySecurityLevel []*ua.EndpointDescription From f3e85a2928361e16b0b577a1255a45e628986177 Mon Sep 17 00:00:00 2001 From: NovaFell Date: Tue, 3 Dec 2024 14:48:05 -0600 Subject: [PATCH 2/2] update SelectEndpoint call --- examples/datetime/datetime.go | 6 +++--- examples/monitor/monitor.go | 6 +++--- examples/subscribe/subscribe.go | 6 +++--- examples/trigger/trigger.go | 6 +++--- examples/udt/udt.go | 6 +++--- 5 files changed, 15 insertions(+), 15 deletions(-) diff --git a/examples/datetime/datetime.go b/examples/datetime/datetime.go index dca80633..02bcb7aa 100644 --- a/examples/datetime/datetime.go +++ b/examples/datetime/datetime.go @@ -31,9 +31,9 @@ func main() { if err != nil { log.Fatal(err) } - ep := opcua.SelectEndpoint(endpoints, *policy, ua.MessageSecurityModeFromString(*mode)) - if ep == nil { - log.Fatal("Failed to find suitable endpoint") + ep, err := opcua.SelectEndpoint(endpoints, *policy, ua.MessageSecurityModeFromString(*mode)) + if err == nil { + log.Fatal(err) } fmt.Println("*", ep.SecurityPolicyURI, ep.SecurityMode) diff --git a/examples/monitor/monitor.go b/examples/monitor/monitor.go index ef0f39ca..f9c06df4 100644 --- a/examples/monitor/monitor.go +++ b/examples/monitor/monitor.go @@ -47,9 +47,9 @@ func main() { log.Fatal(err) } - ep := opcua.SelectEndpoint(endpoints, *policy, ua.MessageSecurityModeFromString(*mode)) - if ep == nil { - log.Fatal("Failed to find suitable endpoint") + ep, err := opcua.SelectEndpoint(endpoints, *policy, ua.MessageSecurityModeFromString(*mode)) + if err == nil { + log.Fatal(err) } log.Print("*", ep.SecurityPolicyURI, ep.SecurityMode) diff --git a/examples/subscribe/subscribe.go b/examples/subscribe/subscribe.go index 8127cd9a..596f4b89 100644 --- a/examples/subscribe/subscribe.go +++ b/examples/subscribe/subscribe.go @@ -43,9 +43,9 @@ func main() { if err != nil { log.Fatal(err) } - ep := opcua.SelectEndpoint(endpoints, *policy, ua.MessageSecurityModeFromString(*mode)) - if ep == nil { - log.Fatal("Failed to find suitable endpoint") + ep, err := opcua.SelectEndpoint(endpoints, *policy, ua.MessageSecurityModeFromString(*mode)) + if err == nil { + log.Fatal(err) } ep.EndpointURL = *endpoint diff --git a/examples/trigger/trigger.go b/examples/trigger/trigger.go index beafb3b3..566bb5cf 100644 --- a/examples/trigger/trigger.go +++ b/examples/trigger/trigger.go @@ -43,9 +43,9 @@ func main() { if err != nil { log.Fatal(err) } - ep := opcua.SelectEndpoint(endpoints, *policy, ua.MessageSecurityModeFromString(*mode)) - if ep == nil { - log.Fatal("Failed to find suitable endpoint") + ep, err := opcua.SelectEndpoint(endpoints, *policy, ua.MessageSecurityModeFromString(*mode)) + if err == nil { + log.Fatal(err) } fmt.Println("*", ep.SecurityPolicyURI, ep.SecurityMode) diff --git a/examples/udt/udt.go b/examples/udt/udt.go index 6fa3ad5a..e5ed1987 100644 --- a/examples/udt/udt.go +++ b/examples/udt/udt.go @@ -39,9 +39,9 @@ func main() { if err != nil { log.Fatal(err) } - ep := opcua.SelectEndpoint(endpoints, *policy, ua.MessageSecurityModeFromString(*mode)) - if ep == nil { - log.Fatal("Failed to find suitable endpoint") + ep, err := opcua.SelectEndpoint(endpoints, *policy, ua.MessageSecurityModeFromString(*mode)) + if err == nil { + log.Fatal(err) } fmt.Println("*", ep.SecurityPolicyURI, ep.SecurityMode)