Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
New function: gras_msg_wait_ext (for a finer control of accepted messages); introduce...
[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                        gras_msgtype_t  msgtype,
22                        void           *payload) {
23
24   static gras_datadesc_type_t string_type=NULL;
25   char c_kind=(char)kind;
26   
27   if (!msgtype)
28     THROW0(arg_error,0,
29            "Cannot send the NULL message (did msgtype_by_name fail?)");
30
31   if (!string_type) {
32     string_type = gras_datadesc_by_name("string");
33     xbt_assert(string_type);
34   }
35
36   DEBUG3("send '%s' to %s:%d", msgtype->name, 
37          gras_socket_peer_name(sock),gras_socket_peer_port(sock));
38   gras_trp_send(sock, _GRAS_header, 6, 1 /* stable */);
39   gras_trp_send(sock, &c_kind,      1, 1 /* stable */);
40
41   gras_datadesc_send(sock, string_type,   &msgtype->name);
42   if (msgtype->ctn_type)
43     gras_datadesc_send(sock, msgtype->ctn_type, payload);
44   gras_trp_flush(sock);
45 }
46
47 /*
48  * receive the next message on the given socket.  
49  */
50 void
51 gras_msg_recv(gras_socket_t    sock,
52               gras_msg_t       msg) {
53
54   xbt_ex_t e;
55   static gras_datadesc_type_t string_type=NULL;
56   char header[6];
57   int cpt;
58   int r_arch;
59   char *msg_name=NULL;
60   char c_kind;
61
62   xbt_assert1(!gras_socket_is_meas(sock), 
63               "Asked to receive a message on the measurement socket %p", sock);
64   if (!string_type) {
65     string_type=gras_datadesc_by_name("string");
66     xbt_assert(string_type);
67   }
68   
69   TRY {
70     gras_trp_recv(sock, header, 6);
71     gras_trp_recv(sock, &c_kind, 1);
72     msg->kind=(e_gras_msg_kind_t)c_kind;
73   } CATCH(e) {
74     RETHROW1("Exception caught while trying to get the mesage header on socket %p: %s",
75              sock);
76   }
77
78   for (cpt=0; cpt<4; cpt++)
79     if (header[cpt] != _GRAS_header[cpt])
80       THROW2(mismatch_error,0,
81              "Incoming bytes do not look like a GRAS message (header='%.4s' not '%.4s')",header,_GRAS_header);
82   if (header[4] != _GRAS_header[4]) 
83     THROW2(mismatch_error,0,"GRAS protocol mismatch (got %d, use %d)",
84            (int)header[4], (int)_GRAS_header[4]);
85   r_arch = (int)header[5];
86   DEBUG2("Handle an incoming message using protocol %d (remote is %s)",
87          (int)header[4],gras_datadesc_arch_name(r_arch));
88
89   gras_datadesc_recv(sock, string_type, r_arch, &msg_name);
90   TRY {
91     msg->type = (gras_msgtype_t)xbt_set_get_by_name(_gras_msgtype_set,msg_name);
92   } CATCH(e) {
93     /* FIXME: Survive unknown messages */
94     RETHROW1("Exception caught while retrieving the type associated to messages '%s' : %s",
95              msg_name);
96   }
97   free(msg_name);
98
99   if (msg->type->ctn_type) {
100     msg->payl_size=gras_datadesc_size(msg->type->ctn_type);
101     xbt_assert2(msg->payl_size > 0,
102                 "%s %s",
103                 "Dynamic array as payload is forbided for now (FIXME?).",
104                 "Reference to dynamic array is allowed.");
105     msg->payl = xbt_malloc(msg->payl_size);
106     gras_datadesc_recv(sock, msg->type->ctn_type, r_arch, msg->payl);
107   } else {
108     msg->payl = NULL;
109     msg->payl_size = 0;
110   }
111 }