Browse Source

PubSub:Fix Regist fail when adding multiple ReaderGroups

 - Add separate function for channel register
 - Call regist function in PubSubConnection and not in addReaderGroup
   to resolve regist fail when adding multiple ReaderGroups
 - Change return type of example API to handle statuscodes

Change-Id: I58ecec6af6cae2a9427b620241e08c7880b10ef7
Selva Suba Jenifer Joseph 5 years ago
parent
commit
3e3c7221c5
3 changed files with 77 additions and 37 deletions
  1. 50 22
      examples/pubsub/tutorial_pubsub_subscribe.c
  2. 24 15
      src/pubsub/ua_pubsub.c
  3. 3 0
      src/pubsub/ua_pubsub.h

+ 50 - 22
examples/pubsub/tutorial_pubsub_subscribe.c

@@ -35,14 +35,15 @@ UA_DataSetReaderConfig readerConfig;
 static void fillTestDataSetMetaData(UA_DataSetMetaDataType *pMetaData);
 
 /* Add new connection to the server */
-static void
+static UA_StatusCode
 addPubSubConnection(UA_Server *server, UA_String *transportProfile,
                     UA_NetworkAddressUrlDataType *networkAddressUrl) {
     if((server == NULL) || (transportProfile == NULL) ||
         (networkAddressUrl == NULL)) {
-        return;
+        return UA_STATUSCODE_BADINTERNALERROR;
     }
 
+    UA_StatusCode retval = UA_STATUSCODE_GOOD;
     /* Configuration creation for the connection */
     UA_PubSubConnectionConfig connectionConfig;
     memset (&connectionConfig, 0, sizeof(UA_PubSubConnectionConfig));
@@ -50,49 +51,60 @@ addPubSubConnection(UA_Server *server, UA_String *transportProfile,
     connectionConfig.transportProfileUri = *transportProfile;
     connectionConfig.enabled = UA_TRUE;
     UA_Variant_setScalar(&connectionConfig.address, networkAddressUrl,
-                          &UA_TYPES[UA_TYPES_NETWORKADDRESSURLDATATYPE]);
+                         &UA_TYPES[UA_TYPES_NETWORKADDRESSURLDATATYPE]);
     connectionConfig.publisherId.numeric = UA_UInt32_random ();
-    UA_Server_addPubSubConnection (server, &connectionConfig, &connectionIdentifier);
+    retval |= UA_Server_addPubSubConnection (server, &connectionConfig, &connectionIdentifier);
+    if (retval != UA_STATUSCODE_GOOD) {
+        return retval;
+    }
+    retval |= UA_PubSubConnection_regist(server, &connectionIdentifier);
+    return retval;
 }
 
 /* Add ReaderGroup to the created connection */
-static void
+static UA_StatusCode
 addReaderGroup(UA_Server *server) {
     if(server == NULL) {
-        return;
+        return UA_STATUSCODE_BADINTERNALERROR;
     }
 
+    UA_StatusCode retval = UA_STATUSCODE_GOOD;
     UA_ReaderGroupConfig readerGroupConfig;
     memset (&readerGroupConfig, 0, sizeof(UA_ReaderGroupConfig));
     readerGroupConfig.name = UA_STRING("ReaderGroup1");
-    UA_Server_addReaderGroup(server, connectionIdentifier, &readerGroupConfig,
-                                        &readerGroupIdentifier);
+    retval |= UA_Server_addReaderGroup(server, connectionIdentifier, &readerGroupConfig,
+                                       &readerGroupIdentifier);
+    return retval;
 }
 
 /* Add DataSetReader to the ReaderGroup */
-static void
+static UA_StatusCode
 addDataSetReader(UA_Server *server) {
     if(server == NULL) {
-        return;
+        return UA_STATUSCODE_BADINTERNALERROR;
     }
 
+    UA_StatusCode retval = UA_STATUSCODE_GOOD;
     memset (&readerConfig, 0, sizeof(UA_DataSetReaderConfig));
     readerConfig.name = UA_STRING("DataSet Reader 1");
     readerConfig.dataSetWriterId = 1;
 
     /* Setting up Meta data configuration in DataSetReader */
     fillTestDataSetMetaData(&readerConfig.dataSetMetaData);
-    UA_Server_addDataSetReader(server, readerGroupIdentifier, &readerConfig,
-                                          &readerIdentifier);
+    retval |= UA_Server_addDataSetReader(server, readerGroupIdentifier, &readerConfig,
+                                         &readerIdentifier);
+    return retval;
 }
 
 /* Set SubscribedDataSet type to TargetVariables data type
  * Add subscribedvariables to the DataSetReader */
-static void addSubscribedVariables (UA_Server *server, UA_NodeId dataSetReaderId) {
+static UA_StatusCode
+addSubscribedVariables (UA_Server *server, UA_NodeId dataSetReaderId) {
     if(server == NULL) {
-        return;
+        return UA_STATUSCODE_BADINTERNALERROR;
     }
 
+    UA_StatusCode retval = UA_STATUSCODE_GOOD;
     UA_NodeId folderId;
     UA_String folderName = readerConfig.dataSetMetaData.name;
     UA_ObjectAttributes oAttr = UA_ObjectAttributes_default;
@@ -114,10 +126,11 @@ static void addSubscribedVariables (UA_Server *server, UA_NodeId dataSetReaderId
                              folderBrowseName, UA_NODEID_NUMERIC (0,
                              UA_NS0ID_BASEOBJECTTYPE), oAttr, NULL, &folderId);
 
-    UA_Server_DataSetReader_addTargetVariables (server, &folderId,
-                                               dataSetReaderId,
-                                               UA_PUBSUB_SDS_TARGET);
+    retval |= UA_Server_DataSetReader_addTargetVariables (server, &folderId,
+                                                          dataSetReaderId,
+                                                          UA_PUBSUB_SDS_TARGET);
     UA_free(readerConfig.dataSetMetaData.fields);
+    return retval;
 }
 
 /* Define MetaData for TargetVariables */
@@ -180,7 +193,7 @@ run(UA_String *transportProfile, UA_NetworkAddressUrlDataType *networkAddressUrl
     signal(SIGINT, stopHandler);
     signal(SIGTERM, stopHandler);
     /* Return value initialized to Status Good */
-    UA_StatusCode retval;
+    UA_StatusCode retval = UA_STATUSCODE_GOOD;
     UA_Server *server = UA_Server_new();
     UA_ServerConfig *config = UA_Server_getConfig(server);
     UA_ServerConfig_setMinimal(config, 4801, NULL);
@@ -204,10 +217,25 @@ run(UA_String *transportProfile, UA_NetworkAddressUrlDataType *networkAddressUrl
 #endif
 
     /* API calls */
-    addPubSubConnection(server, transportProfile, networkAddressUrl);
-    addReaderGroup(server);
-    addDataSetReader(server);
-    addSubscribedVariables(server, readerIdentifier);
+    /* Add PubSubConnection */
+    retval |= addPubSubConnection(server, transportProfile, networkAddressUrl);
+    if (retval != UA_STATUSCODE_GOOD)
+        return EXIT_FAILURE;
+
+    /* Add ReaderGroup to the created PubSubConnection */
+    retval |= addReaderGroup(server);
+    if (retval != UA_STATUSCODE_GOOD)
+        return EXIT_FAILURE;
+
+    /* Add DataSetReader to the created ReaderGroup */
+    retval |= addDataSetReader(server);
+    if (retval != UA_STATUSCODE_GOOD)
+        return EXIT_FAILURE;
+
+    /* Add SubscribedVariables to the created DataSetReader */
+    retval |= addSubscribedVariables(server, readerIdentifier);
+    if (retval != UA_STATUSCODE_GOOD)
+        return EXIT_FAILURE;
 
     retval = UA_Server_run(server, &running);
     UA_Server_delete(server);

+ 24 - 15
src/pubsub/ua_pubsub.c

@@ -122,6 +122,26 @@ UA_PubSubConnection_deleteMembers(UA_Server *server, UA_PubSubConnection *connec
     UA_free(connection->config);
 }
 
+/**
+ * Regist connection given by connectionIdentifier
+ *
+ * @param server
+ * @param connectionIdentifier
+ */
+UA_StatusCode
+UA_PubSubConnection_regist(UA_Server *server, UA_NodeId *connectionIdentifier) {
+    UA_PubSubConnection *connection = UA_PubSubConnection_findConnectionbyId(server, *connectionIdentifier);
+    UA_StatusCode retval = UA_STATUSCODE_GOOD;
+    if(connection == NULL) {
+        return UA_STATUSCODE_BADNOTFOUND;
+    }
+    retval = connection->channel->regist(connection->channel, NULL, NULL);
+    if (retval != UA_STATUSCODE_GOOD) {
+        UA_LOG_WARNING(&server->config.logger, UA_LOGCATEGORY_SERVER, "register channel failed: 0x%x!", retval);
+    }
+    return retval;
+}
+
 UA_StatusCode
 UA_Server_addWriterGroup(UA_Server *server, const UA_NodeId connection,
                          const UA_WriterGroupConfig *writerGroupConfig,
@@ -210,7 +230,7 @@ UA_Server_addReaderGroup(UA_Server *server, UA_NodeId connectionIdentifier,
     UA_StatusCode retval = UA_STATUSCODE_GOOD;
     UA_ReaderGroupConfig tmpReaderGroupConfig;
 
-    /* Search the connection by the given connectionIdentifier */
+    /* Check for valid readergroup configuration */
     if(!readerGroupConfig) {
         return UA_STATUSCODE_BADINVALIDARGUMENT;
     }
@@ -2124,20 +2144,9 @@ void UA_ReaderGroup_subscribeCallback(UA_Server *server, UA_ReaderGroup *readerG
 UA_StatusCode
 UA_ReaderGroup_addSubscribeCallback(UA_Server *server, UA_ReaderGroup *readerGroup) {
     UA_StatusCode retval = UA_STATUSCODE_GOOD;
-    UA_PubSubConnection *connection = UA_PubSubConnection_findConnectionbyId(server, readerGroup->linkedConnection);
-    if(connection != NULL) {
-        retval = connection->channel->regist(connection->channel, NULL, NULL);
-        if(retval == UA_STATUSCODE_GOOD) {
-            retval = UA_PubSubManager_addRepeatedCallback(server,
-                    (UA_ServerCallback) UA_ReaderGroup_subscribeCallback,
-                    readerGroup, 5,
-                    &readerGroup->subscribeCallbackId);
-        }
-        else {
-            UA_LOG_WARNING(&server->config.logger, UA_LOGCATEGORY_SERVER, "register channel failed: 0x%x!", retval);
-        }
-
-    }
+    retval |= UA_PubSubManager_addRepeatedCallback(server, (UA_ServerCallback) UA_ReaderGroup_subscribeCallback,
+                                                   readerGroup, 5,
+                                                   &readerGroup->subscribeCallbackId);
 
     if(retval == UA_STATUSCODE_GOOD) {
         readerGroup->subscribeCallbackIsRegistered = true;

+ 3 - 0
src/pubsub/ua_pubsub.h

@@ -71,6 +71,9 @@ void
 UA_PubSubConnectionConfig_deleteMembers(UA_PubSubConnectionConfig *connectionConfig);
 void
 UA_PubSubConnection_deleteMembers(UA_Server *server, UA_PubSubConnection *connection);
+/* Register channel for given connectionIdentifier */
+UA_StatusCode
+UA_PubSubConnection_regist(UA_Server *server, UA_NodeId *connectionIdentifier);
 
 /**********************************************/
 /*              DataSetWriter                 */