3 /* gras message exchanges */
5 /* Copyright (c) 2003, 2004, 2005, 2006, 2007 Martin Quinson. */
6 /* All rights reserved. */
8 /* This program is free software; you can redistribute it and/or modify it
9 * under the terms of the license (GNU LGPL) which comes with this package. */
12 #include "xbt/ex_interface.h"
13 #include "gras/Msg/msg_private.h"
14 #include "gras/Virtu/virtu_interface.h"
15 #include "gras/Transport/transport_interface.h" /* gras_select */
17 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(gras_msg);
21 const char *e_gras_msg_kind_names[e_gras_msg_kind_count]=
22 {"UNKNOWN","ONEWAY","RPC call","RPC answer","RPC error"};
26 /** \brief Waits for a message to come in over a given socket.
28 * @param timeout: How long should we wait for this message.
29 * @param msgt_want: type of awaited msg (or NULL if I'm enclined to accept any message)
30 * @param expe_want: awaited expeditot (match on hostname, not port; NULL if not relevant)
31 * @param filter: function returning true or false when passed a payload. Messages for which it returns false are not selected. (NULL if not relevant)
32 * @param filter_ctx: context passed as second argument of the filter (a pattern to match?)
33 * @param[out] msg_got: where to write the message we got
35 * Every message of another type received before the one waited will be queued
36 * and used by subsequent call to this function or gras_msg_handle().
40 gras_msg_wait_ext_(double timeout,
42 gras_msgtype_t msgt_want,
43 gras_socket_t expe_want,
44 gras_msg_filter_t filter,
51 gras_msg_procdata_t pd=
52 (gras_msg_procdata_t)gras_libdata_by_id(gras_msg_libdata_id);
55 xbt_assert0(msg_got,"msg_got is an output parameter");
57 start = gras_os_time();
58 VERB1("Waiting for message '%s'",msgt_want?msgt_want->name:"(any)");
60 xbt_dynar_foreach(pd->msg_waitqueue,cpt,msg){
61 if ( ( !msgt_want || (msg.type->code == msgt_want->code))
62 && (!expe_want || (!strcmp( gras_socket_peer_name(msg.expe),
63 gras_socket_peer_name(expe_want))))
64 && (!filter || filter(&msg,filter_ctx))) {
66 memcpy(msg_got,&msg,sizeof(s_gras_msg_t));
67 xbt_dynar_cursor_rm(pd->msg_waitqueue, &cpt);
68 VERB0("The waited message was queued");
73 xbt_dynar_foreach(pd->msg_queue,cpt,msg){
74 if ( ( !msgt_want || (msg.type->code == msgt_want->code))
75 && (!expe_want || (!strcmp( gras_socket_peer_name(msg.expe),
76 gras_socket_peer_name(expe_want))))
77 && (!filter || filter(&msg,filter_ctx))) {
79 memcpy(msg_got,&msg,sizeof(s_gras_msg_t));
80 xbt_dynar_cursor_rm(pd->msg_queue, &cpt);
81 VERB0("The waited message was queued");
90 restart_receive: /* Goto here when the receive of a message failed */
93 memset(&msg,sizeof(msg),0);
96 msg.expe = gras_trp_select(timeout ? timeout - now + start : 0);
97 gras_msg_recv(msg.expe, &msg);
99 if (e.category == system_error &&
100 !strncmp("Socket closed by remote side",e.msg,
101 strlen("Socket closed by remote side"))) {
109 goto restart_receive;
111 DEBUG0("Got a message from the socket");
113 if ( ( !msgt_want || (msg.type->code == msgt_want->code))
114 && (!expe_want || (!strcmp( gras_socket_peer_name(msg.expe),
115 gras_socket_peer_name(expe_want))))
116 && (!filter || filter(&msg,filter_ctx))) {
118 memcpy(msg_got,&msg,sizeof(s_gras_msg_t));
119 DEBUG0("Message matches expectations. Use it.");
122 DEBUG0("Message does not match expectations. Queue it.");
124 /* not expected msg type. Queue it for later */
125 xbt_dynar_push(pd->msg_queue,&msg);
128 if (now - start + 0.001 > timeout) {
129 THROW1(timeout_error, now-start+0.001-timeout,
130 "Timeout while waiting for msg '%s'",
131 msgt_want?msgt_want->name:"(any)");
137 /** \brief Waits for a message to come in over a given socket.
139 * @param timeout: How long should we wait for this message.
140 * @param msgt_want: type of awaited msg
141 * @param[out] expeditor: where to create a socket to answer the incomming message
142 * @param[out] payload: where to write the payload of the incomming message
143 * @return the error code (or no_error).
145 * Every message of another type received before the one waited will be queued
146 * and used by subsequent call to this function or gras_msg_handle().
149 gras_msg_wait_(double timeout,
150 gras_msgtype_t msgt_want,
151 gras_socket_t *expeditor,
155 gras_msg_wait_ext_(timeout,
156 msgt_want, NULL, NULL, NULL,
159 if (msgt_want->ctn_type) {
161 "Message type '%s' convey a payload you must accept",
164 xbt_assert1(!payload,
165 "No payload was declared for message type '%s'",
170 memcpy(payload,msg.payl,msg.payl_size);
175 *expeditor = msg.expe;
178 static int gras_msg_wait_or_filter(gras_msg_t msg, void *ctx) {
179 xbt_dynar_t dyn=(xbt_dynar_t)ctx;
180 int res = xbt_dynar_member(dyn,msg->type);
182 VERB1("Got matching message (type=%s)",msg->type->name);
184 VERB0("Got message not matching our expectations");
187 /** \brief Waits for a message to come in over a given socket.
189 * @param timeout: How long should we wait for this message.
190 * @param msgt_want: a dynar containing all accepted message type
191 * @param[out] ctx: the context of received message (in case it's a RPC call we want to answer to)
192 * @param[out] msgt_got: indice in the dynar of the type of the received message
193 * @param[out] payload: where to write the payload of the incomming message
194 * @return the error code (or no_error).
196 * Every message of a type not in the accepted list received before the one
197 * waited will be queued and used by subsequent call to this function or
200 * If you are interested in the context, pass the address of a s_gras_msg_cb_ctx_t variable.
202 void gras_msg_wait_or(double timeout,
203 xbt_dynar_t msgt_want,
204 gras_msg_cb_ctx_t *ctx,
209 VERB1("Wait %f seconds for several message types",timeout);
210 gras_msg_wait_ext_(timeout,
212 &gras_msg_wait_or_filter, (void*)msgt_want,
215 if (msg.type->ctn_type) {
217 "Message type '%s' convey a payload you must accept",
219 } /* don't check the other side since some of the types may have a payload */
221 if (payload && msg.type->ctn_type) {
222 memcpy(payload,msg.payl,msg.payl_size);
227 *ctx=gras_msg_cb_ctx_new(msg.expe, msg.type, msg.ID,
228 (msg.kind == e_gras_msg_kind_rpccall), 60);
231 *msgt_got = xbt_dynar_search(msgt_want,msg.type);
235 /** \brief Send the data pointed by \a payload as a message of type
236 * \a msgtype to the peer \a sock */
238 gras_msg_send_(gras_socket_t sock,
239 gras_msgtype_t msgtype,
242 if (msgtype->ctn_type) {
244 "Message type '%s' convey a payload you must provide",
247 xbt_assert1(!payload,
248 "No payload was declared for message type '%s'",
252 DEBUG2("Send a oneway message of type '%s'. Payload=%p",
253 msgtype->name,payload);
254 gras_msg_send_ext(sock, e_gras_msg_kind_oneway,0, msgtype, payload);
255 VERB2("Sent a oneway message of type '%s'. Payload=%p",
256 msgtype->name,payload);
259 /** @brief Handle all messages arriving within the given period
261 * @param period: How long to wait for incoming messages (in seconds)
263 * Messages are dealed with just like gras_msg_handle() would do. The
264 * difference is that gras_msg_handle() handles at most one message (or wait up
265 * to timeout second when no message arrives) while this function handles any
266 * amount of messages, and lasts the given period in any case.
269 gras_msg_handleall(double period) {
271 double begin=gras_os_time();
277 if (period - now + begin > 0)
278 gras_msg_handle(period - now + begin);
280 if (e.category != timeout_error)
281 RETHROW0("Error while waiting for messages: %s");
284 } while (now - begin < period);
287 /** @brief Handle an incomming message or timer (or wait up to \a timeOut seconds)
289 * @param timeOut: How long to wait for incoming messages (in seconds)
290 * @return the error code (or no_error).
292 * Messages are passed to the callbacks. See also gras_msg_handleall().
295 gras_msg_handle(double timeOut) {
304 gras_msg_procdata_t pd=(gras_msg_procdata_t)gras_libdata_by_id(gras_msg_libdata_id);
305 gras_cblist_t *list=NULL;
307 s_gras_msg_cb_ctx_t ctx;
309 int timerexpected, timeouted;
312 VERB1("Handling message within the next %.2fs",timeOut);
314 untiltimer = gras_msg_timer_handle();
315 DEBUG1("Next timer in %f sec", untiltimer);
316 if (untiltimer == 0.0) {
317 /* A timer was already elapsed and handled */
320 if (untiltimer != -1.0) {
322 timeOut = MIN(timeOut, untiltimer);
327 /* get a message (from the queue or from the net) */
329 if (xbt_dynar_length(pd->msg_queue)) {
330 DEBUG0("Get a message from the queue");
331 xbt_dynar_shift(pd->msg_queue,&msg);
334 msg.expe = gras_trp_select(timeOut);
336 if (e.category != timeout_error)
344 /* FIXME: if not the right kind, queue it and recall ourself or goto >:-) */
345 gras_msg_recv(msg.expe, &msg);
346 DEBUG1("Received a msg from the socket kind:%s",
347 e_gras_msg_kind_names[msg.kind]);
350 RETHROW4("Error while receiving a message on select()ed socket %p to [%s]%s:%d: %s",
352 gras_socket_peer_proc(msg.expe),gras_socket_peer_name(msg.expe),
353 gras_socket_peer_port(msg.expe));
361 /* A timer elapsed before the arrival of any message even if we select()ed a bit */
362 untiltimer = gras_msg_timer_handle();
363 if (untiltimer == 0.0) {
364 /* we served a timer, we're done */
367 xbt_assert1(untiltimer>0, "Negative timer (%f). I'm 'puzzeled'", untiltimer);
368 WARN1("No timer elapsed, in contrary to expectations (next in %f sec)",
370 THROW1(timeout_error,0,
371 "No timer elapsed, in contrary to expectations (next in %f sec)",
376 /* select timeouted, and no timer elapsed. Nothing to do */
377 THROW1(timeout_error, 0, "No new message or timer (delay was %f)",
383 /* A message was already there or arrived in the meanwhile. handle it */
384 xbt_dynar_foreach(pd->cbl_list,cpt,list) {
385 if (list->id == msg.type->code) {
392 INFO3("No callback for message '%s' from %s:%d. Queue it for later gras_msg_wait() use.",
394 gras_socket_peer_name(msg.expe),gras_socket_peer_port(msg.expe));
395 xbt_dynar_push(pd->msg_waitqueue,&msg);
396 return; /* FIXME: maybe we should call ourselves again until the end of the timer or a proper msg is got */
399 ctx.expeditor = msg.expe;
401 ctx.msgtype = msg.type;
402 ctx.answer_due = (msg.kind == e_gras_msg_kind_rpccall);
405 case e_gras_msg_kind_oneway:
406 case e_gras_msg_kind_rpccall:
409 xbt_dynar_foreach(list->cbs,cpt,cb) {
411 DEBUG4("Use the callback #%d (@%p) for incomming msg %s (payload_size=%d)",
412 cpt+1,cb,msg.type->name,msg.payl_size);
413 if (!(*cb)(&ctx,msg.payl)) {
414 /* cb handled the message */
422 if (msg.type->kind == e_gras_msg_kind_rpccall) {
423 char *old_file=e.file;
424 /* The callback raised an exception, propagate it on the network */
426 /* Make sure we reduce the file name to its basename to avoid issues in tests */
427 char *new_file=strrchr(e.file,'/');
430 /* the exception is born on this machine */
431 e.host = (char*)gras_os_myname();
432 xbt_ex_setup_backtrace(&e);
434 VERB5("Propagate %s exception ('%s') from '%s' RPC cb back to %s:%d",
435 (e.remote ? "remote" : "local"),
438 gras_socket_peer_name(msg.expe),
439 gras_socket_peer_port(msg.expe));
440 gras_msg_send_ext(msg.expe, e_gras_msg_kind_rpcerror,
441 msg.ID, msg.type, &e);
447 RETHROW0("Callback raised an exception: %s");
450 xbt_assert0(!(ctx.answer_due),
451 "RPC callback didn't call gras_msg_rpcreturn");
454 THROW1(mismatch_error,0,
455 "Message '%s' refused by all registered callbacks", msg.type->name);
456 /* FIXME: gras_datadesc_free not implemented => leaking the payload */
460 case e_gras_msg_kind_rpcanswer:
461 INFO1("Unexpected RPC answer discarded (type: %s)", msg.type->name);
462 WARN0("FIXME: gras_datadesc_free not implemented => leaking the payload");
465 case e_gras_msg_kind_rpcerror:
466 INFO1("Unexpected RPC error discarded (type: %s)", msg.type->name);
467 WARN0("FIXME: gras_datadesc_free not implemented => leaking the payload");
471 THROW1(unknown_error,0,
472 "Cannot handle messages of kind %d yet",msg.type->kind);