Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Error return to SelectEndpoint function #753

Merged
merged 2 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,36 +85,36 @@ 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)))
policy = ua.FormatSecurityPolicyURI(policy)

// 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
Expand Down
6 changes: 3 additions & 3 deletions examples/datetime/datetime.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions examples/monitor/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions examples/subscribe/subscribe.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 3 additions & 3 deletions examples/trigger/trigger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions examples/udt/udt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading