-void
-gras_msg_handle(double timeOut) {
-
- double untiltimer;
-
- unsigned int cpt;
- int volatile ran_ok;
-
- s_gras_msg_t msg;
-
- gras_msg_procdata_t pd=(gras_msg_procdata_t)gras_libdata_by_id(gras_msg_libdata_id);
- gras_cblist_t *list=NULL;
- gras_msg_cb_t cb;
- s_gras_msg_cb_ctx_t ctx;
-
- int timerexpected, timeouted;
- xbt_ex_t e;
-
- VERB1("Handling message within the next %.2fs",timeOut);
-
- untiltimer = gras_msg_timer_handle();
- DEBUG1("Next timer in %f sec", untiltimer);
- if (untiltimer == 0.0) {
- /* A timer was already elapsed and handled */
- return;
- }
- if (untiltimer != -1.0) {
- timerexpected = 1;
- timeOut = MIN(timeOut, untiltimer);
- } else {
- timerexpected = 0;
- }
-
- /* get a message (from the queue or from the net) */
- timeouted = 0;
- if (xbt_dynar_length(pd->msg_queue)) {
- DEBUG0("Get a message from the queue");
- xbt_dynar_shift(pd->msg_queue,&msg);
- } else {
- TRY {
- xbt_queue_shift_timed(pd->msg_received,&msg,timeOut);
- // msg.expe = gras_trp_select(timeOut);
- } CATCH(e) {
- if (e.category != timeout_error)
- RETHROW;
- DEBUG0("Damn. Timeout while getting a message from the queue");
- xbt_ex_free(e);
- timeouted = 1;
- }
- }
-
- if (timeouted) {
- if (timerexpected) {
-
- /* A timer elapsed before the arrival of any message even if we select()ed a bit */
- untiltimer = gras_msg_timer_handle();
- if (untiltimer == 0.0) {
- /* we served a timer, we're done */
- return;
- } else {
- xbt_assert1(untiltimer>0, "Negative timer (%f). I'm 'puzzeled'", untiltimer);
- WARN1("No timer elapsed, in contrary to expectations (next in %f sec)",
- untiltimer);
- THROW1(timeout_error,0,
- "No timer elapsed, in contrary to expectations (next in %f sec)",
- untiltimer);
- }
-
- } else {
- /* select timeouted, and no timer elapsed. Nothing to do */
- THROW1(timeout_error, 0, "No new message or timer (delay was %f)",
- timeOut);
- }
-
- }
-
- /* A message was already there or arrived in the meanwhile. handle it */
- xbt_dynar_foreach(pd->cbl_list,cpt,list) {
- if (list->id == msg.type->code) {
- break;
- } else {
- list=NULL;
- }
- }
- if (!list) {
- INFO3("No callback for message '%s' from %s:%d. Queue it for later gras_msg_wait() use.",
- msg.type->name,
- gras_socket_peer_name(msg.expe),gras_socket_peer_port(msg.expe));
- xbt_dynar_push(pd->msg_waitqueue,&msg);
- return; /* FIXME: maybe we should call ourselves again until the end of the timer or a proper msg is got */
- }
-
- ctx.expeditor = msg.expe;
- ctx.ID = msg.ID;
- ctx.msgtype = msg.type;
- ctx.answer_due = (msg.kind == e_gras_msg_kind_rpccall);
-
- switch (msg.kind) {
- case e_gras_msg_kind_oneway:
- case e_gras_msg_kind_rpccall:
- ran_ok=0;
- TRY {
- xbt_dynar_foreach(list->cbs,cpt,cb) {
- if (!ran_ok) {
- DEBUG4("Use the callback #%d (@%p) for incomming msg '%s' (payload_size=%d)",
- cpt+1,cb,msg.type->name,msg.payl_size);
- if (!(*cb)(&ctx,msg.payl)) {
- /* cb handled the message */
- free(msg.payl);
- ran_ok = 1;
- }
- }
- }
- } CATCH(e) {
- free(msg.payl);
- if (msg.type->kind == e_gras_msg_kind_rpccall) {
- char *old_file=e.file;
- /* The callback raised an exception, propagate it on the network */
- if (!e.remote) {
- /* Make sure we reduce the file name to its basename to avoid issues in tests */
- char *new_file=strrchr(e.file,'/');
- if (new_file)
- e.file = new_file;
- /* the exception is born on this machine */
- e.host = (char*)gras_os_myname();
- xbt_ex_setup_backtrace(&e);
- }
- INFO5("Propagate %s exception ('%s') from '%s' RPC cb back to %s:%d",
- (e.remote ? "remote" : "local"),
- e.msg,
- msg.type->name,
- gras_socket_peer_name(msg.expe),
- gras_socket_peer_port(msg.expe));
- if (XBT_LOG_ISENABLED(gras_msg,xbt_log_priority_verbose))
- xbt_ex_display(&e);
- gras_msg_send_ext(msg.expe, e_gras_msg_kind_rpcerror,
- msg.ID, msg.type, &e);
- e.file=old_file;
- xbt_ex_free(e);
- ctx.answer_due = 0;
- ran_ok=1;
- } else {
- RETHROW4("Callback #%d (@%p) to message '%s' (payload size: %d) raised an exception: %s",
- cpt+1,cb,msg.type->name,msg.payl_size);
- }
- }
-
- xbt_assert1(! ctx.answer_due,
- "Bug in user code: RPC callback to message '%s' didn't call gras_msg_rpcreturn",msg.type->name);
- if (ctx.answer_due)
- CRITICAL1("BUGS BOTH IN USER CODE (RPC callback to message '%s' didn't call gras_msg_rpcreturn) "
- "AND IN SIMGRID (process wasn't killed by an assert)",msg.type->name);
- if (!ran_ok)
- THROW1(mismatch_error,0,
- "Message '%s' refused by all registered callbacks (maybe your callback misses a 'return 0' at the end)", msg.type->name);
- /* FIXME: gras_datadesc_free not implemented => leaking the payload */
- break;
-
-
- case e_gras_msg_kind_rpcanswer:
- INFO3("Unexpected RPC answer discarded (type: %s; from:%s:%d)", msg.type->name,
- gras_socket_peer_name(msg.expe),gras_socket_peer_port(msg.expe));
- WARN0("FIXME: gras_datadesc_free not implemented => leaking the payload");
- return;
-
- case e_gras_msg_kind_rpcerror:
- INFO3("Unexpected RPC error discarded (type: %s; from:%s:%d)", msg.type->name,
- gras_socket_peer_name(msg.expe),gras_socket_peer_port(msg.expe));
- WARN0("FIXME: gras_datadesc_free not implemented => leaking the payload");
- return;
-
- default:
- THROW1(unknown_error,0,
- "Cannot handle messages of kind %d yet",msg.type->kind);
- }
+void gras_msg_handle(double timeOut)
+{
+
+ double untiltimer;
+
+ unsigned int cpt;
+ int volatile ran_ok;
+
+ s_gras_msg_t msg;
+
+ gras_msg_procdata_t pd =
+ (gras_msg_procdata_t) gras_libdata_by_id(gras_msg_libdata_id);
+ gras_cblist_t *list = NULL;
+ gras_msg_cb_t cb;
+ s_gras_msg_cb_ctx_t ctx;
+
+ int timerexpected, timeouted;
+ xbt_ex_t e;
+
+ VERB1("Handling message within the next %.2fs", timeOut);
+
+ untiltimer = gras_msg_timer_handle();
+ DEBUG1("Next timer in %f sec", untiltimer);
+ if (untiltimer == 0.0) {
+ /* A timer was already elapsed and handled */
+ return;
+ }
+ if (untiltimer != -1.0) {
+ timerexpected = 1;
+ timeOut = MIN(timeOut, untiltimer);
+ } else {
+ timerexpected = 0;
+ }
+
+ /* get a message (from the queue or from the net) */
+ timeouted = 0;
+ if (xbt_dynar_length(pd->msg_queue)) {
+ DEBUG0("Get a message from the queue");
+ xbt_dynar_shift(pd->msg_queue, &msg);
+ } else {
+ TRY {
+ xbt_queue_shift_timed(pd->msg_received, &msg, timeOut);
+ // msg.expe = gras_trp_select(timeOut);
+ }
+ CATCH(e) {
+ if (e.category != timeout_error)
+ RETHROW;
+ DEBUG0("Damn. Timeout while getting a message from the queue");
+ xbt_ex_free(e);
+ timeouted = 1;
+ }
+ }
+
+ if (timeouted) {
+ if (timerexpected) {
+
+ /* A timer elapsed before the arrival of any message even if we select()ed a bit */
+ untiltimer = gras_msg_timer_handle();
+ if (untiltimer == 0.0) {
+ /* we served a timer, we're done */
+ return;
+ } else {
+ xbt_assert1(untiltimer > 0, "Negative timer (%f). I'm 'puzzeled'",
+ untiltimer);
+ WARN1
+ ("No timer elapsed, in contrary to expectations (next in %f sec)",
+ untiltimer);
+ THROW1(timeout_error, 0,
+ "No timer elapsed, in contrary to expectations (next in %f sec)",
+ untiltimer);
+ }
+
+ } else {
+ /* select timeouted, and no timer elapsed. Nothing to do */
+ THROW1(timeout_error, 0, "No new message or timer (delay was %f)",
+ timeOut);
+ }
+
+ }
+
+ /* A message was already there or arrived in the meanwhile. handle it */
+ xbt_dynar_foreach(pd->cbl_list, cpt, list) {
+ if (list->id == msg.type->code) {
+ break;
+ } else {
+ list = NULL;
+ }
+ }
+ if (!list) {
+ INFO4
+ ("No callback for message '%s' (type:%s) from %s:%d. Queue it for later gras_msg_wait() use.",
+ msg.type->name, e_gras_msg_kind_names[msg.kind],
+ gras_socket_peer_name(msg.expe), gras_socket_peer_port(msg.expe));
+ xbt_dynar_push(pd->msg_waitqueue, &msg);
+ return; /* FIXME: maybe we should call ourselves again until the end of the timer or a proper msg is got */
+ }
+
+ ctx.expeditor = msg.expe;
+ ctx.ID = msg.ID;
+ ctx.msgtype = msg.type;
+ ctx.answer_due = (msg.kind == e_gras_msg_kind_rpccall);
+
+ switch (msg.kind) {
+ case e_gras_msg_kind_oneway:
+ case e_gras_msg_kind_rpccall:
+ ran_ok = 0;
+ TRY {
+ xbt_dynar_foreach(list->cbs, cpt, cb) {
+ if (!ran_ok) {
+ DEBUG4
+ ("Use the callback #%d (@%p) for incomming msg '%s' (payload_size=%d)",
+ cpt + 1, cb, msg.type->name, msg.payl_size);
+ if (!(*cb) (&ctx, msg.payl)) {
+ /* cb handled the message */
+ free(msg.payl);
+ ran_ok = 1;
+ }
+ }
+ }
+ }
+ CATCH(e) {
+ free(msg.payl);
+ if (msg.type->kind == e_gras_msg_kind_rpccall) {
+ char *old_file = e.file;
+ /* The callback raised an exception, propagate it on the network */
+ if (!e.remote) {
+ /* Make sure we reduce the file name to its basename to avoid issues in tests */
+ char *new_file = strrchr(e.file, '/');
+ if (new_file)
+ e.file = new_file;
+ /* the exception is born on this machine */
+ e.host = (char *) gras_os_myname();
+ xbt_ex_setup_backtrace(&e);
+ }
+ INFO5("Propagate %s exception ('%s') from '%s' RPC cb back to %s:%d",
+ (e.remote ? "remote" : "local"),
+ e.msg,
+ msg.type->name,
+ gras_socket_peer_name(msg.expe),
+ gras_socket_peer_port(msg.expe));
+ if (XBT_LOG_ISENABLED(gras_msg, xbt_log_priority_verbose))
+ xbt_ex_display(&e);
+ gras_msg_send_ext(msg.expe, e_gras_msg_kind_rpcerror,
+ msg.ID, msg.type, &e);
+ e.file = old_file;
+ xbt_ex_free(e);
+ ctx.answer_due = 0;
+ ran_ok = 1;
+ } else {
+ RETHROW4
+ ("Callback #%d (@%p) to message '%s' (payload size: %d) raised an exception: %s",
+ cpt + 1, cb, msg.type->name, msg.payl_size);
+ }
+ }
+
+ xbt_assert1(!ctx.answer_due,
+ "Bug in user code: RPC callback to message '%s' didn't call gras_msg_rpcreturn",
+ msg.type->name);
+ if (ctx.answer_due)
+ CRITICAL1
+ ("BUGS BOTH IN USER CODE (RPC callback to message '%s' didn't call gras_msg_rpcreturn) "
+ "AND IN SIMGRID (process wasn't killed by an assert)",
+ msg.type->name);
+ if (!ran_ok)
+ THROW1(mismatch_error, 0,
+ "Message '%s' refused by all registered callbacks (maybe your callback misses a 'return 0' at the end)",
+ msg.type->name);
+ /* FIXME: gras_datadesc_free not implemented => leaking the payload */
+ break;
+
+
+ case e_gras_msg_kind_rpcanswer:
+ INFO3("Unexpected RPC answer discarded (type: %s; from:%s:%d)",
+ msg.type->name, gras_socket_peer_name(msg.expe),
+ gras_socket_peer_port(msg.expe));
+ WARN0("FIXME: gras_datadesc_free not implemented => leaking the payload");
+ return;
+
+ case e_gras_msg_kind_rpcerror:
+ INFO3("Unexpected RPC error discarded (type: %s; from:%s:%d)",
+ msg.type->name, gras_socket_peer_name(msg.expe),
+ gras_socket_peer_port(msg.expe));
+ WARN0("FIXME: gras_datadesc_free not implemented => leaking the payload");
+ return;
+
+ default:
+ THROW1(unknown_error, 0,
+ "Cannot handle messages of kind %d yet", msg.type->kind);
+ }