only for backup, still in coding state - no compile!!!
[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 //#ifdef H323
23 //PMutex mutex_message;
24 //#endif
25
26 /* creates a new message with the given attributes. the message must be filled then. after filling, the message_put must be called */
27 struct message *message_create(int id_from, int id_to, int flow, int type)
28 {
29         struct message *message;
30         int i = 0;
31
32         while(i < 10)
33         {
34                 message = (struct message *)calloc(1, sizeof(struct message));
35                 if (message)
36                         break;
37
38                 if (!i)
39                         PERROR("no mem for message, retrying...\n");
40                 i++;
41                 usleep(300000);
42         }
43         if (!message)
44         {
45                 PERROR("***Fatal error: no mem for message!!! exitting.\n");
46                 exit(-1);
47         }
48         mmemuse++;
49
50         memset(message, 0, sizeof(struct message));
51
52         message->id_from = id_from;
53         message->id_to = id_to;
54         message->flow = flow;
55         message->type = type;
56
57         return(message);
58 }
59
60 /* attaches a message to the end of the message chain */
61 void message_put(struct message *message)
62 {
63         /* the mutex prevents from creating two messages at a time (h323 thread and main thread). */
64 //#ifdef H323
65 //      mutex_message.Wait();
66 //#endif
67
68         if (message->id_to == 0)
69         {
70                 PDEBUG(DEBUG_MSG, "message %s not written, because destination is 0.\n", messages_txt[message->type]);
71                 message_free(message);
72                 return;
73         }
74         
75         if ((options.deb&DEBUG_MSG) && message->type != MESSAGE_DATA)
76                 PDEBUG(DEBUG_MSG, "message %s written from %ld to %ld (memory %x)\n", messages_txt[message->type], message->id_from, message->id_to, message);
77
78         *messagepointer_end = message;
79         messagepointer_end = &(message->next);
80
81 //#ifdef H323
82 //      mutex_message.Signal();
83 //#endif
84 }
85
86
87 /* detaches the first messages from the message chain */
88 struct message *message_get(void)
89 {
90         struct message *message;
91
92         /* the mutex prevents from getting a message while creating a messages at a time (h323 thread and main thread). */
93 //#ifdef H323
94 //      mutex_message.Wait();
95 //#endif
96
97         if (!message_first)
98         {
99 //#ifdef H323
100 //              mutex_message.Signal();
101 //#endif
102                 return(0);
103         }
104
105         message = message_first;
106         message_first = message->next;
107         if (!message_first)
108                 messagepointer_end = &message_first;
109
110 //#ifdef H323
111 //      mutex_message.Signal();
112 //#endif
113
114         if ((options.deb&DEBUG_MSG) && message->type != MESSAGE_DATA)
115                 PDEBUG(DEBUG_MSG, "message %s reading from %ld to %ld (memory %x)\n", messages_txt[message->type], message->id_from, message->id_to, message);
116
117         return(message);
118 }
119
120 /* free a message */
121 void message_free(struct message *message)
122 {
123         memset(message, 0, sizeof(struct message));
124         free(message);
125         mmemuse--;
126 }
127
128