OpcUaCanOpen
CANopen OPC-UA server
OpcServer.cxx
Go to the documentation of this file.
1 /******************************************************************************
2 ** opcserver.cpp
3 **
4 ** Copyright (C) 2008-2009 Unified Automation GmbH. All Rights Reserved.
5 ** Web: http://www.unified-automation.com
6 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
7 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
8 **
9 ** Project: C++ OPC Server SDK sample code
10 **
11 ** Description: Main OPC Server object class.
12 **
13 ******************************************************************************/
14 #include "OpcServer.h"
15 #include "uamutex.h"
16 #include "srvtrace.h"
17 #include "UaCanTrace.h"
18 #include "serverconfigxml.h"
19 
20 ServerConfigXml* OpcServer::m_pServerConfig = 0;
21 
22 
24 class ServerConfigBasicXml: public ServerConfigXml
25 {
26 public:
27  ServerConfigBasicXml(const UaString& sXmlFileName, const UaString& sApplicationPath, OpcServerCallback* pOpcServerCallback);
29  UaStatus afterLoadConfiguration(){return OpcUa_Good;}
30  UaStatus startUp(ServerManager*){return OpcUa_Good;}
31  UaStatus shutDown(){return OpcUa_Good;}
32  Session* createSession(OpcUa_Int32 sessionID, const UaNodeId &authenticationToken);
33  UaStatus logonSessionUser(Session* pSession, UaUserIdentityToken* pUserIdentityToken);
34 private:
35  OpcServerCallback* m_pOpcServerCallback;
36 };
37 
40 {
41 
42  if ( m_isStarted != OpcUa_False )
43  {
44  UaLocalizedText reason("en","Application shut down");
45  stop(0, reason);
46  }
47 
48  if ( m_pUaModule )
49  {
50  delete m_pUaModule;
51  m_pUaModule = NULL;
52  }
53 
54  if ( m_pCoreModule )
55  {
56  delete m_pCoreModule;
57  m_pCoreModule = NULL;
58  }
59 
60  // Delete all node managers
61  list<NodeManager*>::iterator it;
62  for ( it = m_listNodeManagers.begin(); it != m_listNodeManagers.end(); it++ )
63  {
64  delete (*it);
65  (*it) = NULL;
66  }
67  m_listNodeManagers.clear();
68 
69  if ( m_pServerConfig )
70  {
71  delete m_pServerConfig;
72  m_pServerConfig = NULL;
73  }
74 }
75 
87 int OpcServer::setServerConfig(const UaString& configurationFile, const UaString& applicationPath)
88 {
89  UaMutexLocker lock(&m_mutex);
90  if ( m_isStarted != OpcUa_False )
91  {
92  // Error, the method is called after the server was started
93  return -1;
94  }
95 
96  m_configurationFile = configurationFile;
97  m_applicationPath = applicationPath;
98 // m_CanTraceFile = canTraceFile;
99 
100  if ( m_pServerConfig == NULL )
101  {
102  UaUniString sConfigFile(m_configurationFile.toUtf8());
103  sConfigFile = sConfigFile.toLower();
104  if ( sConfigFile.lastIndexOf(".xml") > (sConfigFile.length() - 5) )
105  {
106  m_pServerConfig = new ServerConfigBasicXml(m_configurationFile, m_applicationPath/*, m_CanTraceFile*/, m_pOpcServerCallback);
107  }
108  }
109  // Check trace settings
110  if ( m_pServerConfig->loadConfiguration().isGood() )
111  {
112  // Check trace settings
113  if (m_pServerConfig)
114  {
115  OpcUa_Boolean bTraceEnabled = OpcUa_False;
116  OpcUa_UInt32 uTraceLevel = 0;
117  OpcUa_Boolean bSdkTraceEnabled = OpcUa_False;
118  OpcUa_UInt32 uSdkTraceLevel = 0;
119  OpcUa_UInt32 uMaxTraceEntries = 0;
120  OpcUa_UInt32 uMaxBackupFiles = 0;
121  UaString sTraceFile;
122  OpcUa_Boolean bDisableFlush;
123 
124  m_pServerConfig->getStackTraceSettings(bTraceEnabled, uTraceLevel);
125 
126  m_pServerConfig->getServerTraceSettings(
127  bSdkTraceEnabled,
128  uSdkTraceLevel,
129  uMaxTraceEntries,
130  uMaxBackupFiles,
131  sTraceFile, bDisableFlush);
132 
133  if (bSdkTraceEnabled != OpcUa_False)
134  {
135  SrvT::initTrace((UaTrace::TraceLevel)uSdkTraceLevel, uMaxTraceEntries, uMaxBackupFiles, sTraceFile, "OpcUaCanOpenServer");
136  SrvT::setLocalTimeOutput(true);
137  SrvT::setTraceActive(true);
138  if (bTraceEnabled != OpcUa_False)
139  {
140  SrvT::setStackTraceActive(OpcUa_True, uTraceLevel);
141  }
142  }
143  }
144 
146 
147  LOG(Log::INF, CanOpen::UaCanTrace::NoTrace) << "Load Configuration";
148  cout << "Load Configuration" << endl;
149 
150  return OpcUa_Good;
151  }
152 
153  return 0;
154 }
155 
163 int OpcServer::setServerConfig(ServerConfig* pServerConfig)
164 {
165  UaMutexLocker lock(&m_mutex);
166  if ( m_isStarted != OpcUa_False )
167  {
168  // Error, the method is called after the server was started
169  return -1;
170  }
171 
172  m_pServerConfig = (ServerConfigXml*)pServerConfig;
173  return 0;
174 }
175 
183 int OpcServer::addNodeManager(NodeManager* pNodeManager)
184 {
185  UaMutexLocker lock(&m_mutex);
186 
187  m_listNodeManagers.push_back(pNodeManager);
188 
189  if ( m_isStarted != OpcUa_False )
190  {
191  // Start up node manager if server is already started
192  UaStatus ret = pNodeManager->startUp(m_pServerManager);
193  if ( ret.isNotGood() )
194  {
195  TRACE1_ERROR(SERVER_UI, "Error: OpcServer::addNodeManager - can not start up node manager [ret=0x%lx]", ret.statusCode());
196  return -1;
197  }
198  }
199 
200  return 0;
201 }
210 
229 
355 
422 
439 
450 
468