Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Move msg_send/recv to a RL specific file, so that we can provide a SG specific versio...
[simgrid.git] / src / gras / Msg / sg_msg.c
1 /* $Id$ */
2
3 /* messaging - Function related to messaging code specific to SG            */
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
12 //#include "msg/msg.h"
13 #include "gras/Virtu/virtu_sg.h"
14
15 #include "gras/Msg/msg_private.h"
16
17 #include "gras/DataDesc/datadesc_interface.h"
18 #include "gras/Transport/transport_interface.h" /* gras_trp_chunk_send/recv */
19 #include "gras/Transport/transport_private.h" /* sock->data */
20
21 XBT_LOG_EXTERNAL_CATEGORY(gras_msg);
22 XBT_LOG_DEFAULT_CATEGORY(gras_msg);
23
24 /** \brief Send the data pointed by \a payload as a message of type
25  * \a msgtype to the peer \a sock */
26 void
27 gras_msg_send(gras_socket_t   sock,
28               gras_msgtype_t  msgtype,
29               void           *payload) {
30
31   m_task_t task=NULL;
32   gras_trp_sg_sock_data_t *sock_data = (gras_trp_sg_sock_data_t *)sock->data;
33   gras_msg_t msg;
34
35   xbt_assert1(!gras_socket_is_meas(sock), 
36               "Asked to send a message on the measurement socket %p", sock);
37
38   msg=xbt_new0(s_gras_msg_t,1);
39   msg->type=msgtype;
40
41   msg->payload=xbt_malloc(gras_datadesc_size(msgtype->ctn_type));
42   if (msgtype->ctn_type)
43     msg->payload_size=gras_datadesc_copy(msgtype->ctn_type,payload,msg->payload);
44
45   task=MSG_task_create(msgtype->name,0,
46                        ((double)msg->payload_size)/(1024.0*1024.0),msg);
47
48   if (MSG_task_put(task, sock_data->to_host,sock_data->to_chan) != MSG_OK) 
49     THROW0(system_error,0,"Problem during the MSG_task_put");
50
51 }
52 /*
53  * receive the next message on the given socket.  
54  */
55 void
56 gras_msg_recv(gras_socket_t    sock,
57               gras_msgtype_t  *msgtype,
58               void           **payload,
59               int             *payload_size) {
60
61   m_task_t task=NULL;
62   /*  gras_trp_sg_sock_data_t *sock_data = (gras_trp_sg_sock_data_t *)sock->data;*/
63   gras_msg_t msg;
64   gras_trp_procdata_t pd=(gras_trp_procdata_t)gras_libdata_get("gras_trp");
65
66   xbt_assert1(!gras_socket_is_meas(sock), 
67               "Asked to receive a message on the measurement socket %p", sock);
68
69   if (MSG_task_get(&task, pd->chan) != MSG_OK)
70     THROW0(system_error,0,"Error in MSG_task_get()");
71
72   msg = MSG_task_get_data(task);
73   *msgtype = gras_msgtype_by_id(msg->type->code);
74   *payload = msg->payload;
75   *payload_size = msg->payload_size;
76
77   free(msg);
78   if (MSG_task_destroy(task) != MSG_OK)
79     THROW0(system_error,0,"Error in MSG_task_destroy()");
80
81
82   /*
83   THROW_UNIMPLEMENTED;
84
85   xbt_ex_t e;
86   static gras_datadesc_type_t string_type=NULL;
87   char header[6];
88   int cpt;
89   int r_arch;
90   char *msg_name=NULL;
91
92   if (!string_type) {
93     string_type=gras_datadesc_by_name("string");
94     xbt_assert(string_type);
95   }
96   
97   TRY {
98     gras_trp_chunk_recv(sock, header, 6);
99   } CATCH(e) {
100     RETHROW1("Exception caught while trying to get the mesage header on socket %p: %s",
101              sock);
102   }
103
104   for (cpt=0; cpt<4; cpt++)
105     if (header[cpt] != _GRAS_header[cpt])
106       THROW2(mismatch_error,0,
107              "Incoming bytes do not look like a GRAS message (header='%.4s' not '%.4s')",header,_GRAS_header);
108   if (header[4] != _GRAS_header[4]) 
109     THROW2(mismatch_error,0,"GRAS protocol mismatch (got %d, use %d)",
110            (int)header[4], (int)_GRAS_header[4]);
111   r_arch = (int)header[5];
112   DEBUG2("Handle an incoming message using protocol %d (remote is %s)",
113          (int)header[4],gras_datadesc_arch_name(r_arch));
114
115   gras_datadesc_recv(sock, string_type, r_arch, &msg_name);
116   TRY {
117     *msgtype = (gras_msgtype_t)xbt_set_get_by_name(_gras_msgtype_set,msg_name);
118   } CATCH(e) {
119     / * FIXME: Survive unknown messages * /
120     RETHROW1("Exception caught while retrieving the type associated to messages '%s' : %s",
121              msg_name);
122   }
123   free(msg_name);
124
125   if ((*msgtype)->ctn_type) {
126     *payload_size=gras_datadesc_size((*msgtype)->ctn_type);
127     xbt_assert2(*payload_size > 0,
128                 "%s %s",
129                 "Dynamic array as payload is forbided for now (FIXME?).",
130                 "Reference to dynamic array is allowed.");
131     *payload = xbt_malloc(*payload_size);
132     gras_datadesc_recv(sock, (*msgtype)->ctn_type, r_arch, *payload);
133   } else {
134     *payload = NULL;
135     *payload_size = 0;
136   }
137 */
138 }