Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix copyright headers
[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 "gras/Msg/msg_private.h"
11
12 #include "gras/DataDesc/datadesc_interface.h"
13 #include "gras/Transport/transport_interface.h" /* gras_trp_send/recv */
14
15 XBT_LOG_EXTERNAL_CATEGORY(gras_msg);
16 XBT_LOG_DEFAULT_CATEGORY(gras_msg);
17
18 void gras_msg_send_ext(gras_socket_t sock,
19                        e_gras_msg_kind_t kind,
20                        unsigned long int ID,
21                        gras_msgtype_t msgtype, void *payload)
22 {
23
24   static gras_datadesc_type_t string_type = NULL;
25   static gras_datadesc_type_t ulong_type = NULL;
26   char c_kind = (char) kind;
27
28   xbt_assert0(msgtype, "Cannot send the NULL message");
29
30   if (!string_type) {
31     string_type = gras_datadesc_by_name("string");
32     xbt_assert(string_type);
33   }
34   if (!ulong_type) {
35     ulong_type = gras_datadesc_by_name("unsigned long int");
36     xbt_assert(ulong_type);
37   }
38
39   DEBUG3("send '%s' to %s:%d", msgtype->name,
40          gras_socket_peer_name(sock), gras_socket_peer_port(sock));
41   gras_trp_send(sock, _GRAS_header, 6, 1 /* stable */ );
42   gras_trp_send(sock, &c_kind, 1, 1 /* stable */ );
43   switch (kind) {
44   case e_gras_msg_kind_oneway:
45     break;
46
47   case e_gras_msg_kind_rpccall:
48   case e_gras_msg_kind_rpcanswer:
49   case e_gras_msg_kind_rpcerror:
50     gras_datadesc_send(sock, ulong_type, &ID);
51     break;
52
53   default:
54     THROW1(unknown_error, 0, "Unknown msg kind %d", kind);
55   }
56
57   gras_datadesc_send(sock, string_type, &msgtype->name);
58   if (kind == e_gras_msg_kind_rpcerror) {
59     /* error on remote host, carfull, payload is an exception */
60     gras_datadesc_send(sock, gras_datadesc_by_name("ex_t"), payload);
61   } else if (kind == e_gras_msg_kind_rpcanswer) {
62     if (msgtype->answer_type)
63       gras_datadesc_send(sock, msgtype->answer_type, payload);
64   } else {
65     /* regular message */
66     if (msgtype->ctn_type)
67       gras_datadesc_send(sock, msgtype->ctn_type, payload);
68   }
69
70   gras_trp_flush(sock);
71 }
72
73 const char *hexa_str(unsigned char *data, int size, int downside);
74
75
76 /*
77  * receive the next message on the given socket.
78  */
79 void gras_msg_recv(gras_socket_t sock, gras_msg_t msg)
80 {
81
82   xbt_ex_t e;
83   static gras_datadesc_type_t string_type = NULL;
84   static gras_datadesc_type_t ulong_type = NULL;
85   char header[6];
86   int cpt;
87   int r_arch;
88   char *msg_name = NULL;
89   char c_kind;
90
91   xbt_assert1(!gras_socket_is_meas(sock),
92               "Asked to receive a message on the measurement socket %p",
93               sock);
94   if (!string_type) {
95     string_type = gras_datadesc_by_name("string");
96     xbt_assert(string_type);
97   }
98   if (!ulong_type) {
99     ulong_type = gras_datadesc_by_name("unsigned long int");
100     xbt_assert(ulong_type);
101   }
102
103
104   TRY {
105     gras_trp_recv(sock, header, 6);
106     gras_trp_recv(sock, &c_kind, 1);
107     msg->kind = (e_gras_msg_kind_t) c_kind;
108   }
109   CATCH(e) {
110     RETHROW0("Exception caught while trying to get the mesage header: %s");
111   }
112
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];
122
123   switch (msg->kind) {
124   case e_gras_msg_kind_oneway:
125     break;
126
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);
131     break;
132
133   default:
134     THROW_IMPOSSIBLE;
135   }
136
137   gras_datadesc_recv(sock, string_type, r_arch, &msg_name);
138   DEBUG4
139     ("Handle an incoming message '%s' (%s) using protocol %d (remote is %s)",
140      msg_name, e_gras_msg_kind_names[msg->kind], (int) header[4],
141      gras_datadesc_arch_name(r_arch));
142
143   TRY {
144     msg->type =
145       (gras_msgtype_t) xbt_set_get_by_name(_gras_msgtype_set, msg_name);
146   } CATCH(e) {
147     /* FIXME: Survive unknown messages */
148     if (e.category == not_found_error) {
149       xbt_ex_free(e);
150       THROW1(not_found_error, 0,
151              "Received an unknown message: %s (FIXME: should survive to these)",
152              msg_name);
153     } else
154       RETHROW1
155         ("Exception caught while retrieving the type associated to messages '%s' : %s",
156          msg_name);
157   }
158   free(msg_name);
159
160   if (msg->kind == e_gras_msg_kind_rpcerror) {
161     /* error on remote host. Carfull with that exception, eugene */
162     msg->payl_size = gras_datadesc_size(gras_datadesc_by_name("ex_t"));
163     msg->payl = xbt_malloc(msg->payl_size);
164     gras_datadesc_recv(sock, gras_datadesc_by_name("ex_t"), r_arch,
165                        msg->payl);
166
167   } else if (msg->kind == e_gras_msg_kind_rpcanswer) {
168     /* answer to RPC */
169     if (msg->type->answer_type) {
170       msg->payl_size = gras_datadesc_size(msg->type->answer_type);
171       xbt_assert2(msg->payl_size > 0,
172                   "%s %s",
173                   "Dynamic array as payload is forbided for now (FIXME?).",
174                   "Reference to dynamic array is allowed.");
175       msg->payl = xbt_malloc(msg->payl_size);
176       gras_datadesc_recv(sock, msg->type->answer_type, r_arch, msg->payl);
177     } else {
178       msg->payl = NULL;
179       msg->payl_size = 0;
180     }
181   } else {
182     /* regular message */
183     if (msg->type->ctn_type) {
184       msg->payl_size = gras_datadesc_size(msg->type->ctn_type);
185       xbt_assert2(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       gras_datadesc_recv(sock, msg->type->ctn_type, r_arch, msg->payl);
191     } else {
192       msg->payl = NULL;
193       msg->payl_size = 0;
194     }
195   }
196 }