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_DEFAULT_CATEGORY(gras_msg);
18 void gras_msg_recv(xbt_socket_t sock, gras_msg_t msg);
20 gras_msg_t gras_msg_recv_any(void)
22 gras_msg_t msg = xbt_new0(s_gras_msg_t, 1);
23 msg->expe = gras_trp_select(-1);
24 XBT_DEBUG("Select returned something");
25 gras_msg_recv(msg->expe, msg);
29 void gras_msg_send_ext(xbt_socket_t sock,
30 e_gras_msg_kind_t kind,
32 gras_msgtype_t msgtype, void *payload)
35 static xbt_datadesc_type_t string_type = NULL;
36 static xbt_datadesc_type_t ulong_type = NULL;
37 char c_kind = (char) kind;
39 xbt_assert(msgtype, "Cannot send the NULL message");
42 string_type = xbt_datadesc_by_name("string");
43 xbt_assert(string_type);
46 ulong_type = xbt_datadesc_by_name("unsigned long int");
47 xbt_assert(ulong_type);
50 XBT_DEBUG("send '%s' to %s:%d", msgtype->name,
51 xbt_socket_peer_name(sock), xbt_socket_peer_port(sock));
52 xbt_trp_send(sock, _GRAS_header, 6, 1 /* stable */ );
53 xbt_trp_send(sock, &c_kind, 1, 1 /* stable */ );
55 case e_gras_msg_kind_oneway:
58 case e_gras_msg_kind_rpccall:
59 case e_gras_msg_kind_rpcanswer:
60 case e_gras_msg_kind_rpcerror:
61 xbt_datadesc_send(sock, ulong_type, &ID);
65 THROWF(unknown_error, 0, "Unknown msg kind %d", (int)kind);
68 xbt_datadesc_send(sock, string_type, &msgtype->name);
69 if (kind == e_gras_msg_kind_rpcerror) {
70 /* error on remote host, carfull, payload is an exception */
71 xbt_datadesc_send(sock, xbt_datadesc_by_name("ex_t"), payload);
72 } else if (kind == e_gras_msg_kind_rpcanswer) {
73 if (msgtype->answer_type)
74 xbt_datadesc_send(sock, msgtype->answer_type, payload);
77 if (msgtype->ctn_type)
78 xbt_datadesc_send(sock, msgtype->ctn_type, payload);
84 const char *hexa_str(unsigned char *data, int size, int downside);
88 * receive the next message on the given socket.
90 void gras_msg_recv(xbt_socket_t sock, gras_msg_t msg)
94 static xbt_datadesc_type_t string_type = NULL;
95 static xbt_datadesc_type_t ulong_type = NULL;
99 char *msg_name = NULL;
102 xbt_assert(!xbt_socket_is_meas(sock),
103 "Asked to receive a message on the measurement socket %p",
106 string_type = xbt_datadesc_by_name("string");
107 xbt_assert(string_type);
110 ulong_type = xbt_datadesc_by_name("unsigned long int");
111 xbt_assert(ulong_type);
116 xbt_trp_recv(sock, header, 6);
117 xbt_trp_recv(sock, &c_kind, 1);
118 msg->kind = (e_gras_msg_kind_t) c_kind;
122 ("Exception caught while trying to get the message header: %s");
125 for (cpt = 0; cpt < 4; cpt++)
126 if (header[cpt] != _GRAS_header[cpt])
127 THROWF(mismatch_error, 0,
128 "Incoming bytes do not look like a GRAS message (header='%s' not '%.4s')",
129 hexa_str((unsigned char *) header, 4, 0), _GRAS_header);
130 if (header[4] != _GRAS_header[4])
131 THROWF(mismatch_error, 0, "GRAS protocol mismatch (got %d, use %d)",
132 (int) header[4], (int) _GRAS_header[4]);
133 r_arch = (int) header[5];
136 case e_gras_msg_kind_oneway:
139 case e_gras_msg_kind_rpccall:
140 case e_gras_msg_kind_rpcanswer:
141 case e_gras_msg_kind_rpcerror:
142 xbt_datadesc_recv(sock, ulong_type, r_arch, &msg->ID);
149 xbt_datadesc_recv(sock, string_type, r_arch, &msg_name);
151 ("Handle an incoming message '%s' (%s) using protocol %d (remote is %s)",
152 msg_name, e_gras_msg_kind_names[msg->kind], (int) header[4],
153 xbt_datadesc_arch_name(r_arch));
157 (gras_msgtype_t) xbt_set_get_by_name(_gras_msgtype_set, msg_name);
160 /* FIXME: Survive unknown messages */
161 if (e.category == not_found_error) {
163 THROWF(not_found_error, 0,
164 "Received an unknown message: %s (FIXME: should survive to these)",
168 ("Exception caught while retrieving the type associated to messages '%s' : %s",
173 if (msg->kind == e_gras_msg_kind_rpcerror) {
174 /* error on remote host. Carfull with that exception, eugene */
175 msg->payl_size = xbt_datadesc_size(xbt_datadesc_by_name("ex_t"));
176 msg->payl = xbt_malloc(msg->payl_size);
177 xbt_datadesc_recv(sock, xbt_datadesc_by_name("ex_t"), r_arch,
180 } else if (msg->kind == e_gras_msg_kind_rpcanswer) {
182 if (msg->type->answer_type) {
183 msg->payl_size = xbt_datadesc_size(msg->type->answer_type);
184 xbt_assert(msg->payl_size > 0,
186 "Dynamic array as payload is forbided for now (FIXME?).",
187 "Reference to dynamic array is allowed.");
188 msg->payl = xbt_malloc(msg->payl_size);
189 xbt_datadesc_recv(sock, msg->type->answer_type, r_arch, msg->payl);
195 /* regular message */
196 if (msg->type->ctn_type) {
197 msg->payl_size = xbt_datadesc_size(msg->type->ctn_type);
198 xbt_assert(msg->payl_size > 0,
200 "Dynamic array as payload is forbided for now (FIXME?).",
201 "Reference to dynamic array is allowed.");
202 msg->payl = xbt_malloc(msg->payl_size);
203 xbt_datadesc_recv(sock, msg->type->ctn_type, r_arch, msg->payl);