1 /*****************************************************************************\
5 **---------------------------------------------------------------------------**
6 ** Copyright: Andreas Eversberg **
10 \*****************************************************************************/
16 struct lcr_msg *message_first = NULL;
17 struct lcr_msg **messagepointer_end = &message_first;
18 struct lcr_work message_work;
20 static int work_message(struct lcr_work *work, void *instance, int index);
22 void init_message(void)
24 memset(&message_work, 0, sizeof(message_work));
25 add_work(&message_work, work_message, NULL, 0);
28 void cleanup_message(void)
30 del_work(&message_work);
33 unsigned int lcr_random = 0;
35 /* creates a new message with the given attributes. the message must be filled then. after filling, the message_put must be called */
36 struct lcr_msg *message_create(int id_from, int id_to, int flow, int type)
38 struct lcr_msg *message;
39 struct timeval now_tv;
40 struct timezone now_tz;
42 gettimeofday(&now_tv, &now_tz);
43 lcr_random = (lcr_random << 1) | (lcr_random >> 31);
44 lcr_random ^= now_tv.tv_sec;
45 lcr_random ^= now_tv.tv_usec;
47 message = (struct lcr_msg *)MALLOC(sizeof(struct lcr_msg));
49 FATAL("No memory for message.\n");
52 message->id_from = id_from;
53 message->id_to = id_to;
60 /* attaches a message to the end of the message chain */
61 void _message_put(struct lcr_msg *message, const char *file, int line)
63 if (message->id_to == 0) {
64 PDEBUG(DEBUG_MSG, "message %s not written, because destination is 0.\n", messages_txt[message->type]);
65 message_free(message);
69 if ((options.deb & DEBUG_MSG))
70 PDEBUG(DEBUG_MSG, "message %s written from %ld to %ld (memory %x at file %s, line %d)\n", messages_txt[message->type], message->id_from, message->id_to, message, file, line);
72 *messagepointer_end = message;
73 messagepointer_end = &(message->next);
74 /* Nullify next pointer if recycled messages */
75 *messagepointer_end=NULL;
78 trigger_work(&message_work);
81 struct lcr_msg *message_forward(int id_from, int id_to, int flow, union parameter *param)
83 struct lcr_msg *message;
85 /* get point to message */
86 message = (struct lcr_msg *)((unsigned long)param - ((unsigned long)(&message->param) - (unsigned long)message));
88 /* protect, so forwarded messages are not freed after handling */
91 message->id_from = id_from;
92 message->id_to = id_to;
99 /* detaches the first messages from the message chain */
100 struct lcr_msg *message_get(void)
102 struct lcr_msg *message;
107 message = message_first;
108 message_first = message->next;
110 messagepointer_end = &message_first;
114 if ((options.deb & DEBUG_MSG))
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);
121 void message_free(struct lcr_msg *message)
125 FREE(message, sizeof(struct lcr_msg));
130 static int work_message(struct lcr_work *work, void *instance, int index)
132 struct lcr_msg *message;
134 class Endpoint *epoint;
137 while ((message = message_get())) {
138 switch(message->flow) {
140 epoint = find_epoint_id(message->id_to);
142 if (epoint->ep_app) {
143 epoint->ep_app->ea_message_port(message->id_from, message->type, &message->param);
145 PDEBUG(DEBUG_MSG, "Warning: message %s from port %d to endpoint %d. endpoint doesn't have an application.\n", messages_txt[message->type], message->id_from, message->id_to);
148 PDEBUG(DEBUG_MSG, "Warning: message %s from port %d to endpoint %d. endpoint doesn't exist anymore.\n", messages_txt[message->type], message->id_from, message->id_to);
153 join = find_join_id(message->id_to);
155 join->message_epoint(message->id_from, message->type, &message->param);
157 PDEBUG(DEBUG_MSG, "Warning: message %s from endpoint %d to join %d. join doesn't exist anymore\n", messages_txt[message->type], message->id_from, message->id_to);
162 epoint = find_epoint_id(message->id_to);
164 if (epoint->ep_app) {
165 epoint->ep_app->ea_message_join(message->id_from, message->type, &message->param);
167 PDEBUG(DEBUG_MSG, "Warning: message %s from join %d to endpoint %d. endpoint doesn't have an application.\n", messages_txt[message->type], message->id_from, message->id_to);
170 PDEBUG(DEBUG_MSG, "Warning: message %s from join %d to endpoint %d. endpoint doesn't exist anymore.\n", messages_txt[message->type], message->id_from, message->id_to);
175 port = find_port_id(message->id_to);
177 port->message_epoint(message->id_from, message->type, &message->param);
180 PDEBUG(DEBUG_MSG, "Warning: message %s from endpoint %d to port %d. port doesn't exist anymore\n", messages_txt[message->type], message->id_from, message->id_to);
185 PERROR("Message flow %d unknown.\n", message->flow);
187 message_free(message);