Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
50cf748a94b5b616521592da20e8235deb4b7519
[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,
23                        void           *payload) {
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 {
63      /* regular message */
64      if (msgtype->ctn_type)
65        gras_datadesc_send(sock, msgtype->ctn_type, payload);
66   }
67    
68   gras_trp_flush(sock);
69 }
70
71 /*
72  * receive the next message on the given socket.  
73  */
74 void
75 gras_msg_recv(gras_socket_t    sock,
76               gras_msg_t       msg) {
77
78   xbt_ex_t e;
79   static gras_datadesc_type_t string_type=NULL;
80   static gras_datadesc_type_t ulong_type=NULL;
81   char header[6];
82   int cpt;
83   int r_arch;
84   char *msg_name=NULL;
85   char c_kind;
86
87   xbt_assert1(!gras_socket_is_meas(sock), 
88               "Asked to receive a message on the measurement socket %p", sock);
89   if (!string_type) {
90     string_type=gras_datadesc_by_name("string");
91     xbt_assert(string_type);
92   }
93   if (!ulong_type) {
94     ulong_type = gras_datadesc_by_name("unsigned long int");
95     xbt_assert(ulong_type);
96   }
97
98   
99   TRY {
100     gras_trp_recv(sock, header, 6);
101     gras_trp_recv(sock, &c_kind, 1);
102     msg->kind=(e_gras_msg_kind_t)c_kind;
103   } CATCH(e) {
104     RETHROW0("Exception caught while trying to get the mesage header: %s");
105   }
106
107   for (cpt=0; cpt<4; cpt++)
108     if (header[cpt] != _GRAS_header[cpt])
109       THROW2(mismatch_error,0,
110              "Incoming bytes do not look like a GRAS message (header='%.4s' not '%.4s')",
111              header,_GRAS_header);
112   if (header[4] != _GRAS_header[4]) 
113     THROW2(mismatch_error,0,"GRAS protocol mismatch (got %d, use %d)",
114            (int)header[4], (int)_GRAS_header[4]);
115   r_arch = (int)header[5];
116   DEBUG2("Handle an incoming message using protocol %d (remote is %s)",
117          (int)header[4],gras_datadesc_arch_name(r_arch));
118
119   switch (msg->kind) {
120   case e_gras_msg_kind_oneway: 
121     break;
122     
123   case e_gras_msg_kind_rpccall:
124   case e_gras_msg_kind_rpcanswer:
125   case e_gras_msg_kind_rpcerror:
126     gras_datadesc_recv(sock,ulong_type,r_arch, &msg->ID);
127     break;
128     
129   default:
130     THROW_IMPOSSIBLE;
131   }
132
133   gras_datadesc_recv(sock, string_type, r_arch, &msg_name);
134   TRY {
135     msg->type = (gras_msgtype_t)xbt_set_get_by_name(_gras_msgtype_set,msg_name);
136   } CATCH(e) {
137     /* FIXME: Survive unknown messages */
138     RETHROW1("Exception caught while retrieving the type associated to messages '%s' : %s",
139              msg_name);
140   }
141   free(msg_name);
142
143   if (msg->kind == e_gras_msg_kind_rpcerror) {
144     /* error on remote host. Carfull with that exception, eugene */
145     msg->payl_size=gras_datadesc_size(gras_datadesc_by_name("ex_t"));
146     msg->payl=xbt_malloc(msg->payl_size);
147     gras_datadesc_recv(sock, gras_datadesc_by_name("ex_t"), r_arch, msg->payl);
148
149   } else {
150      /* regular message */
151      if (msg->type->ctn_type) {
152         msg->payl_size=gras_datadesc_size(msg->type->ctn_type);
153         xbt_assert2(msg->payl_size > 0,
154                     "%s %s",
155                     "Dynamic array as payload is forbided for now (FIXME?).",
156                     "Reference to dynamic array is allowed.");
157         msg->payl = xbt_malloc(msg->payl_size);
158         gras_datadesc_recv(sock, msg->type->ctn_type, r_arch, msg->payl);
159      } else {
160         msg->payl = NULL;
161         msg->payl_size = 0;
162      }
163   }
164 }