/**
  Serializes the bytebuffer of any msg to file.
  The default delimeter (:) will be used.
  Each saved msg will be in the format SOURCE:TPORT:SYMBOL:MSGLEN:BUFFER
*/
mama_status mamaCapture_saveMamaMsg (mamaPlaybackCapture* mamaCapture,
                                     mamaMsg* msg)
{
    mamaCaptureConfigImpl* impl = (mamaCaptureConfigImpl*)*mamaCapture;

    const void* buffer       = NULL;
    mama_size_t bufferSize   = 0;
    const char*       temp   = NULL;
    mama_status status       = MAMA_STATUS_OK;
    char*       delim        = CAP_DELIMETER;
    char        bufferLength  [LENGTH];
    char        captureBuffer [MAX_BUFFER];

    if (impl == NULL) return MAMA_STATUS_NULL_ARG;


    if (MAMA_STATUS_OK != (status = mamaMsg_getByteBuffer
                           (*msg, &buffer, &bufferSize)))
    {
        mama_log (MAMA_LOG_LEVEL_NORMAL,
                  "mamaCapture_saveMamaMsg Error!!!:  " \
                  "failure at mamaMsg_getMsgByteBuffer \n");
        exit (0);
    }
    memset (bufferLength, '\0', LENGTH);

    /* Build up format and append bytebuffer */
    if(myPlaybackFile)
    {
        memset (captureBuffer,'\0', MAX_BUFFER);
        mamaCapture_getFeedSource(mamaCapture,&temp);
        strcat (captureBuffer, temp);
        strcat (captureBuffer, delim);
        mamaCapture_getTransportName (mamaCapture,&temp);
        strcat (captureBuffer, temp);
        strcat (captureBuffer, delim);
        mamaCapture_getSymbol (mamaCapture,&temp);
        strcat (captureBuffer, temp);
        strcat (captureBuffer, delim);
        memset (bufferLength, '\0', LENGTH);
        sprintf (bufferLength, "%u", bufferSize);
        strcat (captureBuffer, bufferLength);
        fwrite (captureBuffer, 1, strlen(captureBuffer), myPlaybackFile);
        fputc  ((char)29,myPlaybackFile);
        fwrite (buffer , bufferSize, 1, myPlaybackFile);
        fflush(myPlaybackFile);

    }
    else
    {
        mama_log(MAMA_LOG_LEVEL_NORMAL,
                 "mamaCapture_saveMamaMsg Error!!!: Capture file does not exist" \
                 "or has not been initialized\n");
        return MAMA_STATUS_NULL_ARG;
    }
    return MAMA_STATUS_OK;
}
Ejemplo n.º 2
0
mama_status
qpidBridgeMsgCodec_pack (msgBridge      bridgeMessage,
                         mamaMsg        target,
                         pn_message_t** protonMessage)
{
    pn_data_t*          properties      = NULL;
    pn_data_t*          body            = NULL;
    mamaPayloadType     payloadType     = MAMA_PAYLOAD_UNKNOWN;
    const void*         buffer          = NULL;
    mama_size_t         bufferLen       = 0;
    char*               subject         = NULL;
    char*               destination     = NULL;
    char*               inboxName       = NULL;
    char*               replyTo         = NULL;
    char*               targetSubject   = NULL;
    mama_status         status          = MAMA_STATUS_OK;
    qpidMsgType         type            = QPID_MSG_PUB_SUB;

    if (NULL == bridgeMessage || NULL == target)
    {
        return MAMA_STATUS_NULL_ARG;
    }

    /* Get the underlying payload type */
    mamaMsg_getPayloadType (target, &payloadType);

    /* If this is a qpid payload, we don't need to serialize */
    if (MAMA_PAYLOAD_QPID == payloadType)
    {
        /* This will extract only the underlying handle */
        mamaMsgImpl_getPayloadBuffer (target, &buffer, &bufferLen);

        /* Don't use function's proton message - use the one just extracted */
        *protonMessage = (pn_message_t*) buffer;
    }
    else
    {
        const void*    buffer      = NULL;
        mama_size_t    bufferLen   = 0;

        /* This will extract a serialized version of the payload */
        mamaMsg_getByteBuffer   (target, &buffer, &bufferLen);

        /* Use the function's proton message if this is not a qpid payload */
        body = pn_message_body  (*protonMessage);
        pn_data_put_binary      (body, pn_bytes(bufferLen, (char*)buffer));
    }

    /* Set the subject for the middleware according to the bridge msg */
    status = qpidBridgeMamaMsgImpl_getSendSubject (bridgeMessage, &subject);
    if (MAMA_STATUS_OK != status)
    {
        return status;
    }
    pn_message_set_subject (*protonMessage, subject);

    /* Set the URL destination for the middleware according to the bridge msg */
    status = qpidBridgeMamaMsgImpl_getDestination (bridgeMessage, &destination);
    if (MAMA_STATUS_OK != status)
    {
        return status;
    }
    pn_message_set_address (*protonMessage, destination);

    /* Get the properties from the message */
    properties = pn_message_properties (*protonMessage);

    /* Ensure position is at the start */
    pn_data_rewind (properties);

    /* Main container for meta data should be a list to allow expansion */
    pn_data_put_list (properties);

    /* Enter into the list for access to its elements */
    pn_data_enter (properties);

    /* Set the message type for the middleware according to the bridge msg */
    status = qpidBridgeMamaMsgImpl_getMsgType (bridgeMessage, &type);
    if (MAMA_STATUS_OK != status)
    {
        return status;
    }
    pn_data_put_ubyte (properties, type);


    switch (type)
    {
    /* For inbox requests, set inbox name and reply to URLs */
    case QPID_MSG_INBOX_REQUEST:

        status = qpidBridgeMamaMsgImpl_getInboxName (bridgeMessage, &inboxName);
        if (MAMA_STATUS_OK != status)
        {
            return status;
        }
        pn_data_put_string (properties, pn_bytes (strlen(inboxName),
                                                  inboxName));

        status = qpidBridgeMamaMsgImpl_getReplyTo (bridgeMessage, &replyTo);
        if (MAMA_STATUS_OK != status)
        {
            return status;
        }
        pn_data_put_string (properties, pn_bytes (strlen(replyTo),
                                                  replyTo));

        break;
    /* For inbox responses, set the target subject (e.g. initial for XX) */
    case QPID_MSG_INBOX_RESPONSE:
        status = qpidBridgeMamaMsgImpl_getTargetSubject (bridgeMessage,
                                                         &targetSubject);
        if (MAMA_STATUS_OK != status)
        {
            return status;
        }
        pn_data_put_string (properties, pn_bytes (strlen(targetSubject),
                                                  targetSubject));
        break;
    /* The following message types require no further meta data */
    case QPID_MSG_TERMINATE:
    case QPID_MSG_SUB_REQUEST:
    case QPID_MSG_PUB_SUB:
    default:
        break;
    }

    /* Exit out of the list previously entered */
    pn_data_exit (properties);

    return MAMA_STATUS_OK;
}
Ejemplo n.º 3
0
mama_status
zmqBridgeMamaMsgImpl_serialize (msgBridge      msg,
                                mamaMsg        source,
                                void**         target,
                                size_t*        size)
{
    zmqBridgeMsgImpl*  impl        = (zmqBridgeMsgImpl*) msg;
    mama_size_t msgSize = 0;
    const void* msgBuffer = NULL;
    size_t msgSubjectByteCount = 0;
    size_t msgInboxByteCount = 0;
    //size_t msgReplyToByteCount = 0;
    size_t msgTargetSubjectByteCount = 0;
    size_t serializedSize = 0;

    // Serialize payload
    mama_status status = mamaMsg_getByteBuffer (source, &msgBuffer, &msgSize);

    serializedSize = strlen(impl->mSendSubject) + 10 + msgSize;

    switch (impl->mMsgType)
    {
    case ZMQ_MSG_INBOX_REQUEST:
        serializedSize += strlen (impl->mReplyHandle.mInboxName) + 1;
        break;
    case ZMQ_MSG_INBOX_RESPONSE:
        serializedSize += strlen (impl->mTargetSubject) + 1;
        break;
    case ZMQ_MSG_SUB_REQUEST:
    case ZMQ_MSG_PUB_SUB:
    default:
        break;
    }

    allocateBufferMemory (&impl->mSerializedBuffer,
                          &impl->mSerializedBufferSize,
                          serializedSize);

    // Ok great - we have a buffer now of appropriate size, let's populate it
    uint8_t* bufferPos = (uint8_t*)impl->mSerializedBuffer;

    // Copy across the subject
    msgSubjectByteCount = strlen (impl->mSendSubject) + 1;
    memcpy (bufferPos, impl->mSendSubject, msgSubjectByteCount);
    bufferPos += msgSubjectByteCount;

    // Leave 8 bytes empty - receive side will be thankful for them
    memset ((void*)bufferPos, 0, 8);
    bufferPos += 8;

    // Copy across the message type
    *bufferPos = (uint8_t) impl->mMsgType;
    bufferPos++;

    switch (impl->mMsgType)
    {
    case ZMQ_MSG_INBOX_REQUEST:
        // Copy across inbox name
        msgInboxByteCount = strlen (impl->mReplyHandle.mInboxName) + 1;
        memcpy (bufferPos, impl->mReplyHandle.mInboxName, msgInboxByteCount);
        bufferPos += msgInboxByteCount;
        break;
    case ZMQ_MSG_INBOX_RESPONSE:
        msgTargetSubjectByteCount = strlen (impl->mTargetSubject) + 1;
        memcpy (bufferPos, impl->mTargetSubject, msgTargetSubjectByteCount);
        bufferPos += msgTargetSubjectByteCount;
        break;
    case ZMQ_MSG_SUB_REQUEST:
    case ZMQ_MSG_PUB_SUB:
    default:
        break;
    }

    // Copy across the payload
    memcpy ((void*)bufferPos, msgBuffer, msgSize);
    impl->mPayloadSize = msgSize;

    // Populate return pointers
    *target = impl->mSerializedBuffer;
    *size = serializedSize;

    return status;
}