Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Kill non-portable comment
[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   if (!msgtype)
30     THROW0(arg_error,0,
31            "Cannot send the NULL message (did msgtype_by_name fail?)");
32
33   if (!string_type) {
34     string_type = gras_datadesc_by_name("string");
35     xbt_assert(string_type);
36   }
37   if (!ulong_type) {
38     ulong_type = gras_datadesc_by_name("unsigned long int");
39     xbt_assert(ulong_type);
40   }
41
42   DEBUG3("send '%s' to %s:%d", msgtype->name, 
43          gras_socket_peer_name(sock),gras_socket_peer_port(sock));
44   gras_trp_send(sock, _GRAS_header, 6, 1 /* stable */);
45   gras_trp_send(sock, &c_kind,      1, 1 /* stable */);
46   switch (kind) {
47    case e_gras_msg_kind_oneway: 
48      break;
49      
50    case e_gras_msg_kind_rpccall:
51    case e_gras_msg_kind_rpcanswer:
52    case e_gras_msg_kind_rpcerror:
53      gras_datadesc_send(sock,ulong_type,&ID);
54      break;
55
56    default:
57      THROW1(unknown_error,0,"Unknown msg kind %d",kind);
58   }
59    
60   gras_datadesc_send(sock, string_type,   &msgtype->name);
61   if (kind == e_gras_msg_kind_rpcerror) {
62      /* error on remote host, carfull, payload is an exception */
63     gras_datadesc_send(sock, gras_datadesc_by_name("ex_t"),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 /*
74  * receive the next message on the given socket.  
75  */
76 void
77 gras_msg_recv(gras_socket_t    sock,
78               gras_msg_t       msg) {
79
80   xbt_ex_t e;
81   static gras_datadesc_type_t string_type=NULL;
82   static gras_datadesc_type_t ulong_type=NULL;
83   char header[6];
84   int cpt;
85   int r_arch;
86   char *msg_name=NULL;
87   char c_kind;
88
89   xbt_assert1(!gras_socket_is_meas(sock), 
90               "Asked to receive a message on the measurement socket %p", sock);
91   if (!string_type) {
92     string_type=gras_datadesc_by_name("string");
93     xbt_assert(string_type);
94   }
95   if (!ulong_type) {
96     ulong_type = gras_datadesc_by_name("unsigned long int");
97     xbt_assert(ulong_type);
98   }
99
100   
101   TRY {
102     gras_trp_recv(sock, header, 6);
103     gras_trp_recv(sock, &c_kind, 1);
104     msg->kind=(e_gras_msg_kind_t)c_kind;
105   } CATCH(e) {
106     RETHROW1("Exception caught while trying to get the mesage header on socket %p: %s",
107              sock);
108   }
109
110   for (cpt=0; cpt<4; cpt++)
111     if (header[cpt] != _GRAS_header[cpt])
112       THROW2(mismatch_error,0,
113              "Incoming bytes do not look like a GRAS message (header='%.4s' not '%.4s')",header,_GRAS_header);
114   if (header[4] != _GRAS_header[4]) 
115     THROW2(mismatch_error,0,"GRAS protocol mismatch (got %d, use %d)",
116            (int)header[4], (int)_GRAS_header[4]);
117   r_arch = (int)header[5];
118   DEBUG2("Handle an incoming message using protocol %d (remote is %s)",
119          (int)header[4],gras_datadesc_arch_name(r_arch));
120
121   switch (msg->kind) {
122   case e_gras_msg_kind_oneway: 
123     break;
124     
125   case e_gras_msg_kind_rpccall:
126   case e_gras_msg_kind_rpcanswer:
127   case e_gras_msg_kind_rpcerror:
128     gras_datadesc_recv(sock,ulong_type,r_arch, &msg->ID);
129     break;
130     
131   default:
132     THROW_IMPOSSIBLE;
133   }
134
135   gras_datadesc_recv(sock, string_type, r_arch, &msg_name);
136   TRY {
137     msg->type = (gras_msgtype_t)xbt_set_get_by_name(_gras_msgtype_set,msg_name);
138   } CATCH(e) {
139     /* FIXME: Survive unknown messages */
140     RETHROW1("Exception caught while retrieving the type associated to messages '%s' : %s",
141              msg_name);
142   }
143   free(msg_name);
144
145   if (msg->kind == e_gras_msg_kind_rpcerror) {
146     /* error on remote host. Carfull with that exception, eugene */
147     msg->payl_size=gras_datadesc_size(gras_datadesc_by_name("ex_t"));
148     msg->payl=xbt_malloc(msg->payl_size);
149     gras_datadesc_recv(sock, gras_datadesc_by_name("ex_t"), r_arch, msg->payl);
150
151   } else {
152      /* regular message */
153      if (msg->type->ctn_type) {
154         msg->payl_size=gras_datadesc_size(msg->type->ctn_type);
155         xbt_assert2(msg->payl_size > 0,
156                     "%s %s",
157                     "Dynamic array as payload is forbided for now (FIXME?).",
158                     "Reference to dynamic array is allowed.");
159         msg->payl = xbt_malloc(msg->payl_size);
160         gras_datadesc_recv(sock, msg->type->ctn_type, r_arch, msg->payl);
161      } else {
162         msg->payl = NULL;
163         msg->payl_size = 0;
164      }
165   }
166 }