Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Correct some bugs
[simgrid.git] / src / gras_simix / Transport / gras_simix_transport_plugin_sg.c
1 /* $Id$ */
2
3 /* file trp (transport) - send/receive a bunch of bytes in SG realm         */
4
5 /* Note that this is only used to debug other parts of GRAS since message   */
6 /*  exchange in SG realm is implemented directly without mimicing real life */
7 /*  This would be terribly unefficient.                                     */
8
9 /* Copyright (c) 2004 Martin Quinson. All rights reserved.                  */
10
11 /* This program is free software; you can redistribute it and/or modify it
12  * under the terms of the license (GNU LGPL) which comes with this package. */
13
14 #include "xbt/ex.h" 
15
16 //#include "msg/msg.h"
17
18 #include "gras_simix_transport_private.h"
19 #include "gras_simix/Virtu/gras_simix_virtu_sg.h"
20
21 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(gras_trp_sg,gras_trp,"SimGrid pseudo-transport");
22
23 /***
24  *** Prototypes 
25  ***/
26
27 /* retrieve the port record associated to a numerical port on an host */
28 static void find_port(gras_hostdata_t *hd, int port, gras_sg_portrec_t *hpd);
29
30
31 void gras_trp_sg_socket_client(gras_trp_plugin_t self,
32                                /* OUT */ gras_socket_t sock);
33 void gras_trp_sg_socket_server(gras_trp_plugin_t self,
34                                /* OUT */ gras_socket_t sock);
35 void gras_trp_sg_socket_close(gras_socket_t sd);
36
37 void gras_trp_sg_chunk_send_raw(gras_socket_t sd,
38                                 const char *data,
39                                 unsigned long int size);
40 void gras_trp_sg_chunk_send(gras_socket_t sd,
41                             const char *data,
42                             unsigned long int size,
43                             int stable_ignored);
44
45 int gras_trp_sg_chunk_recv(gras_socket_t sd,
46                             char *data,
47                             unsigned long int size);
48
49 /***
50  *** Specific plugin part
51  ***/
52 typedef struct {
53   int placeholder; /* nothing plugin specific so far */
54 } gras_trp_sg_plug_data_t;
55
56
57 /***
58  *** Code
59  ***/
60 static void find_port(gras_hostdata_t *hd, int port,
61                       gras_sg_portrec_t *hpd) {
62   int cpt;
63   gras_sg_portrec_t pr;
64
65   xbt_assert0(hd,"Please run gras_process_init on each process");
66   
67   xbt_dynar_foreach(hd->ports, cpt, pr) {
68     if (pr.port == port) {
69       memcpy(hpd,&pr,sizeof(gras_sg_portrec_t));
70       return;
71     }
72   }
73   THROW1(mismatch_error,0,"Unable to find any portrec for port #%d",port);
74 }
75
76
77 void
78 gras_trp_sg_setup(gras_trp_plugin_t plug) {
79
80   gras_trp_sg_plug_data_t *data=xbt_new(gras_trp_sg_plug_data_t,1);
81
82   plug->data      = data; 
83
84   plug->socket_client = gras_trp_sg_socket_client;
85   plug->socket_server = gras_trp_sg_socket_server;
86   plug->socket_close  = gras_trp_sg_socket_close;
87
88   plug->raw_send = gras_trp_sg_chunk_send_raw;
89   plug->send = gras_trp_sg_chunk_send;
90   plug->raw_recv = plug->recv = gras_trp_sg_chunk_recv;
91
92   plug->flush         = NULL; /* nothing cached */
93 }
94
95 void gras_trp_sg_socket_client(gras_trp_plugin_t self,
96                                /* OUT */ gras_socket_t sock){
97   xbt_ex_t e;
98
99   smx_host_t peer;
100   gras_hostdata_t *hd;
101   gras_hostdata_t *local_hd;
102   gras_trp_sg_sock_data_t *data;
103   gras_sg_portrec_t pr;
104         int i;
105
106   /* make sure this socket will reach someone */
107   if (!(peer=SIMIX_host_get_by_name(sock->peer_name))) 
108     THROW1(mismatch_error,0,"Can't connect to %s: no such host.\n",sock->peer_name);
109
110   if (!(hd=(gras_hostdata_t *)SIMIX_host_get_data(peer))) 
111     THROW1(mismatch_error,0,
112            "can't connect to %s: no process on this host",
113            sock->peer_name);
114   
115   TRY {
116     find_port(hd,sock->peer_port,&pr);
117   } CATCH(e) {
118     if (e.category == mismatch_error) {
119       xbt_ex_free(e);
120       THROW2(mismatch_error,0,
121              "can't connect to %s:%d, no process listen on this port",
122              sock->peer_name,sock->peer_port);
123     } 
124     RETHROW;
125   }
126
127   if (pr.meas && !sock->meas) {
128     THROW2(mismatch_error,0,
129            "can't connect to %s:%d in regular mode, the process listen "
130            "in meas mode on this port",sock->peer_name,sock->peer_port);
131   }
132   if (!pr.meas && sock->meas) {
133     THROW2(mismatch_error,0,
134            "can't connect to %s:%d in meas mode, the process listen "
135            "in regular mode on this port",sock->peer_name,sock->peer_port);
136   }
137   /* create the socket */
138   data = xbt_new(gras_trp_sg_sock_data_t,1);
139   data->from_process = SIMIX_process_self();
140         data->to_process         = pr.process;
141   data->to_host      = peer;
142
143         /* searches for a free port on this host */
144         local_hd = (gras_hostdata_t *)SIMIX_host_get_data(SIMIX_host_self());
145         for (i=1;i<65536;i++) {
146                 if (local_hd->cond_port[i] == NULL)
147                         break;
148         }
149         if (i == 65536) THROW0(system_error,0,"No port free");
150         sock->port = i;
151         local_hd->cond_port[i] = SIMIX_cond_init();
152         local_hd->mutex_port[i] = SIMIX_mutex_init();
153
154   sock->data = data;
155   sock->incoming = 1;
156
157   DEBUG5("%s (PID %ld) connects in %s mode to %s:%d",
158           SIMIX_process_get_name(SIMIX_process_self()), gras_os_getpid(),
159           sock->meas?"meas":"regular",
160          sock->peer_name,sock->peer_port);
161 }
162
163 void gras_trp_sg_socket_server(gras_trp_plugin_t self,
164                                gras_socket_t sock){
165
166   gras_hostdata_t *hd=(gras_hostdata_t *)SIMIX_host_get_data(SIMIX_host_self());
167   //gras_trp_procdata_t pd=(gras_trp_procdata_t)gras_libdata_by_id(gras_trp_libdata_id);
168   gras_sg_portrec_t pr;
169   gras_trp_sg_sock_data_t *data;
170   volatile int found;
171   
172   const char *host=SIMIX_host_get_name(SIMIX_host_self());
173
174   xbt_ex_t e;
175
176   xbt_assert0(hd,"Please run gras_process_init on each process");
177
178   sock->accepting = 0; /* no such nuisance in SG */
179   found = 0;
180   TRY {
181     find_port(hd,sock->port,&pr);
182     found = 1;
183   } CATCH(e) {
184     if (e.category == mismatch_error)
185       xbt_ex_free(e);
186     else
187       RETHROW;
188   }
189    
190   if (found) 
191     THROW2(mismatch_error,0,
192            "can't listen on address %s:%d: port already in use.",
193            host,sock->port);
194
195   pr.port   = sock->port;
196   pr.meas    = sock->meas;
197         pr.process = SIMIX_process_self();
198   xbt_dynar_push(hd->ports,&pr);
199
200         hd->cond_port[sock->port] = SIMIX_cond_init();
201         hd->mutex_port[sock->port] = SIMIX_mutex_init();
202   
203   /* Create the socket */
204   data = xbt_new(gras_trp_sg_sock_data_t,1);
205   data->from_process     = SIMIX_process_self();
206   data->to_process       = NULL;
207         data->to_host      = SIMIX_host_self();
208   
209   sock->data = data;
210
211   VERB6("'%s' (%ld) ears on %s:%d%s (%p)",
212     SIMIX_process_get_name(SIMIX_process_self()), gras_os_getpid(),
213     host,sock->port,sock->meas? " (mode meas)":"",sock);
214
215 }
216
217 void gras_trp_sg_socket_close(gras_socket_t sock){
218
219   gras_hostdata_t *hd=(gras_hostdata_t *)SIMIX_host_get_data(SIMIX_host_self());
220   int cpt;
221   gras_sg_portrec_t pr; 
222
223   XBT_IN1(" (sock=%p)",sock);
224    
225   if (!sock) return;
226
227   xbt_assert0(hd,"Please run gras_process_init on each process");
228
229   if (sock->data)
230     free(sock->data);
231         
232 /*
233         SIMIX_cond_destroy(hd->cond_port[sock->port]);
234         hd->cond_port[sock->port] = NULL;
235         SIMIX_mutex_destroy(hd->mutex_port[sock->port]);
236         hd->mutex_port[sock->port] = NULL;
237 */
238   if (sock->incoming && !sock->outgoing && sock->port >= 0) {
239     /* server mode socket. Unregister it from 'OS' tables */
240     xbt_dynar_foreach(hd->ports, cpt, pr) {
241       DEBUG2("Check pr %d of %lu", cpt, xbt_dynar_length(hd->ports));
242       if (pr.port == sock->port) {
243         xbt_dynar_cursor_rm(hd->ports, &cpt);
244         XBT_OUT;
245         return;
246       }
247     }
248     WARN2("socket_close called on the unknown incoming socket %p (port=%d)",
249           sock,sock->port);
250   }
251   XBT_OUT;
252
253 }
254
255 typedef struct {
256   int size;
257   void *data;
258 } sg_task_data_t;
259
260 void gras_trp_sg_chunk_send(gras_socket_t sock,
261                             const char *data,
262                             unsigned long int size,
263                             int stable_ignored) {
264   gras_trp_sg_chunk_send_raw(sock,data,size);
265 }
266
267 void gras_trp_sg_chunk_send_raw(gras_socket_t sock,
268                                 const char *data,
269                                 unsigned long int size) {
270                                 /*
271   m_task_t task=NULL;
272   static unsigned int count=0;
273   char name[256];
274   gras_trp_sg_sock_data_t *sock_data = (gras_trp_sg_sock_data_t *)sock->data;
275   sg_task_data_t *task_data;
276   
277   xbt_assert0(sock->meas, "SG chunk exchange shouldn't be used on non-measurement sockets");
278
279   sprintf(name,"Chunk[%d]",count++);
280
281   task_data=xbt_new(sg_task_data_t,1);
282   task_data->size = size;
283   if (data) {
284     task_data->data=(void*)xbt_malloc(size);
285     memcpy(task_data->data,data,size);
286   } else {
287     task_data->data = NULL;
288   }
289
290   task=MSG_task_create(name,0,((double)size),task_data);
291
292   DEBUG5("send chunk %s from %s to  %s:%d (size=%ld)",
293          name, MSG_host_get_name(MSG_host_self()),
294          MSG_host_get_name(sock_data->to_host), sock_data->to_chan,size);
295   if (MSG_task_put_with_timeout(task, sock_data->to_host,sock_data->to_chan,60.0) != MSG_OK) {
296     THROW0(system_error,0,"Problem during the MSG_task_put with timeout 60");
297   }*/
298 }
299
300 int gras_trp_sg_chunk_recv(gras_socket_t sock,
301                             char *data,
302                             unsigned long int size){
303                                         /*
304   gras_trp_procdata_t pd=(gras_trp_procdata_t)gras_libdata_by_id(gras_trp_libdata_id);
305
306   m_task_t task=NULL;
307   sg_task_data_t *task_data;
308   gras_trp_sg_sock_data_t *sock_data = sock->data;
309
310   xbt_assert0(sock->meas, "SG chunk exchange shouldn't be used on non-measurement sockets");
311   XBT_IN;
312   DEBUG4("recv chunk on %s ->  %s:%d (size=%ld)",
313          MSG_host_get_name(sock_data->to_host),
314          MSG_host_get_name(MSG_host_self()), sock_data->to_chan, size);
315   if (MSG_task_get_with_time_out(&task, 
316                                  (sock->meas ? pd->measChan : pd->chan),
317                                  60) != MSG_OK)
318     THROW0(system_error,0,"Error in MSG_task_get()");
319   DEBUG1("Got chuck %s",MSG_task_get_name(task));
320
321   task_data = MSG_task_get_data(task);
322   if (task_data->size != size)
323     THROW5(mismatch_error,0,
324            "Got %d bytes when %ld where expected (in %s->%s:%d)",
325            task_data->size, size,
326            MSG_host_get_name(sock_data->to_host),
327            MSG_host_get_name(MSG_host_self()), sock_data->to_chan);
328   if (data) 
329     memcpy(data,task_data->data,size);
330   if (task_data->data)
331     free(task_data->data);
332   free(task_data);
333
334   if (MSG_task_destroy(task) != MSG_OK)
335     THROW0(system_error,0,"Error in MSG_task_destroy()");
336
337   XBT_OUT;
338   return size;*/
339         return 0;
340 }
341