Browse Source

reduced duplicated code within the service routines

FlorianPalm 10 years ago
parent
commit
224af20d95

+ 9 - 6
Makefile

@@ -38,19 +38,22 @@ RM = /usr/bin/cmake -E remove -f
 # Escaping for special characters.
 EQUALS = =
 
+# The program to use to edit the cache.
+CMAKE_EDIT_COMMAND = /usr/bin/cmake-gui
+
 # The top-level source directory on which CMake was run.
-CMAKE_SOURCE_DIR = /home/fp/Downloads/open62541_ext
+CMAKE_SOURCE_DIR = /home/opcua/Downloads/open62541_external_datasource
 
 # The top-level build directory on which CMake was run.
-CMAKE_BINARY_DIR = /home/fp/Downloads/open62541_ext
+CMAKE_BINARY_DIR = /home/opcua/Downloads/open62541_external_datasource
 
 #=============================================================================
 # Targets provided globally by CMake.
 
 # Special rule for the target edit_cache
 edit_cache:
-	@$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "Running interactive CMake command-line interface..."
-	/usr/bin/cmake -i .
+	@$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "Running CMake cache editor..."
+	/usr/bin/cmake-gui -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)
 .PHONY : edit_cache
 
 # Special rule for the target edit_cache
@@ -69,9 +72,9 @@ rebuild_cache/fast: rebuild_cache
 
 # The main all target
 all: cmake_check_build_system
-	$(CMAKE_COMMAND) -E cmake_progress_start /home/fp/Downloads/open62541_ext/CMakeFiles /home/fp/Downloads/open62541_ext/CMakeFiles/progress.marks
+	$(CMAKE_COMMAND) -E cmake_progress_start /home/opcua/Downloads/open62541_external_datasource/CMakeFiles /home/opcua/Downloads/open62541_external_datasource/CMakeFiles/progress.marks
 	$(MAKE) -f CMakeFiles/Makefile2 all
-	$(CMAKE_COMMAND) -E cmake_progress_start /home/fp/Downloads/open62541_ext/CMakeFiles 0
+	$(CMAKE_COMMAND) -E cmake_progress_start /home/opcua/Downloads/open62541_external_datasource/CMakeFiles 0
 .PHONY : all
 
 # The main clean target

+ 52 - 0
src/server/ua_services.h

@@ -312,4 +312,56 @@ void Service_Write(UA_Server *server, UA_Session *session,
 
 /** @} */ // end of group
 
+/**
+ * @brief this macro browses through a request, looking for nodeIds belonging to the same namespace (same namespaceIndex). For each found different namespaceIndex an entry in
+ * ASSOCIATED_INDEX_ARRAY is saved. Furthermore for each occurence of the same namespace the corresponding entry of NUMBER_OF_FOUND_INDICES_ARRAY is incremented
+ *
+ *
+ * a request with 10 nodeIds, with the following namespaceIndices is received
+ * 1
+ * 1
+ * 1
+ * 3
+ * 4
+ * 5
+ * 5
+ * 5
+ * 1
+ * 1
+ * After a call of the macro the outputs would look like that:
+ * ASSOCIATED_INDEX_ARRAY[0] = 1
+ * ASSOCIATED_INDEX_ARRAY[1] = 3
+ * ASSOCIATED_INDEX_ARRAY[2] = 4
+ * ASSOCIATED_INDEX_ARRAY[3] = 5
+ *
+ * NUMBER_OF_FOUND_INDICES_ARRAY[0] = 5
+ * NUMBER_OF_FOUND_INDICES_ARRAY[1] = 1
+ * NUMBER_OF_FOUND_INDICES_ARRAY[2] = 1
+ * NUMBER_OF_FOUND_INDICES_ARRAY[3] = 3
+ *
+ *
+ */
+#define BUILD_INDEX_ARRAYS(SIZE,REQUEST_ARRAY,NODEID_PROPERTY,DIFFERENT_INDEX_COUNT,ASSOCIATED_INDEX_ARRAY,NUMBER_OF_FOUND_INDICES_ARRAY) do{ \
+		DIFFERENT_INDEX_COUNT = 0;\
+		for (UA_Int32 i = 0; i < SIZE; i++) { \
+			UA_UInt32 j = 0; \
+			do { \
+				if (ASSOCIATED_INDEX_ARRAY[j] \
+						== REQUEST_ARRAY[i].NODEID_PROPERTY.namespaceIndex) { \
+					if (DIFFERENT_INDEX_COUNT == 0) { \
+						DIFFERENT_INDEX_COUNT++; \
+					} \
+					NUMBER_OF_FOUND_INDICES_ARRAY[j]++; \
+					break; \
+				} else if (j == (DIFFERENT_INDEX_COUNT - 1)) { \
+					ASSOCIATED_INDEX_ARRAY[j + 1] = \
+					REQUEST_ARRAY[i].NODEID_PROPERTY.namespaceIndex; \
+					NUMBER_OF_FOUND_INDICES_ARRAY[j + 1] = 1; \
+					DIFFERENT_INDEX_COUNT++; \
+					break; \
+			} \
+			j++; \
+		} while (j <= DIFFERENT_INDEX_COUNT); \
+	}\
+}while(0)
 #endif

+ 11 - 189
src/server/ua_services_attribute.c

@@ -247,28 +247,8 @@ void Service_Read(UA_Server *server, UA_Session *session,
 		return;
 	}
 	// find out count of different namespace indices
+	BUILD_INDEX_ARRAYS(request->nodesToReadSize,request->nodesToRead,nodeId,differentNamespaceIndexCount,associatedIndices,numberOfFoundIndices);
 
-	for (UA_Int32 i = 0; i < request->nodesToReadSize; i++) {
-		//for(UA_UInt32 j = 0; j <= differentNamespaceIndexCount; j++){
-		UA_UInt32 j = 0;
-		do {
-			if (associatedIndices[j]
-					== request->nodesToRead[i].nodeId.namespaceIndex) {
-				if (differentNamespaceIndexCount == 0) {
-					differentNamespaceIndexCount++;
-				}
-				numberOfFoundIndices[j]++;
-				break;
-			} else if (j == (differentNamespaceIndexCount - 1)) {
-				associatedIndices[j + 1] =
-						request->nodesToRead[i].nodeId.namespaceIndex;
-				associatedIndices[j + 1] = 1;
-				differentNamespaceIndexCount++;
-				break;
-			}
-			j++;
-		} while (j <= differentNamespaceIndexCount);
-	}
 
 	UA_UInt32 *readValueIdIndices;
 	if (UA_Array_new((void **) &readValueIdIndices, request->nodesToReadSize,
@@ -308,149 +288,14 @@ void Service_Read(UA_Server *server, UA_Session *session,
 //	 for(UA_Int32 i = 0;i < response->resultsSize;i++){
 //	 response->results[i] = service_read_node(server, &request->nodesToRead[i]);
 //	 }
-
-
 }
-/*
-static UA_StatusCode Service_Write_writeNode(UA_Server *server,
-		UA_WriteValue *writeValue) {
-	UA_StatusCode retval = UA_STATUSCODE_GOOD;
-	const UA_Node *node;
-	retval = UA_NodeStoreExample_get(server->nodestore, &writeValue->nodeId,
-			&node);
-	if (retval)
-		return retval;
-
-	switch (writeValue->attributeId) {
-	case UA_ATTRIBUTEID_NODEID:
-	*/
-		/* if(writeValue->value.encodingMask == UA_DATAVALUE_ENCODINGMASK_VARIANT){ } */
-//		retval = UA_STATUSCODE_BADWRITENOTSUPPORTED;
-		//		break;
-
-//	case UA_ATTRIBUTEID_NODECLASS:
-//		/* if(writeValue->value.encodingMask == UA_DATAVALUE_ENCODINGMASK_VARIANT){ } */
-//		retval = UA_STATUSCODE_BADWRITENOTSUPPORTED;
-//		break;
-//
-//	case UA_ATTRIBUTEID_BROWSENAME:
-//		/* if(writeValue->value.encodingMask == UA_DATAVALUE_ENCODINGMASK_VARIANT){} */
-//		retval = UA_STATUSCODE_BADWRITENOTSUPPORTED;
-//		break;
-//
-//	case UA_ATTRIBUTEID_DISPLAYNAME:
-//		/* if(writeValue->value.encodingMask == UA_DATAVALUE_ENCODINGMASK_VARIANT){} */
-//		retval = UA_STATUSCODE_BADWRITENOTSUPPORTED;
-//		break;
-//
-//	case UA_ATTRIBUTEID_DESCRIPTION:
-//		/* if(writeValue->value.encodingMask == UA_DATAVALUE_ENCODINGMASK_VARIANT){} */
-//		retval = UA_STATUSCODE_BADWRITENOTSUPPORTED;
-//		break;
-//
-//	case UA_ATTRIBUTEID_WRITEMASK:
-//		/* if(writeValue->value.encodingMask == UA_DATAVALUE_ENCODINGMASK_VARIANT){} */
-//		retval = UA_STATUSCODE_BADWRITENOTSUPPORTED;
-//		break;
-//
-//	case UA_ATTRIBUTEID_USERWRITEMASK:
-//		/* if(writeValue->value.encodingMask == UA_DATAVALUE_ENCODINGMASK_VARIANT){} */
-//		retval = UA_STATUSCODE_BADWRITENOTSUPPORTED;
-//		break;
-//
-//	case UA_ATTRIBUTEID_ISABSTRACT:
-//
-//		/* if(writeValue->value.encodingMask == UA_DATAVALUE_ENCODINGMASK_VARIANT){} */
-//		retval = UA_STATUSCODE_BADWRITENOTSUPPORTED;
-//		break;
-//
-//	case UA_ATTRIBUTEID_SYMMETRIC:
-//		/* if(writeValue->value.encodingMask == UA_DATAVALUE_ENCODINGMASK_VARIANT){} */
-//		retval = UA_STATUSCODE_BADWRITENOTSUPPORTED;
-//		break;
-//
-//	case UA_ATTRIBUTEID_INVERSENAME:
-//		/* if(writeValue->value.encodingMask == UA_DATAVALUE_ENCODINGMASK_VARIANT){} */
-//		retval = UA_STATUSCODE_BADWRITENOTSUPPORTED;
-//		break;
-//
-//	case UA_ATTRIBUTEID_CONTAINSNOLOOPS:
-//		/* if(writeValue->value.encodingMask == UA_DATAVALUE_ENCODINGMASK_VARIANT){} */
-//		retval = UA_STATUSCODE_BADWRITENOTSUPPORTED;
-//		break;
-//
-//	case UA_ATTRIBUTEID_EVENTNOTIFIER:
-//		/* if(writeValue->value.encodingMask == UA_DATAVALUE_ENCODINGMASK_VARIANT){} */
-//		retval = UA_STATUSCODE_BADWRITENOTSUPPORTED;
-//		break;
-//
-//	case UA_ATTRIBUTEID_VALUE:
-//		if (writeValue->value.encodingMask
-//				== UA_DATAVALUE_ENCODINGMASK_VARIANT) {
-//			retval |= UA_Variant_copy(&writeValue->value.value,
-//					&((UA_VariableNode *) node)->value); // todo: zero-copy
-//		}
-//		break;
-//
-//	case UA_ATTRIBUTEID_DATATYPE:
-//		/* if(writeValue->value.encodingMask == UA_DATAVALUE_ENCODINGMASK_VARIANT){} */
-//		retval = UA_STATUSCODE_BADWRITENOTSUPPORTED;
-//		break;
-//
-//	case UA_ATTRIBUTEID_VALUERANK:
-//		/* if(writeValue->value.encodingMask == UA_DATAVALUE_ENCODINGMASK_VARIANT){} */
-//		retval = UA_STATUSCODE_BADWRITENOTSUPPORTED;
-//		break;
-//
-//	case UA_ATTRIBUTEID_ARRAYDIMENSIONS:
-//		/* if(writeValue->value.encodingMask == UA_DATAVALUE_ENCODINGMASK_VARIANT){} */
-//		retval = UA_STATUSCODE_BADWRITENOTSUPPORTED;
-//		break;
-//
-//	case UA_ATTRIBUTEID_ACCESSLEVEL:
-//		/* if(writeValue->value.encodingMask == UA_DATAVALUE_ENCODINGMASK_VARIANT){} */
-//		retval = UA_STATUSCODE_BADWRITENOTSUPPORTED;
-//		break;
-//
-//	case UA_ATTRIBUTEID_USERACCESSLEVEL:
-//		/* if(writeValue->value.encodingMask == UA_DATAVALUE_ENCODINGMASK_VARIANT){} */
-//		retval = UA_STATUSCODE_BADWRITENOTSUPPORTED;
-//		break;
-//
-//	case UA_ATTRIBUTEID_MINIMUMSAMPLINGINTERVAL:
-//		/* if(writeValue->value.encodingMask == UA_DATAVALUE_ENCODINGMASK_VARIANT){} */
-//		retval = UA_STATUSCODE_BADWRITENOTSUPPORTED;
-//		break;
-//
-//	case UA_ATTRIBUTEID_HISTORIZING:
-//		/* if(writeValue->value.encodingMask == UA_DATAVALUE_ENCODINGMASK_VARIANT){} */
-//		retval = UA_STATUSCODE_BADWRITENOTSUPPORTED;
-//		break;
-//
-//	case UA_ATTRIBUTEID_EXECUTABLE:
-//		/* if(writeValue->value.encodingMask == UA_DATAVALUE_ENCODINGMASK_VARIANT){} */
-//		retval = UA_STATUSCODE_BADWRITENOTSUPPORTED;
-//		break;
-//
-//	case UA_ATTRIBUTEID_USEREXECUTABLE:
-//		/* if(writeValue->value.encodingMask == UA_DATAVALUE_ENCODINGMASK_VARIANT){} */
-//		retval = UA_STATUSCODE_BADWRITENOTSUPPORTED;
-//		break;
-//
-//	default:
-//		retval = UA_STATUSCODE_BADATTRIBUTEIDINVALID;
-//		break;
-//	}
-//
-//	UA_NodeStoreExample_releaseManagedNode(node);
-//	return retval;
-//
-//}
 
 void Service_Write(UA_Server *server, UA_Session *session,
 		const UA_WriteRequest *request, UA_WriteResponse *response) {
 	UA_assert(server != UA_NULL && session != UA_NULL && request != UA_NULL && response != UA_NULL);
 
+	response->resultsSize = request->nodesToWriteSize;
+
 	if (UA_Array_new((void **) &response->results, request->nodesToWriteSize,
 			&UA_[UA_STATUSCODE])) {
 		response->responseHeader.serviceResult = UA_STATUSCODE_BADOUTOFMEMORY;
@@ -474,7 +319,7 @@ void Service_Write(UA_Server *server, UA_Session *session,
 		response->responseHeader.serviceResult = UA_STATUSCODE_BADOUTOFMEMORY;
 		return;
 	}
-	response->resultsSize = request->nodesToWriteSize;
+
 
 	UA_Int32 *numberOfFoundIndices;
 	UA_UInt16 *associatedIndices;
@@ -491,31 +336,11 @@ void Service_Write(UA_Server *server, UA_Session *session,
 		return;
 	}
 	// find out count of different namespace indices
+	BUILD_INDEX_ARRAYS(request->nodesToWriteSize,request->nodesToWrite,nodeId,differentNamespaceIndexCount,associatedIndices,numberOfFoundIndices);
 
-	for (UA_Int32 i = 0; i < request->nodesToWriteSize; i++) {
-		//for(UA_UInt32 j = 0; j <= differentNamespaceIndexCount; j++){
-		UA_UInt32 j = 0;
-		do {
-			if (associatedIndices[j]
-					== request->nodesToWrite[i].nodeId.namespaceIndex) {
-				if (differentNamespaceIndexCount == 0) {
-					differentNamespaceIndexCount++;
-				}
-				numberOfFoundIndices[j]++;
-				break;
-			} else if (j == (differentNamespaceIndexCount - 1)) {
-				associatedIndices[j + 1] =
-						request->nodesToWrite[i].nodeId.namespaceIndex;
-				associatedIndices[j + 1] = 1;
-				differentNamespaceIndexCount++;
-				break;
-			}
-			j++;
-		} while (j <= differentNamespaceIndexCount);
-	}
 
-	UA_UInt32 *readValueIdIndices;
-	if (UA_Array_new((void **) &readValueIdIndices, request->nodesToWriteSize,
+	UA_UInt32 *writeValues;
+	if (UA_Array_new((void **) &writeValues, request->nodesToWriteSize,
 			&UA_[UA_UINT32]) != UA_STATUSCODE_GOOD) {
 		response->responseHeader.serviceResult = UA_STATUSCODE_BADOUTOFMEMORY;
 		return;
@@ -532,22 +357,19 @@ void Service_Write(UA_Server *server, UA_Session *session,
 			for (UA_Int32 j = 0; j < request->nodesToWriteSize; j++) {
 				if (request->nodesToWrite[j].nodeId.namespaceIndex
 						== associatedIndices[i]) {
-					readValueIdIndices[n] = j;
+					writeValues[n] = j;
 					n++;
 				}
 			}
 			//call read for every namespace
 			tmpNamespace->nodeStore->writeNodes(request->nodesToWrite,
-					readValueIdIndices, numberOfFoundIndices[i],
+					writeValues, numberOfFoundIndices[i],
 					response->results,
 					response->diagnosticInfos);
 		}
 	}
-	UA_free(readValueIdIndices);
+	UA_free(writeValues);
 	UA_free(numberOfFoundIndices);
 	UA_free(associatedIndices);
-	//response->resultsSize = request->nodesToWriteSize;
-	//for (UA_Int32 i = 0; i < request->nodesToWriteSize; i++){}
-	//	response->results[i] = Service_Write_writeNode(server,
-	//			&request->nodesToWrite[i]);
+
 }

+ 5 - 296
src/server/ua_services_nodemanagement.c

@@ -6,192 +6,7 @@
 #include "ua_namespace_manager.h"
 #include "ua_session.h"
 #include "ua_util.h"
-/*
- #define COPY_STANDARDATTRIBUTES do {                                    \
-    if(attr.specifiedAttributes & UA_NODEATTRIBUTESMASK_DISPLAYNAME) {  \
-        vnode->displayName = attr.displayName;                          \
-        UA_LocalizedText_init(&attr.displayName);                       \
-    }                                                                   \
-    if(attr.specifiedAttributes & UA_NODEATTRIBUTESMASK_DESCRIPTION) {  \
-        vnode->description = attr.description;                          \
-        UA_LocalizedText_init(&attr.description);                       \
-    }                                                                   \
-    if(attr.specifiedAttributes & UA_NODEATTRIBUTESMASK_WRITEMASK)      \
-        vnode->writeMask = attr.writeMask;                              \
-    if(attr.specifiedAttributes & UA_NODEATTRIBUTESMASK_USERWRITEMASK)  \
-        vnode->userWriteMask = attr.userWriteMask;                      \
-    } while(0)
-*/
-//static UA_StatusCode parseVariableNode(UA_ExtensionObject *attributes,
-//		UA_Node **new_node, const UA_VTable_Entry **vt) {
-//	if (attributes->typeId.identifier.numeric != 357) // VariableAttributes_Encoding_DefaultBinary,357,Object
-//		return UA_STATUSCODE_BADNODEATTRIBUTESINVALID;
-//
-//	UA_VariableAttributes attr;
-//	UA_UInt32 pos = 0;
-//	// todo return more informative error codes from decodeBinary
-//	if (UA_VariableAttributes_decodeBinary(&attributes->body, &pos, &attr)
-//			!= UA_STATUSCODE_GOOD)
-//		return UA_STATUSCODE_BADNODEATTRIBUTESINVALID;
-//
-//	UA_VariableNode *vnode;
-//	if (UA_VariableNode_new(&vnode) != UA_STATUSCODE_GOOD) {
-//		UA_VariableAttributes_deleteMembers(&attr);
-//		return UA_STATUSCODE_BADOUTOFMEMORY;
-//	}
-//
-//	// now copy all the attributes. This potentially removes them from the decoded attributes.
-//	COPY_STANDARDATTRIBUTES;
-//
-//	if (attr.specifiedAttributes & UA_NODEATTRIBUTESMASK_ACCESSLEVEL)
-//		vnode->accessLevel = attr.accessLevel;
-//
-//	if (attr.specifiedAttributes & UA_NODEATTRIBUTESMASK_USERACCESSLEVEL)
-//		vnode->userAccessLevel = attr.userAccessLevel;
-//
-//	if (attr.specifiedAttributes & UA_NODEATTRIBUTESMASK_HISTORIZING)
-//		vnode->historizing = attr.historizing;
-//
-//	if (attr.specifiedAttributes
-//			& UA_NODEATTRIBUTESMASK_MINIMUMSAMPLINGINTERVAL)
-//		vnode->minimumSamplingInterval = attr.minimumSamplingInterval;
-//
-//	if (attr.specifiedAttributes & UA_NODEATTRIBUTESMASK_VALUERANK)
-//		vnode->valueRank = attr.valueRank;
-//
-//	if (attr.specifiedAttributes & UA_NODEATTRIBUTESMASK_ARRAYDIMENSIONS) {
-//		vnode->arrayDimensionsSize = attr.arrayDimensionsSize;
-//		vnode->arrayDimensions = attr.arrayDimensions;
-//		attr.arrayDimensionsSize = -1;
-//		attr.arrayDimensions = UA_NULL;
-//	}
-//
-//	if (attr.specifiedAttributes & UA_NODEATTRIBUTESMASK_DATATYPE
-//			|| attr.specifiedAttributes
-//					& UA_NODEATTRIBUTESMASK_OBJECTTYPEORDATATYPE) {
-//		vnode->dataType = attr.dataType;
-//		UA_NodeId_init(&attr.dataType);
-//	}
-//
-//	if (attr.specifiedAttributes & UA_NODEATTRIBUTESMASK_VALUE) {
-//		vnode->value = attr.value;
-//		UA_Variant_init(&attr.value);
-//	}
-//
-//	UA_VariableAttributes_deleteMembers(&attr);
-//
-//	*new_node = (UA_Node*) vnode;
-//	*vt = &UA_[UA_VARIABLENODE];
-//	return UA_STATUSCODE_GOOD;
-//}
-//
-//UA_Int32 AddReference(UA_NodeStoreExample *nodestore, UA_Node *node,
-//		UA_ReferenceNode *reference);
-//
-///**
-// If adding the node succeeds, the pointer will be set to zero. If the nodeid
-// of the node is null (ns=0,i=0), a unique new nodeid will be assigned and
-// returned in the AddNodesResult.
-// */
-//UA_AddNodesResult AddNode(UA_Server *server, UA_Session *session,
-//		UA_Node **node, UA_ExpandedNodeId *parentNodeId,
-//		UA_NodeId *referenceTypeId) {
-//	UA_AddNodesResult result;
-//	UA_AddNodesResult_init(&result);
-//
-//	const UA_Node *parent;
-//	if (UA_NodeStoreExample_get(server->nodestore, &parentNodeId->nodeId,
-//			&parent) != UA_STATUSCODE_GOOD) {
-//		result.statusCode = UA_STATUSCODE_BADPARENTNODEIDINVALID;
-//		return result;
-//	}
-//
-//	const UA_ReferenceTypeNode *referenceType;
-//	if (UA_NodeStoreExample_get(server->nodestore, referenceTypeId,
-//			(const UA_Node**) &referenceType) != UA_STATUSCODE_GOOD) {
-//		result.statusCode = UA_STATUSCODE_BADREFERENCETYPEIDINVALID;
-//		goto ret;
-//	}
-//
-//	if (referenceType->nodeClass != UA_NODECLASS_REFERENCETYPE) {
-//		result.statusCode = UA_STATUSCODE_BADREFERENCETYPEIDINVALID;
-//		goto ret2;
-//	}
-//
-//	if (referenceType->isAbstract == UA_TRUE) {
-//		result.statusCode = UA_STATUSCODE_BADREFERENCENOTALLOWED;
-//		goto ret2;
-//	}
-//
-//	// todo: test if the referenetype is hierarchical
-//
-//	if (UA_NodeId_isNull(&(*node)->nodeId)) {
-//		if (UA_NodeStoreExample_insert(server->nodestore, node,
-//		UA_NODESTORE_INSERT_UNIQUE | UA_NODESTORE_INSERT_GETMANAGED)
-//				!= UA_STATUSCODE_GOOD) {
-//			result.statusCode = UA_STATUSCODE_BADOUTOFMEMORY;
-//			goto ret2;
-//		}
-//		result.addedNodeId = (*node)->nodeId; // cannot fail as unique nodeids are numeric
-//	} else {
-//		if (UA_NodeId_copy(&(*node)->nodeId, &result.addedNodeId)
-//				!= UA_STATUSCODE_GOOD) {
-//			result.statusCode = UA_STATUSCODE_BADOUTOFMEMORY;
-//			goto ret2;
-//		}
-//
-//		if (UA_NodeStoreExample_insert(server->nodestore, node,
-//				UA_NODESTORE_INSERT_GETMANAGED) != UA_STATUSCODE_GOOD) {
-//			result.statusCode = UA_STATUSCODE_BADNODEIDEXISTS; // todo: differentiate out of memory
-//			UA_NodeId_deleteMembers(&result.addedNodeId);
-//			goto ret2;
-//		}
-//	}
-//
-//	UA_ReferenceNode ref;
-//	UA_ReferenceNode_init(&ref);
-//	ref.referenceTypeId = referenceType->nodeId; // is numeric
-//	ref.isInverse = UA_TRUE; // todo: check if they are all not inverse..
-//	ref.targetId.nodeId = parent->nodeId;
-//	AddReference(server->nodestore, *node, &ref);
-//
-//	// todo: error handling. remove new node from nodestore
-//
-//	UA_NodeStoreExample_releaseManagedNode(*node);
-//	*node = UA_NULL;
-//
-//	ret2: UA_NodeStoreExample_releaseManagedNode((UA_Node*) referenceType);
-//	ret: UA_NodeStoreExample_releaseManagedNode(parent);
-//
-//	return result;
-//}
-//
-//static void addNodeFromAttributes(UA_Server *server, UA_Session *session,
-//		UA_AddNodesItem *item, UA_AddNodesResult *result) {
-//	if (item->requestedNewNodeId.nodeId.namespaceIndex == 0) {
-//		// adding nodes to ns0 is not allowed over the wire
-//		result->statusCode = UA_STATUSCODE_BADNODEIDREJECTED;
-//		return;
-//	}
-//
-//	UA_Node *newNode;
-//	const UA_VTable_Entry *newNodeVT = UA_NULL;
-//	if (item->nodeClass == UA_NODECLASS_VARIABLE)
-//		result->statusCode = parseVariableNode(&item->nodeAttributes, &newNode,
-//				&newNodeVT);
-//	else
-//		// add more node types here..
-//		result->statusCode = UA_STATUSCODE_BADNOTIMPLEMENTED;
-//
-//	if (result->statusCode != UA_STATUSCODE_GOOD)
-//		return;
-//
-//	*result = AddNode(server, session, &newNode, &item->parentNodeId,
-//			&item->referenceTypeId);
-//	if (result->statusCode != UA_STATUSCODE_GOOD)
-//		newNodeVT->delete(newNode);
-//}
-//
+
 void Service_AddNodes(UA_Server *server, UA_Session *session,
 		const UA_AddNodesRequest *request, UA_AddNodesResponse *response) {
 	UA_assert(server != UA_NULL && session != UA_NULL);
@@ -231,28 +46,7 @@ void Service_AddNodes(UA_Server *server, UA_Session *session,
 		return;
 	}
 	// find out count of different namespace indices
-
-	for (UA_Int32 i = 0; i < request->nodesToAddSize; i++) {
-		//for(UA_UInt32 j = 0; j <= differentNamespaceIndexCount; j++){
-		UA_UInt32 j = 0;
-		do {
-			if (associatedIndices[j]
-					== request->nodesToAdd[i].requestedNewNodeId.nodeId.namespaceIndex) {
-				if (differentNamespaceIndexCount == 0) {
-					differentNamespaceIndexCount++;
-				}
-				numberOfFoundIndices[j]++;
-				break;
-			} else if (j == (differentNamespaceIndexCount - 1)) {
-				associatedIndices[j + 1] =
-						request->nodesToAdd[i].requestedNewNodeId.nodeId.namespaceIndex;
-				associatedIndices[j + 1] = 1;
-				differentNamespaceIndexCount++;
-				break;
-			}
-			j++;
-		} while (j <= differentNamespaceIndexCount);
-	}
+	BUILD_INDEX_ARRAYS(request->nodesToAddSize,request->nodesToAdd,requestedNewNodeId.nodeId,differentNamespaceIndexCount,associatedIndices,numberOfFoundIndices);
 
 	UA_UInt32 *addNodesIndices;
 	if (UA_Array_new((void **) &addNodesIndices,
@@ -280,80 +74,14 @@ void Service_AddNodes(UA_Server *server, UA_Session *session,
 			tmpNamespace->nodeStore->addNodes(request->nodesToAdd,
 					addNodesIndices, numberOfFoundIndices[i],
 					response->results, response->diagnosticInfos);
-
-			//	response->results[i] = service_read_node(server, &request->nodesToRead[i]);
 		}
 	}
 	UA_free(addNodesIndices);
 	UA_free(numberOfFoundIndices);
 	UA_free(associatedIndices);
+}
 
-/*	if (request->nodesToAddSize <= 0) {
-		response->responseHeader.serviceResult = UA_STATUSCODE_BADNOTHINGTODO;
-		return;
-	}
 
-	if (UA_Array_new((void **) &response->results, request->nodesToAddSize,
-			&UA_[UA_ADDNODESRESULT]) != UA_STATUSCODE_GOOD) {
-		response->responseHeader.serviceResult = UA_STATUSCODE_BADOUTOFMEMORY;
-		return;
-	}
-
-	response->resultsSize = request->nodesToAddSize;
-	for (int i = 0; i < request->nodesToAddSize; i++)
-		addNodeFromAttributes(server, session, &request->nodesToAdd[i],
-				&response->results[i]);
-				*/
-}
-//
-//static UA_Int32 AddSingleReference(UA_Node *node, UA_ReferenceNode *reference) {
-//	// TODO: Check if reference already exists
-//	UA_Int32 count = node->referencesSize;
-//	UA_ReferenceNode *old_refs = node->references;
-//	UA_ReferenceNode *new_refs;
-//
-//	if (count < 0)
-//		count = 0;
-//
-//	if (!(new_refs = UA_alloc(sizeof(UA_ReferenceNode) * (count + 1))))
-//		return UA_STATUSCODE_BADOUTOFMEMORY;
-//
-//	UA_memcpy(new_refs, old_refs, sizeof(UA_ReferenceNode) * count);
-//	if (UA_ReferenceNode_copy(reference, &new_refs[count])
-//			!= UA_STATUSCODE_GOOD) {
-//		UA_free(new_refs);
-//		return UA_STATUSCODE_BADOUTOFMEMORY;
-//	}
-//
-//	node->references = new_refs;
-//	node->referencesSize = count + 1;
-//	UA_free(old_refs);
-//	return UA_STATUSCODE_GOOD;
-//}
-//
-//UA_Int32 AddReference(UA_NodeStoreExample *nodestore, UA_Node *node,
-//		UA_ReferenceNode *reference) {
-//	UA_Int32 retval = AddSingleReference(node, reference);
-//	UA_Node *targetnode;
-//	UA_ReferenceNode inversereference;
-//	if (retval != UA_STATUSCODE_GOOD || nodestore == UA_NULL)
-//		return retval;
-//
-//	// Do a copy every time?
-//	if (UA_NodeStoreExample_get(nodestore, &reference->targetId.nodeId,
-//			(const UA_Node **) &targetnode) != UA_STATUSCODE_GOOD)
-//		return UA_STATUSCODE_BADINTERNALERROR;
-//
-//	inversereference.referenceTypeId = reference->referenceTypeId;
-//	inversereference.isInverse = !reference->isInverse;
-//	inversereference.targetId.nodeId = node->nodeId;
-//	inversereference.targetId.namespaceUri = UA_STRING_NULL;
-//	inversereference.targetId.serverIndex = 0;
-//	retval = AddSingleReference(targetnode, &inversereference);
-//	UA_NodeStoreExample_releaseManagedNode(targetnode);
-//
-//	return retval;
-//}
 
 void Service_AddReferences(UA_Server *server, UA_Session *session,
 		const UA_AddReferencesRequest *request,
@@ -395,27 +123,8 @@ void Service_AddReferences(UA_Server *server, UA_Session *session,
 	}
 	// find out count of different namespace indices
 
-	for (UA_Int32 i = 0; i < request->referencesToAddSize; i++) {
-		//for(UA_UInt32 j = 0; j <= differentNamespaceIndexCount; j++){
-		UA_UInt32 j = 0;
-		do {
-			if (associatedIndices[j]
-					== request->referencesToAdd[i].sourceNodeId.namespaceIndex) {
-				if (differentNamespaceIndexCount == 0) {
-					differentNamespaceIndexCount++;
-				}
-				numberOfFoundIndices[j]++;
-				break;
-			} else if (j == (differentNamespaceIndexCount - 1)) {
-				associatedIndices[j + 1] =
-						request->referencesToAdd[i].sourceNodeId.namespaceIndex;
-				associatedIndices[j + 1] = 1;
-				differentNamespaceIndexCount++;
-				break;
-			}
-			j++;
-		} while (j <= differentNamespaceIndexCount);
-	}
+
+	BUILD_INDEX_ARRAYS(request->referencesToAddSize, request->referencesToAdd, sourceNodeId,differentNamespaceIndexCount, associatedIndices, numberOfFoundIndices);
 
 	UA_UInt32 *readValueIdIndices;
 	if (UA_Array_new((void **) &readValueIdIndices,

+ 2 - 22
src/server/ua_services_view.c

@@ -33,28 +33,8 @@ void Service_Browse(UA_Server *server, UA_Session *session,
     	response->responseHeader.serviceResult = UA_STATUSCODE_BADOUTOFMEMORY;
     	return ;
     }
-    // find out count of different namespace indices
-	for (UA_Int32 i = 0; i < request->nodesToBrowseSize; i++) {
-		//for(UA_UInt32 j = 0; j <= differentNamespaceIndexCount; j++){
-		UA_UInt32 j = 0;
-		do {
-			if (associatedIndices[j]
-					== request->nodesToBrowse[i].nodeId.namespaceIndex) {
-				if (differentNamespaceIndexCount == 0) {
-					differentNamespaceIndexCount++;
-				}
-				numberOfFoundIndices[j]++;
-				break;
-			} else if (j == (differentNamespaceIndexCount - 1)) {
-				associatedIndices[j + 1] =
-						request->nodesToBrowse[i].nodeId.namespaceIndex;
-				associatedIndices[j + 1] = 1;
-				differentNamespaceIndexCount++;
-				break;
-			}
-			j++;
-		} while (j <= differentNamespaceIndexCount);
-	}
+
+    BUILD_INDEX_ARRAYS(request->nodesToBrowseSize,request->nodesToBrowse,nodeId,differentNamespaceIndexCount,associatedIndices,numberOfFoundIndices);
 
 	UA_UInt32 *browseDescriptionIndices;
     if(UA_Array_new((void **)&browseDescriptionIndices,request->nodesToBrowseSize,&UA_[UA_UINT32]) != UA_STATUSCODE_GOOD){