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 void gras_msg_send_ext(gras_socket_t sock,
20 e_gras_msg_kind_t kind,
22 gras_msgtype_t msgtype,
25 static gras_datadesc_type_t string_type=NULL;
26 static gras_datadesc_type_t ulong_type=NULL;
27 char c_kind=(char)kind;
29 xbt_assert0(msgtype,"Cannot send the NULL message");
32 string_type = gras_datadesc_by_name("string");
33 xbt_assert(string_type);
36 ulong_type = gras_datadesc_by_name("unsigned long int");
37 xbt_assert(ulong_type);
40 DEBUG3("send '%s' to %s:%d", msgtype->name,
41 gras_socket_peer_name(sock),gras_socket_peer_port(sock));
42 gras_trp_send(sock, _GRAS_header, 6, 1 /* stable */);
43 gras_trp_send(sock, &c_kind, 1, 1 /* stable */);
45 case e_gras_msg_kind_oneway:
48 case e_gras_msg_kind_rpccall:
49 case e_gras_msg_kind_rpcanswer:
50 case e_gras_msg_kind_rpcerror:
51 gras_datadesc_send(sock,ulong_type,&ID);
55 THROW1(unknown_error,0,"Unknown msg kind %d",kind);
58 gras_datadesc_send(sock, string_type, &msgtype->name);
59 if (kind == e_gras_msg_kind_rpcerror) {
60 /* error on remote host, carfull, payload is an exception */
61 gras_datadesc_send(sock, gras_datadesc_by_name("ex_t"),payload);
62 } else if (kind == e_gras_msg_kind_rpcanswer) {
63 if (msgtype->answer_type)
64 gras_datadesc_send(sock, msgtype->answer_type, payload);
67 if (msgtype->ctn_type)
68 gras_datadesc_send(sock, msgtype->ctn_type, payload);
74 const char *hexa_str(unsigned char *data, int size, int downside);
78 * receive the next message on the given socket.
81 gras_msg_recv(gras_socket_t sock,
85 static gras_datadesc_type_t string_type=NULL;
86 static gras_datadesc_type_t ulong_type=NULL;
93 xbt_assert1(!gras_socket_is_meas(sock),
94 "Asked to receive a message on the measurement socket %p", sock);
96 string_type=gras_datadesc_by_name("string");
97 xbt_assert(string_type);
100 ulong_type = gras_datadesc_by_name("unsigned long int");
101 xbt_assert(ulong_type);
106 gras_trp_recv(sock, header, 6);
107 gras_trp_recv(sock, &c_kind, 1);
108 msg->kind=(e_gras_msg_kind_t)c_kind;
110 RETHROW0("Exception caught while trying to get the mesage header: %s");
113 for (cpt=0; cpt<4; cpt++)
114 if (header[cpt] != _GRAS_header[cpt])
115 THROW2(mismatch_error,0,
116 "Incoming bytes do not look like a GRAS message (header='%s' not '%.4s')",
117 hexa_str((unsigned char*)header,4,0),_GRAS_header);
118 if (header[4] != _GRAS_header[4])
119 THROW2(mismatch_error,0,"GRAS protocol mismatch (got %d, use %d)",
120 (int)header[4], (int)_GRAS_header[4]);
121 r_arch = (int)header[5];
124 case e_gras_msg_kind_oneway:
127 case e_gras_msg_kind_rpccall:
128 case e_gras_msg_kind_rpcanswer:
129 case e_gras_msg_kind_rpcerror:
130 gras_datadesc_recv(sock,ulong_type,r_arch, &msg->ID);
137 gras_datadesc_recv(sock, string_type, r_arch, &msg_name);
138 DEBUG4("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],gras_datadesc_arch_name(r_arch));
142 msg->type = (gras_msgtype_t)xbt_set_get_by_name(_gras_msgtype_set,msg_name);
144 /* FIXME: Survive unknown messages */
145 if (e.category == not_found_error) {
147 THROW1(not_found_error,0, "Received an unknown message: %s (FIXME: should survive to these)", msg_name);
149 RETHROW1("Exception caught while retrieving the type associated to messages '%s' : %s",
154 if (msg->kind == e_gras_msg_kind_rpcerror) {
155 /* error on remote host. Carfull with that exception, eugene */
156 msg->payl_size=gras_datadesc_size(gras_datadesc_by_name("ex_t"));
157 msg->payl=xbt_malloc(msg->payl_size);
158 gras_datadesc_recv(sock, gras_datadesc_by_name("ex_t"), r_arch, msg->payl);
160 } else if (msg->kind == e_gras_msg_kind_rpcanswer) {
162 if (msg->type->answer_type) {
163 msg->payl_size=gras_datadesc_size(msg->type->answer_type);
164 xbt_assert2(msg->payl_size > 0,
166 "Dynamic array as payload is forbided for now (FIXME?).",
167 "Reference to dynamic array is allowed.");
168 msg->payl = xbt_malloc(msg->payl_size);
169 gras_datadesc_recv(sock, msg->type->answer_type, r_arch, msg->payl);
175 /* regular message */
176 if (msg->type->ctn_type) {
177 msg->payl_size=gras_datadesc_size(msg->type->ctn_type);
178 xbt_assert2(msg->payl_size > 0,
180 "Dynamic array as payload is forbided for now (FIXME?).",
181 "Reference to dynamic array is allowed.");
182 msg->payl = xbt_malloc(msg->payl_size);
183 gras_datadesc_recv(sock, msg->type->ctn_type, r_arch, msg->payl);