Skip to content

Commit

Permalink
migrate remaing client methods
Browse files Browse the repository at this point in the history
  • Loading branch information
CrowdHailer committed Jan 20, 2022
1 parent 692efa9 commit 169303b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
21 changes: 15 additions & 6 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func GetEndpoints(ctx context.Context, endpoint string, opts ...Option) ([]*ua.E
return nil, err
}
defer c.Close()
res, err := c.GetEndpoints()
res, err := c.GetEndpointsWithContext(ctx)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -810,22 +810,25 @@ func (c *Client) ActivateSessionWithContext(ctx context.Context, s *Session) err
//
// See Part 4, 5.6.4
func (c *Client) CloseSession() error {
return c.CloseSessionWithContext(context.Background())
}
func (c *Client) CloseSessionWithContext(ctx context.Context) error {
stats.Client().Add("CloseSession", 1)
if err := c.closeSession(c.Session()); err != nil {
if err := c.closeSession(ctx, c.Session()); err != nil {
return err
}
c.setSession(nil)
return nil
}

// closeSession closes the given session.
func (c *Client) closeSession(s *Session) error {
func (c *Client) closeSession(ctx context.Context, s *Session) error {
if s == nil {
return nil
}
req := &ua.CloseSessionRequest{DeleteSubscriptions: true}
var res *ua.CloseSessionResponse
return c.Send(req, func(v interface{}) error {
return c.SendWithContext(ctx, req, func(v interface{}) error {
return safeAssign(v, &res)
})
}
Expand Down Expand Up @@ -876,13 +879,16 @@ func (c *Client) Node(id *ua.NodeID) *Node {
}

func (c *Client) GetEndpoints() (*ua.GetEndpointsResponse, error) {
return c.GetEndpointsWithContext(context.Background())
}
func (c *Client) GetEndpointsWithContext(ctx context.Context) (*ua.GetEndpointsResponse, error) {
stats.Client().Add("GetEndpoints", 1)

req := &ua.GetEndpointsRequest{
EndpointURL: c.endpointURL,
}
var res *ua.GetEndpointsResponse
err := c.Send(req, func(v interface{}) error {
err := c.SendWithContext(ctx, req, func(v interface{}) error {
return safeAssign(v, &res)
})
return res, err
Expand Down Expand Up @@ -1077,6 +1083,9 @@ func (c *Client) UnregisterNodesWithContext(ctx context.Context, req *ua.Unregis
}

func (c *Client) HistoryReadRawModified(nodes []*ua.HistoryReadValueID, details *ua.ReadRawModifiedDetails) (*ua.HistoryReadResponse, error) {
return c.HistoryReadRawModifiedWithContext(context.Background(), nodes, details)
}
func (c *Client) HistoryReadRawModifiedWithContext(ctx context.Context, nodes []*ua.HistoryReadValueID, details *ua.ReadRawModifiedDetails) (*ua.HistoryReadResponse, error) {
stats.Client().Add("HistoryReadRawModified", 1)
stats.Client().Add("HistoryReadValueID", int64(len(nodes)))

Expand All @@ -1093,7 +1102,7 @@ func (c *Client) HistoryReadRawModified(nodes []*ua.HistoryReadValueID, details
}

var res *ua.HistoryReadResponse
err := c.Send(req, func(v interface{}) error {
err := c.SendWithContext(ctx, req, func(v interface{}) error {
return safeAssign(v, &res)
})
return res, err
Expand Down
3 changes: 2 additions & 1 deletion client_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package opcua

import (
"context"
"testing"

"github.com/gopcua/opcua/id"
Expand All @@ -10,7 +11,7 @@ import (

func TestClient_Send_DoesNotPanicWhenDisconnected(t *testing.T) {
c := NewClient("opc.tcp://example.com:4840")
err := c.Send(&ua.ReadRequest{}, func(i interface{}) error {
err := c.SendWithContext(context.Background(), &ua.ReadRequest{}, func(i interface{}) error {
return nil
})
verify.Values(t, "", err, ua.StatusBadServerNotConnected)
Expand Down

0 comments on commit 169303b

Please sign in to comment.