0e4fd67d25cbfedbd006a3f869c33dd0ab620b74
[lcr.git] / message.c
1 /*****************************************************************************\
2 **                                                                           **
3 ** PBX4Linux                                                                 **
4 **                                                                           **
5 **---------------------------------------------------------------------------**
6 ** Copyright: Andreas Eversberg                                              **
7 **                                                                           **
8 ** message handling                                                          **
9 **                                                                           **
10 \*****************************************************************************/ 
11
12 #include <stdio.h>
13 #include <string.h>
14 #include <stdlib.h>
15 #include "main.h"
16
17 MESSAGES
18
19 struct message *message_first = NULL;
20 struct message **messagepointer_end = &message_first;
21
22 /* creates a new message with the given attributes. the message must be filled then. after filling, the message_put must be called */
23 struct message *message_create(int id_from, int id_to, int flow, int type)
24 {
25         struct message *message;
26         int i = 0;
27
28         while(i < 10)
29         {
30                 message = (struct message *)calloc(1, sizeof(struct message));
31                 if (message)
32                         break;
33
34                 if (!i)
35                         PERROR("no mem for message, retrying...\n");
36                 i++;
37                 usleep(300000);
38         }
39         if (!message)
40         {
41                 PERROR("***Fatal error: no mem for message!!! exitting.\n");
42                 exit(-1);
43         }
44         mmemuse++;
45
46         memset(message, 0, sizeof(struct message));
47
48         message->id_from = id_from;
49         message->id_to = id_to;
50         message->flow = flow;
51         message->type = type;
52
53         return(message);
54 }
55
56 /* attaches a message to the end of the message chain */
57 void message_put(struct message *message)
58 {
59         if (message->id_to == 0)
60         {
61                 PDEBUG(DEBUG_MSG, "message %s not written, because destination is 0.\n", messages_txt[message->type]);
62                 message_free(message);
63                 return;
64         }
65         
66         if ((options.deb&DEBUG_MSG) && message->type != MESSAGE_DATA)
67                 PDEBUG(DEBUG_MSG, "message %s written from %ld to %ld (memory %x)\n", messages_txt[message->type], message->id_from, message->id_to, message);
68
69         *messagepointer_end = message;
70         messagepointer_end = &(message->next);
71 }
72
73
74 /* detaches the first messages from the message chain */
75 struct message *message_get(void)
76 {
77         struct message *message;
78
79         if (!message_first)
80         {
81                 return(0);
82         }
83
84         message = message_first;
85         message_first = message->next;
86         if (!message_first)
87                 messagepointer_end = &message_first;
88
89         if ((options.deb&DEBUG_MSG) && message->type != MESSAGE_DATA)
90                 PDEBUG(DEBUG_MSG, "message %s reading from %ld to %ld (memory %x)\n", messages_txt[message->type], message->id_from, message->id_to, message);
91
92         return(message);
93 }
94
95 /* free a message */
96 void message_free(struct message *message)
97 {
98         memset(message, 0, sizeof(struct message));
99         free(message);
100         mmemuse--;
101 }
102
103