summaryrefslogtreecommitdiffstats
path: root/matchblox/common/message_queue.c
blob: ad28b6ebb0b335458b3e9b94f26cd53d6c7ec774 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 2; tab-width: 2 -*- */
/*
 * message_queue.c
 * Copyright (C) Oliver 2008 <o.m.schinagl@student.tue.nl>
 * 
 * main.c is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * main.c is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
#ifdef G_OS_WIN32
#define WIN32_LEAN_AND_MEAN 1
#include <windows.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "message_queue.h"


#define MAX_MESSAGES MESSAGE_WINDOW_SIZE


struct messageq_s messageq[MAX_MESSAGES];
static int messageq_index, messageq_serial;
static int messageq_last_free;


void messageq_init(void)
{
	int i;
	
	messageq_index = 0;
	messageq_serial = 0;
	messageq_last_free = 0;
	for( i = 0; i < MAX_MESSAGES; i++) {
		messageq[i].sender = 0;		
		messageq[i].recipient = 0;
		messageq[i].payload = NULL;
		messageq[i].payload_size = 0;
	}
}


static void msgcpy(struct messageq_s *dest, const struct messageq_s *src)
{
	dest->sender = src->sender;
	dest->recipient = src->recipient;
	dest->payload = (void *)malloc(src->payload_size);
	memcpy(dest->payload, src->payload, src->payload_size);
	dest->payload_size = src->payload_size;
}

static void msgfree(int index) {
	messageq[index].sender = 0;
	messageq[index].recipient = 0;
	free(messageq[index].payload);
	messageq[index].payload_size = 0;
}


struct messageq_s *messageq_get(int recipient)
{
	int i;
	
	i = messageq_index;
	while (messageq[i].recipient != recipient && i != (messageq_index -1)) {
			i = (i < MAX_MESSAGES -1) ? i +1 : 0;
	}
	
	/*
	 * We found the message for our recipient. We'll now set the recipient field to 0 since the recipient
	 * should know the message was for him, he asked for it. This is needed to prefent the above loop
	 * to find the same message again, since the message will stay alive in the system until its expired.
	 */
	messageq[i].recipient = MESSAGE_NONE;

	return &messageq[i];
}


void messageq_send(struct messageq_s *message)
{
	/* Before sending the message, we clean out old cruft. We do this here
	 * since we will be overwriting the contens of the message here anyway.
	 */
	msgfree(messageq_index);
	msgcpy(&messageq[messageq_index], message);
	messageq_index = ++messageq_serial %MAX_MESSAGES;
}