Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
7a277cb91fbe7597ae670e40d6ba69d564a9216e
[simgrid.git] / src / gras / Msg / rl_msg.c
1 /* messaging - Function related to messaging code specific to RL            */
2
3 /* Copyright (c) 2003-2005 Martin Quinson. All rights reserved.             */
4
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. */
7
8 #include "xbt/ex.h"
9 #include "gras/Msg/msg_private.h"
10
11 #include "gras/DataDesc/datadesc_interface.h"
12 #include "gras/Transport/transport_interface.h" /* gras_trp_send/recv */
13
14 XBT_LOG_EXTERNAL_CATEGORY(gras_msg);
15 XBT_LOG_DEFAULT_CATEGORY(gras_msg);
16
17 void gras_msg_send_ext(gras_socket_t sock,
18                        e_gras_msg_kind_t kind,
19                        unsigned long int ID,
20                        gras_msgtype_t msgtype, void *payload)
21 {
22
23   static gras_datadesc_type_t string_type = NULL;
24   static gras_datadesc_type_t ulong_type = NULL;
25   char c_kind = (char) kind;
26
27   xbt_assert0(msgtype, "Cannot send the NULL message");
28
29   if (!string_type) {
30     string_type = gras_datadesc_by_name("string");
31     xbt_assert(string_type);
32   }
33   if (!ulong_type) {
34     ulong_type = gras_datadesc_by_name("unsigned long int");
35     xbt_assert(ulong_type);
36   }
37
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 */ );
42   switch (kind) {
43   case e_gras_msg_kind_oneway:
44     break;
45
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);
50     break;
51
52   default:
53     THROW1(unknown_error, 0, "Unknown msg kind %d", kind);
54   }
55
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);
63   } else {
64     /* regular message */
65     if (msgtype->ctn_type)
66       gras_datadesc_send(sock, msgtype->ctn_type, payload);
67   }
68
69   gras_trp_flush(sock);
70 }
71
72 const char *hexa_str(unsigned char *data, int size, int downside);
73
74
75 /*
76  * receive the next message on the given socket.
77  */
78 void gras_msg_recv(gras_socket_t sock, gras_msg_t msg)
79 {
80
81   xbt_ex_t e;
82   static gras_datadesc_type_t string_type = NULL;
83   static gras_datadesc_type_t ulong_type = NULL;
84   char header[6];
85   int cpt;
86   int r_arch;
87   char *msg_name = NULL;
88   char c_kind;
89
90   xbt_assert1(!gras_socket_is_meas(sock),
91               "Asked to receive a message on the measurement socket %p",
92               sock);
93   if (!string_type) {
94     string_type = gras_datadesc_by_name("string");
95     xbt_assert(string_type);
96   }
97   if (!ulong_type) {
98     ulong_type = gras_datadesc_by_name("unsigned long int");
99     xbt_assert(ulong_type);
100   }
101
102
103   TRY {
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;
107   }
108   CATCH(e) {
109     RETHROW0("Exception caught while trying to get the mesage header: %s");
110   }
111
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];
121
122   switch (msg->kind) {
123   case e_gras_msg_kind_oneway:
124     break;
125
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);
130     break;
131
132   default:
133     THROW_IMPOSSIBLE;
134   }
135
136   gras_datadesc_recv(sock, string_type, r_arch, &msg_name);
137   DEBUG4
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));
141
142   TRY {
143     msg->type =
144       (gras_msgtype_t) xbt_set_get_by_name(_gras_msgtype_set, msg_name);
145   } CATCH(e) {
146     /* FIXME: Survive unknown messages */
147     if (e.category == not_found_error) {
148       xbt_ex_free(e);
149       THROW1(not_found_error, 0,
150              "Received an unknown message: %s (FIXME: should survive to these)",
151              msg_name);
152     } else
153       RETHROW1
154         ("Exception caught while retrieving the type associated to messages '%s' : %s",
155          msg_name);
156   }
157   free(msg_name);
158
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,
164                        msg->payl);
165
166   } else if (msg->kind == e_gras_msg_kind_rpcanswer) {
167     /* answer to RPC */
168     if (msg->type->answer_type) {
169       msg->payl_size = gras_datadesc_size(msg->type->answer_type);
170       xbt_assert2(msg->payl_size > 0,
171                   "%s %s",
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);
176     } else {
177       msg->payl = NULL;
178       msg->payl_size = 0;
179     }
180   } else {
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,
185                   "%s %s",
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);
190     } else {
191       msg->payl = NULL;
192       msg->payl_size = 0;
193     }
194   }
195 }