Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
7a690bc9bb94a06c66ecc9a3cc1677a82a9c88dc
[simgrid.git] / src / gras / Msg / sg_msg.c
1 /* messaging - Function related to messaging code specific to SG            */
2
3 /* Copyright (c) 2005, 2006, 2007, 2008, 2009, 2010. The SimGrid Team.
4  * All rights reserved.                                                     */
5
6 /* This program is free software; you can redistribute it and/or modify it
7  * under the terms of the license (GNU LGPL) which comes with this package. */
8
9 #include "xbt/ex.h"
10
11 #include "gras/Virtu/virtu_sg.h"
12
13 #include "gras/Msg/msg_private.h"
14
15 #include "gras/DataDesc/datadesc_interface.h"
16 #include "gras/Transport/transport_interface.h" /* gras_trp_chunk_send/recv */
17 #include "gras/Transport/transport_private.h"   /* sock->data */
18
19 XBT_LOG_EXTERNAL_CATEGORY(gras_msg);
20 XBT_LOG_DEFAULT_CATEGORY(gras_msg);
21
22 typedef void *gras_trp_bufdata_;
23
24 void gras_msg_send_ext(gras_socket_t sock,
25                        e_gras_msg_kind_t kind,
26                        unsigned long int ID,
27                        gras_msgtype_t msgtype, void *payload)
28 {
29
30   smx_action_t act;             /* simix action */
31   gras_trp_sg_sock_data_t *sock_data;
32   gras_hostdata_t *hd;
33   gras_trp_procdata_t trp_remote_proc;
34   gras_msg_procdata_t msg_remote_proc;
35   gras_msg_t msg;               /* message to send */
36   int whole_payload_size = 0;   /* msg->payload_size is used to memcpy the payload.
37                                    This is used to report the load onto the simulator. It also counts the size of pointed stuff */
38
39   sock_data = (gras_trp_sg_sock_data_t *) sock->data;
40
41   hd = (gras_hostdata_t *) SIMIX_host_get_data(SIMIX_host_self());
42
43   xbt_assert1(!gras_socket_is_meas(sock),
44               "Asked to send a message on the measurement socket %p", sock);
45
46   /*initialize gras message */
47   msg = xbt_new(s_gras_msg_t, 1);
48   msg->expe = sock;
49   msg->kind = kind;
50   msg->type = msgtype;
51   msg->ID = ID;
52   if (kind == e_gras_msg_kind_rpcerror) {
53     /* error on remote host, carfull, payload is an exception */
54     msg->payl_size = gras_datadesc_size(gras_datadesc_by_name("ex_t"));
55     msg->payl = xbt_malloc(msg->payl_size);
56     whole_payload_size = gras_datadesc_memcpy(gras_datadesc_by_name("ex_t"),
57                                               payload, msg->payl);
58   } else if (kind == e_gras_msg_kind_rpcanswer) {
59     msg->payl_size = gras_datadesc_size(msgtype->answer_type);
60     if (msg->payl_size)
61       msg->payl = xbt_malloc(msg->payl_size);
62     else
63       msg->payl = NULL;
64
65     if (msgtype->answer_type)
66       whole_payload_size = gras_datadesc_memcpy(msgtype->answer_type,
67                                                 payload, msg->payl);
68   } else {
69     msg->payl_size = gras_datadesc_size(msgtype->ctn_type);
70     msg->payl = msg->payl_size ? xbt_malloc(msg->payl_size) : NULL;
71     if (msgtype->ctn_type)
72       whole_payload_size = gras_datadesc_memcpy(msgtype->ctn_type,
73                                                 payload, msg->payl);
74   }
75
76   /* put the selectable socket on the queue */
77   trp_remote_proc = (gras_trp_procdata_t)
78     gras_libdata_by_name_from_remote("gras_trp", sock_data->to_process);
79
80   xbt_queue_push(trp_remote_proc->msg_selectable_sockets, &sock);
81
82   /* put message on msg_queue */
83   msg_remote_proc = (gras_msg_procdata_t)
84     gras_libdata_by_name_from_remote("gras_msg", sock_data->to_process);
85   xbt_fifo_push(msg_remote_proc->msg_to_receive_queue, msg);
86
87   /* wait for the receiver */
88   SIMIX_cond_wait(sock_data->cond, sock_data->mutex);
89
90   /* creates simix action and waits its ends, waits in the sender host
91      condition */
92   act = SIMIX_action_communicate(SIMIX_host_self(),
93                                  sock_data->to_host, msgtype->name,
94                                  (double) whole_payload_size, -1);
95   SIMIX_register_action_to_condition(act, sock_data->cond);
96
97   VERB5("Sending to %s(%s) a message type '%s' kind '%s' ID %lu",
98         SIMIX_host_get_name(sock_data->to_host),
99         SIMIX_process_get_name(sock_data->to_process),
100         msg->type->name, e_gras_msg_kind_names[msg->kind], msg->ID);
101
102   SIMIX_cond_wait(sock_data->cond, sock_data->mutex);
103   SIMIX_unregister_action_to_condition(act, sock_data->cond);
104   /* error treatmeant (FIXME) */
105
106   /* cleanup structures */
107   SIMIX_action_destroy(act);
108   SIMIX_mutex_unlock(sock_data->mutex);
109
110   VERB0("Message sent");
111
112 }
113
114 /*
115  * receive the next message on the given socket.
116  */
117 void gras_msg_recv(gras_socket_t sock, gras_msg_t msg)
118 {
119
120   gras_trp_sg_sock_data_t *sock_data;
121   gras_trp_sg_sock_data_t *remote_sock_data;
122   gras_hostdata_t *remote_hd;
123   gras_msg_t msg_got;
124   gras_msg_procdata_t msg_procdata =
125     (gras_msg_procdata_t) gras_libdata_by_name("gras_msg");
126
127   xbt_assert1(!gras_socket_is_meas(sock),
128               "Asked to receive a message on the measurement socket %p",
129               sock);
130
131   xbt_assert0(msg, "msg is an out parameter of gras_msg_recv...");
132
133   sock_data = (gras_trp_sg_sock_data_t *) sock->data;
134   remote_sock_data =
135     ((gras_trp_sg_sock_data_t *) sock->data)->to_socket->data;
136   DEBUG3("Remote host %s, Remote Port: %d Local port %d",
137          SIMIX_host_get_name(sock_data->to_host), sock->peer_port,
138          sock->port);
139   remote_hd = (gras_hostdata_t *) SIMIX_host_get_data(sock_data->to_host);
140
141   if (xbt_fifo_size(msg_procdata->msg_to_receive_queue) == 0) {
142     THROW_IMPOSSIBLE;
143   }
144   DEBUG1("Size msg_to_receive buffer: %d",
145          xbt_fifo_size(msg_procdata->msg_to_receive_queue));
146   msg_got = xbt_fifo_shift(msg_procdata->msg_to_receive_queue);
147
148   SIMIX_mutex_lock(remote_sock_data->mutex);
149   /* ok, I'm here, you can continuate the communication */
150   SIMIX_cond_signal(remote_sock_data->cond);
151
152   /* wait for communication end */
153   SIMIX_cond_wait(remote_sock_data->cond, remote_sock_data->mutex);
154
155   msg_got->expe = msg->expe;
156   memcpy(msg, msg_got, sizeof(s_gras_msg_t));
157   xbt_free(msg_got);
158   SIMIX_mutex_unlock(remote_sock_data->mutex);
159
160   VERB3("Received a message type '%s' kind '%s' ID %lu",        // from %s",
161         msg->type->name, e_gras_msg_kind_names[msg->kind], msg->ID);
162 }