Logo AND Algorithmique Numérique Distribuée

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