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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
/* -*- 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>
#else
#define FALSE 0
#define TRUE !FALSE
#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;
void messageq_init(void)
{
int i;
messageq_index = 0;
messageq_serial = 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)
{
struct messageq_s *return_value;
int i, message_found;
message_found = FALSE;
i = messageq_index;
do {
i = (i < MAX_MESSAGES -1) ? i +1 : 0;
if (messageq[i].recipient & recipient) {
message_found++;
printf("message[%d].recp = %d (messageq_index= %d)\n", i, messageq[i].recipient, messageq_index);
printf("Message found! %d\n", message_found);
}
} while ((i != (messageq_index)) && (!message_found));
/*
* We found the message for our recipient. We'll now substract the recipient field since the recipient
* should not look at this field. This is needed to so that multi-recipients can still read messages, and
* to make sure the loop above is able to end. Since the message will stay alive in the system until its expired,
* anybody (in that is a recepient) can still read it.
*/
if (message_found) {
messageq[i].recipient -= recipient;
return_value = &messageq[i];
} else {
return_value = NULL;
}
return return_value;
}
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;
}
|