static void Outprocess_Receive(MODULE_HANDLE moduleHandle, MESSAGE_HANDLE messageHandle)
{
	OUTPROCESS_HANDLE_DATA* handleData = moduleHandle;
	/*Codes_SRS_OUTPROCESS_MODULE_17_022: [ If module or message_handle is NULL, this function shall do nothing. ]*/
	if (handleData != NULL && messageHandle != NULL)
	{
		/*Codes_SRS_OUTPROCESS_MODULE_17_046: [ This function shall clone the message to ensure the message is kept allocated until forwarded to module host. ]*/
		MESSAGE_HANDLE queued_message = Message_Clone(messageHandle);
		if (queued_message == NULL)
		{
			LogError("unable to clone message");
		}
		else
		{
			/*Codes_SRS_OUTPROCESS_MODULE_17_045: [ This function shall ensure thread safety for the module data. ]*/
			if (Lock(handleData->handle_lock) != LOCK_OK)
			{
				LogError("unable to Lock handle data");
				Message_Destroy(queued_message);
			}
			else
			{
				/*Codes_SRS_OUTPROCESS_MODULE_17_047: [ This function shall push the message onto the end of the outgoing gateway message queue. ]*/
				if (MESSAGE_QUEUE_push(handleData->outgoing_messages, queued_message) != 0)
				{
					LogError("unable to queue the message");
					Message_Destroy(queued_message);
				}
				(void)Unlock(handleData->handle_lock);
			}
		}
	}
}
예제 #2
0
BROKER_RESULT Broker_Publish(BROKER_HANDLE broker, MODULE_HANDLE source, MESSAGE_HANDLE message)
{
    BROKER_RESULT result;
    /*Codes_SRS_BROKER_13_030: [If broker or message is NULL the function shall return BROKER_INVALIDARG.]*/
    if (broker == NULL || source == NULL || message == NULL)
    {
        result = BROKER_INVALIDARG;
        LogError("Broker handle, source, and/or message handle is NULL");
    }
    else
    {
        BROKER_HANDLE_DATA* broker_data = (BROKER_HANDLE_DATA*)broker;
        /*Codes_SRS_BROKER_17_022: [ Broker_Publish shall Lock the modules lock. ]*/
        if (Lock(broker_data->modules_lock) != LOCK_OK)
        {
            /*Codes_SRS_BROKER_13_053: [This function shall return BROKER_ERROR if an underlying API call to the platform causes an error or BROKER_OK otherwise.]*/
            LogError("Lock on broker_data->modules_lock failed");
            result = BROKER_ERROR;
        }
        else
        {
            int32_t msg_size;
            int32_t buf_size;
            /*Codes_SRS_BROKER_17_007: [ Broker_Publish shall clone the message. ]*/
            MESSAGE_HANDLE msg = Message_Clone(message);
            /*Codes_SRS_BROKER_17_008: [ Broker_Publish shall serialize the message. ]*/
            msg_size = Message_ToByteArray(message, NULL, 0);
            if (msg_size < 0)
            {
                /*Codes_SRS_BROKER_13_053: [This function shall return BROKER_ERROR if an underlying API call to the platform causes an error or BROKER_OK otherwise.]*/
                LogError("unable to serialize a message [%p]", msg);
                Message_Destroy(msg);
                result = BROKER_ERROR;
            }
            else
            {
                /*Codes_SRS_BROKER_17_025: [ Broker_Publish shall allocate a nanomsg buffer the size of the serialized message + sizeof(MODULE_HANDLE). ]*/
                buf_size = msg_size + sizeof(MODULE_HANDLE);
                void* nn_msg = nn_allocmsg(buf_size, 0);
                if (nn_msg == NULL)
                {
                    /*Codes_SRS_BROKER_13_053: [This function shall return BROKER_ERROR if an underlying API call to the platform causes an error or BROKER_OK otherwise.]*/
                    LogError("unable to serialize a message [%p]", msg);
                    result = BROKER_ERROR;
                }
                else
                {
                    /*Codes_SRS_BROKER_17_026: [ Broker_Publish shall copy source into the beginning of the nanomsg buffer. ]*/
                    unsigned char *nn_msg_bytes = (unsigned char *)nn_msg;
                    memcpy(nn_msg_bytes, &source, sizeof(MODULE_HANDLE));
                    /*Codes_SRS_BROKER_17_027: [ Broker_Publish shall serialize the message into the remainder of the nanomsg buffer. ]*/
                    nn_msg_bytes += sizeof(MODULE_HANDLE);
                    Message_ToByteArray(message, nn_msg_bytes, msg_size);

                    /*Codes_SRS_BROKER_17_010: [ Broker_Publish shall send a message on the publish_socket. ]*/
                    int nbytes = nn_send(broker_data->publish_socket, &nn_msg, NN_MSG, 0);
                    if (nbytes != buf_size)
                    {
                        /*Codes_SRS_BROKER_13_053: [This function shall return BROKER_ERROR if an underlying API call to the platform causes an error or BROKER_OK otherwise.]*/
                        LogError("unable to send a message [%p]", msg);
                        /*Codes_SRS_BROKER_17_012: [ Broker_Publish shall free the message. ]*/
                        nn_freemsg(nn_msg);
                        result = BROKER_ERROR;
                    }
                    else
                    {
                        result = BROKER_OK;
                    }
                }
                /*Codes_SRS_BROKER_17_012: [ Broker_Publish shall free the message. ]*/
                Message_Destroy(msg);
                /*Codes_SRS_BROKER_17_011: [ Broker_Publish shall free the serialized message data. ]*/
            }
            /*Codes_SRS_BROKER_17_023: [ Broker_Publish shall Unlock the modules lock. ]*/
            Unlock(broker_data->modules_lock);
        }

    }
    /*Codes_SRS_BROKER_13_037: [ This function shall return BROKER_ERROR if an underlying API call to the platform causes an error or BROKER_OK otherwise. ]*/
    return result;
}
예제 #3
0
/* Codes_SRS_BROKER_17_026: [ N/A - Broker_Publish shall copy source into the beginning of the nanomsg buffer. ] */
BROKER_RESULT
Broker_Publish (
    BROKER_HANDLE broker,
    MODULE_HANDLE source,
    MESSAGE_HANDLE message
) {
    (void)source;
    REMOTE_MODULE_HANDLE remote_module = (REMOTE_MODULE_HANDLE)broker;
    BROKER_RESULT result;

    /* Codes_SRS_BROKER_13_030: [If broker or message is NULL the function shall return BROKER_INVALIDARG.] */
    if (broker == NULL || message == NULL)
    {
        result = BROKER_INVALIDARG;
        LogError("Broker handle and/or message handle is NULL");
    }
    else
    {
        // Send message_ to nanomsg
        int32_t msg_size;
        int32_t buf_size;
        /* Codes_SRS_BROKER_17_007: [ Broker_Publish shall clone the message. ] */
        MESSAGE_HANDLE msg = Message_Clone(message);
        /* Codes_SRS_BROKER_17_008: [ Broker_Publish shall serialize the message. ] */
        msg_size = Message_ToByteArray(message, NULL, 0);
        if (msg_size < 0)
        {
            /* Codes_SRS_BROKER_13_037: [ This function shall return BROKER_ERROR if an underlying API call to the platform causes an error or BROKER_OK otherwise. ] */
            LogError("unable to serialize a message [%p]", msg);
            Message_Destroy(msg);
            result = BROKER_ERROR;
        }
        else
        {
            /* Codes_SRS_BROKER_17_025: [ Broker_Publish shall allocate a nanomsg buffer the size of the serialized message + sizeof(MODULE_HANDLE). ] */
            buf_size = msg_size;
            void* nn_msg = nn_allocmsg(buf_size, 0);
            if (nn_msg == NULL)
            {
                /* Codes_SRS_BROKER_13_037: [ This function shall return BROKER_ERROR if an underlying API call to the platform causes an error or BROKER_OK otherwise. ] */
                LogError("unable to serialize a message [%p]", msg);
                result = BROKER_ERROR;
            }
            else
            {
                unsigned char *nn_msg_bytes = (unsigned char *)nn_msg;
                /* Codes_SRS_BROKER_17_027: [ Broker_Publish shall serialize the message into the remainder of the nanomsg buffer. ] */
                Message_ToByteArray(message, nn_msg_bytes, msg_size);

                /* Codes_SRS_BROKER_17_010: [ Broker_Publish shall send a message on the publish_socket. ] */
                int nbytes = nn_really_send(remote_module->message_socket, &nn_msg, NN_MSG, 0);
                if (nbytes != buf_size)
                {
                    /* Codes_SRS_BROKER_13_037: [ This function shall return BROKER_ERROR if an underlying API call to the platform causes an error or BROKER_OK otherwise. ] */
                    LogError("unable to send a message [%p]", msg);
                    /* Codes_SRS_BROKER_17_012: [ Broker_Publish shall free the message. ] */
                    nn_freemsg(nn_msg);
                    result = BROKER_ERROR;
                }
                else
                {
                    result = BROKER_OK;
                }
            }
            /* Codes_SRS_BROKER_17_012: [ Broker_Publish shall free the message. ] */
            Message_Destroy(msg);
            /* Codes_SRS_BROKER_17_011: [ Broker_Publish shall free the serialized message data. ] */
        }

    }

    /* Codes_SRS_BROKER_13_037: [ This function shall return BROKER_ERROR if an underlying API call to the platform causes an error or BROKER_OK otherwise. ] */
    return result;
}