Beispiel #1
0
CONSTMAP_HANDLE ConstMap_Create(MAP_HANDLE sourceMap)
{
    CONSTMAP_HANDLE_DATA* result = REFCOUNT_TYPE_CREATE(CONSTMAP_HANDLE_DATA);

	if (result == NULL)
	{
		LOG_CONSTMAP_ERROR(CONSTMAP_INVALIDARG);
	}
	else
    {
		/*Codes_SRS_CONSTMAP_17_048: [ConstMap_Create shall accept any non-NULL MAP_HANDLE as input.]*/
		/*Codes_SRS_CONSTMAP_17_001: [ConstMap_Create shall create an immutable map, populated by the key, value pairs in the source map.]*/
        result->map = Map_Clone(sourceMap);
        if (result->map == NULL)
        {
            free(result);
			/*Codes_SRS_CONSTMAP_17_002: [If during creation there are any errors, then ConstMap_Create shall return NULL.]*/
            result = NULL;
			LOG_CONSTMAP_ERROR(CONSTMAP_ERROR);
        }

    }
	/*Codes_SRS_CONSTMAP_17_003: [Otherwise, it shall return a non-NULL handle that can be used in subsequent calls.]*/
    return (CONSTMAP_HANDLE)result;
}
POS_HANDLE Pos_Create(int x)
{
    pos* result = REFCOUNT_TYPE_CREATE(pos);
    if (result != NULL)
    {
        result->x = x;
    }
    return result;
}
Beispiel #3
0
MESSAGE_HANDLE Message_Create(const MESSAGE_CONFIG * cfg)
{
    MESSAGE_HANDLE_DATA* result;
    /*Codes_SRS_MESSAGE_02_002: [If cfg is NULL then Message_Create shall return NULL.]*/
    if (cfg == NULL)
    {
        result = NULL;
        LogError("invalid parameter (NULL).");
    }
    /*Codes_SRS_MESSAGE_02_003: [If field source of cfg is NULL and size is not zero, then Message_Create shall fail and return NULL.] */
    else if ((cfg->size > 0) && (cfg->source == NULL))
    {
        result = NULL;
        LogError("invalid parameter combination cfg->size=%zd, cfg->source=%p", cfg->size, cfg->source);
    }
    else
    {
        /*Codes_SRS_MESSAGE_02_006: [Otherwise, Message_Create shall return a non-NULL handle and shall set the internal ref count to "1".]*/
        result = REFCOUNT_TYPE_CREATE(MESSAGE_HANDLE_DATA);
        if (result == NULL)
        {
            LogError("malloc returned NULL");
            /*return as is*/
        }
        else
        {
            /*Codes_SRS_MESSAGE_02_004: [Mesages shall be allowed to be created from zero-size content.]*/
			/*Codes_SRS_MESSAGE_02_015: [The MESSAGE_CONTENT's field size shall have the same value as the cfg's field size.]*/
			/*Codes_SRS_MESSAGE_17_003: [Message_Create shall copy the source to a readonly CONSTBUFFER.]*/
			result->content = CONSTBUFFER_Create(cfg->source, cfg->size);
			if (result->content == NULL)
			{
				LogError("CONSBUFFER Create failed");
				free(result);
				result = NULL;
			}
			else
			{
				/*Codes_SRS_MESSAGE_02_019: [Message_Create shall clone the sourceProperties to a readonly CONSTMAP.]*/
				result->properties = ConstMap_Create(cfg->sourceProperties);
				if (result->properties == NULL)
				{
					/*Codes_SRS_MESSAGE_02_005: [If Message_Create encounters an error while building the internal structures of the message, then it shall return NULL.] */
					LogError("ConstMap_Create failed");
					CONSTBUFFER_Destroy(result->content);
					free(result);
					result = NULL;
				}
				else
				{
					/*all is fine, return as is.*/
				}
			}
        }
    }
    return (MESSAGE_HANDLE)result;
}
static CONSTBUFFER_HANDLE CONSTBUFFER_Create_Internal(const unsigned char* source, size_t size)
{
    CONSTBUFFER_HANDLE result;
    /*Codes_SRS_CONSTBUFFER_02_005: [The non-NULL handle returned by CONSTBUFFER_Create shall have its ref count set to "1".]*/
    /*Codes_SRS_CONSTBUFFER_02_010: [The non-NULL handle returned by CONSTBUFFER_CreateFromBuffer shall have its ref count set to "1".]*/
    result = REFCOUNT_TYPE_CREATE(CONSTBUFFER_HANDLE_DATA);
    if (result == NULL)
    {
        /*Codes_SRS_CONSTBUFFER_02_003: [If creating the copy fails then CONSTBUFFER_Create shall return NULL.]*/
        /*Codes_SRS_CONSTBUFFER_02_008: [If copying the content fails, then CONSTBUFFER_CreateFromBuffer shall fail and return NULL.] */
        LogError("unable to malloc");
        /*return as is*/
    }
    else
    {
        /*Codes_SRS_CONSTBUFFER_02_002: [Otherwise, CONSTBUFFER_Create shall create a copy of the memory area pointed to by source having size bytes.]*/
        result->alias.size = size;
        if (size == 0)
        {
            result->alias.buffer = NULL;
        }
        else
        {
            unsigned char* temp = malloc(size);
            if (temp == NULL)
            {
                /*Codes_SRS_CONSTBUFFER_02_003: [If creating the copy fails then CONSTBUFFER_Create shall return NULL.]*/
                /*Codes_SRS_CONSTBUFFER_02_008: [If copying the content fails, then CONSTBUFFER_CreateFromBuffer shall fail and return NULL.] */
                LogError("unable to malloc");
                free(result);
                result = NULL;
            }
            else
            {

                /*Codes_SRS_CONSTBUFFER_02_004: [Otherwise CONSTBUFFER_Create shall return a non-NULL handle.]*/
                /*Codes_SRS_CONSTBUFFER_02_007: [Otherwise, CONSTBUFFER_CreateFromBuffer shall copy the content of buffer.]*/
                /*Codes_SRS_CONSTBUFFER_02_009: [Otherwise, CONSTBUFFER_CreateFromBuffer shall return a non-NULL handle.]*/
                memcpy(temp, source, size);
                result->alias.buffer = temp;
            }
        }
    }
    return result;
}
CONSTBUFFER_ARRAY_HANDLE constbuffer_array_create_empty(void)
{
    CONSTBUFFER_ARRAY_HANDLE result;

    /*Codes_SRS_CONSTBUFFER_ARRAY_02_004: [ constbuffer_array_create_empty shall allocate memory for a new CONSTBUFFER_ARRAY_HANDLE. ]*/
    result = REFCOUNT_TYPE_CREATE(CONSTBUFFER_ARRAY_HANDLE_DATA); /*explicit 0*/
    if (result == NULL)
    {
        /*Codes_SRS_CONSTBUFFER_ARRAY_02_001: [ If are any failure is encountered, `constbuffer_array_create_empty` shall fail and return `NULL`. ]*/
        LogError("failure allocating const buffer array");
        /*return as is*/
    }
    else
    {
        /*Codes_SRS_CONSTBUFFER_ARRAY_02_041: [ constbuffer_array_create_empty shall succeed and return a non-NULL value. ]*/
        result->nBuffers = 0;
    }
    return result;
}
BROKER_HANDLE Broker_Create(void)
{
    BROKER_HANDLE_DATA* result;

    /*Codes_SRS_BROKER_13_067: [Broker_Create shall malloc a new instance of BROKER_HANDLE_DATA and return NULL if it fails.]*/
    result = REFCOUNT_TYPE_CREATE(BROKER_HANDLE_DATA);
    if (result == NULL)
    {
        LogError("malloc returned NULL");
        /*return as is*/
    }
    else
    {
        /*Codes_SRS_BROKER_13_007: [Broker_Create shall initialize BROKER_HANDLE_DATA::modules with a valid VECTOR_HANDLE.]*/
        result->modules = singlylinkedlist_create();
        if (result->modules == NULL)
        {
            /*Codes_SRS_BROKER_13_003: [This function shall return NULL if an underlying API call to the platform causes an error.]*/
            LogError("VECTOR_create failed");
            free(result);
            result = NULL;
        }
        else
        {
            /*Codes_SRS_BROKER_13_023: [Broker_Create shall initialize BROKER_HANDLE_DATA::modules_lock with a valid LOCK_HANDLE.]*/
            result->modules_lock = Lock_Init();
            if (result->modules_lock == NULL)
            {
                /*Codes_SRS_BROKER_13_003: [This function shall return NULL if an underlying API call to the platform causes an error.]*/
                LogError("Lock_Init failed");
                singlylinkedlist_destroy(result->modules);
                free(result);
                result = NULL;
            }
            else
            {
                /*Codes_SRS_BROKER_17_001: [ Broker_Create shall initialize a socket for publishing messages. ]*/
                result->publish_socket = nn_socket(AF_SP, NN_PUB);
                if (result->publish_socket < 0)
                {
                    /*Codes_SRS_BROKER_13_003: [ This function shall return NULL if an underlying API call to the platform causes an error. ]*/
                    LogError("nanomsg puclish socket create failedL %d", result->publish_socket);
                    singlylinkedlist_destroy(result->modules);
                    Lock_Deinit(result->modules_lock);
                    free(result);
                    result = NULL;
                }
                else
                {
                    result->url = construct_url();
                    if (result->url == NULL)
                    {
                        /*Codes_SRS_BROKER_13_003: [ This function shall return NULL if an underlying API call to the platform causes an error. ]*/
                        singlylinkedlist_destroy(result->modules);
                        Lock_Deinit(result->modules_lock);
                        nn_close(result->publish_socket);
                        free(result);
                        LogError("Unable to generate unique url.");
                        result = NULL;
                    }
                    else
                    {
                        /*Codes_SRS_BROKER_17_004: [ Broker_Create shall bind the socket to the BROKER_HANDLE_DATA::url. ]*/
                        if (nn_bind(result->publish_socket, STRING_c_str(result->url)) < 0)
                        {
                            /*Codes_SRS_BROKER_13_003: [ This function shall return NULL if an underlying API call to the platform causes an error. ]*/
                            LogError("nanomsg bind failed");
                            singlylinkedlist_destroy(result->modules);
                            Lock_Deinit(result->modules_lock);
                            nn_close(result->publish_socket);
                            STRING_delete(result->url);                
                            free(result);
                            result = NULL;
                        }
                    }
                }
            }
        }
    }

    /*Codes_SRS_BROKER_13_001: [This API shall yield a BROKER_HANDLE representing the newly created message broker. This handle value shall not be equal to NULL when the API call is successful.]*/
    return result;
}
Beispiel #7
0
MESSAGE_HANDLE Message_CreateFromBuffer(const MESSAGE_BUFFER_CONFIG* cfg)
{
	MESSAGE_HANDLE_DATA* result;
	/*Codes_SRS_MESSAGE_17_008: [ If cfg is NULL then Message_CreateFromBuffer shall return NULL.] */
	if (cfg == NULL)
	{
		result = NULL;
		LogError("invalid parameter (NULL).");
	}
	/* Codes_SRS_MESSAGE_17_009: [If field sourceContent of cfg is NULL, then Message_CreateFromBuffer shall fail and return NULL.]*/
	else if (cfg->sourceContent == NULL)
	{
		result = NULL;
		LogError("invalid CONSTBUFFER (NULL)");
	}
	/*Codes_SRS_MESSAGE_17_010: [If field sourceProperties of cfg is NULL, then Message_CreateFromBuffer shall fail and return NULL.]*/
	else if (cfg->sourceProperties == NULL)
	{
		result = NULL;
		LogError("invalid properties (NULL)");
	}
	else
	{
		/*Codes_SRS_MESSAGE_17_011: [If Message_CreateFromBuffer encounters an error while building the internal structures of the message, then it shall return NULL.]*/
		/*Codes_SRS_MESSAGE_17_014: [On success, Message_CreateFromBuffer shall return a non-NULL handle and set the internal ref count to "1".]*/
		result = REFCOUNT_TYPE_CREATE(MESSAGE_HANDLE_DATA);
		if (result == NULL)
		{
			LogError("malloc returned NULL");
			/*return as is*/
		}
		else
		{
			/*Codes_SRS_MESSAGE_17_013: [Message_CreateFromBuffer shall clone the CONSTBUFFER sourceBuffer.]*/
			result->content = CONSTBUFFER_Clone(cfg->sourceContent);
			if (result->content == NULL)
			{
				LogError("CONSBUFFER Clone failed");
				free(result);
				result = NULL;
			}
			else
			{
				/*Codes_SRS_MESSAGE_17_012: [Message_CreateFromBuffer shall copy the sourceProperties to a readonly CONSTMAP.]*/
				result->properties = ConstMap_Create(cfg->sourceProperties);
				if (result->properties == NULL)
				{
					LogError("ConstMap_Create failed");
					CONSTBUFFER_Destroy(result->content);
					free(result);
					result = NULL;
				}
				else
				{
					/*all is fine, return as is.*/
				}
			}
		}
	}
	return (MESSAGE_HANDLE)result;
}