Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Initial structure to implement gras using simix.
[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         /*
98   xbt_ex_t e;
99
100   smx_host_t peer;
101   gras_hostdata_t *hd;
102   gras_trp_sg_sock_data_t *data;
103   gras_sg_portrec_t pr;
104 */
105   /* make sure this socket will reach someone */
106   /*if (!(peer=SIMIX_get_host_by_name(sock->peer_name))) 
107     THROW1(mismatch_error,0,"Can't connect to %s: no such host.\n",sock->peer_name);
108
109   if (!(hd=(gras_hostdata_t *)SIMIX_host_get_data(peer))) 
110     THROW1(mismatch_error,0,
111            "can't connect to %s: no process on this host",
112            sock->peer_name);
113   
114   TRY {
115     find_port(hd,sock->peer_port,&pr);
116   } CATCH(e) {
117     if (e.category == mismatch_error) {
118       xbt_ex_free(e);
119       THROW2(mismatch_error,0,
120              "can't connect to %s:%d, no process listen on this port",
121              sock->peer_name,sock->peer_port);
122     } 
123     RETHROW;
124   }
125
126   if (pr.meas && !sock->meas) {
127     THROW2(mismatch_error,0,
128            "can't connect to %s:%d in regular mode, the process listen "
129            "in meas mode on this port",sock->peer_name,sock->peer_port);
130   }
131   if (!pr.meas && sock->meas) {
132     THROW2(mismatch_error,0,
133            "can't connect to %s:%d in meas mode, the process listen "
134            "in regular mode on this port",sock->peer_name,sock->peer_port);
135   }
136 */
137   /* create the socket */
138   /*data = xbt_new(gras_trp_sg_sock_data_t,1);
139   //data->from_PID     = gras_os_getpid();
140   data->from_process = SIMIX_process_self();
141         data->to_process         = pr.process;
142         //data->to_PID       = hd->proc[ pr.tochan ];
143         data->to_port                    = pr;
144   data->to_host      = peer;
145   //data->to_chan      = pr.tochan;
146
147   sock->data = data;
148   sock->incoming = 1;
149
150   DEBUG6("%s (PID %d) connects in %s mode to %s:%d (to_PID=%d)",
151           SIMIX_process_get_name(SIMIX_process_self()), gras_os_getpid,
152           sock->meas?"meas":"regular",
153          sock->peer_name,sock->peer_port,data->to_PID);
154 */
155 }
156
157 void gras_trp_sg_socket_server(gras_trp_plugin_t self,
158                                gras_socket_t sock){
159 /*
160   gras_hostdata_t *hd=(gras_hostdata_t *)SIMIX_host_get_data(SIMIX_host_self());
161   gras_trp_procdata_t pd=(gras_trp_procdata_t)gras_libdata_by_id(gras_trp_libdata_id);
162   gras_sg_portrec_t pr;
163   gras_trp_sg_sock_data_t *data;
164   volatile int found;
165   
166   const char *host=SIMIX_host_get_name(SIMIX_host_self());
167
168   xbt_ex_t e;
169
170   xbt_assert0(hd,"Please run gras_process_init on each process");
171
172   sock->accepting = 0;*/ /* no such nuisance in SG */
173 /*
174   found = 0;
175   TRY {
176     find_port(hd,sock->port,&pr);
177     found = 1;
178   } CATCH(e) {
179     if (e.category == mismatch_error)
180       xbt_ex_free(e);
181     else
182       RETHROW;
183   }
184    
185   if (found) 
186     THROW2(mismatch_error,0,
187            "can't listen on address %s:%d: port already in use.",
188            host,sock->port);
189
190   //pr.tochan = sock->meas ? pd->measChan : pd->chan;
191   pr.port   = sock->port;
192   pr.meas    = sock->meas;
193         pr.process = SIMIX_process_self();
194   xbt_dynar_push(hd->ports,&pr);
195   */
196   /* Create the socket */
197   /*data = xbt_new(gras_trp_sg_sock_data_t,1);
198   data->from_process     = NULL;
199   data->to_PID       = SIMIX_process_self();;
200   //data->to_process       = gras_os_getpid();
201   data->to_host      = SIMIX_host_self();
202         data->to_port            = pr;
203   //data->to_chan      = pd->chan;
204   
205   sock->data = data;
206
207   VERB6("'%s' (%d) ears on %s:%d%s (%p)",
208     SIMIX_process_get_name(SIMIX_process_self()), gras_os_getpid(),
209     host,sock->port,sock->meas? " (mode meas)":"",sock);
210 */
211 }
212
213 void gras_trp_sg_socket_close(gras_socket_t sock){
214 /*
215   gras_hostdata_t *hd=(gras_hostdata_t *)SIMIX_host_get_data(SIMIX_host_self());
216   int cpt;
217   
218   gras_sg_portrec_t pr;
219
220   XBT_IN1(" (sock=%p)",sock);
221    
222   if (!sock) return;
223   xbt_assert0(hd,"Please run gras_process_init on each process");
224
225   if (sock->data)
226     free(sock->data);
227
228   if (sock->incoming && sock->port >= 0) {*/
229     /* server mode socket. Unregister it from 'OS' tables */
230 /*    xbt_dynar_foreach(hd->ports, cpt, pr) {
231       DEBUG2("Check pr %d of %lu", cpt, xbt_dynar_length(hd->ports));
232       if (pr.port == sock->port) {
233         xbt_dynar_cursor_rm(hd->ports, &cpt);
234         XBT_OUT;
235         return;
236       }
237     }
238     WARN2("socket_close called on the unknown incoming socket %p (port=%d)",
239           sock,sock->port);
240   }
241   XBT_OUT;
242 */
243 }
244
245 typedef struct {
246   int size;
247   void *data;
248 } sg_task_data_t;
249
250 void gras_trp_sg_chunk_send(gras_socket_t sock,
251                             const char *data,
252                             unsigned long int size,
253                             int stable_ignored) {
254   gras_trp_sg_chunk_send_raw(sock,data,size);
255 }
256
257 void gras_trp_sg_chunk_send_raw(gras_socket_t sock,
258                                 const char *data,
259                                 unsigned long int size) {
260                                 /*
261   m_task_t task=NULL;
262   static unsigned int count=0;
263   char name[256];
264   gras_trp_sg_sock_data_t *sock_data = (gras_trp_sg_sock_data_t *)sock->data;
265   sg_task_data_t *task_data;
266   
267   xbt_assert0(sock->meas, "SG chunk exchange shouldn't be used on non-measurement sockets");
268
269   sprintf(name,"Chunk[%d]",count++);
270
271   task_data=xbt_new(sg_task_data_t,1);
272   task_data->size = size;
273   if (data) {
274     task_data->data=(void*)xbt_malloc(size);
275     memcpy(task_data->data,data,size);
276   } else {
277     task_data->data = NULL;
278   }
279
280   task=MSG_task_create(name,0,((double)size),task_data);
281
282   DEBUG5("send chunk %s from %s to  %s:%d (size=%ld)",
283          name, MSG_host_get_name(MSG_host_self()),
284          MSG_host_get_name(sock_data->to_host), sock_data->to_chan,size);
285   if (MSG_task_put_with_timeout(task, sock_data->to_host,sock_data->to_chan,60.0) != MSG_OK) {
286     THROW0(system_error,0,"Problem during the MSG_task_put with timeout 60");
287   }*/
288 }
289
290 int gras_trp_sg_chunk_recv(gras_socket_t sock,
291                             char *data,
292                             unsigned long int size){
293                                         /*
294   gras_trp_procdata_t pd=(gras_trp_procdata_t)gras_libdata_by_id(gras_trp_libdata_id);
295
296   m_task_t task=NULL;
297   sg_task_data_t *task_data;
298   gras_trp_sg_sock_data_t *sock_data = sock->data;
299
300   xbt_assert0(sock->meas, "SG chunk exchange shouldn't be used on non-measurement sockets");
301   XBT_IN;
302   DEBUG4("recv chunk on %s ->  %s:%d (size=%ld)",
303          MSG_host_get_name(sock_data->to_host),
304          MSG_host_get_name(MSG_host_self()), sock_data->to_chan, size);
305   if (MSG_task_get_with_time_out(&task, 
306                                  (sock->meas ? pd->measChan : pd->chan),
307                                  60) != MSG_OK)
308     THROW0(system_error,0,"Error in MSG_task_get()");
309   DEBUG1("Got chuck %s",MSG_task_get_name(task));
310
311   task_data = MSG_task_get_data(task);
312   if (task_data->size != size)
313     THROW5(mismatch_error,0,
314            "Got %d bytes when %ld where expected (in %s->%s:%d)",
315            task_data->size, size,
316            MSG_host_get_name(sock_data->to_host),
317            MSG_host_get_name(MSG_host_self()), sock_data->to_chan);
318   if (data) 
319     memcpy(data,task_data->data,size);
320   if (task_data->data)
321     free(task_data->data);
322   free(task_data);
323
324   if (MSG_task_destroy(task) != MSG_OK)
325     THROW0(system_error,0,"Error in MSG_task_destroy()");
326
327   XBT_OUT;
328   return size;*/
329         return 0;
330 }
331