client.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright (C) 2014-2014 by Sintef Raufoss Manufacturing *
3  * olivier.roulet@gmail.com *
4  * *
5  * This library is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU Lesser General Public License as *
7  * published by the Free Software Foundation; version 3 of the License. *
8  * *
9  * This library is distributed in the hope that it will be useful, *
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12  * GNU Lesser General Public License for more details. *
13  * *
14  * You should have received a copy of the GNU Lesser General Public License *
15  * along with this library; if not, write to the *
16  * Free Software Foundation, Inc., *
17  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
18  ******************************************************************************/
19 
20 
21 #pragma once
22 
23 #include <opc/ua/node.h>
25 #include <opc/ua/subscription.h>
28 #include <opc/common/logger.h>
29 
30 #include <thread>
31 #include <condition_variable>
32 #include <chrono>
33 #include <atomic>
34 
35 
36 namespace OpcUa
37 {
38 
40 {
41 public:
43  // Send keepalive request to server so it does not disconnect us
44  KeepAliveThread(const Common::Logger::SharedPtr & logger = nullptr) : StopRequest(false), Running(false), Logger(logger) {}
45  void Start(Services::SharedPtr server, Node node, Duration period);
46  void Stop();
47 
48  void SetLogger(const Common::Logger::SharedPtr & logger) { Logger = logger; }
49 
50 private:
51  void Run();
52  mutable std::thread Thread;
54  Services::SharedPtr Server;
55  Duration Period = 1200000;
56  std::atomic<bool> StopRequest;
57  std::atomic<bool> Running;
58  std::condition_variable Condition;
59  std::mutex Mutex;
60  Common::Logger::SharedPtr Logger;
61 };
62 
63 
64 class UaClient
65 {
66 public:
76  UaClient(bool debug = false);
77  UaClient(std::shared_ptr<spdlog::logger> logger) : KeepAlive(logger), Logger(logger) {}
78  virtual ~UaClient();
79 
80  UaClient(const UaClient &&) = delete;
81  UaClient(const UaClient &) = delete;
82  UaClient & operator=(const UaClient &) = delete;
83 
85  void SetSessionName(const std::string & str) { SessionName = str; }
86  std::string GetSessionName() const { return SessionName; }
87 
89  // a connection will be made to server to get endpoint description
90  // an endpoint description will be selected and then a connection will attempted
91  void Connect(const std::string & endpoint);
92 
94  // EndpointDescription can be defined by hand or gotten through
95  // a call to GetServerEndpoints()
96  void Connect(const EndpointDescription &);
97 
99  // close communication with OPC-UA server, close all threads and subscriptions
100  void Disconnect();
101 
103  // abort communication with OPC-UA server, close all threads and subcsriptions
104  // Like Disconnect() but without CloseSession() call, which is not possible on faulty connection anyway
105  void Abort();
106 
108  std::vector<EndpointDescription> GetServerEndpoints(const std::string & endpoint);
109 
111  EndpointDescription SelectEndpoint(const std::string &);
112 
114  std::vector<EndpointDescription> GetServerEndpoints();
115  std::string GetEndpoint() const { return Endpoint.EndpointUrl; }
116 
118  std::string GetApplicationURI() const { return ApplicationUri; }
119  void SetApplicationURI(std::string uri) { ApplicationUri = uri; }
120  std::string GetProductURI() const { return ProductUri; }
121  void SetProductURI(std::string uri) { ProductUri = uri; }
122 
124  // anyway freeopcua currently only support MessageSecurityMode::None
125  void SetSecurityPolicy(std::string sec) {SecurityPolicy = sec;}
126  std::string GetSecurityPolicy() const { return SecurityPolicy; }
127 
129  // Deduce index from order or call GetNamespaceIndex(uri)
130  std::vector<std::string> GetServerNamespaces();
131  uint32_t GetNamespaceIndex(std::string uri);
132 
134  // you can also access a standard node from addressspace using
135  // ObjectId, for example:
136  // Node mynode = GetNode(ObjectId::Server);
137  // using a string is also possible:
138  // Node mynode = GetNode("ns=3;i=55");
139  Node GetNode(const NodeId & nodeid) const;
140  Node GetNode(const std::string & nodeid) const;
141 
143  Node GetRootNode() const;
144  Node GetObjectsNode() const;
145  Node GetServerNode() const;
146 
147  void DeleteNodes(std::vector<OpcUa::Node> & nodes, bool recursive = false);
148 
150  // returned object can then be used to subscribe
151  // to datachange or custom events from server
152  Subscription::SharedPtr CreateSubscription(unsigned int period, SubscriptionHandler & client);
153 
155  ServerOperations CreateServerOperations();
156 
157 private:
158  void OpenSecureChannel();
159  void CloseSecureChannel();
160 
161  std::vector<OpcUa::Node> AddChilds(std::vector<OpcUa::Node> nodes);
162 
163 protected:
164  virtual void EncryptPassword(OpcUa::UserIdentifyToken &identity, const CreateSessionResponse &response);
165 protected:
167  // defined some sensible defaults that should let us connect to most servers
168  std::string SessionName = "Open source OPC-UA Client Session";
169  std::string ApplicationUri = "urn:freeopcua:client";
170  std::string ProductUri = "urn:freeopcua.github.no:client";
171  std::string SecurityPolicy = "none";
173  uint32_t SecureChannelId;
174  Common::Logger::SharedPtr Logger;
175  uint32_t DefaultTimeout = 3600000;
176  Services::SharedPtr Server;
177 };
178 
179 } // namespace OpcUa
std::condition_variable Condition
Definition: client.h:58
void SetApplicationURI(std::string uri)
Definition: client.h:119
double Duration
Definition: datetime.h:53
Services::SharedPtr Server
Definition: client.h:54
uint32_t SecureChannelId
Definition: client.h:173
KeepAliveThread KeepAlive
Definition: client.h:172
std::atomic< bool > Running
Definition: client.h:57
void SetProductURI(std::string uri)
Definition: client.h:121
std::string GetSecurityPolicy() const
Definition: client.h:126
std::string GetProductURI() const
Definition: client.h:120
KeepAliveThread(const Common::Logger::SharedPtr &logger=nullptr)
Internal.
Definition: client.h:44
Definition: client.py:1
std::string GetApplicationURI() const
set application description
Definition: client.h:118
void SetLogger(const Common::Logger::SharedPtr &logger)
Definition: client.h:48
std::mutex Mutex
Definition: client.h:59
void SetSessionName(const std::string &str)
set session name
Definition: client.h:85
UaClient(std::shared_ptr< spdlog::logger > logger)
Definition: client.h:77
void SetSecurityPolicy(std::string sec)
set security policy
Definition: client.h:125
Moved from session.h.
Definition: types_manual.h:19
Common::Logger::SharedPtr Logger
Definition: client.h:174
OPC UA Address space part. GNU LGPL.
string uri
Definition: client.py:31
std::string GetSessionName() const
Definition: client.h:86
std::thread Thread
Definition: client.h:52
EndpointDescription Endpoint
Definition: client.h:166
A Node object represent an OPC-UA node. It is high level object intended for developper who want to e...
Definition: node.h:42
Services::SharedPtr Server
Definition: client.h:176
void Start(Services::SharedPtr server, Node node, Duration period)
Definition: client.cpp:39
std::atomic< bool > StopRequest
Definition: client.h:56
Common::Logger::SharedPtr Logger
Definition: client.h:60
std::unique_ptr< RemoteConnection > Connect(const std::string &host, unsigned port, const Common::Logger::SharedPtr &logger)
Definition: server.py:1
std::string GetEndpoint() const
Definition: client.h:115


ros_opcua_impl_freeopcua
Author(s): Denis Štogl
autogenerated on Tue Jan 19 2021 03:06:04