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 /* creates a new message with the given attributes. the message must be filled then. after filling, the message_put must be called */
34 struct lcr_msg *message_create(int id_from, int id_to, int flow, int type)
36 struct lcr_msg *message;
38 message = (struct lcr_msg *)MALLOC(sizeof(struct lcr_msg));
40 FATAL("No memory for message.\n");
43 message->id_from = id_from;
44 message->id_to = id_to;
51 /* attaches a message to the end of the message chain */
52 void message_put(struct lcr_msg *message)
54 if (message->id_to == 0) {
55 PDEBUG(DEBUG_MSG, "message %s not written, because destination is 0.\n", messages_txt[message->type]);
56 message_free(message);
60 if ((options.deb&DEBUG_MSG) && message->type != MESSAGE_DATA)
61 PDEBUG(DEBUG_MSG, "message %s written from %ld to %ld (memory %x)\n", messages_txt[message->type], message->id_from, message->id_to, message);
63 *messagepointer_end = message;
64 messagepointer_end = &(message->next);
65 /* Nullify next pointer if recycled messages */
66 *messagepointer_end=NULL;
69 trigger_work(&message_work);
72 struct lcr_msg *message_forward(int id_from, int id_to, int flow, union parameter *param)
74 struct lcr_msg *message;
76 /* get point to message */
77 message = (struct lcr_msg *)((unsigned long)param - ((unsigned long)(&message->param) - (unsigned long)message));
79 /* protect, so forwarded messages are not freed after handling */
82 message->id_from = id_from;
83 message->id_to = id_to;
90 /* detaches the first messages from the message chain */
91 struct lcr_msg *message_get(void)
93 struct lcr_msg *message;
98 message = message_first;
99 message_first = message->next;
101 messagepointer_end = &message_first;
105 if ((options.deb&DEBUG_MSG) && message->type != MESSAGE_DATA)
107 PDEBUG(DEBUG_MSG, "message %s reading from %ld to %ld (memory %x)\n", messages_txt[message->type], message->id_from, message->id_to, message);
113 void message_free(struct lcr_msg *message)
117 FREE(message, sizeof(struct lcr_msg));
122 static int work_message(struct lcr_work *work, void *instance, int index)
124 struct lcr_msg *message;
126 class Endpoint *epoint;
129 while ((message = message_get())) {
130 switch(message->flow) {
132 epoint = find_epoint_id(message->id_to);
134 if (epoint->ep_app) {
135 epoint->ep_app->ea_message_port(message->id_from, message->type, &message->param);
137 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);
140 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);
145 join = find_join_id(message->id_to);
147 join->message_epoint(message->id_from, message->type, &message->param);
149 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);
154 epoint = find_epoint_id(message->id_to);
156 if (epoint->ep_app) {
157 epoint->ep_app->ea_message_join(message->id_from, message->type, &message->param);
159 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);
162 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);
167 port = find_port_id(message->id_to);
169 port->message_epoint(message->id_from, message->type, &message->param);
172 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);
177 PERROR("Message flow %d unknown.\n", message->flow);
179 message_free(message);