Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Don't pass NULL to xbt_die.
[simgrid.git] / src / gras / Msg / rl_msg.c
1 /* messaging - Function related to messaging code specific to RL            */
2
3 /* Copyright (c) 2005, 2006, 2007, 2009, 2010. The SimGrid Team.
4  * All rights reserved.                                                     */
5
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. */
8
9 #include "xbt/ex.h"
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"
15
16 XBT_LOG_EXTERNAL_CATEGORY(gras_msg);
17 XBT_LOG_DEFAULT_CATEGORY(gras_msg);
18
19 void gras_msg_recv(xbt_socket_t sock, gras_msg_t msg);
20
21 gras_msg_t gras_msg_recv_any(void)
22 {
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);
27   return msg;
28 }
29
30 void gras_msg_send_ext(xbt_socket_t sock,
31                        e_gras_msg_kind_t kind,
32                        unsigned long int ID,
33                        gras_msgtype_t msgtype, void *payload)
34 {
35
36   static xbt_datadesc_type_t string_type = NULL;
37   static xbt_datadesc_type_t ulong_type = NULL;
38   char c_kind = (char) kind;
39
40   xbt_assert(msgtype, "Cannot send the NULL message");
41
42   if (!string_type) {
43     string_type = xbt_datadesc_by_name("string");
44     xbt_assert(string_type);
45   }
46   if (!ulong_type) {
47     ulong_type = xbt_datadesc_by_name("unsigned long int");
48     xbt_assert(ulong_type);
49   }
50
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 */ );
55   switch (kind) {
56   case e_gras_msg_kind_oneway:
57     break;
58
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);
63     break;
64
65   default:
66     THROWF(unknown_error, 0, "Unknown msg kind %d", kind);
67   }
68
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);
76   } else {
77     /* regular message */
78     if (msgtype->ctn_type)
79       xbt_datadesc_send(sock, msgtype->ctn_type, payload);
80   }
81
82   xbt_trp_flush(sock);
83 }
84
85 const char *hexa_str(unsigned char *data, int size, int downside);
86
87
88 /*
89  * receive the next message on the given socket.
90  */
91 void gras_msg_recv(xbt_socket_t sock, gras_msg_t msg)
92 {
93
94   xbt_ex_t e;
95   static xbt_datadesc_type_t string_type = NULL;
96   static xbt_datadesc_type_t ulong_type = NULL;
97   char header[6];
98   int cpt;
99   int r_arch;
100   char *msg_name = NULL;
101   char c_kind;
102
103   xbt_assert(!xbt_socket_is_meas(sock),
104               "Asked to receive a message on the measurement socket %p",
105               sock);
106   if (!string_type) {
107     string_type = xbt_datadesc_by_name("string");
108     xbt_assert(string_type);
109   }
110   if (!ulong_type) {
111     ulong_type = xbt_datadesc_by_name("unsigned long int");
112     xbt_assert(ulong_type);
113   }
114
115
116   TRY {
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;
120   }
121   CATCH_ANONYMOUS {
122     RETHROWF
123         ("Exception caught while trying to get the message header: %s");
124   }
125
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];
135
136   switch (msg->kind) {
137   case e_gras_msg_kind_oneway:
138     break;
139
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);
144     break;
145
146   default:
147     THROW_IMPOSSIBLE;
148   }
149
150   xbt_datadesc_recv(sock, string_type, r_arch, &msg_name);
151   XBT_DEBUG
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));
155
156   TRY {
157     msg->type =
158         (gras_msgtype_t) xbt_set_get_by_name(_gras_msgtype_set, msg_name);
159   }
160   CATCH(e) {
161     /* FIXME: Survive unknown messages */
162     if (e.category == not_found_error) {
163       xbt_ex_free(e);
164       THROWF(not_found_error, 0,
165              "Received an unknown message: %s (FIXME: should survive to these)",
166              msg_name);
167     } else
168       RETHROWF
169           ("Exception caught while retrieving the type associated to messages '%s' : %s",
170            msg_name);
171   }
172   free(msg_name);
173
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,
179                        msg->payl);
180
181   } else if (msg->kind == e_gras_msg_kind_rpcanswer) {
182     /* answer to RPC */
183     if (msg->type->answer_type) {
184       msg->payl_size = xbt_datadesc_size(msg->type->answer_type);
185       xbt_assert(msg->payl_size > 0,
186                   "%s %s",
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);
191     } else {
192       msg->payl = NULL;
193       msg->payl_size = 0;
194     }
195   } else {
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,
200                   "%s %s",
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);
205     } else {
206       msg->payl = NULL;
207       msg->payl_size = 0;
208     }
209   }
210 }