Logo AND Algorithmique Numérique Distribuée

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