Blame winpr/libwinpr/utils/collections/PubSub.c

Packit 1fb8d4
/**
Packit 1fb8d4
 * WinPR: Windows Portable Runtime
Packit 1fb8d4
 * Publisher/Subscriber Pattern
Packit 1fb8d4
 *
Packit 1fb8d4
 * Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
Packit 1fb8d4
 *
Packit 1fb8d4
 * Licensed under the Apache License, Version 2.0 (the "License");
Packit 1fb8d4
 * you may not use this file except in compliance with the License.
Packit 1fb8d4
 * You may obtain a copy of the License at
Packit 1fb8d4
 *
Packit 1fb8d4
 *     http://www.apache.org/licenses/LICENSE-2.0
Packit 1fb8d4
 *
Packit 1fb8d4
 * Unless required by applicable law or agreed to in writing, software
Packit 1fb8d4
 * distributed under the License is distributed on an "AS IS" BASIS,
Packit 1fb8d4
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Packit 1fb8d4
 * See the License for the specific language governing permissions and
Packit 1fb8d4
 * limitations under the License.
Packit 1fb8d4
 */
Packit 1fb8d4
Packit 1fb8d4
#ifdef HAVE_CONFIG_H
Packit 1fb8d4
#include "config.h"
Packit 1fb8d4
#endif
Packit 1fb8d4
Packit 1fb8d4
#include <winpr/crt.h>
Packit 1fb8d4
Packit 1fb8d4
#include <winpr/collections.h>
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Events (C# Programming Guide)
Packit 1fb8d4
 * http://msdn.microsoft.com/en-us/library/awbftdfh.aspx
Packit 1fb8d4
 */
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Properties
Packit 1fb8d4
 */
Packit 1fb8d4
Packit 1fb8d4
wEventType* PubSub_GetEventTypes(wPubSub* pubSub, int* count)
Packit 1fb8d4
{
Packit 1fb8d4
	if (count)
Packit 1fb8d4
		*count = pubSub->count;
Packit 1fb8d4
Packit 1fb8d4
	return pubSub->events;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Methods
Packit 1fb8d4
 */
Packit 1fb8d4
Packit 1fb8d4
void PubSub_Lock(wPubSub* pubSub)
Packit 1fb8d4
{
Packit 1fb8d4
	EnterCriticalSection(&pubSub->lock);
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
void PubSub_Unlock(wPubSub* pubSub)
Packit 1fb8d4
{
Packit 1fb8d4
	LeaveCriticalSection(&pubSub->lock);
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
wEventType* PubSub_FindEventType(wPubSub* pubSub, const char* EventName)
Packit 1fb8d4
{
Packit 1fb8d4
	int index;
Packit 1fb8d4
	wEventType* event = NULL;
Packit 1fb8d4
Packit 1fb8d4
	for (index = 0; index < pubSub->count; index++)
Packit 1fb8d4
	{
Packit 1fb8d4
		if (strcmp(pubSub->events[index].EventName, EventName) == 0)
Packit 1fb8d4
		{
Packit 1fb8d4
			event = &(pubSub->events[index]);
Packit 1fb8d4
			break;
Packit 1fb8d4
		}
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	return event;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
void PubSub_AddEventTypes(wPubSub* pubSub, wEventType* events, int count)
Packit 1fb8d4
{
Packit 1fb8d4
	if (pubSub->synchronized)
Packit 1fb8d4
		PubSub_Lock(pubSub);
Packit 1fb8d4
Packit 1fb8d4
	while (pubSub->count + count >= pubSub->size)
Packit 1fb8d4
	{
Packit 1fb8d4
		int new_size;
Packit Service 5a9772
		wEventType* new_event;
Packit 1fb8d4
Packit 1fb8d4
		new_size = pubSub->size * 2;
Packit Service 5a9772
		new_event = (wEventType*)realloc(pubSub->events, new_size * sizeof(wEventType));
Packit 1fb8d4
		if (!new_event)
Packit 1fb8d4
			return;
Packit 1fb8d4
		pubSub->size = new_size;
Packit 1fb8d4
		pubSub->events = new_event;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	CopyMemory(&pubSub->events[pubSub->count], events, count * sizeof(wEventType));
Packit 1fb8d4
	pubSub->count += count;
Packit 1fb8d4
Packit 1fb8d4
	if (pubSub->synchronized)
Packit 1fb8d4
		PubSub_Unlock(pubSub);
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
int PubSub_Subscribe(wPubSub* pubSub, const char* EventName, pEventHandler EventHandler)
Packit 1fb8d4
{
Packit 1fb8d4
	wEventType* event;
Packit 1fb8d4
	int status = -1;
Packit 1fb8d4
Packit 1fb8d4
	if (pubSub->synchronized)
Packit 1fb8d4
		PubSub_Lock(pubSub);
Packit 1fb8d4
Packit 1fb8d4
	event = PubSub_FindEventType(pubSub, EventName);
Packit 1fb8d4
Packit 1fb8d4
	if (event)
Packit 1fb8d4
	{
Packit 1fb8d4
		status = 0;
Packit 1fb8d4
Packit Service 5a9772
		if (event->EventHandlerCount < MAX_EVENT_HANDLERS)
Packit Service 5a9772
			event->EventHandlers[event->EventHandlerCount++] = EventHandler;
Packit 1fb8d4
		else
Packit 1fb8d4
			status = -1;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if (pubSub->synchronized)
Packit 1fb8d4
		PubSub_Unlock(pubSub);
Packit 1fb8d4
Packit 1fb8d4
	return status;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
int PubSub_Unsubscribe(wPubSub* pubSub, const char* EventName, pEventHandler EventHandler)
Packit 1fb8d4
{
Packit 1fb8d4
	int index;
Packit 1fb8d4
	wEventType* event;
Packit 1fb8d4
	int status = -1;
Packit 1fb8d4
Packit 1fb8d4
	if (pubSub->synchronized)
Packit 1fb8d4
		PubSub_Lock(pubSub);
Packit 1fb8d4
Packit 1fb8d4
	event = PubSub_FindEventType(pubSub, EventName);
Packit 1fb8d4
Packit 1fb8d4
	if (event)
Packit 1fb8d4
	{
Packit 1fb8d4
		status = 0;
Packit 1fb8d4
Packit 1fb8d4
		for (index = 0; index < event->EventHandlerCount; index++)
Packit 1fb8d4
		{
Packit 1fb8d4
			if (event->EventHandlers[index] == EventHandler)
Packit 1fb8d4
			{
Packit 1fb8d4
				event->EventHandlers[index] = NULL;
Packit 1fb8d4
				event->EventHandlerCount--;
Packit 1fb8d4
				MoveMemory(&event->EventHandlers[index], &event->EventHandlers[index + 1],
Packit Service 5a9772
				           (MAX_EVENT_HANDLERS - index - 1) * sizeof(pEventHandler));
Packit 1fb8d4
				status = 1;
Packit 1fb8d4
			}
Packit 1fb8d4
		}
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if (pubSub->synchronized)
Packit 1fb8d4
		PubSub_Unlock(pubSub);
Packit 1fb8d4
Packit 1fb8d4
	return status;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
int PubSub_OnEvent(wPubSub* pubSub, const char* EventName, void* context, wEventArgs* e)
Packit 1fb8d4
{
Packit 1fb8d4
	int index;
Packit 1fb8d4
	wEventType* event;
Packit 1fb8d4
	int status = -1;
Packit 1fb8d4
Packit 1fb8d4
	if (pubSub->synchronized)
Packit 1fb8d4
		PubSub_Lock(pubSub);
Packit 1fb8d4
Packit 1fb8d4
	event = PubSub_FindEventType(pubSub, EventName);
Packit 1fb8d4
Packit 1fb8d4
	if (pubSub->synchronized)
Packit 1fb8d4
		PubSub_Unlock(pubSub);
Packit 1fb8d4
Packit 1fb8d4
	if (event)
Packit 1fb8d4
	{
Packit 1fb8d4
		status = 0;
Packit 1fb8d4
Packit 1fb8d4
		for (index = 0; index < event->EventHandlerCount; index++)
Packit 1fb8d4
		{
Packit 1fb8d4
			if (event->EventHandlers[index])
Packit 1fb8d4
			{
Packit 1fb8d4
				event->EventHandlers[index](context, e);
Packit 1fb8d4
				status++;
Packit 1fb8d4
			}
Packit 1fb8d4
		}
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	return status;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Construction, Destruction
Packit 1fb8d4
 */
Packit 1fb8d4
Packit 1fb8d4
wPubSub* PubSub_New(BOOL synchronized)
Packit 1fb8d4
{
Packit 1fb8d4
	wPubSub* pubSub = NULL;
Packit 1fb8d4
Packit Service 5a9772
	pubSub = (wPubSub*)malloc(sizeof(wPubSub));
Packit 1fb8d4
Packit 1fb8d4
	if (!pubSub)
Packit 1fb8d4
		return NULL;
Packit 1fb8d4
Packit 1fb8d4
	pubSub->synchronized = synchronized;
Packit 1fb8d4
Packit 1fb8d4
	if (pubSub->synchronized && !InitializeCriticalSectionAndSpinCount(&pubSub->lock, 4000))
Packit 1fb8d4
	{
Packit 1fb8d4
		free(pubSub);
Packit 1fb8d4
		return NULL;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	pubSub->count = 0;
Packit 1fb8d4
	pubSub->size = 64;
Packit 1fb8d4
Packit Service 5a9772
	pubSub->events = (wEventType*)calloc(pubSub->size, sizeof(wEventType));
Packit 1fb8d4
	if (!pubSub->events)
Packit 1fb8d4
	{
Packit 1fb8d4
		if (pubSub->synchronized)
Packit 1fb8d4
			DeleteCriticalSection(&pubSub->lock);
Packit 1fb8d4
		free(pubSub);
Packit 1fb8d4
		return NULL;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	return pubSub;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
void PubSub_Free(wPubSub* pubSub)
Packit 1fb8d4
{
Packit 1fb8d4
	if (pubSub)
Packit 1fb8d4
	{
Packit 1fb8d4
		if (pubSub->synchronized)
Packit 1fb8d4
			DeleteCriticalSection(&pubSub->lock);
Packit 1fb8d4
Packit 1fb8d4
		free(pubSub->events);
Packit 1fb8d4
		free(pubSub);
Packit 1fb8d4
	}
Packit 1fb8d4
}