server.cpp
Go to the documentation of this file.
00001 /******************************************************************************
00002  *   Copyright (C) 2013-2014 by Olivier Roulet-Dubonnet                       *
00003  *                                                                            *
00004  *   This library is free software; you can redistribute it and/or modify     *
00005  *   it under the terms of the GNU Lesser General Public License as           *
00006  *   published by the Free Software Foundation; version 3 of the License.     *
00007  *                                                                            *
00008  *   This library is distributed in the hope that it will be useful,          *
00009  *   but WITHOUT ANY WARRANTY; without even the implied warranty of           *
00010  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
00011  *   GNU Lesser General Public License for more details.                      *
00012  *                                                                            *
00013  *   You should have received a copy of the GNU Lesser General Public License *
00014  *   along with this library; if not, write to the                            *
00015  *   Free Software Foundation, Inc.,                                          *
00016  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.                *
00017  ******************************************************************************/
00018 
00019 #include <opc/ua/server/server.h>
00020 
00021 #include <opc/ua/server/addons/common_addons.h>
00022 #include <opc/ua/protocol/string_utils.h>
00023 
00024 #include <opc/ua/server/addons/services_registry.h>
00025 #include <opc/ua/server/addons/subscription_service.h>
00026 #include <iostream>
00027 
00028 namespace OpcUa
00029 {
00030   UaServer::UaServer()
00031   {
00032   }
00033 
00034   UaServer::UaServer(bool debug)
00035     : Debug(debug)
00036   {
00037   }
00038 
00039   void UaServer::SetEndpoint(const std::string& endpoint)
00040   {
00041           Endpoint = endpoint;
00042   }
00043 
00044   void UaServer::SetProductURI(const std::string& uri)
00045   {
00046           ProductUri = uri;
00047   }
00048 
00049   void UaServer::SetServerURI(const std::string& uri)
00050   {
00051           ServerUri = uri;
00052   }
00053 
00054   void UaServer::SetServerName(const std::string& name)
00055   {
00056           Name = name;
00057   }
00058 
00059   void UaServer::AddAddressSpace(const std::string& path)
00060   {
00061           XmlAddressSpaces.push_back(path);
00062   }
00063 
00064   void UaServer::CheckStarted() const
00065   {
00066     if ( ! Registry )
00067     {
00068       throw(std::runtime_error("Server is not started"));
00069     }
00070   }
00071 
00072   uint32_t UaServer::RegisterNamespace(std::string uri)
00073   {
00074     CheckStarted();
00075     Node namespacearray(Registry->GetServer(), ObjectId::Server_NamespaceArray);
00076     std::vector<std::string> uris = namespacearray.GetValue().As<std::vector<std::string>>();
00077     uint32_t index = uris.size();
00078     uris.push_back(uri);
00079     namespacearray.SetValue(uris);
00080     return index;
00081   }
00082 
00083   uint32_t UaServer::GetNamespaceIndex(std::string uri)
00084   {
00085     CheckStarted();
00086     Node namespacearray(Registry->GetServer(), ObjectId::Server_NamespaceArray);
00087     std::vector<std::string> uris = namespacearray.GetValue().As<std::vector<std::string>>();;
00088     for ( uint32_t i=0; i<uris.size(); ++i)
00089     {
00090       if (uris[i] == uri )
00091       {
00092         return i;
00093       }
00094     }
00095     throw(std::runtime_error("Error namespace uri does not exists in server")); 
00096     //return -1;
00097   }
00098 
00099   void UaServer::Start()
00100   {
00101     ApplicationDescription appDesc;
00102     appDesc.ApplicationName = LocalizedText(Name);
00103     appDesc.ApplicationUri = ServerUri;
00104     appDesc.ApplicationType = ApplicationType::Server;
00105     appDesc.ProductUri = ProductUri;
00106 
00107     OpcUa::Server::Parameters params;
00108     params.Debug = Debug;
00109     params.Endpoint.Server = appDesc;
00110     params.Endpoint.EndpointUrl = Endpoint;
00111     params.Endpoint.SecurityMode = SecurityMode;
00112     params.Endpoint.SecurityPolicyUri = "http://opcfoundation.org/UA/SecurityPolicy#None";
00113     params.Endpoint.TransportProfileUri = "http://opcfoundation.org/UA-Profile/Transport/uatcp-uasc-uabinary";
00114     //setting up policy is required for some client, this should be in a constructor
00115     UserTokenPolicy policy;
00116     policy.TokenType = UserTokenType::Anonymous;
00117     params.Endpoint.UserIdentityTokens.push_back(policy);
00118 
00119     Addons = Common::CreateAddonsManager();
00120     Server::RegisterCommonAddons(params, *Addons);
00121     Addons->Start();
00122 
00123     Registry = Addons->GetAddon<Server::ServicesRegistry>(Server::ServicesRegistryAddonId);
00124     SubscriptionService = Addons->GetAddon<Server::SubscriptionService>(Server::SubscriptionServiceAddonId);
00125 
00126     Node ServerArray = GetNode(OpcUa::ObjectId::Server_ServerArray);
00127     ServerArray.SetValue(std::vector<std::string>({Endpoint}));
00128 
00129     EnableEventNotification(); //Enabling event notification, it probably does hurt anyway and users will forgot to set it up
00130   }
00131 
00132   Node UaServer::GetNode(const std::string& nodeid) const
00133   {
00134     return GetNode(ToNodeId(nodeid));
00135   }
00136 
00137   Node UaServer::GetNode(const NodeId& nodeid) const
00138   {
00139     CheckStarted();
00140     return Node(Registry->GetServer(), nodeid);
00141   }
00142 
00143   Node UaServer::GetNodeFromPath(const std::vector<QualifiedName>& path) const
00144   {
00145     return GetRootNode().GetChild(path);
00146   }
00147 
00148   Node UaServer::GetNodeFromPath(const std::vector<std::string>& path) const
00149   {
00150     return GetRootNode().GetChild(path);
00151   }
00152 
00153   void UaServer::Stop()
00154   {
00155     std::cout << "Stopping opcua server application" << std::endl;
00156     CheckStarted();
00157     Addons->Stop();
00158   }
00159 
00160   Node UaServer::GetRootNode() const
00161   {
00162     return GetNode(OpcUa::ObjectId::RootFolder);
00163   }
00164 
00165   Node UaServer::GetObjectsNode() const
00166   {
00167     return GetNode(ObjectId::ObjectsFolder);
00168   }
00169 
00170   Node UaServer::GetServerNode() const
00171   {
00172     return GetNode(ObjectId::Server);
00173   }
00174 
00175   void UaServer::EnableEventNotification()
00176   {
00177     Node server = GetServerNode();
00178     
00179     uint8_t notifierval = 0;
00180     notifierval |= EventNotifier::SubscribeToEvents;
00181 
00182     DataValue dval(notifierval);
00183     dval.SetSourceTimestamp(DateTime::Current());
00184 
00185     server.SetAttribute(AttributeId::EventNotifier, dval);
00186   }
00187 
00188   std::unique_ptr<Subscription> UaServer::CreateSubscription(unsigned int period, SubscriptionHandler& callback)
00189   {
00190     CheckStarted();
00191     CreateSubscriptionParameters params;
00192     params.RequestedPublishingInterval = period;
00193     return std::unique_ptr<Subscription>(new Subscription (Registry->GetServer(), params, callback, Debug));
00194   }
00195 
00196   ServerOperations UaServer::CreateServerOperations()
00197   {
00198           return std::move(ServerOperations(Registry->GetServer()));
00199   }
00200 
00201   void UaServer::TriggerEvent(Event event)
00202   {
00203     SubscriptionService->TriggerEvent(ObjectId::Server, event);
00204   }
00205 
00206 }


ros_opcua_impl_freeopcua
Author(s): Denis Štogl
autogenerated on Sat Jun 8 2019 18:24:56