Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
afd8d342e3fb36abf403c1c1640abb8a706caef9
[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 typedef void* gras_trp_bufdata_;
24
25 void gras_msg_send_ext(gras_socket_t   sock,
26                      e_gras_msg_kind_t kind,
27                        unsigned long int ID,
28                        gras_msgtype_t  msgtype,
29                        void           *payload) {
30
31         smx_action_t act; /* simix action */
32         gras_trp_sg_sock_data_t *sock_data; 
33         gras_hostdata_t *hd;
34         gras_trp_procdata_t trp_remote_proc;
35         gras_msg_procdata_t msg_remote_proc;
36         gras_msg_t msg; /* message to send */
37   int whole_payload_size=0; /* msg->payload_size is used to memcpy the payload. 
38                                This is used to report the load onto the simulator. It also counts the size of pointed stuff */
39
40         sock_data = (gras_trp_sg_sock_data_t *)sock->data;
41
42         hd = (gras_hostdata_t *)SIMIX_host_get_data(SIMIX_host_self());
43
44   xbt_assert1(!gras_socket_is_meas(sock), 
45               "Asked to send a message on the measurement socket %p", sock);
46         
47         /* got the mutex my port */
48         DEBUG1("Sock port %d",sock->port);
49         SIMIX_mutex_lock(sock_data->mutex);
50
51         /*initialize gras message */
52         msg = xbt_new(s_gras_msg_t,1);
53         msg->expe = sock;
54         msg->kind = kind;
55         msg->type = msgtype;
56         msg->ID = ID;
57   if (kind == e_gras_msg_kind_rpcerror) {
58      /* error on remote host, carfull, payload is an exception */
59     msg->payl_size=gras_datadesc_size(gras_datadesc_by_name("ex_t"));
60     msg->payl=xbt_malloc(msg->payl_size);
61     whole_payload_size = gras_datadesc_copy(gras_datadesc_by_name("ex_t"),
62                                             payload,msg->payl);
63   } else if (kind == e_gras_msg_kind_rpcanswer) {
64     msg->payl_size=gras_datadesc_size(msgtype->answer_type);
65     msg->payl=xbt_malloc(msg->payl_size);
66     if (msgtype->answer_type)
67       whole_payload_size = gras_datadesc_copy(msgtype->answer_type,
68                                               payload, msg->payl);
69   } else {
70     msg->payl_size=gras_datadesc_size(msgtype->ctn_type);
71     msg->payl=msg->payl_size?xbt_malloc(msg->payl_size):NULL;
72     if (msgtype->ctn_type)
73       whole_payload_size = gras_datadesc_copy(msgtype->ctn_type,
74                                               payload, msg->payl);
75   }
76         /* put message on msg_queue */
77         msg_remote_proc = (gras_msg_procdata_t)gras_libdata_by_name_from_remote("gras_msg",sock_data->to_process);
78         xbt_fifo_push(msg_remote_proc->msg_to_receive_queue,msg);
79         
80         /* wake-up the receiver */
81         trp_remote_proc = (gras_trp_procdata_t)gras_libdata_by_name_from_remote("gras_trp",sock_data->to_process);
82         xbt_fifo_push(trp_remote_proc->active_socket,sock);
83
84         SIMIX_cond_signal(trp_remote_proc->cond);
85
86         /* wait for the receiver */
87         SIMIX_cond_wait(sock_data->cond, sock_data->mutex);
88
89         /* creates simix action and waits its ends, waits in the sender host condition*/
90         act = SIMIX_action_communicate(SIMIX_host_self(), sock_data->to_host,msgtype->name, (double)whole_payload_size, -1);
91         SIMIX_register_action_to_condition(act,sock_data->cond);
92         SIMIX_register_condition_to_action(act,sock_data->cond);
93
94   VERB5("Sending to %s(%s) a message type '%s' kind '%s' ID %lu",
95         SIMIX_host_get_name(sock_data->to_host),SIMIX_process_get_name(sock_data->to_process),
96         msg->type->name,e_gras_msg_kind_names[msg->kind],       msg->ID);
97         
98         SIMIX_cond_wait(sock_data->cond, sock_data->mutex);
99         /* error treatmeant */
100
101         /* cleanup structures */
102         SIMIX_action_destroy(act);
103         SIMIX_mutex_unlock(sock_data->mutex);
104
105         VERB0("Message sent");
106
107 }
108 /*
109  * receive the next message on the given socket.  
110  */
111 void
112 gras_msg_recv(gras_socket_t    sock,
113               gras_msg_t       msg) {
114
115         gras_trp_sg_sock_data_t *sock_data; 
116         gras_trp_sg_sock_data_t *remote_sock_data; 
117         gras_hostdata_t *remote_hd;
118   gras_msg_t msg_got;
119         gras_msg_procdata_t msg_procdata = (gras_msg_procdata_t)gras_libdata_by_name("gras_msg");
120
121   xbt_assert1(!gras_socket_is_meas(sock), 
122               "Asked to receive a message on the measurement socket %p", sock);
123
124   xbt_assert0(msg,"msg is an out parameter of gras_msg_recv...");
125
126         sock_data = (gras_trp_sg_sock_data_t *)sock->data;
127         remote_sock_data = ((gras_trp_sg_sock_data_t *)sock->data)->to_socket->data;
128         DEBUG3("Remote host %s, Remote Port: %d Local port %d", SIMIX_host_get_name(sock_data->to_host), sock->peer_port, sock->port);
129         remote_hd = (gras_hostdata_t *)SIMIX_host_get_data(sock_data->to_host);
130
131         if (xbt_fifo_size(msg_procdata->msg_to_receive_queue) == 0 ) {
132                 THROW_IMPOSSIBLE;
133         }
134         DEBUG1("Size msg_to_receive buffer: %d", xbt_fifo_size(msg_procdata->msg_to_receive_queue));
135   msg_got = xbt_fifo_shift(msg_procdata->msg_to_receive_queue);
136
137         SIMIX_mutex_lock(remote_sock_data->mutex);
138 /* ok, I'm here, you can continuate the communication */
139         SIMIX_cond_signal(remote_sock_data->cond);
140
141 /* wait for communication end */
142         SIMIX_cond_wait(remote_sock_data->cond,remote_sock_data->mutex);
143
144         msg_got->expe= msg->expe;
145   memcpy(msg,msg_got,sizeof(s_gras_msg_t));
146         xbt_free(msg_got);
147         SIMIX_mutex_unlock(remote_sock_data->mutex);
148
149         VERB3("Received a message type '%s' kind '%s' ID %lu",// from %s",
150         msg->type->name,
151         e_gras_msg_kind_names[msg->kind],
152         msg->ID);
153 }