Julius Pfrommer 8 years ago
parent
commit
49946cd989
5 changed files with 45 additions and 18 deletions
  1. 24 10
      examples/server.cpp
  2. 7 2
      include/ua_server.h
  3. 6 6
      include/ua_types.h
  4. 3 0
      src/server/ua_server.c
  5. 5 0
      src/server/ua_server_internal.h

+ 24 - 10
examples/server.cpp

@@ -2,6 +2,8 @@
  * This work is licensed under a Creative Commons CCZero 1.0 Universal License.
  * See http://creativecommons.org/publicdomain/zero/1.0/ for more information.
  */
+
+#include <signal.h>
 #include <iostream>
 #include <cstring>
 
@@ -21,30 +23,42 @@
 
 using namespace std;
 
-int main()
-{
+UA_Logger logger;
+UA_Boolean running = 1;
+
+static void stopHandler(int sign) {
+    UA_LOG_INFO(logger, UA_LOGCATEGORY_SERVER, "received ctrl-c");
+    running = 0;
+}
+
+int main() {
+    signal(SIGINT, stopHandler); /* catches ctrl-c */
+
     UA_Server *server = UA_Server_new(UA_ServerConfig_standard);
     UA_Server_addNetworkLayer(server, ServerNetworkLayerTCP_new(UA_ConnectionConfig_standard, 16664));
-
-    UA_Logger logger = Logger_Stdout_new();
+    logger = Logger_Stdout_new();
     UA_Server_setLogger(server, logger);
 
     // add a variable node to the adresspace
     UA_VariableAttributes attr;
     UA_VariableAttributes_init(&attr);
     UA_Int32 myInteger = 42;
-    UA_Variant_setScalar(&attr.value, &myInteger, &UA_TYPES[UA_TYPES_INT32]);
-    attr.description = UA_LOCALIZEDTEXT("en_US","the answer");
-    attr.displayName = UA_LOCALIZEDTEXT("en_US","the answer");
-    UA_NodeId myIntegerNodeId = UA_NODEID_STRING(1, "the.answer");
-    UA_QualifiedName myIntegerName = UA_QUALIFIEDNAME(1, "the answer");
+    UA_Variant_setScalarCopy(&attr.value, &myInteger, &UA_TYPES[UA_TYPES_INT32]);
+    attr.description = UA_LOCALIZEDTEXT_ALLOC("en_US","the answer");
+    attr.displayName = UA_LOCALIZEDTEXT_ALLOC("en_US","the answer");
+    UA_NodeId myIntegerNodeId = UA_NODEID_STRING_ALLOC(1, "the.answer");
+    UA_QualifiedName myIntegerName = UA_QUALIFIEDNAME_ALLOC(1, "the answer");
     UA_NodeId parentNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
     UA_NodeId parentReferenceNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES);
     UA_Server_addVariableNode(server, myIntegerNodeId, parentNodeId,
                               parentReferenceNodeId, myIntegerName,
                               UA_NODEID_NULL, attr, NULL);
 
-    UA_Boolean running = UA_TRUE;
+    /* allocations on the heap need to be freed */
+    UA_VariableAttributes_deleteMembers(&attr);
+    UA_NodeId_deleteMembers(&myIntegerNodeId);
+    UA_QualifiedName_deleteMembers(&myIntegerName);
+
     UA_StatusCode retval = UA_Server_run(server, 1, &running);
 	UA_Server_delete(server);
 

+ 7 - 2
include/ua_server.h

@@ -541,6 +541,7 @@ UA_StatusCode UA_EXPORT
 UA_Server_getNodeAttribute_method(UA_Server *server, UA_NodeId methodNodeId, UA_MethodCallback *method);
 #endif
 
+#ifdef UA_EXTERNAL_NAMESPACES
 /**
  * An external application that manages its own data and data model. To plug in
  * outside data sources, one can use
@@ -606,9 +607,13 @@ typedef struct UA_ExternalNodeStore {
 	UA_ExternalNodeStore_delete destroy;
 } UA_ExternalNodeStore;
 
-#ifdef UA_EXTERNAL_NAMESPACES
 UA_StatusCode UA_EXPORT
 UA_Server_addExternalNamespace(UA_Server *server, UA_UInt16 namespaceIndex, const UA_String *url,
                                UA_ExternalNodeStore *nodeStore);
-#endif /* __cplusplus */
+#endif
+
+#ifdef __cplusplus
+}
+#endif
+
 #endif /* UA_SERVER_H_ */

+ 6 - 6
include/ua_types.h

@@ -424,7 +424,7 @@ static UA_INLINE UA_NodeId UA_NODEID_STRING(UA_UInt16 nsIndex, char *chars) {
     id.identifierType = UA_NODEIDTYPE_STRING;
     id.identifier.string = UA_STRING(chars);
     return id; }
-static UA_INLINE UA_NodeId UA_NODEID_STRING_ALLOC(UA_UInt16 nsIndex, char *chars) {
+static UA_INLINE UA_NodeId UA_NODEID_STRING_ALLOC(UA_UInt16 nsIndex, const char *chars) {
     UA_NodeId id;
     id.namespaceIndex = nsIndex;
     id.identifierType = UA_NODEIDTYPE_STRING;
@@ -442,7 +442,7 @@ static UA_INLINE UA_NodeId UA_NODEID_BYTESTRING(UA_UInt16 nsIndex, char *chars)
     id.identifierType = UA_NODEIDTYPE_BYTESTRING;
     id.identifier.byteString = UA_BYTESTRING(chars);
     return id; }
-static UA_INLINE UA_NodeId UA_NODEID_BYTESTRING_ALLOC(UA_UInt16 nsIndex, char *chars) {
+static UA_INLINE UA_NodeId UA_NODEID_BYTESTRING_ALLOC(UA_UInt16 nsIndex, const char *chars) {
     UA_NodeId id;
     id.namespaceIndex = nsIndex;
     id.identifierType = UA_NODEIDTYPE_BYTESTRING;
@@ -469,7 +469,7 @@ static UA_INLINE UA_ExpandedNodeId UA_EXPANDEDNODEID_STRING(UA_UInt16 nsIndex, c
     id.serverIndex = 0;
     id.namespaceUri = UA_STRING_NULL;
     return id; }
-static UA_INLINE UA_ExpandedNodeId UA_EXPANDEDNODEID_STRING_ALLOC(UA_UInt16 nsIndex, char *chars) {
+static UA_INLINE UA_ExpandedNodeId UA_EXPANDEDNODEID_STRING_ALLOC(UA_UInt16 nsIndex, const char *chars) {
     UA_ExpandedNodeId id;
     id.nodeId = UA_NODEID_STRING_ALLOC(nsIndex, chars);
     id.serverIndex = 0;
@@ -487,7 +487,7 @@ static UA_INLINE UA_ExpandedNodeId UA_EXPANDEDNODEID_BYTESTRING(UA_UInt16 nsInde
     id.serverIndex = 0;
     id.namespaceUri = UA_STRING_NULL;
     return id; }
-static UA_INLINE UA_ExpandedNodeId UA_EXPANDEDNODEID_BYTESTRING_ALLOC(UA_UInt16 nsIndex, char *chars) {
+static UA_INLINE UA_ExpandedNodeId UA_EXPANDEDNODEID_BYTESTRING_ALLOC(UA_UInt16 nsIndex, const char *chars) {
     UA_ExpandedNodeId id;
     id.nodeId = UA_NODEID_BYTESTRING_ALLOC(nsIndex, chars);
     id.serverIndex = 0;
@@ -513,7 +513,7 @@ static UA_INLINE UA_QualifiedName UA_QUALIFIEDNAME(UA_UInt16 nsIndex, char *char
     qn.namespaceIndex = nsIndex;
     qn.name = UA_STRING(chars);
     return qn; }
-static UA_INLINE UA_QualifiedName UA_QUALIFIEDNAME_ALLOC(UA_UInt16 nsIndex, char *chars) {
+static UA_INLINE UA_QualifiedName UA_QUALIFIEDNAME_ALLOC(UA_UInt16 nsIndex, const char *chars) {
     UA_QualifiedName qn;
     qn.namespaceIndex = nsIndex;
     qn.name = UA_STRING_ALLOC(chars);
@@ -530,7 +530,7 @@ static UA_INLINE UA_LocalizedText UA_LOCALIZEDTEXT(char *locale, char *text) {
     lt.locale = UA_STRING(locale);
     lt.text = UA_STRING(text);
     return lt; }
-static UA_INLINE UA_LocalizedText UA_LOCALIZEDTEXT_ALLOC(char *locale, char *text) {
+static UA_INLINE UA_LocalizedText UA_LOCALIZEDTEXT_ALLOC(const char *locale, const char *text) {
     UA_LocalizedText lt;
     lt.locale = UA_STRING_ALLOC(locale);
     lt.text = UA_STRING_ALLOC(text);

+ 3 - 0
src/server/ua_server.c

@@ -451,8 +451,11 @@ UA_Server * UA_Server_new(UA_ServerConfig config) {
     server->description.applicationName =
         UA_LOCALIZEDTEXT_ALLOC("en_US", server->config.Application_applicationName);
     server->description.applicationType = UA_APPLICATIONTYPE_SERVER;
+
+#ifdef UA_EXTERNAL_NAMESPACES
     server->externalNamespacesSize = 0;
     server->externalNamespaces = UA_NULL;
+#endif
 
     /* ns0 and ns1 */
     server->namespaces = UA_Array_new(&UA_TYPES[UA_TYPES_STRING], 2);

+ 5 - 0
src/server/ua_server_internal.h

@@ -15,6 +15,7 @@
 #define ANONYMOUS_POLICY "open62541-anonymous-policy"
 #define USERNAME_POLICY "open62541-username-policy"
 
+#ifdef UA_EXTERNAL_NAMESPACES
 /** Mapping of namespace-id and url to an external nodestore. For namespaces
     that have no mapping defined, the internal nodestore is used by default. */
 typedef struct UA_ExternalNamespace {
@@ -22,6 +23,7 @@ typedef struct UA_ExternalNamespace {
 	UA_String url;
 	UA_ExternalNodeStore externalNodeStore;
 } UA_ExternalNamespace;
+#endif
 
 struct UA_Server {
     /* Config */
@@ -49,8 +51,11 @@ struct UA_Server {
     UA_NodeStore *nodestore;
     size_t namespacesSize;
     UA_String *namespaces;
+
+#ifdef UA_EXTERNAL_NAMESPACES
     size_t externalNamespacesSize;
     UA_ExternalNamespace *externalNamespaces;
+#endif
      
     /* Jobs with a repetition interval */
     LIST_HEAD(RepeatedJobsList, RepeatedJobs) repeatedJobs;