1 /* messaging - Function related to messaging code specific to RL */
3 /* Copyright (c) 2003-2005 Martin Quinson. All rights reserved. */
5 /* This program is free software; you can redistribute it and/or modify it
6 * under the terms of the license (GNU LGPL) which comes with this package. */
9 #include "gras/Msg/msg_private.h"
11 #include "gras/DataDesc/datadesc_interface.h"
12 #include "gras/Transport/transport_interface.h" /* gras_trp_send/recv */
14 XBT_LOG_EXTERNAL_CATEGORY(gras_msg);
15 XBT_LOG_DEFAULT_CATEGORY(gras_msg);
17 void gras_msg_send_ext(gras_socket_t sock,
18 e_gras_msg_kind_t kind,
20 gras_msgtype_t msgtype, void *payload)
23 static gras_datadesc_type_t string_type = NULL;
24 static gras_datadesc_type_t ulong_type = NULL;
25 char c_kind = (char) kind;
27 xbt_assert0(msgtype, "Cannot send the NULL message");
30 string_type = gras_datadesc_by_name("string");
31 xbt_assert(string_type);
34 ulong_type = gras_datadesc_by_name("unsigned long int");
35 xbt_assert(ulong_type);
38 DEBUG3("send '%s' to %s:%d", msgtype->name,
39 gras_socket_peer_name(sock), gras_socket_peer_port(sock));
40 gras_trp_send(sock, _GRAS_header, 6, 1 /* stable */ );
41 gras_trp_send(sock, &c_kind, 1, 1 /* stable */ );
43 case e_gras_msg_kind_oneway:
46 case e_gras_msg_kind_rpccall:
47 case e_gras_msg_kind_rpcanswer:
48 case e_gras_msg_kind_rpcerror:
49 gras_datadesc_send(sock, ulong_type, &ID);
53 THROW1(unknown_error, 0, "Unknown msg kind %d", kind);
56 gras_datadesc_send(sock, string_type, &msgtype->name);
57 if (kind == e_gras_msg_kind_rpcerror) {
58 /* error on remote host, carfull, payload is an exception */
59 gras_datadesc_send(sock, gras_datadesc_by_name("ex_t"), payload);
60 } else if (kind == e_gras_msg_kind_rpcanswer) {
61 if (msgtype->answer_type)
62 gras_datadesc_send(sock, msgtype->answer_type, payload);
65 if (msgtype->ctn_type)
66 gras_datadesc_send(sock, msgtype->ctn_type, payload);
72 const char *hexa_str(unsigned char *data, int size, int downside);
76 * receive the next message on the given socket.
78 void gras_msg_recv(gras_socket_t sock, gras_msg_t msg)
82 static gras_datadesc_type_t string_type = NULL;
83 static gras_datadesc_type_t ulong_type = NULL;
87 char *msg_name = NULL;
90 xbt_assert1(!gras_socket_is_meas(sock),
91 "Asked to receive a message on the measurement socket %p",
94 string_type = gras_datadesc_by_name("string");
95 xbt_assert(string_type);
98 ulong_type = gras_datadesc_by_name("unsigned long int");
99 xbt_assert(ulong_type);
104 gras_trp_recv(sock, header, 6);
105 gras_trp_recv(sock, &c_kind, 1);
106 msg->kind = (e_gras_msg_kind_t) c_kind;
109 RETHROW0("Exception caught while trying to get the mesage header: %s");
112 for (cpt = 0; cpt < 4; cpt++)
113 if (header[cpt] != _GRAS_header[cpt])
114 THROW2(mismatch_error, 0,
115 "Incoming bytes do not look like a GRAS message (header='%s' not '%.4s')",
116 hexa_str((unsigned char *) header, 4, 0), _GRAS_header);
117 if (header[4] != _GRAS_header[4])
118 THROW2(mismatch_error, 0, "GRAS protocol mismatch (got %d, use %d)",
119 (int) header[4], (int) _GRAS_header[4]);
120 r_arch = (int) header[5];
123 case e_gras_msg_kind_oneway:
126 case e_gras_msg_kind_rpccall:
127 case e_gras_msg_kind_rpcanswer:
128 case e_gras_msg_kind_rpcerror:
129 gras_datadesc_recv(sock, ulong_type, r_arch, &msg->ID);
136 gras_datadesc_recv(sock, string_type, r_arch, &msg_name);
138 ("Handle an incoming message '%s' (%s) using protocol %d (remote is %s)",
139 msg_name, e_gras_msg_kind_names[msg->kind], (int) header[4],
140 gras_datadesc_arch_name(r_arch));
144 (gras_msgtype_t) xbt_set_get_by_name(_gras_msgtype_set, msg_name);
146 /* FIXME: Survive unknown messages */
147 if (e.category == not_found_error) {
149 THROW1(not_found_error, 0,
150 "Received an unknown message: %s (FIXME: should survive to these)",
154 ("Exception caught while retrieving the type associated to messages '%s' : %s",
159 if (msg->kind == e_gras_msg_kind_rpcerror) {
160 /* error on remote host. Carfull with that exception, eugene */
161 msg->payl_size = gras_datadesc_size(gras_datadesc_by_name("ex_t"));
162 msg->payl = xbt_malloc(msg->payl_size);
163 gras_datadesc_recv(sock, gras_datadesc_by_name("ex_t"), r_arch,
166 } else if (msg->kind == e_gras_msg_kind_rpcanswer) {
168 if (msg->type->answer_type) {
169 msg->payl_size = gras_datadesc_size(msg->type->answer_type);
170 xbt_assert2(msg->payl_size > 0,
172 "Dynamic array as payload is forbided for now (FIXME?).",
173 "Reference to dynamic array is allowed.");
174 msg->payl = xbt_malloc(msg->payl_size);
175 gras_datadesc_recv(sock, msg->type->answer_type, r_arch, msg->payl);
181 /* regular message */
182 if (msg->type->ctn_type) {
183 msg->payl_size = gras_datadesc_size(msg->type->ctn_type);
184 xbt_assert2(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 gras_datadesc_recv(sock, msg->type->ctn_type, r_arch, msg->payl);