Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Implement gras_socket_im_the_server() the CRUDE way. this lets pmm work on simulation...
[simgrid.git] / src / gras / Transport / transport_plugin_sg.c
1 /* file trp (transport) - send/receive a bunch of bytes in SG realm         */
2
3 /* Note that this is only used to debug other parts of GRAS since message   */
4 /*  exchange in SG realm is implemented directly without mimicing real life */
5 /*  This would be terribly unefficient.                                     */
6
7 /* Copyright (c) 2004, 2005, 2006, 2007, 2009, 2010. The SimGrid Team.
8  * All rights reserved.                                                     */
9
10 /* This program is free software; you can redistribute it and/or modify it
11  * under the terms of the license (GNU LGPL) which comes with this package. */
12
13 #include "xbt/ex.h"
14
15 #include "simix/simix.h"
16 #include "gras/Msg/msg_private.h"
17 #include "gras/Transport/transport_private.h"
18 #include "gras/Virtu/virtu_sg.h"
19
20 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(gras_trp_sg, gras_trp,
21                                 "SimGrid pseudo-transport");
22
23 /***
24  *** Prototypes 
25  ***/
26
27 /* retrieve the port record associated to a numerical port on an host */
28 static gras_sg_portrec_t find_port(gras_hostdata_t * hd, int port);
29
30 void gras_trp_sg_socket_client(gras_trp_plugin_t self,
31                                const char*host,
32                                int port,
33                                /* OUT */ gras_socket_t sock);
34 void gras_trp_sg_socket_server(gras_trp_plugin_t self,
35                                int port,
36                                /* OUT */ gras_socket_t sock);
37 void gras_trp_sg_socket_close(gras_socket_t sd);
38
39 void gras_trp_sg_chunk_send_raw(gras_socket_t sd,
40                                 const char *data, unsigned long int size);
41 void gras_trp_sg_chunk_send(gras_socket_t sd,
42                             const char *data,
43                             unsigned long int size, int stable_ignored);
44
45 int gras_trp_sg_chunk_recv(gras_socket_t sd,
46                            char *data, unsigned long int size);
47
48 /***
49  *** Specific plugin part
50  ***/
51 typedef struct {
52   int placeholder;              /* nothing plugin specific so far */
53 } gras_trp_sg_plug_data_t;
54
55
56 /***
57  *** Code
58  ***/
59 static gras_sg_portrec_t find_port(gras_hostdata_t * hd, int port)
60 {
61   unsigned int cpt;
62   gras_sg_portrec_t pr;
63
64   xbt_assert0(hd, "Please run gras_process_init on each process");
65
66   xbt_dynar_foreach(hd->ports, cpt, pr) {
67     if (pr->port == port)
68       return pr;
69   }
70   return NULL;
71 }
72
73 /***
74  *** Info about who's speaking
75  ***/
76 static int gras_trp_sg_my_port(gras_socket_t s) {
77   gras_trp_sg_sock_data_t sockdata = s->data;
78   if (gras_socket_im_the_server(s))
79     return sockdata->server_port;
80   else
81     return sockdata->client_port;
82 }
83 static int gras_trp_sg_peer_port(gras_socket_t s) {
84   gras_trp_sg_sock_data_t sockdata = s->data;
85   if (gras_socket_im_the_server(s))
86     return sockdata->client_port;
87   else
88     return sockdata->server_port;
89 }
90 static const char* gras_trp_sg_peer_name(gras_socket_t s) {
91   gras_trp_sg_sock_data_t sockdata = s->data;
92   if (gras_socket_im_the_server(s))
93     return SIMIX_host_get_name(SIMIX_process_get_host(sockdata->client));
94   else {
95     return SIMIX_host_get_name(SIMIX_process_get_host(sockdata->server));
96   }
97 }
98 static const char* gras_trp_sg_peer_proc(gras_socket_t s) {
99   THROW_UNIMPLEMENTED;
100 }
101 static void gras_trp_sg_peer_proc_set(gras_socket_t s,char *name) {
102   THROW_UNIMPLEMENTED;
103 }
104
105 void gras_trp_sg_setup(gras_trp_plugin_t plug)
106 {
107
108   plug->my_port = gras_trp_sg_my_port;
109   plug->peer_port = gras_trp_sg_peer_port;
110   plug->peer_name = gras_trp_sg_peer_name;
111   plug->peer_proc = gras_trp_sg_peer_proc;
112   plug->peer_proc_set = gras_trp_sg_peer_proc_set;
113
114   gras_trp_sg_plug_data_t *data = xbt_new(gras_trp_sg_plug_data_t, 1);
115
116   plug->data = data;
117
118   plug->socket_client = gras_trp_sg_socket_client;
119   plug->socket_server = gras_trp_sg_socket_server;
120   plug->socket_close = gras_trp_sg_socket_close;
121
122   plug->raw_send = gras_trp_sg_chunk_send_raw;
123   plug->send = gras_trp_sg_chunk_send;
124   plug->raw_recv = plug->recv = gras_trp_sg_chunk_recv;
125
126   plug->flush = NULL;           /* nothing cached */
127 }
128
129 void gras_trp_sg_socket_client(gras_trp_plugin_t self,
130                                const char*host,
131                                int port,
132                                /* OUT */ gras_socket_t sock)
133 {
134
135   smx_host_t peer;
136   gras_hostdata_t *hd;
137   gras_trp_sg_sock_data_t data;
138   gras_sg_portrec_t pr;
139
140   /* make sure this socket will reach someone */
141   if (!(peer = SIMIX_host_get_by_name(host)))
142     THROW1(mismatch_error, 0,
143            "Can't connect to %s: no such host.\n", host);
144
145   if (!(hd = (gras_hostdata_t *) SIMIX_host_get_data(peer)))
146     THROW1(mismatch_error, 0,
147            "can't connect to %s: no process on this host",
148            host);
149
150   pr = find_port(hd, port);
151
152   if (pr == NULL) {
153     THROW2(mismatch_error, 0,
154            "can't connect to %s:%d, no process listen on this port",
155            host, port);
156   }
157
158   /* Ensure that the listener is expecting the kind of stuff we want to send */
159   if (pr->meas && !sock->meas) {
160     THROW2(mismatch_error, 0,
161            "can't connect to %s:%d in regular mode, the process listen "
162            "in measurement mode on this port", host,
163            port);
164   }
165   if (!pr->meas && sock->meas) {
166     THROW2(mismatch_error, 0,
167            "can't connect to %s:%d in measurement mode, the process listen "
168            "in regular mode on this port", host,
169            port);
170   }
171
172   /* create simulation data of the socket */
173   data = xbt_new0(s_gras_trp_sg_sock_data_t, 1);
174   data->client = SIMIX_process_self();
175   data->server = pr->server;
176   data->server_port = port;
177   data->client_port = gras_os_myport();
178
179   /* initialize synchronization stuff on the socket */
180   data->rdv_server = pr->rdv;
181   data->rdv_client = SIMIX_rdv_create(NULL);
182   data->comm_recv = SIMIX_network_irecv(data->rdv_client, NULL, 0);
183
184   /* connect that simulation data to the socket */
185   sock->data = data;
186   sock->incoming = 1;
187
188   DEBUG8("%s (PID %d) connects in %s mode to %s:%d (rdv_ser:%p, rdv_cli:%p, comm:%p)",
189          SIMIX_process_get_name(SIMIX_process_self()), gras_os_getpid(),
190          sock->meas ? "meas" : "regular", host, port,
191          data->rdv_server,data->rdv_client,data->comm_recv);
192 }
193
194 void gras_trp_sg_socket_server(gras_trp_plugin_t self, int port, gras_socket_t sock)
195 {
196
197   gras_hostdata_t *hd =
198       (gras_hostdata_t *) SIMIX_host_get_data(SIMIX_host_self());
199   gras_sg_portrec_t pr;
200   gras_trp_sg_sock_data_t data;
201
202   xbt_assert0(hd, "Please run gras_process_init on each process");
203
204   sock->accepting = 1;
205
206   /* Check whether a server is already listening on that port or not */
207   pr = find_port(hd, port);
208
209   if (pr)
210     THROW2(mismatch_error, 0,
211            "can't listen on address %s:%d: port already in use.",
212            SIMIX_host_get_name(SIMIX_host_self()), port);
213
214   /* This port is free, let's take it */
215   pr = xbt_new(s_gras_sg_portrec_t, 1);
216   pr->port = port;
217   pr->meas = sock->meas;
218   pr->server = SIMIX_process_self();
219   pr->rdv = SIMIX_rdv_create(NULL);
220   xbt_dynar_push(hd->ports, &pr);
221
222   /* Create the socket */
223   data = xbt_new0(s_gras_trp_sg_sock_data_t, 1);
224   data->server = SIMIX_process_self();
225   data->server_port = port;
226   data->client = NULL;
227   data->rdv_server = pr->rdv;
228   data->rdv_client = NULL;
229   data->comm_recv = SIMIX_network_irecv(pr->rdv, NULL, 0);
230
231   sock->data = data;
232
233   VERB10
234       ("'%s' (%d) ears on %s:%d%s (%p; data:%p); Here rdv: %p; Remote rdv: %p; Comm %p",
235        SIMIX_process_get_name(SIMIX_process_self()), gras_os_getpid(),
236        SIMIX_host_get_name(SIMIX_host_self()), port,
237        sock->meas ? " (mode meas)" : "", sock, data,
238        (data->server ==
239         SIMIX_process_self())? data->rdv_server : data->rdv_client,
240        (data->server ==
241         SIMIX_process_self())? data->rdv_client : data->rdv_server,
242        data->comm_recv);
243
244 }
245
246 void gras_trp_sg_socket_close(gras_socket_t sock)
247 {
248   gras_hostdata_t *hd =
249       (gras_hostdata_t *) SIMIX_host_get_data(SIMIX_host_self());
250   unsigned int cpt;
251   gras_sg_portrec_t pr;
252
253   XBT_IN1(" (sock=%p)", sock);
254
255   if (!sock)
256     return;
257
258   xbt_assert0(hd, "Please run gras_process_init on each process");
259
260   gras_trp_sg_sock_data_t sockdata = sock->data;
261
262   if (sock->incoming && !sock->outgoing && sockdata->server_port >= 0) {
263     /* server mode socket. Unregister it from 'OS' tables */
264     xbt_dynar_foreach(hd->ports, cpt, pr) {
265       DEBUG2("Check pr %d of %lu", cpt, xbt_dynar_length(hd->ports));
266       if (pr->port == sockdata->server_port) {
267         xbt_dynar_cursor_rm(hd->ports, &cpt);
268         XBT_OUT;
269         return;
270       }
271     }
272     WARN2
273         ("socket_close called on the unknown incoming socket %p (port=%d)",
274          sock, sockdata->server_port);
275   }
276   if (sock->data) {
277     /* FIXME: kill the rdv point if receiver side */
278     free(sock->data);
279   }
280   XBT_OUT;
281 }
282
283 typedef struct {
284   int size;
285   void *data;
286 } sg_task_data_t;
287
288 void gras_trp_sg_chunk_send(gras_socket_t sock,
289                             const char *data,
290                             unsigned long int size, int stable_ignored)
291 {
292   gras_trp_sg_chunk_send_raw(sock, data, size);
293 }
294
295 void gras_trp_sg_chunk_send_raw(gras_socket_t sock,
296                                 const char *data, unsigned long int size)
297 {
298 #ifdef KILLME
299   char name[256];
300   static unsigned int count = 0;
301
302   smx_action_t act;             /* simix action */
303   gras_trp_procdata_t trp_remote_proc;
304   gras_msg_procdata_t msg_remote_proc;
305   gras_msg_t msg;               /* message to send */
306
307   //gras_trp_sg_sock_data_t sock_data = (gras_trp_sg_sock_data_t) sock->data;
308   xbt_assert0(sock->meas,
309               "SG chunk exchange shouldn't be used on non-measurement sockets");
310
311
312   /* creates simix action and waits its ends, waits in the sender host
313      condition */
314   /*
315   if (XBT_LOG_ISENABLED(gras_trp_sg, xbt_log_priority_debug)) {
316     smx_process_t remote_dude =
317         (sock_data->server ==
318          SIMIX_process_self())? (sock_data->client) : (sock_data->server);
319     smx_host_t remote_host = SIMIX_process_get_host(remote_dude);
320   }
321   */
322   //SIMIX_network_send(sock_data->rdv,size,1,-1,NULL,0,NULL,NULL);
323 #endif
324   THROW_UNIMPLEMENTED;
325 }
326
327 int gras_trp_sg_chunk_recv(gras_socket_t sock,
328                            char *data, unsigned long int size)
329 {
330   //gras_trp_sg_sock_data_t *sock_data =
331   //    (gras_trp_sg_sock_data_t *) sock->data;
332
333   //SIMIX_network_recv(sock_data->rdv,-1,NULL,0,NULL);
334   THROW_UNIMPLEMENTED;
335 #ifdef KILLME
336   gras_trp_sg_sock_data_t *remote_sock_data;
337   gras_socket_t remote_socket = NULL;
338   gras_msg_t msg_got;
339   gras_msg_procdata_t msg_procdata =
340       (gras_msg_procdata_t) gras_libdata_by_name("gras_msg");
341   gras_trp_procdata_t trp_proc =
342       (gras_trp_procdata_t) gras_libdata_by_id(gras_trp_libdata_id);
343
344   xbt_assert0(sock->meas,
345               "SG chunk exchange shouldn't be used on non-measurement sockets");
346   xbt_queue_shift_timed(trp_proc->meas_selectable_sockets,
347                         &remote_socket, 60);
348
349   if (remote_socket == NULL) {
350     THROW0(timeout_error, 0, "Timeout");
351   }
352
353   remote_sock_data = (gras_trp_sg_sock_data_t *) remote_socket->data;
354   msg_got = xbt_fifo_shift(msg_procdata->msg_to_receive_queue_meas);
355
356   sock_data = (gras_trp_sg_sock_data_t *) sock->data;
357
358   /* ok, I'm here, you can continue the communication */
359   SIMIX_cond_signal(remote_sock_data->cond);
360
361   SIMIX_mutex_lock(remote_sock_data->mutex);
362   /* wait for communication end */
363   SIMIX_cond_wait(remote_sock_data->cond, remote_sock_data->mutex);
364
365   if (msg_got->payl_size != size)
366     THROW5(mismatch_error, 0,
367            "Got %d bytes when %ld where expected (in %s->%s:%d)",
368            msg_got->payl_size, size,
369            SIMIX_host_get_name(sock_data->to_host),
370            SIMIX_host_get_name(SIMIX_host_self()), sock->peer_port);
371
372   if (data)
373     memcpy(data, msg_got->payl, size);
374
375   if (msg_got->payl)
376     xbt_free(msg_got->payl);
377
378   xbt_free(msg_got);
379   SIMIX_mutex_unlock(remote_sock_data->mutex);
380 #endif
381   return 0;
382 }