From 16d7b8adb6feeafd3865b3d9a7c09b8de6940d38 Mon Sep 17 00:00:00 2001
From: Christopher Haine <chaine@hpe.com>
Date: Tue, 12 Oct 2021 12:05:35 +0200
Subject: [PATCH] fix PC handles demand event

---
 docs/sphinx/essentials.rst      |  4 ++--
 maestro/subscription_registry.c | 24 +++++++++++++++++++++++-
 2 files changed, 25 insertions(+), 3 deletions(-)

diff --git a/docs/sphinx/essentials.rst b/docs/sphinx/essentials.rst
index 30d5ea9b..643ddaf3 100644
--- a/docs/sphinx/essentials.rst
+++ b/docs/sphinx/essentials.rst
@@ -221,7 +221,7 @@ Then events may be polled
    mstro_subscription_poll(cdo_subscription, &event);
 
 .. NOTE::
-   ``mstro_subscription_poll()`` returns in *event* a list of events, where *event* is the head and where the next element is event->next.
+   ``mstro_subscription_poll()`` returns in *event* a list of events, where *event* is the head and where the next element is ``event->next``.
 
 and inspected for CDO properties
 
@@ -265,7 +265,7 @@ elements may be inspected
 
 before being potentially individually ``required``.
 
-.. WARNING::
+.. NOTE::
  * All group participant must know all CDOs, at least their names
  * Consumer must know all CDO names at least
 
diff --git a/maestro/subscription_registry.c b/maestro/subscription_registry.c
index 0ccd7c6d..88851bf4 100644
--- a/maestro/subscription_registry.c
+++ b/maestro/subscription_registry.c
@@ -1986,6 +1986,28 @@ mstro_pool_event_consume(const Mstro__Pool__Event *eventmsg)
             ev->require.cdo_name, ev->require.appid);
       break;
 
+    case MSTRO_POOL_EVENT_DEMAND:
+      assert(eventmsg->payload_case==MSTRO__POOL__EVENT__PAYLOAD_DEMAND);
+      assert(eventmsg->origin_id!=NULL
+             && eventmsg->origin_id->id!=MSTRO_APP_ID_INVALID);
+      assert(eventmsg->demand->cdoid!=NULL);
+      ev->demand.appid = eventmsg->origin_id->id;
+      if(eventmsg->cdo_name==NULL) {
+        ERR("DEMAND event missing a CDO name\n");
+        free(ev);
+        return MSTRO_FAIL;
+      } else {
+        ev->demand.cdo_name = strdup(eventmsg->cdo_name);
+        if(ev->demand.cdo_name == NULL) {
+          ERR("Failed to allocate event data\n");
+          free(ev);
+          return MSTRO_NOMEM;
+        }
+      }
+      
+      DEBUG("Event: DEMAND for |%s| from %" PRIu64 "\n",
+            ev->demand.cdo_name, ev->demand.appid);
+      break;
     case MSTRO_POOL_EVENT_WITHDRAW:
       assert(eventmsg->payload_case==MSTRO__POOL__EVENT__PAYLOAD_WITHDRAW);
       assert(eventmsg->origin_id!=NULL
@@ -2017,7 +2039,7 @@ mstro_pool_event_consume(const Mstro__Pool__Event *eventmsg)
       break;
 
     case MSTRO_POOL_EVENT_SEAL_GROUP:
-    case MSTRO_POOL_EVENT_DEMAND:
+    
     case MSTRO_POOL_EVENT_RETRACT:
     case MSTRO_POOL_EVENT_DISPOSE:
     case MSTRO_POOL_EVENT_TRANSPORT_INIT:
-- 
GitLab