[PATCH] [mama] Extend startcallback


Ian Bell <IBell@...>
 

From c4030019d9d2fdc57288a60b63214d26dfd10f62 Mon Sep 17 00:00:00 2001

Message-Id: <c4030019d9d2fdc57288a60b63214d26dfd10f62.1348588330.git.ibell@...>

From: Ian Bell <ibell@...>

Date: Tue, 25 Sep 2012 16:51:43 +0100

Subject: [PATCH] [mama] Extend startcallback

 

Added a new version of htestart callback called stop callback

as its more accurate.  Also extended it to take a closure so

that a different callback can be called for each bridge.  It

also passes back the bridge in teh callback.

 

Signed-off-by: Ian Bell <ibell@...>

---

mama/c_cpp/src/c/mama.c          |   54 +++++++++++++++++++++++++++++---------

mama/c_cpp/src/c/mama/mama.h     |   30 +++++++++++++++++++++

mama/c_cpp/src/c/transportimpl.h |    5 ++++

mama/c_cpp/src/cpp/mamacpp.cpp   |   12 +++------

4 files changed, 80 insertions(+), 21 deletions(-)

 

diff --git a/mama/c_cpp/src/c/mama.c b/mama/c_cpp/src/c/mama.c

index 9218aee..78ecc92 100644

--- a/mama/c_cpp/src/c/mama.c

+++ b/mama/c_cpp/src/c/mama.c

@@ -44,6 +44,7 @@

#include <statsgeneratorinternal.h>

#include <statsgeneratorinternal.h>

#include <mama/statscollector.h>

+#include "transportimpl.h"

 #define PROPERTY_FILE "mama.properties"

#define WOMBAT_PATH_ENV "WOMBAT_PATH"

@@ -1360,13 +1361,14 @@ mama_start (mamaBridge bridgeImpl)

 struct startBackgroundClosure

{

-    mamaStartCB mStartCallback;

-    mamaBridge  mBridgeImpl;

+    mamaStopCB   mStopCallback;

+    mamaStopCBEx mStopCallbackEx;

+    mamaBridge   mBridgeImpl;

+    void*        mClosure;

};

 static void* mamaStartThread (void* closure)

{

-    /* size_t cast prevents compiler warning */

     struct startBackgroundClosure* cb =

                 (struct startBackgroundClosure*)closure;

     mama_status rval = MAMA_STATUS_OK;

@@ -1375,7 +1377,11 @@ static void* mamaStartThread (void* closure)

     rval = mama_start (cb->mBridgeImpl);

-    cb->mStartCallback (rval);

+    if (cb->mStopCallback)

+        cb->mStopCallback (rval);

+

+    if (cb->mStopCallbackEx)

+        cb->mStopCallbackEx (rval, cb->mBridgeImpl, cb->mClosure);

     /* Free the closure object */

     free(cb);

@@ -1383,24 +1389,33 @@ static void* mamaStartThread (void* closure)

     return NULL;

}

-mama_status

-mama_startBackground (mamaBridge   bridgeImpl,

-                      mamaStartCB callback)

+static mama_status

+mama_startBackgroundHelper (mamaBridge   bridgeImpl,

+                            mamaStopCB   callback,

+                            mamaStopCBEx exCallback,

+                            void*        closure)

{

     struct startBackgroundClosure*  closureData;

     wthread_t       t = 0;

     if (!bridgeImpl)

     {

-        mama_log (MAMA_LOG_LEVEL_ERROR, "mama_startBackground(): NULL bridge "

+        mama_log (MAMA_LOG_LEVEL_ERROR, "mama_startBackgroundHelper(): NULL bridge "

                   " impl.");

         return MAMA_STATUS_NO_BRIDGE_IMPL;

     }

-    if (!callback)

+    if (!callback && !exCallback)

+    {

+        mama_log (MAMA_LOG_LEVEL_ERROR, "mama_startBackgroundHelper(): No "

+                  "stop callback or extended stop callback specified.");

+        return MAMA_STATUS_INVALID_ARG;

+    }

+

+    if (callback && exCallback)

     {

-        mama_log (MAMA_LOG_LEVEL_ERROR, "mama_startBackground(): No "

-                  "callback specified.");

+        mama_log (MAMA_LOG_LEVEL_ERROR, "mama_startBackgroundHelper(): Both "

+                "stop callback and extended stop callback specified.");

         return MAMA_STATUS_INVALID_ARG;

     }

@@ -1409,8 +1424,10 @@ mama_startBackground (mamaBridge   bridgeImpl,

     if (!closureData)

         return MAMA_STATUS_NOMEM;

-    closureData->mStartCallback = callback;

+    closureData->mStopCallback   = callback;

+    closureData->mStopCallbackEx = exCallback;

     closureData->mBridgeImpl    = bridgeImpl;

+    closureData->mClosure        = closure;

     if (0 != wthread_create(&t, NULL, mamaStartThread, (void*) closureData))

     {

@@ -1422,6 +1439,19 @@ mama_startBackground (mamaBridge   bridgeImpl,

     return MAMA_STATUS_OK;

}

+mama_status

+mama_startBackground (mamaBridge bridgeImpl, mamaStartCB callback)

+{

+    /* Passing these NULLs tells mama_startBackgroundHelper to use old functionality */

+    return mama_startBackgroundHelper (bridgeImpl, (mamaStopCB)callback, NULL, NULL);

+}

+

+mama_status

+mama_startBackgroundEx (mamaBridge bridgeImpl, mamaStopCBEx exCallback, void* closure)

+{

+    /* Passing this NULL tells mama_StartBackgroundHelper to use new functionality */

+    return mama_startBackgroundHelper (bridgeImpl, NULL, exCallback, closure);

+}

/**

  * Stop processing messages

  */

diff --git a/mama/c_cpp/src/c/mama/mama.h b/mama/c_cpp/src/c/mama/mama.h

index 8be6752..597164f 100644

--- a/mama/c_cpp/src/c/mama/mama.h

+++ b/mama/c_cpp/src/c/mama/mama.h

@@ -324,9 +324,18 @@ extern "C"

     typedef void (MAMACALLTYPE *mamaStartCB) (mama_status status);

     /**

+     * Callback invoked when default thread for middleware has finished processing.

+     */

+    typedef void (MAMACALLTYPE *mamaStopCB) (mama_status);

+

+    /**

      * Start Mama in the background. This method invokes mama_start() in a

      * separate thread.

      *

+     * This API uses the deprecated mamaStartCB callback type. mama_startBackgroundEx

+     * uses the replacement type mamaStopCBEx. To retain backward compatability

+     * mama_startBackground casts callback to mamaStopCB for further processing.

+     *

      * @param bridgeImpl The bridge specific structure.

      * @param callback The callback for asynchronous status.

      * @return MAMA_STATUS_OK if successful.

@@ -335,6 +344,27 @@ extern "C"

     extern mama_status

     mama_startBackground (mamaBridge    bridgeImpl,

                           mamaStartCB   callback);

+    /**

+     * Extended stop callback that improves on mamaStopCB by including a bridge impl

+     * pointer and closure in the signature.

+     */

+    typedef void (MAMACALLTYPE *mamaStopCBEx) (mama_status, mamaBridge, void*);

+

+    /**

+     * Start Mama in the background, with extended parameters.

+     *

+     * This method performs the same functionality as mama_startBackground accept it

+     * provides the facility to pass in a closure. The C++ wrapper layer uses this

+     * version of the function, and stores the MamaStartCallback object in the closure.

+     *

+     * @param[in] bridgeImpl The bridge specific structure.

+     * @param[in] callback The extended callback for asynchronous status.

+     * @return MAMA_STATUS_OK if successful.

+     */

+    MAMAExpDLL

+    extern mama_status mama_startBackgroundEx (mamaBridge   bridgeImpl,

+                                               mamaStopCBEx callback,

+                                               void*        closure);

     /**

      * Stop dispatching on the default event queue for the specified bridge.

diff --git a/mama/c_cpp/src/c/transportimpl.h b/mama/c_cpp/src/c/transportimpl.h

index a3115b1..ac4c3c2 100644

--- a/mama/c_cpp/src/c/transportimpl.h

+++ b/mama/c_cpp/src/c/transportimpl.h

@@ -252,6 +252,11 @@ mama_status mamaTransport_addPublisher(mamaTransport transport, mamaPublisher pu

mama_status mamaTransport_removePublisher(mamaTransport transport, void *handle);

preInitialScheme mamaTransportImpl_getPreInitialScheme (mamaTransport transport);

+dqStartegyScheme

+mamaTransportImpl_getDqStrategyScheme (mamaTransport transport);

+

+dqftStrategyScheme

+mamaTransportImpl_getFtStrategyScheme (mamaTransport transport);

/**

  * This function will allocate an internal transport for use with the internal event queue, this sort

  * of transport is limited and does not support certain features, including

diff --git a/mama/c_cpp/src/cpp/mamacpp.cpp b/mama/c_cpp/src/cpp/mamacpp.cpp

index ddfd3a0..5494300 100644

--- a/mama/c_cpp/src/cpp/mamacpp.cpp

+++ b/mama/c_cpp/src/cpp/mamacpp.cpp

@@ -162,24 +162,18 @@ namespace Wombat

         mamaTry (mama_start (bridgeImpl));

     }

-    static MamaStartCallback* gMamaStartCallback = NULL;

-

     extern "C"

     {

-        void MAMACALLTYPE startCb (mama_status status)

+        void MAMACALLTYPE stopCb (mama_status status, mamaBridge, void* closure)

         {

-            if (gMamaStartCallback != NULL)

-            {

-                gMamaStartCallback->onStartComplete (MamaStatus (status));

-            }

+            static_cast<MamaStartCallback*>(closure)->onStartComplete(MamaStatus(status));

         }

     }

     void Mama::startBackground (mamaBridge bridgeImpl,

                                 MamaStartCallback* cb)

     {

-        gMamaStartCallback = cb;

-        mamaTry (mama_startBackground (bridgeImpl, startCb));

+        mamaTry (mama_startBackgroundEx (bridgeImpl, mamaStopCBEx(stopCb), static_cast<void*>(cb)));

     }

     void Mama::stop (mamaBridge bridgeImpl)

--

1.7.9.5

 




Please consider the environment before printing this e-mail.

This e-mail may contain confidential and/or privileged information. If you are not the intended recipient or have received this e-mail in error, please advise the sender immediately by reply e-mail and delete this message and any attachments without retaining a copy.

Any unauthorised copying, disclosure or distribution of the material in this e-mail is strictly forbidden.

Join Openmama-dev@lists.openmama.org to automatically receive all group messages.