+ THROW_IMPOSSIBLE;
+}
+/** \brief Waits for a message to come in over a given socket.
+ *
+ * @param timeout: How long should we wait for this message.
+ * @param msgt_want: type of awaited msg
+ * @param[out] expeditor: where to create a socket to answer the incomming message
+ * @param[out] payload: where to write the payload of the incomming message
+ * @return the error code (or no_error).
+ *
+ * Every message of another type received before the one waited will be queued
+ * and used by subsequent call to this function or gras_msg_handle().
+ */
+void
+gras_msg_wait_(double timeout,
+ gras_msgtype_t msgt_want,
+ gras_socket_t *expeditor,
+ void *payload) {
+ s_gras_msg_t msg;
+
+ gras_msg_wait_ext_(timeout,
+ msgt_want, NULL, NULL, NULL,
+ &msg);
+
+ if (msgt_want->ctn_type) {
+ xbt_assert1(payload,
+ "Message type '%s' convey a payload you must accept",
+ msgt_want->name);
+ } else {
+ xbt_assert1(!payload,
+ "No payload was declared for message type '%s'",
+ msgt_want->name);
+ }
+
+ if (payload) {
+ memcpy(payload,msg.payl,msg.payl_size);
+ free(msg.payl);
+ }
+
+ if (expeditor)
+ *expeditor = msg.expe;
+}
+
+static int gras_msg_wait_or_filter(gras_msg_t msg, void *ctx) {
+ xbt_dynar_t dyn=(xbt_dynar_t)ctx;
+ int res = xbt_dynar_member(dyn,msg->type);
+ if (res)
+ VERB1("Got matching message (type=%s)",msg->type->name);
+ else
+ VERB0("Got message not matching our expectations");
+ return res;
+}
+/** \brief Waits for a message to come in over a given socket.
+ *
+ * @param timeout: How long should we wait for this message.
+ * @param msgt_want: a dynar containing all accepted message type
+ * @param[out] ctx: the context of received message (in case it's a RPC call we want to answer to)
+ * @param[out] msgt_got: indice in the dynar of the type of the received message
+ * @param[out] payload: where to write the payload of the incomming message
+ * @return the error code (or no_error).
+ *
+ * Every message of a type not in the accepted list received before the one
+ * waited will be queued and used by subsequent call to this function or
+ * gras_msg_handle().
+ *
+ * If you are interested in the context, pass the address of a s_gras_msg_cb_ctx_t variable.
+ */
+void gras_msg_wait_or(double timeout,
+ xbt_dynar_t msgt_want,
+ gras_msg_cb_ctx_t *ctx,
+ int *msgt_got,
+ void *payload) {
+ s_gras_msg_t msg;
+
+ VERB1("Wait %f seconds for several message types",timeout);
+ gras_msg_wait_ext_(timeout,
+ NULL, NULL,
+ &gras_msg_wait_or_filter, (void*)msgt_want,
+ &msg);
+
+ if (msg.type->ctn_type) {
+ xbt_assert1(payload,
+ "Message type '%s' convey a payload you must accept",
+ msg.type->name);
+ } /* don't check the other side since some of the types may have a payload */
+
+ if (payload && msg.type->ctn_type) {
+ memcpy(payload,msg.payl,msg.payl_size);
+ free(msg.payl);
+ }
+
+ if (ctx)
+ *ctx=gras_msg_cb_ctx_new(msg.expe, msg.type, msg.ID,
+ (msg.kind == e_gras_msg_kind_rpccall), 60);
+
+ if (msgt_got)
+ *msgt_got = xbt_dynar_search(msgt_want,msg.type);
+}
+
+
+/** \brief Send the data pointed by \a payload as a message of type
+ * \a msgtype to the peer \a sock */
+void
+gras_msg_send_(gras_socket_t sock,
+ gras_msgtype_t msgtype,
+ void *payload) {
+
+ if (msgtype->ctn_type) {
+ xbt_assert1(payload,
+ "Message type '%s' convey a payload you must provide",
+ msgtype->name);
+ } else {
+ xbt_assert1(!payload,
+ "No payload was declared for message type '%s'",
+ msgtype->name);
+ }
+
+ DEBUG2("Send a oneway message of type '%s'. Payload=%p",
+ msgtype->name,payload);
+ gras_msg_send_ext(sock, e_gras_msg_kind_oneway,0, msgtype, payload);
+ VERB2("Sent a oneway message of type '%s'. Payload=%p",
+ msgtype->name,payload);
+}
+
+/** @brief Handle all messages arriving within the given period
+ *
+ * @param period: How long to wait for incoming messages (in seconds)
+ *
+ * Messages are dealed with just like gras_msg_handle() would do. The
+ * difference is that gras_msg_handle() handles at most one message (or wait up
+ * to timeout second when no message arrives) while this function handles any
+ * amount of messages, and lasts the given period in any case.
+ */
+void
+gras_msg_handleall(double period) {
+ xbt_ex_t e;
+ double begin=gras_os_time();
+ double now;
+
+ do {
+ now=gras_os_time();
+ TRY{
+ if (period - now + begin > 0)
+ gras_msg_handle(period - now + begin);
+ } CATCH(e) {
+ if (e.category != timeout_error)
+ RETHROW0("Error while waiting for messages: %s");
+ xbt_ex_free(e);
+ }
+ } while (now - begin < period);