Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
143f6a19f060d581c401e6e9a319dc95de72660b
[simgrid.git] / src / gras / Msg / rl_msg.c
1 /* $Id$ */
2
3 /* messaging - Function related to messaging code specific to RL            */
4
5 /* Copyright (c) 2003-2005 Martin Quinson. All rights reserved.             */
6
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. */
9
10 #include "xbt/ex.h"
11 #include "gras/Msg/msg_private.h"
12
13 #include "gras/DataDesc/datadesc_interface.h"
14 #include "gras/Transport/transport_interface.h" /* gras_trp_send/recv */
15
16 XBT_LOG_EXTERNAL_CATEGORY(gras_msg);
17 XBT_LOG_DEFAULT_CATEGORY(gras_msg);
18
19 void gras_msg_send_ext(gras_socket_t sock,
20                        e_gras_msg_kind_t kind,
21                        unsigned long int ID,
22                        gras_msgtype_t msgtype, void *payload)
23 {
24
25   static gras_datadesc_type_t string_type = NULL;
26   static gras_datadesc_type_t ulong_type = NULL;
27   char c_kind = (char) kind;
28
29   xbt_assert0(msgtype, "Cannot send the NULL message");
30
31   if (!string_type) {
32     string_type = gras_datadesc_by_name("string");
33     xbt_assert(string_type);
34   }
35   if (!ulong_type) {
36     ulong_type = gras_datadesc_by_name("unsigned long int");
37     xbt_assert(ulong_type);
38   }
39
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 */ );
44   switch (kind) {
45   case e_gras_msg_kind_oneway:
46     break;
47
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);
52     break;
53
54   default:
55     THROW1(unknown_error, 0, "Unknown msg kind %d", kind);
56   }
57
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);
65   } else {
66     /* regular message */
67     if (msgtype->ctn_type)
68       gras_datadesc_send(sock, msgtype->ctn_type, payload);
69   }
70
71   gras_trp_flush(sock);
72 }
73
74 const char *hexa_str(unsigned char *data, int size, int downside);
75
76
77 /*
78  * receive the next message on the given socket.
79  */
80 void gras_msg_recv(gras_socket_t sock, gras_msg_t msg)
81 {
82
83   xbt_ex_t e;
84   static gras_datadesc_type_t string_type = NULL;
85   static gras_datadesc_type_t ulong_type = NULL;
86   char header[6];
87   int cpt;
88   int r_arch;
89   char *msg_name = NULL;
90   char c_kind;
91
92   xbt_assert1(!gras_socket_is_meas(sock),
93               "Asked to receive a message on the measurement socket %p",
94               sock);
95   if (!string_type) {
96     string_type = gras_datadesc_by_name("string");
97     xbt_assert(string_type);
98   }
99   if (!ulong_type) {
100     ulong_type = gras_datadesc_by_name("unsigned long int");
101     xbt_assert(ulong_type);
102   }
103
104
105   TRY {
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;
109   }
110   CATCH(e) {
111     RETHROW0("Exception caught while trying to get the mesage header: %s");
112   }
113
114   for (cpt = 0; cpt < 4; cpt++)
115     if (header[cpt] != _GRAS_header[cpt])
116       THROW2(mismatch_error, 0,
117              "Incoming bytes do not look like a GRAS message (header='%s'  not '%.4s')",
118              hexa_str((unsigned char *) header, 4, 0), _GRAS_header);
119   if (header[4] != _GRAS_header[4])
120     THROW2(mismatch_error, 0, "GRAS protocol mismatch (got %d, use %d)",
121            (int) header[4], (int) _GRAS_header[4]);
122   r_arch = (int) header[5];
123
124   switch (msg->kind) {
125   case e_gras_msg_kind_oneway:
126     break;
127
128   case e_gras_msg_kind_rpccall:
129   case e_gras_msg_kind_rpcanswer:
130   case e_gras_msg_kind_rpcerror:
131     gras_datadesc_recv(sock, ulong_type, r_arch, &msg->ID);
132     break;
133
134   default:
135     THROW_IMPOSSIBLE;
136   }
137
138   gras_datadesc_recv(sock, string_type, r_arch, &msg_name);
139   DEBUG4
140     ("Handle an incoming message '%s' (%s) using protocol %d (remote is %s)",
141      msg_name, e_gras_msg_kind_names[msg->kind], (int) header[4],
142      gras_datadesc_arch_name(r_arch));
143
144   TRY {
145     msg->type =
146       (gras_msgtype_t) xbt_set_get_by_name(_gras_msgtype_set, msg_name);
147   } CATCH(e) {
148     /* FIXME: Survive unknown messages */
149     if (e.category == not_found_error) {
150       xbt_ex_free(e);
151       THROW1(not_found_error, 0,
152              "Received an unknown message: %s (FIXME: should survive to these)",
153              msg_name);
154     } else
155       RETHROW1
156         ("Exception caught while retrieving the type associated to messages '%s' : %s",
157          msg_name);
158   }
159   free(msg_name);
160
161   if (msg->kind == e_gras_msg_kind_rpcerror) {
162     /* error on remote host. Carfull with that exception, eugene */
163     msg->payl_size = gras_datadesc_size(gras_datadesc_by_name("ex_t"));
164     msg->payl = xbt_malloc(msg->payl_size);
165     gras_datadesc_recv(sock, gras_datadesc_by_name("ex_t"), r_arch,
166                        msg->payl);
167
168   } else if (msg->kind == e_gras_msg_kind_rpcanswer) {
169     /* answer to RPC */
170     if (msg->type->answer_type) {
171       msg->payl_size = gras_datadesc_size(msg->type->answer_type);
172       xbt_assert2(msg->payl_size > 0,
173                   "%s %s",
174                   "Dynamic array as payload is forbided for now (FIXME?).",
175                   "Reference to dynamic array is allowed.");
176       msg->payl = xbt_malloc(msg->payl_size);
177       gras_datadesc_recv(sock, msg->type->answer_type, r_arch, msg->payl);
178     } else {
179       msg->payl = NULL;
180       msg->payl_size = 0;
181     }
182   } else {
183     /* regular message */
184     if (msg->type->ctn_type) {
185       msg->payl_size = gras_datadesc_size(msg->type->ctn_type);
186       xbt_assert2(msg->payl_size > 0,
187                   "%s %s",
188                   "Dynamic array as payload is forbided for now (FIXME?).",
189                   "Reference to dynamic array is allowed.");
190       msg->payl = xbt_malloc(msg->payl_size);
191       gras_datadesc_recv(sock, msg->type->ctn_type, r_arch, msg->payl);
192     } else {
193       msg->payl = NULL;
194       msg->payl_size = 0;
195     }
196   }
197 }