3 /* messaging - Function related to messaging code specific to RL */
5 /* Copyright (c) 2003-2005 Martin Quinson. All rights reserved. */
7 /* This program is free software; you can redistribute it and/or modify it
8 * under the terms of the license (GNU LGPL) which comes with this package. */
11 #include "gras/Msg/msg_private.h"
13 #include "gras/DataDesc/datadesc_interface.h"
14 #include "gras/Transport/transport_interface.h" /* gras_trp_send/recv */
16 XBT_LOG_EXTERNAL_CATEGORY(gras_msg);
17 XBT_LOG_DEFAULT_CATEGORY(gras_msg);
19 /** \brief Send the data pointed by \a payload as a message of type
20 * \a msgtype to the peer \a sock */
22 gras_msg_send(gras_socket_t sock,
23 gras_msgtype_t msgtype,
26 static gras_datadesc_type_t string_type=NULL;
30 "Cannot send the NULL message (did msgtype_by_name fail?)");
33 string_type = gras_datadesc_by_name("string");
34 xbt_assert(string_type);
37 DEBUG3("send '%s' to %s:%d", msgtype->name,
38 gras_socket_peer_name(sock),gras_socket_peer_port(sock));
39 gras_trp_send(sock, _GRAS_header, 6, 1 /* stable */);
41 gras_datadesc_send(sock, string_type, &msgtype->name);
42 if (msgtype->ctn_type)
43 gras_datadesc_send(sock, msgtype->ctn_type, payload);
47 * receive the next message on the given socket.
50 gras_msg_recv(gras_socket_t sock,
51 gras_msgtype_t *msgtype,
56 static gras_datadesc_type_t string_type=NULL;
62 xbt_assert1(!gras_socket_is_meas(sock),
63 "Asked to receive a message on the measurement socket %p", sock);
65 string_type=gras_datadesc_by_name("string");
66 xbt_assert(string_type);
70 gras_trp_recv(sock, header, 6);
72 RETHROW1("Exception caught while trying to get the mesage header on socket %p: %s",
76 for (cpt=0; cpt<4; cpt++)
77 if (header[cpt] != _GRAS_header[cpt])
78 THROW2(mismatch_error,0,
79 "Incoming bytes do not look like a GRAS message (header='%.4s' not '%.4s')",header,_GRAS_header);
80 if (header[4] != _GRAS_header[4])
81 THROW2(mismatch_error,0,"GRAS protocol mismatch (got %d, use %d)",
82 (int)header[4], (int)_GRAS_header[4]);
83 r_arch = (int)header[5];
84 DEBUG2("Handle an incoming message using protocol %d (remote is %s)",
85 (int)header[4],gras_datadesc_arch_name(r_arch));
87 gras_datadesc_recv(sock, string_type, r_arch, &msg_name);
89 *msgtype = (gras_msgtype_t)xbt_set_get_by_name(_gras_msgtype_set,msg_name);
91 /* FIXME: Survive unknown messages */
92 RETHROW1("Exception caught while retrieving the type associated to messages '%s' : %s",
97 if ((*msgtype)->ctn_type) {
98 *payload_size=gras_datadesc_size((*msgtype)->ctn_type);
99 xbt_assert2(*payload_size > 0,
101 "Dynamic array as payload is forbided for now (FIXME?).",
102 "Reference to dynamic array is allowed.");
103 *payload = xbt_malloc(*payload_size);
104 gras_datadesc_recv(sock, (*msgtype)->ctn_type, r_arch, *payload);