Skip to content

Commit

Permalink
Merge pull request #753 from NovaFell/main
Browse files Browse the repository at this point in the history
Added Error return to SelectEndpoint function
  • Loading branch information
magiconair authored Dec 5, 2024
2 parents 476afe8 + f3e85a2 commit 5194af7
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 22 deletions.
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

0 comments on commit 5194af7

Please sign in to comment.