[PATCH 1/2] [MAMA ALL] New accessor mamaPublisher_getTransport()


Gary Molloy <g.molloy@...>
 

Testing Strategy

--------------------

 

Modify the relevant mamaPublisher example applications to add the following (after you have created the publisher):

 

C

 

    mamaTransport aTransport = NULL;

    status = mamaPublisher_getTransport (gPublisher, &aTransport);

    if (MAMA_STATUS_OK == status)

    {

        const char* tportName = NULL;

        mamaTransport_getName (aTransport, &tportName);

        printf ("TEST: getTransport: %s\n", tportName);

    }

 

 

CPP

 

    MamaTransport* aTransport = NULL;

 

   aTransport = new MamaTransport ();

    aTransport = publisher.getTransport ();

    const char* tportName;

    tportName = aTransport ->getName ();

    printf ("TEST: getTransport: %s\n", tportName);

 

 

JNI

 

    MamaTransport aTransport = new MamaTransport();

    aTransport = myPublisher.getTransport();

    String tportName = aTransport.getName();

    System.out.println( "TEST: getTransport: " + tportName + "\n");

 

 

C#

 

    MamaTransport aTransport = new MamaTransport();

    aTransport = publisher.getTransport();

    string tportName = aTransport.getName();

    Console.WriteLine("TEST: getTransport: {0}", tportName);

 

From each you can expected the following output, for example:

 

$ mamapublisherc -m wmw -tport tcp_sub

Created inbound subscription.

TEST: getTransport: tcp_sub

Publishing message to MAMA_TOPIC.

 

 

There is also an update to the unittests.

Running UnitTestMamaC you can expect to see the following:

 

...

...

[----------] 2 tests from MamaPublisherTestC

[ RUN      ] MamaPublisherTestC.CreateDestroy

[       OK ] MamaPublisherTestC.CreateDestroy (16 ms)

[ RUN      ] MamaPublisherTestC.GetTransport                   <-------------- new unittest

[       OK ] MamaPublisherTestC.GetTransport (16 ms)

[----------] 2 tests from MamaPublisherTestC (32 ms total)

...

...

 

======================================================================================================================

 

From dbb860193ac5fd07c7116eb479fc41360b12b6cd Mon Sep 17 00:00:00 2001

From: Gary Molloy <g.molloy@...>

Date: Thu, 21 May 2015 17:27:10 +0100

Subject: [PATCH 1/2] [MAMA ALL] New accessor mamaPublisher_getTransport()

 

Replace the existing mamaPublisherImpl_getTransportImpl() function and add for all languages

 

Signed-off-by: Gary Molloy <g.molloy@...>

---

mama/c_cpp/src/c/mama/publisher.h               | 15 +++++++

mama/c_cpp/src/c/publisher.c                    | 12 ++++--

mama/c_cpp/src/c/publisherimpl.h                |  3 --

mama/c_cpp/src/cpp/MamaPublisher.cpp            | 10 ++++-

mama/c_cpp/src/cpp/mama/MamaPublisher.h         |  8 +++-

mama/c_cpp/src/gunittest/c/publishertest.cpp    | 33 ++++++++++++++++

mama/dotnet/src/cs/MamaPublisher.cs             | 32 ++++++++++++++-

mama/jni/src/c/mamapublisherjni.c               | 52 ++++++++++++++++++++++++-

mama/jni/src/com/wombat/mama/MamaPublisher.java | 28 +++++++++++++

mama/jni/src/com/wombat/mama/MamaTransport.java |  5 +++

10 files changed, 185 insertions(+), 13 deletions(-)

 

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

index bb192c6..a4bbde1 100644

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

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

@@ -181,6 +181,21 @@ MAMAExpDLL

extern mama_status

mamaPublisher_destroy (mamaPublisher publisher);

+/**

+ * @brief Return the <code>mamaTransport</code> for this publisher.

+ *

+ * @param[in] publisher     The mamaPublisher from which to retrieve the transport for.

+ * @param[out] transport    A pointer to hold the transport.

+ *

+ * @return mama_status return code can be one of:

+ *              MAMA_STATUS_NULL_ARG

+ *              MAMA_STATUS_OK

+ */

+MAMAExpDLL

+extern mama_status

+mamaPublisher_getTransport (mamaPublisher   publisher,

+                            mamaTransport*  transport);

+

#if defined( __cplusplus )

}

#endif /* defined( __cplusplus ) */

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

index 2efd88d..89e4f5d 100644

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

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

@@ -600,11 +600,15 @@ mama_status mamaPublisherImpl_clearTransport (mamaPublisher publisher)

     return mamaPublisherImpl_destroy((mamaPublisherImpl *)publisher);

}

-mamaTransport

-mamaPublisherImpl_getTransportImpl (mamaPublisher publisher)

+mama_status

+mamaPublisher_getTransport (mamaPublisher   publisher,

+                            mamaTransport*  transport)

{

     mamaPublisherImpl* impl   = (mamaPublisherImpl*)publisher;

-    if (!impl) return NULL;

-    return impl->mTport;

+    if (!impl) return MAMA_STATUS_NULL_ARG;

+    *transport = impl->mTport;

+

+    return MAMA_STATUS_OK;

}

+

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

index b9b9c43..f336fd8 100644

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

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

@@ -44,9 +44,6 @@ mamaPublisher_sendFromInboxByIndex (mamaPublisher publisher,

 mama_status mamaPublisherImpl_clearTransport (mamaPublisher publisher);

-extern mamaTransport

-mamaPublisherImpl_getTransportImpl (mamaPublisher publisher);

-

#if defined(__cplusplus)

}

#endif

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

index 9e68f80..1a130a6 100644

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

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

@@ -119,11 +119,11 @@ namespace Wombat

     }

     MamaPublisher::MamaPublisher (MamaPublisherImpl* impl)

-        : mPimpl (impl)

+        : mPimpl (impl)

     {

     }

-    MamaPublisher::MamaPublisher (void)

+    MamaPublisher::MamaPublisher (void)

         : mPimpl (new MamaPublisherImpl (this))

     {

     }

@@ -134,6 +134,7 @@ namespace Wombat

                                 const char*        root)

     {

         mPimpl->create (transport, topic, source, root);

+        mTransport = transport;

     }

     void MamaPublisher::send (MamaMsg* msg) const

@@ -178,6 +179,11 @@ namespace Wombat

         mPimpl->destroy ();

     }

+    MamaTransport* MamaPublisher::getTransport (void) const

+    {

+        return mTransport;

+    }

+

     void MamaPublisherImpl::send (MamaMsg *msg) const

     {

         mamaTry (mamaPublisher_send (mPublisher, msg->getUnderlyingMsg ()));

diff --git a/mama/c_cpp/src/cpp/mama/MamaPublisher.h b/mama/c_cpp/src/cpp/mama/MamaPublisher.h

index 1f6c9da..ed6be6e 100644

--- a/mama/c_cpp/src/cpp/mama/MamaPublisher.h

+++ b/mama/c_cpp/src/cpp/mama/MamaPublisher.h

@@ -89,10 +89,16 @@ namespace Wombat

         virtual void destroy (void);

+        /**

+         * Return the MamaTransport for the MamaPublisher.

+         */

+        virtual MamaTransport* getTransport (void) const;

+

     protected:

         MamaPublisher (MamaPublisherImpl*);

-       

+

         MamaPublisherImpl*  mPimpl;

+        MamaTransport*      mTransport;

     };

 } // namespace Wombat

diff --git a/mama/c_cpp/src/gunittest/c/publishertest.cpp b/mama/c_cpp/src/gunittest/c/publishertest.cpp

index 651613a..e58e24d 100644

--- a/mama/c_cpp/src/gunittest/c/publishertest.cpp

+++ b/mama/c_cpp/src/gunittest/c/publishertest.cpp

@@ -143,3 +143,36 @@ TEST_F (MamaPublisherTestC, DISABLED_Send)

     ASSERT_EQ (MAMA_STATUS_OK, mama_close());

}

+/*  Description:  Get the transport from the mamaPublisher and make sure its the same transport

+ *                that was used to create the publisher.

+ *

+ *  Expected Result: MAMA_STATUS_OK

+ */

+TEST_F (MamaPublisherTestC, GetTransport)

+{

+    mamaPublisher    publisher  = NULL;

+    mamaTransport    tport      = NULL;

+    const char*      source     = "SRC";

+    mamaTransport    aTransport = NULL;

+

+    ASSERT_EQ (MAMA_STATUS_OK,

+               mamaTransport_allocate (&tport));

+

+    ASSERT_EQ (MAMA_STATUS_OK,

+               mamaTransport_create (tport, "test_tport", mBridge));

+

+    ASSERT_EQ (MAMA_STATUS_OK,

+               mamaPublisher_create (&publisher, tport, source, NULL, NULL));

+

+    ASSERT_EQ (MAMA_STATUS_OK,

+               mamaPublisher_getTransport (publisher, &aTransport));

+

+    ASSERT_EQ (tport, aTransport);

+

+    ASSERT_EQ (MAMA_STATUS_OK,

+               mamaPublisher_destroy (publisher));

+

+    ASSERT_EQ (MAMA_STATUS_OK,

+               mamaTransport_destroy (tport));

+}

+

diff --git a/mama/dotnet/src/cs/MamaPublisher.cs b/mama/dotnet/src/cs/MamaPublisher.cs

index 14d1be8..e5e312e 100644

--- a/mama/dotnet/src/cs/MamaPublisher.cs

+++ b/mama/dotnet/src/cs/MamaPublisher.cs

@@ -39,7 +39,8 @@ namespace Wombat

         /// when using the throttle.

         /// </summary>

         private MamaCallbackStore<MamaSendCompleteCallback, MamaThrottledSendCompleteDelegate> mCallbackStore;

-                              private MamaMsg mReusableMsg;

+        private MamaMsg mReusableMsg;

+        private MamaTransport mReusableTransport;

         #endregion

@@ -424,6 +425,30 @@ namespace Wombat

                                               }

                               }

+        /// <summary>

+        /// Get the MamaTransport.

+        /// The result contains the reusable MamaTransport object of the

+        /// nativeHandle object. Applications calling this method will receive the same

+        /// reusable object for repeated calls on same nativeHandle object.

+        /// </summary>

+        public MamaTransport getTransport()

+        {

+            EnsurePeerCreated();

+            IntPtr transport = IntPtr.Zero;

+            int code = NativeMethods.mamaPublisher_getTransport(nativeHandle, ref transport);

+            CheckResultCode(code);

+

+            if (mReusableTransport == null)

+            {

+                mReusableTransport = new MamaTransport (transport);

+            }

+            else

+            {

+                mReusableTransport.setNativeHandle(transport);

+            }

+            return mReusableTransport;

+        }

+

                               // Interop API

                               private struct NativeMethods

                               {

@@ -469,6 +494,11 @@ namespace Wombat

                                                [DllImport(Mama.DllName, CallingConvention = CallingConvention.Cdecl)]

                                               public static extern int mamaPublisher_destroy(IntPtr publisher);

+

+            [DllImport(Mama.DllName, CallingConvention = CallingConvention.Cdecl)]

+            public static extern int mamaPublisher_getTransport(

+                IntPtr publisher,

+                ref IntPtr result);

                               }

                                private Hashtable mCallbacks = new Hashtable();

diff --git a/mama/jni/src/c/mamapublisherjni.c b/mama/jni/src/c/mamapublisherjni.c

index aa1c4c6..e7172a9 100644

--- a/mama/jni/src/c/mamapublisherjni.c

+++ b/mama/jni/src/c/mamapublisherjni.c

@@ -49,13 +49,14 @@ typedef struct sendMsgCallback_

/******************************************************************************

* Global/Static variables

*******************************************************************************/

-static  jfieldID    publisherPointerFieldId_g   =   NULL;

+static jfieldID     publisherPointerFieldId_g       =  NULL;

+static jfieldID     mamaTransportObjectFieldId_g    =  NULL;

 extern jfieldID     transportPointerFieldId_g;

extern jfieldID     messagePointerFieldId_g;

extern jfieldID     inboxPointerFieldId_g;

-static  jmethodID   sendCallbackMethod_g         =   NULL;

+static jmethodID   sendCallbackMethod_g             =  NULL;

 

 extern  JavaVM*     javaVM_g;

@@ -75,6 +76,47 @@ static void MAMACALLTYPE sendCompleteCB (mamaPublisher publisher,

*******************************************************************************/

/*

  * Class:     com_wombat_mama_MamaPublisher

+ * Method:    _getTransport

+ * Signature: ()V

+ */

+JNIEXPORT void JNICALL Java_com_wombat_mama_MamaPublisher__1getTransport

+(JNIEnv * env, jobject this)

+{

+    jlong           publisherPointer    = 0;

+    mamaTransport   c_result            = NULL;

+    jobject         result              = NULL;

+    mama_status     status              = MAMA_STATUS_OK;

+    char            errorString[UTILS_MAX_ERROR_STRING_LENGTH];

+

+    assert(publisherPointerFieldId_g!=NULL);

+    publisherPointer = (*env)->GetLongField(env,this,publisherPointerFieldId_g);

+    MAMA_THROW_NULL_PARAMETER_RETURN_VOID(publisherPointer,

+        "Null parameter, MamaPublisher may have already been destroyed.");

+

+    if(MAMA_STATUS_OK!=(mamaPublisher_getTransport(

+                    CAST_JLONG_TO_POINTER(mamaPublisher,publisherPointer),

+                    &c_result)))

+    {

+         utils_buildErrorStringForStatus(

+                errorString, UTILS_MAX_ERROR_STRING_LENGTH,

+                "Failed to get mamaTransport from publisher.", status);

+        utils_throwExceptionForMamaStatus(env,status,errorString);

+    }

+

+    /*re-use the existing field object */

+    if (c_result)

+    {

+        result = (*env)->GetObjectField(env,this,

+                                        mamaTransportObjectFieldId_g);

+        assert(NULL!=result);/*throws an exception*/

+

+        (*env)->SetLongField(env, result, transportPointerFieldId_g,

+                             CAST_POINTER_TO_JLONG(c_result));

+    }

+}

+

+/*

+ * Class:     com_wombat_mama_MamaPublisher

  * Method:    _create

  * Signature: (Lcom/wombat/mama/MamaTransport;Ljava/lang/String;Ljava/lang/String;)V

  */

@@ -444,6 +486,12 @@ JNIEXPORT void JNICALL Java_com_wombat_mama_MamaPublisher_initIDs

             class, "publisherPointer_i", UTILS_JAVA_POINTER_TYPE_SIGNATURE);

     if (!publisherPointerFieldId_g) return;/*Exception auto thrown*/

+    /*The MamaTransport object.*/

+    mamaTransportObjectFieldId_g = (*env)->GetFieldID(env,

+                              class,"mamaTransport_i",

+                              "Lcom/wombat/mama/MamaTransport;");

+    if(!mamaTransportObjectFieldId_g) return;

+

     /* get our callback class */

     sendCallbackClass = (*env)->FindClass(env,

                 "com/wombat/mama/MamaThrottleCallback");

diff --git a/mama/jni/src/com/wombat/mama/MamaPublisher.java b/mama/jni/src/com/wombat/mama/MamaPublisher.java

index ccd39fb..7a3c0f3 100644

--- a/mama/jni/src/com/wombat/mama/MamaPublisher.java

+++ b/mama/jni/src/com/wombat/mama/MamaPublisher.java

@@ -35,6 +35,9 @@ public class MamaPublisher

     /*A long value containing a pointer to the underlying C publisher structure*/

     private long    publisherPointer_i   =   0;

+    /* Reusable MamaTransport object. */

+    private MamaTransport mamaTransport_i = null;

+

               public void create (MamaTransport transport, String topic)

     {

         _create(transport,topic,null);

@@ -81,6 +84,31 @@ public class MamaPublisher

         _sendFromInbox(inbox,msg);

     }

+    public MamaTransport getTransport ()

+    {

+        /* Calls the native method first. This will

+           reuse the reusable mamaTransport in MamaPublisher

+           Only create the MamaTransport if we actually need to

+        */

+        if (mamaTransport_i == null)

+        {

+            mamaTransport_i = new MamaTransport();

+        }

+

+        mamaTransport_i.setPointerVal(0);

+

+        _getTransport ();

+

+        if (mamaTransport_i.getPointerVal() == 0)

+        {

+            return null;

+        }

+

+        return mamaTransport_i;

+    }

+

+    private native void  _getTransport ();

+

     private native void _create (MamaTransport transport, String topic, String source);

     private native void _send (MamaMsg msg);

diff --git a/mama/jni/src/com/wombat/mama/MamaTransport.java b/mama/jni/src/com/wombat/mama/MamaTransport.java

index aef2282..a9c9651 100644

--- a/mama/jni/src/com/wombat/mama/MamaTransport.java

+++ b/mama/jni/src/com/wombat/mama/MamaTransport.java

@@ -410,6 +410,11 @@ public class MamaTransport

         return transportPointer_i;

     }

+    protected void setPointerVal(long pointerVal)

+    {

+        transportPointer_i = pointerVal;

+    }

+

     public void create (MamaBridge bridge)

     {

         myBridge = bridge;

--

2.1.0

 

 

 

Gary Molloy – SR Labs

Adelaide Exchange | 24-26 Adelaide Street | Belfast | BT2 8GD

g.molloy@...