Skip to content

Commit

Permalink
add context to node methods
Browse files Browse the repository at this point in the history
  • Loading branch information
CrowdHailer committed Jan 20, 2022
1 parent 9bb60b4 commit d545cfe
Show file tree
Hide file tree
Showing 2 changed files with 220 additions and 53 deletions.
100 changes: 67 additions & 33 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -836,7 +836,15 @@ func (c *Client) closeSession(ctx context.Context, s *Session) error {
// DetachSession removes the session from the client without closing it. The
// caller is responsible to close or re-activate the session. If the client
// does not have an active session the function returns no error.
//
// Note: Starting with v0.5 this method will require a context
// and the corresponding XXXWithContext(ctx) method will be removed.
func (c *Client) DetachSession() (*Session, error) {
return c.DetachSessionWithContext(context.Background())
}

// Note: Starting with v0.5 this method is superseded by the non 'WithContext' method.
func (c *Client) DetachSessionWithContext(ctx context.Context) (*Session, error) {
stats.Client().Add("DetachSession", 1)
s := c.Session()
c.setSession(nil)
Expand All @@ -846,14 +854,14 @@ func (c *Client) DetachSession() (*Session, error) {
// Send sends the request via the secure channel and registers a handler for
// the response. If the client has an active session it injects the
// authentication token.
// Deprecated: Starting with v0.5 this method will require a context
//
// Note: Starting with v0.5 this method will require a context
// and the corresponding XXXWithContext(ctx) method will be removed.
func (c *Client) Send(req ua.Request, h func(interface{}) error) error {
return c.SendWithContext(context.Background(), req, h)
}

// Note: This method will be replaced by the non "WithContext()" version
// of this method.
// Note: Starting with v0.5 this method is superseded by the non 'WithContext' method.
func (c *Client) SendWithContext(ctx context.Context, req ua.Request, h func(interface{}) error) error {
stats.Client().Add("Send", 1)

Expand Down Expand Up @@ -883,14 +891,15 @@ func (c *Client) Node(id *ua.NodeID) *Node {
return &Node{ID: id, c: c}
}

// Deprecated: Starting with v0.5 this method will require a context
// GetEndpoints returns the list of available endpoints of the server.
//
// Note: Starting with v0.5 this method will require a context
// and the corresponding XXXWithContext(ctx) method will be removed.
func (c *Client) GetEndpoints() (*ua.GetEndpointsResponse, error) {
return c.GetEndpointsWithContext(context.Background())
}

// Note: This method will be replaced by the non "WithContext()" version
// of this method.
// Note: Starting with v0.5 this method is superseded by the non 'WithContext' method.
func (c *Client) GetEndpointsWithContext(ctx context.Context) (*ua.GetEndpointsResponse, error) {
stats.Client().Add("GetEndpoints", 1)

Expand Down Expand Up @@ -928,14 +937,14 @@ func cloneReadRequest(req *ua.ReadRequest) *ua.ReadRequest {
//
// By default, the function requests the value of the nodes
// in the default encoding of the server.
// Deprecated: Starting with v0.5 this method will require a context
//
// Note: Starting with v0.5 this method will require a context
// and the corresponding XXXWithContext(ctx) method will be removed.
func (c *Client) Read(req *ua.ReadRequest) (*ua.ReadResponse, error) {
return c.ReadWithContext(context.Background(), req)
}

// Note: This method will be replaced by the non "WithContext()" version
// of this method.
// Note: Starting with v0.5 this method is superseded by the non 'WithContext' method.
func (c *Client) ReadWithContext(ctx context.Context, req *ua.ReadRequest) (*ua.ReadResponse, error) {
stats.Client().Add("Read", 1)
stats.Client().Add("NodesToRead", int64(len(req.NodesToRead)))
Expand Down Expand Up @@ -971,14 +980,14 @@ func (c *Client) ReadWithContext(ctx context.Context, req *ua.ReadRequest) (*ua.
}

// Write executes a synchronous write request.
// Deprecated: Starting with v0.5 this method will require a context
//
// Note: Starting with v0.5 this method will require a context
// and the corresponding XXXWithContext(ctx) method will be removed.
func (c *Client) Write(req *ua.WriteRequest) (*ua.WriteResponse, error) {
return c.WriteWithContext(context.Background(), req)
}

// Note: This method will be replaced by the non "WithContext()" version
// of this method.
// Note: Starting with v0.5 this method is superseded by the non 'WithContext' method.
func (c *Client) WriteWithContext(ctx context.Context, req *ua.WriteRequest) (*ua.WriteResponse, error) {
stats.Client().Add("Write", 1)
stats.Client().Add("NodesToWrite", int64(len(req.NodesToWrite)))
Expand Down Expand Up @@ -1015,14 +1024,14 @@ func cloneBrowseRequest(req *ua.BrowseRequest) *ua.BrowseRequest {
}

// Browse executes a synchronous browse request.
// Deprecated: Starting with v0.5 this method will require a context
//
// Note: Starting with v0.5 this method will require a context
// and the corresponding XXXWithContext(ctx) method will be removed.
func (c *Client) Browse(req *ua.BrowseRequest) (*ua.BrowseResponse, error) {
return c.BrowseWithContext(context.Background(), req)
}

// Note: This method will be replaced by the non "WithContext()" version
// of this method.
// Note: Starting with v0.5 this method is superseded by the non 'WithContext' method.
func (c *Client) BrowseWithContext(ctx context.Context, req *ua.BrowseRequest) (*ua.BrowseResponse, error) {
stats.Client().Add("Browse", 1)
stats.Client().Add("NodesToBrowse", int64(len(req.NodesToBrowse)))
Expand All @@ -1039,14 +1048,14 @@ func (c *Client) BrowseWithContext(ctx context.Context, req *ua.BrowseRequest) (
}

// Call executes a synchronous call request for a single method.
// Deprecated: Starting with v0.5 this method will require a context
//
// Note: Starting with v0.5 this method will require a context
// and the corresponding XXXWithContext(ctx) method will be removed.
func (c *Client) Call(req *ua.CallMethodRequest) (*ua.CallMethodResult, error) {
return c.CallWithContext(context.Background(), req)
}

// Note: This method will be replaced by the non "WithContext()" version
// of this method.
// Note: Starting with v0.5 this method is superseded by the non 'WithContext' method.
func (c *Client) CallWithContext(ctx context.Context, req *ua.CallMethodRequest) (*ua.CallMethodResult, error) {
stats.Client().Add("Call", 1)

Expand All @@ -1067,14 +1076,14 @@ func (c *Client) CallWithContext(ctx context.Context, req *ua.CallMethodRequest)
}

// BrowseNext executes a synchronous browse request.
// Deprecated: Starting with v0.5 this method will require a context
//
// Note: Starting with v0.5 this method will require a context
// and the corresponding XXXWithContext(ctx) method will be removed.
func (c *Client) BrowseNext(req *ua.BrowseNextRequest) (*ua.BrowseNextResponse, error) {
return c.BrowseNextWithContext(context.Background(), req)
}

// Note: This method will be replaced by the non "WithContext()" version
// of this method.
// Note: Starting with v0.5 this method is superseded by the non 'WithContext' method.
func (c *Client) BrowseNextWithContext(ctx context.Context, req *ua.BrowseNextRequest) (*ua.BrowseNextResponse, error) {
stats.Client().Add("BrowseNext", 1)

Expand All @@ -1086,15 +1095,16 @@ func (c *Client) BrowseNextWithContext(ctx context.Context, req *ua.BrowseNextRe
}

// RegisterNodes registers node ids for more efficient reads.
//
// Part 4, Section 5.8.5
// Deprecated: Starting with v0.5 this method will require a context
//
// Note: Starting with v0.5 this method will require a context
// and the corresponding XXXWithContext(ctx) method will be removed.
func (c *Client) RegisterNodes(req *ua.RegisterNodesRequest) (*ua.RegisterNodesResponse, error) {
return c.RegisterNodesWithContext(context.Background(), req)
}

// Note: This method will be replaced by the non "WithContext()" version
// of this method.
// Note: Starting with v0.5 this method is superseded by the non 'WithContext' method.
func (c *Client) RegisterNodesWithContext(ctx context.Context, req *ua.RegisterNodesRequest) (*ua.RegisterNodesResponse, error) {
stats.Client().Add("RegisterNodes", 1)
stats.Client().Add("NodesToRegister", int64(len(req.NodesToRegister)))
Expand All @@ -1107,15 +1117,16 @@ func (c *Client) RegisterNodesWithContext(ctx context.Context, req *ua.RegisterN
}

// UnregisterNodes unregisters node ids previously registered with RegisterNodes.
//
// Part 4, Section 5.8.6
// Deprecated: Starting with v0.5 this method will require a context
//
// Note: Starting with v0.5 this method will require a context
// and the corresponding XXXWithContext(ctx) method will be removed.
func (c *Client) UnregisterNodes(req *ua.UnregisterNodesRequest) (*ua.UnregisterNodesResponse, error) {
return c.UnregisterNodesWithContext(context.Background(), req)
}

// Note: This method will be replaced by the non "WithContext()" version
// of this method.
// Note: Starting with v0.5 this method is superseded by the non 'WithContext' method.
func (c *Client) UnregisterNodesWithContext(ctx context.Context, req *ua.UnregisterNodesRequest) (*ua.UnregisterNodesResponse, error) {
stats.Client().Add("UnregisterNodes", 1)
stats.Client().Add("NodesToUnregister", int64(len(req.NodesToUnregister)))
Expand All @@ -1127,14 +1138,13 @@ func (c *Client) UnregisterNodesWithContext(ctx context.Context, req *ua.Unregis
return res, err
}

// Deprecated: Starting with v0.5 this method will require a context
// Note: Starting with v0.5 this method will require a context
// and the corresponding XXXWithContext(ctx) method will be removed.
func (c *Client) HistoryReadRawModified(nodes []*ua.HistoryReadValueID, details *ua.ReadRawModifiedDetails) (*ua.HistoryReadResponse, error) {
return c.HistoryReadRawModifiedWithContext(context.Background(), nodes, details)
}

// Note: This method will be replaced by the non "WithContext()" version
// of this method.
// Note: Starting with v0.5 this method is superseded by the non 'WithContext' method.
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 @@ -1159,10 +1169,18 @@ func (c *Client) HistoryReadRawModifiedWithContext(ctx context.Context, nodes []
}

// NamespaceArray returns the list of namespaces registered on the server.
//
// Note: Starting with v0.5 this method will require a context
// and the corresponding XXXWithContext(ctx) method will be removed.
func (c *Client) NamespaceArray() ([]string, error) {
return c.NamespaceArrayWithContext(context.Background())
}

// Note: Starting with v0.5 this method is superseded by the non 'WithContext' method.
func (c *Client) NamespaceArrayWithContext(ctx context.Context) ([]string, error) {
stats.Client().Add("NamespaceArray", 1)
node := c.Node(ua.NewNumericNodeID(0, id.Server_NamespaceArray))
v, err := node.Value()
v, err := node.ValueWithContext(ctx)
if err != nil {
return nil, err
}
Expand All @@ -1175,9 +1193,17 @@ func (c *Client) NamespaceArray() ([]string, error) {
}

// FindNamespace returns the id of the namespace with the given name.
//
// Note: Starting with v0.5 this method will require a context
// and the corresponding XXXWithContext(ctx) method will be removed.
func (c *Client) FindNamespace(name string) (uint16, error) {
return c.FindNamespaceWithContext(context.Background(), name)
}

// Note: Starting with v0.5 this method is superseded by the non 'WithContext' method.
func (c *Client) FindNamespaceWithContext(ctx context.Context, name string) (uint16, error) {
stats.Client().Add("FindNamespace", 1)
nsa, err := c.NamespaceArray()
nsa, err := c.NamespaceArrayWithContext(ctx)
if err != nil {
return 0, err
}
Expand All @@ -1190,9 +1216,17 @@ func (c *Client) FindNamespace(name string) (uint16, error) {
}

// UpdateNamespaces updates the list of cached namespaces from the server.
//
// Note: Starting with v0.5 this method will require a context
// and the corresponding XXXWithContext(ctx) method will be removed.
func (c *Client) UpdateNamespaces() error {
return c.UpdateNamespacesWithContext(context.Background())
}

// Note: Starting with v0.5 this method is superseded by the non 'WithContext' method.
func (c *Client) UpdateNamespacesWithContext(ctx context.Context) error {
stats.Client().Add("UpdateNamespaces", 1)
ns, err := c.NamespaceArray()
ns, err := c.NamespaceArrayWithContext(ctx)
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit d545cfe

Please sign in to comment.