1 /* messaging - Function related to messaging code specific to RL */
3 /* Copyright (c) 2005, 2006, 2007, 2009, 2010. The SimGrid Team.
4 * All rights reserved. */
6 /* This program is free software; you can redistribute it and/or modify it
7 * under the terms of the license (GNU LGPL) which comes with this package. */
10 #include "xbt/datadesc.h"
11 #include "xbt/datadesc/datadesc_interface.h"
12 #include "xbt/socket.h"
13 #include "gras/Msg/msg_private.h"
14 #include "gras/Transport/transport_interface.h"
16 XBT_LOG_EXTERNAL_CATEGORY(gras_msg);
17 XBT_LOG_DEFAULT_CATEGORY(gras_msg);
19 void gras_msg_recv(xbt_socket_t sock, gras_msg_t msg);
21 gras_msg_t gras_msg_recv_any(void)
23 gras_msg_t msg = xbt_new0(s_gras_msg_t, 1);
24 msg->expe = gras_trp_select(-1);
25 XBT_DEBUG("Select returned something");
26 gras_msg_recv(msg->expe, msg);
30 void gras_msg_send_ext(xbt_socket_t sock,
31 e_gras_msg_kind_t kind,
33 gras_msgtype_t msgtype, void *payload)
36 static xbt_datadesc_type_t string_type = NULL;
37 static xbt_datadesc_type_t ulong_type = NULL;
38 char c_kind = (char) kind;
40 xbt_assert(msgtype, "Cannot send the NULL message");
43 string_type = xbt_datadesc_by_name("string");
44 xbt_assert(string_type);
47 ulong_type = xbt_datadesc_by_name("unsigned long int");
48 xbt_assert(ulong_type);
51 XBT_DEBUG("send '%s' to %s:%d", msgtype->name,
52 xbt_socket_peer_name(sock), xbt_socket_peer_port(sock));
53 xbt_trp_send(sock, _GRAS_header, 6, 1 /* stable */ );
54 xbt_trp_send(sock, &c_kind, 1, 1 /* stable */ );
56 case e_gras_msg_kind_oneway:
59 case e_gras_msg_kind_rpccall:
60 case e_gras_msg_kind_rpcanswer:
61 case e_gras_msg_kind_rpcerror:
62 xbt_datadesc_send(sock, ulong_type, &ID);
66 THROWF(unknown_error, 0, "Unknown msg kind %d", kind);
69 xbt_datadesc_send(sock, string_type, &msgtype->name);
70 if (kind == e_gras_msg_kind_rpcerror) {
71 /* error on remote host, carfull, payload is an exception */
72 xbt_datadesc_send(sock, xbt_datadesc_by_name("ex_t"), payload);
73 } else if (kind == e_gras_msg_kind_rpcanswer) {
74 if (msgtype->answer_type)
75 xbt_datadesc_send(sock, msgtype->answer_type, payload);
78 if (msgtype->ctn_type)
79 xbt_datadesc_send(sock, msgtype->ctn_type, payload);
85 const char *hexa_str(unsigned char *data, int size, int downside);
89 * receive the next message on the given socket.
91 void gras_msg_recv(xbt_socket_t sock, gras_msg_t msg)
95 static xbt_datadesc_type_t string_type = NULL;
96 static xbt_datadesc_type_t ulong_type = NULL;
100 char *msg_name = NULL;
103 xbt_assert(!xbt_socket_is_meas(sock),
104 "Asked to receive a message on the measurement socket %p",
107 string_type = xbt_datadesc_by_name("string");
108 xbt_assert(string_type);
111 ulong_type = xbt_datadesc_by_name("unsigned long int");
112 xbt_assert(ulong_type);
117 xbt_trp_recv(sock, header, 6);
118 xbt_trp_recv(sock, &c_kind, 1);
119 msg->kind = (e_gras_msg_kind_t) c_kind;
123 ("Exception caught while trying to get the message header: %s");
126 for (cpt = 0; cpt < 4; cpt++)
127 if (header[cpt] != _GRAS_header[cpt])
128 THROWF(mismatch_error, 0,
129 "Incoming bytes do not look like a GRAS message (header='%s' not '%.4s')",
130 hexa_str((unsigned char *) header, 4, 0), _GRAS_header);
131 if (header[4] != _GRAS_header[4])
132 THROWF(mismatch_error, 0, "GRAS protocol mismatch (got %d, use %d)",
133 (int) header[4], (int) _GRAS_header[4]);
134 r_arch = (int) header[5];
137 case e_gras_msg_kind_oneway:
140 case e_gras_msg_kind_rpccall:
141 case e_gras_msg_kind_rpcanswer:
142 case e_gras_msg_kind_rpcerror:
143 xbt_datadesc_recv(sock, ulong_type, r_arch, &msg->ID);
150 xbt_datadesc_recv(sock, string_type, r_arch, &msg_name);
152 ("Handle an incoming message '%s' (%s) using protocol %d (remote is %s)",
153 msg_name, e_gras_msg_kind_names[msg->kind], (int) header[4],
154 xbt_datadesc_arch_name(r_arch));
158 (gras_msgtype_t) xbt_set_get_by_name(_gras_msgtype_set, msg_name);
161 /* FIXME: Survive unknown messages */
162 if (e.category == not_found_error) {
164 THROWF(not_found_error, 0,
165 "Received an unknown message: %s (FIXME: should survive to these)",
169 ("Exception caught while retrieving the type associated to messages '%s' : %s",
174 if (msg->kind == e_gras_msg_kind_rpcerror) {
175 /* error on remote host. Carfull with that exception, eugene */
176 msg->payl_size = xbt_datadesc_size(xbt_datadesc_by_name("ex_t"));
177 msg->payl = xbt_malloc(msg->payl_size);
178 xbt_datadesc_recv(sock, xbt_datadesc_by_name("ex_t"), r_arch,
181 } else if (msg->kind == e_gras_msg_kind_rpcanswer) {
183 if (msg->type->answer_type) {
184 msg->payl_size = xbt_datadesc_size(msg->type->answer_type);
185 xbt_assert(msg->payl_size > 0,
187 "Dynamic array as payload is forbided for now (FIXME?).",
188 "Reference to dynamic array is allowed.");
189 msg->payl = xbt_malloc(msg->payl_size);
190 xbt_datadesc_recv(sock, msg->type->answer_type, r_arch, msg->payl);
196 /* regular message */
197 if (msg->type->ctn_type) {
198 msg->payl_size = xbt_datadesc_size(msg->type->ctn_type);
199 xbt_assert(msg->payl_size > 0,
201 "Dynamic array as payload is forbided for now (FIXME?).",
202 "Reference to dynamic array is allowed.");
203 msg->payl = xbt_malloc(msg->payl_size);
204 xbt_datadesc_recv(sock, msg->type->ctn_type, r_arch, msg->payl);